I have created a serial communication between my laptop and 16F877A mcu. I have used MAX232EPE IC as level converter also. According to my code If I send a character to the MCU, MCU will return a String related to the character.
Now, what's happening is, when I am sending a character, MCU is getting it properly, because corresponding LED is getting turned on but what MCU is return as String is complete gibberish.
// Configuration Byte
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000
void main() {
//Initialize USART with baud rate 9600
USARTInit(9600);
TRISB = 0;
PORTBbits.RB0 = 0; //pin 33-Green
int green_led = 0;
PORTBbits.RB2 = 0; //pin 35 - Red
int red_led = 0;
PORTBbits.RB4 = 0; //pin 37 - Blue
int blue_led = 0;
PORTBbits.RB5 = 0; //pin 38 - Yellow
int yellow_led = 0;
USARTWriteLine("Connected");
while(1) {
uint8_t n = USARTDataAvailable();
if(n != 0) {
char data = USARTReadData();
if(data == 'G'){
if(green_led == 0){
PORTBbits.RB0 = 1;
green_led = 1;
USARTWriteLine("Green LED turned on");
}else{
PORTBbits.RB0 = 0;
green_led = 0;
USARTWriteLine("Green LED turned off");
}
}else if(data == 'R'){
if(red_led == 0){
PORTBbits.RB2 = 1;
red_led = 1;
USARTWriteLine("Red LED turned on");
}else{
PORTBbits.RB2 = 0;
red_led = 0;
USARTWriteLine("Red LED turned off");
}
}else if(data == 'B'){
if(blue_led == 0){
PORTBbits.RB4 = 1;
blue_led = 1;
USARTWriteLine("Blue LED turned on");
}else{
PORTBbits.RB4 = 0;
blue_led = 0;
USARTWriteLine("Blue LED turned off");
}
}else if(data == 'Y'){
if(yellow_led == 0){
PORTBbits.RB5 = 1;
yellow_led = 1;
USARTWriteLine("Yellow LED turned on");
}else{
PORTBbits.RB5 = 0;
yellow_led = 0;
USARTWriteLine("Yellow LED turned off");
}
}else{
USARTWriteChar(13);
USARTWriteChar(10);
USARTWriteString("Invalid command : ");
USARTWriteChar(data);
USARTWriteChar(13);
USARTWriteChar(10);
}
n = 0;
USARTFlushBuffer();
}
}
}
When I am sending "G", I am receiving "��g����wf�}w��]w���" where I should receive "Green LED turned on", both are 19 character in length
If anyone can help me, it will be really great.