51單片機(jī)之LCD1602的驅(qū)動(dòng)程序
#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; //選擇輸入的數(shù)據(jù)為數(shù)據(jù)
}
else
{
RS = 0; //選擇輸入的數(shù)據(jù)為命令
}
RW = 0; //寫(xiě)
E = 1; //選中l(wèi)cd
LCDPORT = byte;
delay_ms(5);
E = 0; //失能LCD 高電平有效
}
void lcd_init()
{
delay_ms(15);
lcd1602_write(0x38,LCD_WRITE_COM); //設(shè)置8位數(shù)據(jù)接口,兩行指令,5*8點(diǎn)陣字符
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); //關(guān)閉顯示,關(guān)閉光標(biāo)和閃爍
delay_ms(5);
lcd1602_write(0x01,LCD_WRITE_COM); //清屏
delay_ms(5);
lcd1602_write(0x06,LCD_WRITE_COM); //設(shè)置指針?lè)绞剑?huà)面不平移
delay_ms(5);
lcd1602_write(0x0c,LCD_WRITE_COM); //設(shè)置指針?lè)绞?,?huà)面不平移
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() //時(shí)間的顯示函數(shù)
{
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() //顯示字符或數(shù)字
{
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();
}
}