0
\$\begingroup\$

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.Diagram I have referred

    // 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.

\$\endgroup\$
9
  • \$\begingroup\$ does the string you get back related to the string you expect? i.e. is it the right length? are any characters correct? have you checked settings for no of bits and parity - at both ends? What happens if you send shorter strings or just single characters? \$\endgroup\$
    – Icy
    Commented Nov 12, 2015 at 12:46
  • \$\begingroup\$ Along with suggestions by @Icy, Do you get string "Connected" - to start with! Is baud rate same on both sides? \$\endgroup\$
    – Swanand
    Commented Nov 12, 2015 at 12:57
  • \$\begingroup\$ If you have a scope, check the RS232 waveforms. Suspect dodgy voltage levels, or incorrect start/stop bit settings. \$\endgroup\$
    – user16324
    Commented Nov 12, 2015 at 13:03
  • \$\begingroup\$ baud rate is same in both side. The string I am getting back is sometimes related and same length but not the exact same. I have checked the stop bit and parity bit. According to the code will it only accept the first character even if I send a string. When I am sending "G" Green LED is turning on but the "Green LED turned on" string is not coming properly \$\endgroup\$ Commented Nov 12, 2015 at 13:04
  • \$\begingroup\$ I don't have the scope to check the actual voltage and waveform but I will try. But all you answers are noted. I will recheck. \$\endgroup\$ Commented Nov 12, 2015 at 13:11

1 Answer 1

2
\$\begingroup\$

I didn't look at the code, but here are some possibilities:

  1. Lack of bypass cap on the PIC. Until you fix this, all else is irrelevant.

  2. Possibly not waiting for the UART to be ready before writing a character to it. Try sending just one character intead of a string. If that works, then this is a likely cause.

Normally I'd add baud rate to this list, but since you are receiving correctly, that's probably not it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.