Reputation: 59
device PIC18F67k40 UART RECEIVER Part Work Proper BUT Transmitter Part not Work ?? UART Receiver part work properly. can any one suggest me what i am doing wrong in below code. device is on 16Mhz and baud rate is 115200 device PIC18F67k40 UART RECEIVER Part Work Proper BUT Transmitter Part not Work ?? UART Receiver part work properly. can any one suggest me what i am doing wrong in below code. device is on 16Mhz and baud rate is 115200
#include <xc.h>
#include <p18f67k40.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// PIC18F67K40 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config FEXTOSC = HS // External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINTOSC_64MHZ// Power-up default value for COSC bits (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
// CONFIG1H
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
// CONFIG2L
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RG5 pin function is MCLR )
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power up timer disabled)
#pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)
// CONFIG2H
#pragma config BORV = VBOR_2P45 // Brown Out Reset Voltage selection bits (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
// CONFIG3L
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled)
// CONFIG3H
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4L
#pragma config WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (00C000-00FFFFh) not write-protected)
#pragma config WRT4 = OFF // Write Protection Block 3 (Block 4 (010000-013FFFh) not write-protected)
#pragma config WRT5 = OFF // Write Protection Block 3 (Block 5 (014000-017FFFh) not write-protected)
#pragma config WRT6 = OFF // Write Protection Block 3 (Block 6 (018000-01BFFFh) not write-protected)
#pragma config WRT7 = OFF // Write Protection Block 3 (Block 7 (01C000-01FFFFh) not write-protected)
// CONFIG4H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config SCANE = ON // Scanner Enable bit (Scanner module is available for use, SCANMD bit can control the module)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)
// CONFIG5L
#pragma config CP = OFF // UserNVM Program Memory Code Protection bit (UserNVM code protection disabled)
#pragma config CPD = OFF // DataNVM Memory Code Protection bit (DataNVM code protection disabled)
// CONFIG5H
// CONFIG6L
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR4 = OFF // Table Read Protection Block 4 (Block 4 (010000-013FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR5 = OFF // Table Read Protection Block 5 (Block 5 (014000-017FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR6 = OFF // Table Read Protection Block 6 (Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR7 = OFF // Table Read Protection Block 7 (Block 7 (01C000-01FFFFh) not protected from table reads executed in other blocks)
// CONFIG6H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 16000000 //16Mhz
#define LED2 RC2 //LED2
char string_rx1[100],uart_rx1=0,string_rx2[100],uart_rx2=0,str2=0;
void System_Init() // System_Init
{
OSCFRQ=0b00000101; //16Mhz
TRISC2=0;//LED2 Pin-33 Output
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
INTCONbits.IPEN=1;
}
void UART_2_Init()//UART_2_Init()
{
TRISGbits.TRISG1=1; // TX2 Pin-4
TRISGbits.TRISG2=1; // RX2 Pin-5
RX2PPS=0b00110010;
TX2PPS=0b00110001;
RC2IE=1;
SP2BRG=34; // Baud Rate 115200
BAUD2CON = 0x08; // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
//TX2STA Register
TX2STAbits.CSRC=0; // Asynchronous mode:Don?t care bit-7
TX2STAbits.TX9=1; // 8-bit transmission bit-6
TX2STAbits.TXEN=0; // Transmit is Disabled bit-5
TX2STAbits.TXEN=1; // Transmit is enabled bit-5
TX2STAbits.SYNC=0; // Asynchronous mode bit-4
TX2STAbits.SENDB=1; // Sync Break transmission disabled or completed bit-3
TX2STAbits.BRGH=1; // High Baud Rate Select bit bit-2
TX2STAbits.TRMT=1; // Transmit Shift Register Status bit bit-1
TX2STAbits.TX9D=1; // Ninth bit of Transmit Data bit-0
//RX2STA Register
RC2STAbits.SPEN=1; // Serial Port Enable bit bit-7
RC2STAbits.RX9=0; // 8-bit reception bit-6
RC2STAbits.SREN=0; // Single Receive Enable bit Asynchronous mode:Don?t care bit-5
RC2STAbits.CREN=1; // Continuous Receive Enable bit bit-4
RC2STAbits.ADDEN=0; // bit-3
ANSELGbits.ANSELG1=0; //
ANSELGbits.ANSELG2=0;//
RC2IP = 1;//
TX2IP = 1;//
}
void UART_2_Tx(unsigned char z[]) //UART_2_Tx
{
unsigned int uart_tx2=0;
while(z[uart_tx2]!='\0')
{
while(!TX2IF);
TX2REG=z[uart_tx2];
uart_tx2++;
}
}
void UART_Tx2(const char *buffer) // UART2 Transmmitr
{
unsigned int size = strlen(buffer);
while( size)
{
while(TX2IF==0); // wait while TX buffer full
TX2REG = *buffer; // send single character to transmit buffer
buffer++; // transmit next character on following loop
size--; // loop until all characters sent (when size = 0)
}
while( !TX2STAbits.TRMT); // wait for last transmission to finish
}
void UART_2_Rx()//UART_2_Rx
{
string_rx2[uart_rx2]=RC2REG;
uart_rx2++;
}
void interrupt ISR(void) //interrupt ISR
{
if(RC2IF && RC2IE) // Through Application UART_2
{
UART_2_Rx();
if(RC2STAbits.OERR){RC2STAbits.CREN=0;RC2STAbits.CREN=1;}
str2=1;
}
}
void main(void) // main
{
System_Init();
UART_2_Init();
UART_2_Tx("HELLO\r\n"); //1st transmitter logic
UART_Tx2("HELLO\r\n"); //2nd transmitter logic
LED2=0;
while(1)
{
if(string_rx2[0]=='1'){LED2=1;__delay_ms(10);string_rx2[0]='\0';uart_rx2=0;}
if(string_rx2[0]=='0'){LED2=0;__delay_ms(10);string_rx2[0]='\0';uart_rx2=0;}
if(str2){str2=0;__delay_ms(20);uart_rx2=0;}
}
}
Upvotes: 1
Views: 455
Reputation: 4288
The TX Pin should be an output. So use:
TRISGbits.TRISG1=0; // TX2 Pin-4
And please remember the PPS register. RG1 ist not the default one for the receiver.
RG1PPS = 0x14 //for UART Nr.5
Please have a look at the section 17 in the datasheet.
Upvotes: 1