C Example: Serial LCD

From Mech
Jump to: navigation, search

Serial LCD with the PIC

An example serial LCD can be found at: [1] This is a 2x16 backlit LCD that displays ASCII characters at 2400, 9600, and 19200 baud. It requires a 5V connection, a ground connection, and only 1 i/o line for communication. The manual for this LCD can be found at: [2] This LCD requires TTL level RS-232 communication, which matches the output of the PIC, so a level shifting chip is not required. Connect your PIC output pin directly to the LCD rx pin.

Code

Program to display output using a serial LCD

First include header file with definitions for specific PIC. Set fuses. HS is type of external clock, low voltage protection (LVP) is off, and the watchdog timer (WDT) is off. External clock frequency of 20 MHz is specified.

  #include <18f4520.h>
  #fuses HS,NOLVP,NOWDT
  #use delay (clock=20000000)

Setup the PIC to use pin A0 (or any other pin) for RS-232. Match the baud rate to your LCD.

  #use rs232(baud=19200, xmit=PIN_A0)

Begin main body of program.

  void main(void) {

Turn on the LCD by placing the communication pin high for 100mS

  output_high(PIN_A0);
  delay_ms(100);

Send the LCD a character to initialize the screen. A list of characters (0-255) and what they do appears in the manual. It is a good idea to add a slight delay after outputting to the LCD to avoid the LCD missing signals.

  putc(25); //turn lcd on, cursor on and character blink
  delay_ms(1);
  putc(17); //backlight on
  delay_ms(1);

Note that the command for clearing the LCD and setting the cursor to the origin takes the LCD several steps and needs a longer pause.

  putc(12); //return to origin and clear, must wait 5ms
  delay_ms(5);

Output some text to the LCD, show it for a second.

  printf("LCD Test Program");
  delay_ms(1000);

Use while to set up infinite loop.

     while(TRUE){

Clear the LCD screen and write something. Lookup the printf() function for instructions on how to send variables. Example: printf("temp = %f deg C", temperature); will place the truncated float temperature in place of the %f

     putc(12); //return to origin and clear, must wait 5ms
     delay_ms(5);
     printf("Something");
     delay_ms(500);
     }
  }


Associated Circuitry

Personal tools