Usart data to LCD problem

General discussion on mikroC.
Post Reply
Author
Message
GrunF
Posts: 13
Joined: 05 Dec 2010 17:01

Usart data to LCD problem

#1 Post by GrunF » 19 May 2011 23:23

hello

so it isn't nothing new but i just cant understand where i making a mistake, try to catch usart data and display it on lcd 2x16, but sometimes it work sometimes not....any advice...

Code: Select all

int i;
char busdata[15];
void main() {
 CM1CON0=0; //'disable COMPARATOR 1.
 CM2CON0=0; //'disabl
 ANSEL  = 0;                              // Configure AN pins as digital I/O
ANSELH = 0;
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init

  LCD_Cmd(LCD_CLEAR);       // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);  // Turn cursor off
  LCD_Out(1,1, "Poslani text:");       // Print text to LCD
   USART_init(9600);                     // init USART
i=1;                                    
while(1)
{
while (!Usart_Data_Ready ()) {} // Wait for a byte

        if (Usart_Read() == '#') // When we get a #
        {
           busdata[0]='#';  // Load '#' to  busdata Array

            // Receive the "real" data

            do
            {
              while (!Usart_Data_Ready ()) {} // Wait for a byte
              busdata[i++]= Usart_Read(); // Add character to array
            }while(busdata[i]!='\r');// Get to here when <CR> received

        }
        if (busdata[i]=='\r')
        do{
        busdata[i++]=' ';
        }while (i!=16);
        LCD_Out(2,1,busdata);
        i=1;
     // Delay_ms(200);
  //       LCD_Cmd(LCD_CLEAR);
    //     LCD_Out(1,1, "test");
}
 }

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Usart data to LCD problem

#2 Post by Muphy » 20 May 2011 09:33

OK, without seeing the data stream you are sending to the UART, it's a wee bit tricky but the most obvious problem I can see on first glance is this code:

Code: Select all

 do
            {
              while (!Usart_Data_Ready ()) {} // Wait for a byte
              busdata[i++]= Usart_Read(); // Add character to array
            }while(busdata[i]!='\r');// Get to here when <CR> received

Your array busdata is defined as being 15 bytes long but if you receive more than 15 bytes before you get \r then
you have overflowed the array and lost the data.


HTH

M

GrunF
Posts: 13
Joined: 05 Dec 2010 17:01

Re: Usart data to LCD problem

#3 Post by GrunF » 20 May 2011 21:21

whell i try it with a 64 byte size no improvement? any help! thx for reply

GrunF
Posts: 13
Joined: 05 Dec 2010 17:01

Re: Usart data to LCD problem

#4 Post by GrunF » 21 May 2011 23:15

ok i download ademo version of mikroC Pro and with Uart_Read_Text function everything is working just fine!

Post Reply

Return to “mikroC General”