51单片机之LCD1602的驱动程序

#include 
#include "./delay/delay.h"

#define LCDPORT P0
#define LCD_WRITE_DATA   1
#define LCD_WRITE_COM    0
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
unsigned char flag = 1;
unsigned char shi = 23, fen = 59, miao = 50;

void lcd1602_write(unsigned char byte, unsigned char flag)
{
		if(flag)
		{
				RS = 1;                    //选择输入的数据为数据
		}
		else
		{
				RS = 0;                      //选择输入的数据为命令
		}
		RW = 0;                           //写
		E = 1;                            //选中lcd
		LCDPORT = byte;
		delay_ms(5);
		E = 0;                          //失能LCD      高电平有效
		
}

void lcd_init()
{
		delay_ms(15);
	  lcd1602_write(0x38,LCD_WRITE_COM);     //设置8位数据接口,两行指令,5*8点阵字符
	  delay_ms(5);
	  lcd1602_write(0x38,LCD_WRITE_COM);
	  delay_ms(5);
	  lcd1602_write(0x38,LCD_WRITE_COM);
	  delay_ms(5);
	  lcd1602_write(0x38,LCD_WRITE_COM);
	  delay_ms(5);
	  lcd1602_write(0x08,LCD_WRITE_COM);            //关闭显示,关闭光标和闪烁
	  delay_ms(5);
	  lcd1602_write(0x01,LCD_WRITE_COM);          //清屏
	  delay_ms(5);
	  lcd1602_write(0x06,LCD_WRITE_COM);        //设置指针方式,画面不平移
	  delay_ms(5);
	  lcd1602_write(0x0c,LCD_WRITE_COM);        //设置指针方式,画面不平移
	  delay_ms(5);
}
void lcd_dis_char(unsigned char x, unsigned char y, unsigned char *s)
{
		if(0 == x)
		{
			 lcd1602_write(y + 0x80,LCD_WRITE_COM);
			 
		}
		else if(1 == x)
		{
			 lcd1602_write(y + 0xc0,LCD_WRITE_COM);
		}
		while(*s)
		{
			
			lcd1602_write(*s, LCD_WRITE_DATA);
			s++;
		}
}

void time0_init()
{
		EA = 1;
	  TMOD |= 0x01;
	  TH0 = (65536 - 20000) / 255;
	  TL0 = (65536 - 20000) % 255;
	  ET0 = 1;
	  TR0 = 1;
}

void time0_isr() interrupt 1
{
	  static unsigned char i = 0;
		TH0 = (65536 - 20000) / 255;
	  TL0 = (65536 - 20000) % 255;
	  i++;
	  if(50 == i)
		{
				i = 0;
			  miao++;
			  if(60 == miao)
				{
						miao = 0;
					  fen++;
					  if(60 == fen)
						{
								fen = 0;
							  shi++;
							  if(24 == shi)
								{
										shi = 0;
								}
						}
				}
		}
}

void lcd_diaplay_time()      //时间的显示函数
{
		lcd1602_write(0x80 + 7, LCD_WRITE_COM);
	  lcd1602_write((shi / 10) + 0x30,LCD_WRITE_DATA);
	  lcd1602_write((shi % 10) + 0x30,LCD_WRITE_DATA);
		lcd1602_write(':',LCD_WRITE_DATA);
		lcd1602_write((fen / 10) + 0x30,LCD_WRITE_DATA);
		lcd1602_write((fen % 10) + 0x30,LCD_WRITE_DATA);
		lcd1602_write(':',LCD_WRITE_DATA);
		lcd1602_write((miao / 10) + 0x30,LCD_WRITE_DATA);
		lcd1602_write((miao % 10) + 0x30,LCD_WRITE_DATA);
		
}

void lcd_dis_hex()        //显示字符或数字
{
		unsigned char temp = 0x2b;
	  lcd1602_write(0x80,LCD_WRITE_COM);
	  lcd1602_write((temp / 16) + 0x30,LCD_WRITE_DATA);
	  lcd1602_write((temp % 16) + 0x37,LCD_WRITE_DATA);
}



void main()
{
	  time0_init();
    lcd_init();
	  lcd_dis_hex();
	 

	    lcd_dis_char(0,2,"time:");

	  while(1)
		{			
					lcd_diaplay_time();
					
			    			 
		}
	
}

你可能感兴趣的:(单片机)