serial lcd program

General discussion on mikroBasic.
Post Reply
Author
Message
gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

serial lcd program

#1 Post by gambrose » 13 Jan 2005 10:19

I adapted this code from the USART example. Its suppose to print the char I send using the terminal to the LCD and return the char back to show all is well with the USART
But when I run it the USART is working as I get the char returned fine but the LCD just fills up with gibberish.
Have I done something very silly or is there something on the easyPIC I should have set up?

Code: Select all

program Serial_LCD


dim i as byte

main:

     Usart_init(9600)         ' initalize USART (9600 baud rate, 1 stop bit, ...

     Lcd_Init(PORTB)          ' initialize LCD connected to portb
     lcd_cmd(LCD_CLEAR)       ' send command to LCD "clear display"
     lcd_cmd(LCD_CURSOR_OFF)  ' send command cursor off

     while true
           if USART_data_ready <> 0 then      ' if data is received
              i = USART_read()                ' read the received data
              Lcd_Out_CP(i)                   ' print on LCD
              Usart_write(i)                  ' send data via USART
           end if
     wend
end.

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: serial lcd program

#2 Post by pizon » 13 Jan 2005 11:46

This is how it works:

Code: Select all

program Test_2005011301


dim i as byte[2]

main:

     Usart_init(9600)         ' initalize USART (9600 baud rate, 1 stop bit, ...

     LCD_Init(PORTB)          ' initialize LCD connected to portb
     LCD_Cmd(LCD_CLEAR)       ' send command to LCD "clear display"
     LCD_Cmd(LCD_CURSOR_OFF)  ' send command cursor off

     i[0] = 1                 ' the 0-th element contains the array length
                              '   information

     while true
           if USART_data_ready <> 0 then      ' if data is received
              i[1] = USART_read()             ' read the received data
              LCD_Out_CP(i)                   ' print on LCD
              Usart_write(i[1])               ' send data via USART
           end if
     wend
end.
The point is (as given in mikroBasic help):
sub procedure LCD_Out_CP(dim byref Text as char[255])
' Prints <Text> (string variable) at current cursor position.
' Both string variables and string constants can be passed.
So, all LCD_Out_xxx routines accept an array as argument. Furthermore, since mikroBasic and mikroPascal share the same LCD_ library, the text arrays in mikroBasic also share the (Pascal-like) convention with mikroPascal, that the 0-th array element contains the length of the text to be displayed.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 13 Jan 2005 12:07

Thanks Pizon

I had made a silly mistake I should have been using LCD_Chr_CP() instead of LCD_Out_CP() that's why I just passed a byte not a text array.

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#4 Post by gambrose » 13 Jan 2005 12:27

Thinking about it shouldn’t the compiler have flagged this up as an error as I was passing the wrong data type?

Anyway I’m looking forward to when we get operator overloading and I can use one function name and not have to remember lots of different ones of the different data types.

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#5 Post by pizon » 13 Jan 2005 12:35

gambrose wrote:Thinking about it shouldn’t the compiler have flagged this up as an error as I was passing the wrong data type?
Yes, there are things to be done in the compiler to improve the data type handling. Some of these are planned for the next release, but I can't give you the details at this moment.
pizon

Post Reply

Return to “mikroBasic General”