51单片机之LCD1602的驱动程序

Posted 风尘璞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51单片机之LCD1602的驱动程序相关的知识,希望对你有一定的参考价值。

#include <reg52.h>
#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();
					
			    			 
		
	

以上是关于51单片机之LCD1602的驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

Proteus仿真51单片机+LCD1602驱动模板

Proteus仿真51单片机+LCD1602驱动显示模板程序

51单片机+驱动LCD1602模板范例+Proteus仿真

Proteus仿真51单片机+LCD1602+74HC164驱动显示

51单片机+DS1302+lcd1602+按键时间调整+Proteus仿真

51单片机DS18B20+lcd1602温度报警器( Proteus仿真程序)