UTM Degree ++‎ > ‎

Sample PIC Program for LCD

// SAMPLE PROGRAM TO DISPLAY ON LCD 
//
//  Yeong: LCD Display Task C Language
//  modified from example code provided by cytron.com.my  

#include <pic.h>
__CONFIG ( 0x3F32 );//configuration for the  microcontroller
//define
#define rs RB4    //RS pin of the LCD display
#define e  RB5    //E pin of the LCD display
#define lcd_data PORTD    //LCD 8-bit data PORT
// function prototype
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
// main function
void main()
{
//set I/O input output
TRISB = 0b00000011; //configure PORTB I/O direction
TRISD = 0b00000000; //configure PORTD I/O direction
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(3); //set the lcd cursor to location 3
send_string("Hello World!"); //display "Hello World"
lcd_goto(25); //set the lcd cursor to location 25
send_string(" Yeong "); //display "Yeong"
while(1)  { }
}
// functions
void delay(unsigned long data) //delay function, the delay time
{ for( ;data>0;data--);           //depend on the given value
}
void send_config(unsigned char data) //send lcd configuration
{ rs=0; //set lcd to configuration mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay(50);
e=0;
delay(50); }
void send_char(unsigned char data) //send lcd character
{ rs=1; //set lcd to display mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay(10);
e=0;
delay(10);
}
void lcd_goto(unsigned char data)  //set the location of the lcd cursor
{ if(data<16)
{ send_config(0x80+data); }
else
{ data=data-20;
send_config(0xc0+data); }
}
void lcd_clr(void) //clear the lcd
{  send_config(0x01);
delay(600);  
}
void send_string(const char *s) //send a string to display in the lcd
{         
  while (s && *s)send_char (*s++);
}
Comments