Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

getting wrong character in serial commmunication

Status
Not open for further replies.

piyushpandey

Member level 4
Joined
Mar 26, 2012
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,051
Hello guys

I am using pic16f688 with the MPLAB X IDE and using the Hitech compiler version 9.83 .

I have made a simple program for the receiver section of the pic micrcontroller and want to blink the LEDs connected at the PORTC pins 2 and 3.

Here is my code:

Code:
#include <pic.h>

//#define OSCILLATOR (OSCCON = 0x71)

#define _XTAL_FREQ 8000000

__CONFIG(WDTE_OFF & FOSC_INTOSCCLK & MCLRE_ON);


void EUSART_init(void)
{
    // Set the baud rate
    SPBRG = 12;   // 9600 baud rate at 8.0 Mhz
    TXSTAbits.SYNC = 0;     // asynchronous mode
    TXSTAbits.BRGH = 0;    // low baud rate
    BAUDCTLbits.BRG16 = 0;   // 8 bit baud rate generator

    // Disable all interrupts
    INTCONbits.GIE = 0;
    INTCONbits.PEIE = 0;

    // Only 8-bit mode for the receiver

    RCSTAbits.RX9 = 0;

    // Enable the transmitter and receiver
    TXSTAbits.TXEN = 1;
    RCSTAbits.CREN = 1;

    // Enable the Serial communication

    RCSTAbits.SPEN = 1;
}

void EUSART_transmit_data(unsigned char data)
{
    while(!TXIF)
        continue;
        NOP();
    _nop();
    _nop();
        
    TXREG = data;
    return;
}

unsigned char EUSART_recieve_data(void)
{
    unsigned char data;
   // RCREG = data;
    
 /*  if(FERR==1)
    {
        RCSTAbits.SPEN = 0;
    EUSART_receiver_init();
    }*/
    while(!RCIF)
        continue;  
    data = RCREG;

   // return RCREG;
    return data ;
}

void main(void) {

     unsigned int x;

     OSCCON = 0x71;
    // Initialise the portc bits
//#define ports
//#ifdef ports
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
//#endif
    
    // Disable the analog inputs
    ANSEL = 0x00;
    
    // Disable the comparator
    CMCON0 = 0x07;
   
  //  EUSART_transmitter_enable();
    EUSART_init();

    while(1)
   // for (unsigned int j = 0;j<=20;j++)
    {
      //  EUSART_transmit_data(0x41);

        x = EUSART_recieve_data();
    //    EUSART_transmit_data(x);
   //     __delay_ms(100);



        // to blink the LEDs
        switch (x)
        {
            case 'a':
            case 'A':
            case 1:
                PORTCbits.RC3 = 1;
                break ;
            case 'g':
            case 'G':
            case 2:
                PORTCbits.RC2 = 1;
                break ;
            case 'p':
            case 'P':
            case 3:
                PORTCbits.RC3 = 0;
                break ;
            case 'r':
            case 'R':
            case 4:
                PORTCbits.RC2 = 0;
                break ;
            default:
                PORTCbits.RC2 = 0;
                PORTCbits.RC3 = 0;
        }

      }
}


But I am not able to blink the LEDS on the pins 2 and 3 when I send the characters g and a .

Please guys tell me what is the problem I am facing am I missing something.


Thanks
 

try un-remarking the "#define ports" line
 

nothing worked yuvko


here is my few changes to the code:


Code:
#include <pic.h>

//#define OSCILLATOR (OSCCON = 0x71)

#define _XTAL_FREQ 4000000
#define BAUD 2400

#define DIVIDER ((int)(_XTAL_FREQ/(64UL * BAUD) -1))

__CONFIG(WDTE_OFF & FOSC_INTOSCCLK & MCLRE_ON);


void EUSART_init(void)
{
    // Set the baud rate
    SPBRG = DIVIDER;
    TXSTAbits.SYNC = 0;     // asynchronous mode
    TXSTAbits.BRGH = 0;    // low baud rate
    BAUDCTLbits.BRG16 = 0;   // 8 bit baud rate generator

    // Disable all interrupts
    INTCONbits.GIE = 0;
    INTCONbits.PEIE = 0;

    // Only 8-bit mode for the receiver

    RCSTAbits.RX9 = 0;

    // Enable the transmitter and receiver
    TXSTAbits.TXEN = 1;
    RCSTAbits.CREN = 1;

    // Enable the Serial communication

    RCSTAbits.SPEN = 1;
}

void EUSART_transmit_data(unsigned char data)
{
    while(!TXIF)
        continue;
        NOP();
    _nop();
    _nop();
        
    TXREG = data;
    return;
}

unsigned char EUSART_recieve_data()
{
    unsigned char data;
   // RCREG = data;
    
 /*  if(FERR==1)
    {
        RCSTAbits.SPEN = 0;
    EUSART_receiver_init();
    }*/
    while(!RCIF)
        continue;  
    data = RCREG;

   // return RCREG;
    return data ;
}

