Sunday, March 6, 2011

Easy coding for Hitachi HD44780 LCD using open source of PIC compiler



Hitachi HD44780 became standard for microcontroller hobbyist because it was easy to use and cheap in price. That make GCBasic support it. So here I glad to share a tutorial how to code LCD using GCBasic.
This type of LCD can use in two mode; 4 bit mode and 8 bit mode. The different between two is 4 bit mode requires less wire connection than 8 bit mode. In this tutorial I use 4-bit mode and the circuit as below.



I discover that using this compiler more easily than using PicBasic Pro. It only requires 8 lines of definition and the LCD ready to work. The definition code as below:

'Chip model
#chip 16F84A, 4                                 ‘I use 16F84A chip
#define LCD_IO 4                               ‘tell the compiler to use 4-bit mode
#define LCD_RS PORTB.0                 ‘set Register Select to Port B pin 0
#define LCD_NO_RW                        ‘tell the compiler Read/Write pin is ground
#define LCD_Enable PORTB.1          ‘set Enable pin to Port B pin 1
#define LCD_DB4 PORTB.2               ‘set D4 to Port B pin 2 and so on…
#define LCD_DB5 PORTB.3
#define LCD_DB6 PORTB.4
#define LCD_DB7 PORTB.5

After that you can start to display any text or number at your LCD. Use Print command to display the text or number as below but you must put Locate command as well to tell the compiler where to put the text. The syntax of Locate is Locate line, column.

Locate 0, 0
Print "Hello World"
wait 10 ms

And the last part is to make an animation of star. The code is below.

For StarPos = 0 To 16
If StarPos = 0 Then
Put 0, 16, 32
Put 0, 0, 42
Else
Put 0, StarPos - 1, 32
Put 0, StarPos, 42
End If
Wait 250 ms
Next

Now you can compiler the code and test it. You can have more info here


Full code as below :

'Chip model
#chip 16F84A, 4

#define LCD_IO 4
#define LCD_RS PORTB.0
#define LCD_NO_RW
#define LCD_Enable PORTB.1
#define LCD_DB4 PORTB.2
#define LCD_DB5 PORTB.3
#define LCD_DB6 PORTB.4
#define LCD_DB7 PORTB.5


Locate 0, 0
Print "Hello World"
wait 10 ms


For StarPos = 0 To 16
If StarPos = 0 Then
Put 0, 16, 32
Put 0, 0, 42
Else
Put 0, StarPos - 1, 32
Put 0, StarPos, 42
End If
Wait 250 ms
Next