LCD1602 C语言驱动程序

来源:互联网 发布:初级程序员考试时间 编辑:程序博客网 时间:2024/05/18 00:03

程序匠人 发表于 2006-4-5 22:32:00  阅读全文(2227) | 回复(-1) | 引用通告(16) | 编辑

 

LCD1602 C语言驱动程序     -|zl0801 发表于 2005-8-25 9:18:00 
 

//lcd1602 drive program
//for 51 mcu
//designed by zhaoliang
//2005-6-14 20:16
#i nclude <reg51.hJ>
/********************************************************************/
//lcd part
#define  LINE1     0
#define  LINE2     1
#define  LINE1_HEAD    0x80
#define  LINE2_HEAD    0xC0
#define  LCD_DELAY_TIME   40
#define  DATA_MODE    0x38
#define  OPEN_SCREEN    0x0C
#define  DISPLAY_ADDRESS   0x80
#define  CLEARSCREEN    LCD_en_command(0x01)
//common part
#define  HIGH   1
#define  LOW    0
#define  TRUE    1
#define  FALSE    0
#define  ZERO    0
#define  MSB    0x80
#define  LSB    0x01

/*******************************************************************/
//change this part at different board
#define  LCDIO     P2
sbit LCD1602_RS=P0^7;   //data command select  1 data  0 command  pin 4
sbit LCD1602_RW=P0^6;   //read write select   1 read   0 write     pin 5
sbit LCD1602_EN=P0^5;   //LCD enable signal             pin 6

/********************************************************************/
void LCD_delay(void);//lcd delay function
void LCD_en_command(unsigned char command);//write command function
void LCD_en_dat(unsigned char temp);//write data function
void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
void LCD_init(void);//lcd initize function

/********************************************************************/
void delay_nms(unsigned int n);//delay function

/********************************************************************/
void main(void)
{
 LCD_init();
 while(TRUE )   
 {
   CLEARSCREEN;
 
  delay_nms(2);
         LCD_write_string(0,LINE1,"   LCD TEST     ");
         LCD_write_string(0,LINE2,"   SUCCESSFUL   ");
       
         delay_nms(200);
        
         CLEARSCREEN;
       
  delay_nms(2);
         LCD_write_string(0,LINE1,"   lcd test     ");
         LCD_write_string(0,LINE2,"   successful   ");
        
         delay_nms(200);                 
 }
}
/********************************************************************/
/******************** LCD PART *************************************/
void LCD_delay(void)  
{
 unsigned char i;
 for(i=LCD_DELAY_TIME;i>ZERO;i--)//be sure lcd reset
   ;
}
/********************************************************************/ 
void LCD_en_command(unsigned char command)
{
 LCDIO=command;
 LCD1602_RS=LOW;  
 LCD1602_RW=LOW;
 LCD1602_EN=LOW;
 LCD_delay();
 LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
 LCDIO=dat;
 LCD1602_RS=HIGH;
 LCD1602_RW=LOW;
 LCD1602_EN=LOW;
 LCD_delay();
 LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
 unsigned char address;
 if (y == LINE1)
  address = LINE1_HEAD + x;
 else
     address = LINE2_HEAD + x;
 LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
 LCD_set_xy( x, y );
 LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y ); //set address
    while (*s)  // write character
    {
     LCDIO=*s;
        LCD_en_dat(*s);  
 s ++;
    }
}
/********************************************************************/
void LCD_init(void)
{
 CLEARSCREEN;//clear screen
 LCD_en_command(DATA_MODE);//set 8 bit data transmission mode
 LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
 LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address
 CLEARSCREEN;//clear screen
}
/********************************************************************/
/*********************** OTHER PART *********************************/
void delay_nms(unsigned int n)     
{
    unsigned int i=0,j=0;
    for (i=n;i>0;i--)
     for (j=0;j<1140;j++); 
}
/********************************************************************/

 
原创粉丝点击