void main(void) {

     unsigned char x;

     OSCCON = 0x61;

    // Initialise the portc bits

    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    TRISCbits.TRISC4 = 0;
    TRISCbits.TRISC5 = 1;

    
    // Disable the analog inputs
    ANSEL = 0x00;
    
    // Disable the comparator
    CMCON0 = 0x07;
   
 
    EUSART_init();
  

    while(1)
 
    {
     

        x = EUSART_recieve_data();
    

        if(x=='g')
        {
            PORTCbits.RC2 = 1;
        }
        if (x=='a')
        {
            PORTCbits.RC3 = 1;
        }
        else
        {
            RC2 = 1;
            RC3 = 1;
            __delay_ms(10);
            RC2 = 0;
            RC3 = 0;
        }
        EUSART_transmit_data(x);

        __delay_ms(10);
    }

   
}

I have put the last two steps after the else to check whether I am getting the characters to be received by the uart or not.

So what happens that both LED blinks quickly and than wait for the next character as what is supposed to be in the receiver function and what the output should be of the else if not matching with the 'g' or 'a' characters.

I also made the transmitter function to check what value it is transmitting.

So Now what I am getting as a result is that when I am transmitting the character 'g' I am getting a garbage character whose hex value comes out to be F8 and not 68 which is the ascii value of the character 'g'.

Here is the screenshot what I am getting in the bray's terminal:

serial error.png

Please guys tell me my all functions are working well but I don't know where I am going wrong it's very unusual behaviour of my pic micrcontroller.



Thanks
 

well thanks engshahrul for sharing your code with me.

Now I must tell you guys that my problem of communicating with the UART has been solved with my code .

The code is here:

Code:
#include <pic.h>

//#define OSCILLATOR (OSCCON = 0x71)

#define _XTAL_FREQ 4000000
#define BAUD 2400

#define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))

__CONFIG(WDTE_OFF & FOSC_INTOSCCLK & MCLRE_ON);


void EUSART_init(void)
{
    // Set the baud rate
    SPBRG = DIVIDER;
    TXSTAbits.SYNC = 0;     // asynchronous mode
    TXSTAbits.BRGH = 1;    // low baud rate
    BAUDCTLbits.BRG16 = 0;   // 8 bit baud rate generator

    // Disable all interrupts
    INTCONbits.GIE = 0;
    INTCONbits.PEIE = 0;

    // Only 8-bit mode for the receiver

    RCSTAbits.RX9 = 0;

    // Enable the transmitter and receiver
    TXSTAbits.TXEN = 1;
    RCSTAbits.CREN = 1;

    // Enable the Serial communication

    RCSTAbits.SPEN = 1;
}

void EUSART_transmit_data(unsigned char data)
{
    while(!TXIF)
        continue;
        NOP();
    _nop();
    _nop();
        
    TXREG = data;
    return;
}

unsigned char EUSART_recieve_data()
{
    unsigned char data;
   // RCREG = data;
    
 /*  if(FERR==1)
    {
        RCSTAbits.SPEN = 0;
    EUSART_receiver_init();
    }*/
    while(!RCIF)
        continue;  
    data = RCREG;

   // return RCREG;
    return data ;
}


void main(void) {

     unsigned char x;

     OSCCON = 0x61;             ///  has choosen  4 MHz internal clock

    // Initialise the portc bits


    TRISCbits.TRISC2 = 0;          // LED connected to this pin for output
    TRISCbits.TRISC3 = 0;          // LED connected to this pin for output
    TRISCbits.TRISC4 = 0;          // transmitter pin for output
    TRISCbits.TRISC5 = 1;          // the receiver input pin

    
    // Disable the analog inputs
    ANSEL = 0x00;
    
    // Disable the comparator
    CMCON0 = 0x07;
   
  

    EUSART_init();


    while(1)

  

        x = EUSART_recieve_data();
   
            
        if(x=='g')
        {
            PORTCbits.RC2 = 1;
            __delay_ms(1000);
        }
        else if (x=='a')
        {
            PORTCbits.RC3 = 1;
              __delay_ms(1000);
        }
        else
        {

            RC2 = 0;
            RC3 = 0;
            __delay_ms(1000);
            RC2 = 1;
            RC3 = 1;
            __delay_ms(1000);

            RC2 = 0;
            RC3 = 0;

            __delay_ms(1000);
        }

        EUSART_transmit_data(x);

        __delay_ms(10);
    }

 }



This is my code which I have also posted earlier as you can see I have not changed the main receiver and transmitter function codes and all of the main is also same just few changes of adding couple of delays and nothing much rest of the working is almost same.

But now the whole code is running fine and working fine.


Actually freinds I want to know that why my this code was not running well .


Let me tell you what I observed when I was testing:


The code was not showing the received value well that is it was not able to receive the code in correct format whereas the receiver code is well and good and have been check many times , same thing is with the transmitter as well.

But now all of sudden same code has started working well , which was showing error before writing this article here........................:)


But now my freinds I am not still able to find out that why the code was not working well at that time and why it has started working in good manner now.


Please you guys if have some suggestion or reason for the pic16f688 with HiTech compiler in the MPLAB X IDE and with the bray terminal showing this unusual and strange behaviour , than please do tell me the reason for it.



Thank you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top