How to write text to an LCD using a pic chip with mikroc
How to write text to an LCD with MikroC using a PIC
In this project, we'll use a pic chip to write text to a 2 row (2x16) LCD screen (HD44780 or similar). I'll be using an 18F8520 pic chip but most 16fxx and 18fxxx micro-controllers from Microchip can be used. I have a BIGPIC 5 development kit which has many features, including Touch Screen, Pencil, MMC/SD card, DS1820, Serial Cable, LCD and GLCD onboard! I'll be using MikroC for development which is ideal, having a built in library that provides communication through a four bit interface. That is perfect for this project and we keep it "high level" with no bit by bit communication with interrupt services routines for multiplexing. It is easy to achieve a simple "hello world" project which you can include as a function in future projects. If you update the 'char' constant with text and call the function, the lcd will print the text. We will only be using three functions from the MikroC library, Lcd_Init, Lcd_Cmd and Lcd_Out.
The code has to initialise the LCD to communicate with the PIC, in this project, we'll connect the LCD to PORTB so that must be declared. MikroC has a routine that automatically configures the PIC to use any port we specify. They also have a custom routine so you can wire customise the wiring but we won't get into that just now. You can use Proteus Isis to simulate the project in a VSM environment also. See the diagram below on how to wire the chip:
Programming text to LCD
Now that we have the wiring sorted out, start up MikroC and make sure everything is set to default. You can do that by clicking the default button on the "new project" window. Set the clock speed to 8 Mhz, with no watchdog timer. The beauty of the whole thing is how simple it is to get the screen displaying text. Copy and paste it into the IDE and compile it. Keep in mind that this is specific to the Mikroelectonika MikroC lcd library. If you need any advice or have any questions, feel free to contact me!
Completed code
char *text = "Hello World"; //Constant holding text to be displayed void main() //Main program { TRISB = 0; // PORTB is output Lcd_Init(&PORTB); // Initialize LCD connected to PORTB Lcd_Cmd(Lcd_CLEAR); // Clear data on lcd Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off while(1) //while loop, not necessarily needed { Lcd_Out(1, 1, text); // Print text to LCD, 2nd row, 1st column } }