带I2C的LCD1602液晶显示51单片机程序 实现功能:液晶屏上显示日期及动态时间,由中断函数来实现时间的动态变换
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define C51_SCL P3^0
#define C51_SDA P3^1
#define ADDR 0X4E
#define String_len1 16
#define String_len2 16
uchar miao,shi,fen;
uchar count;
sbit SCL = C51_SCL;
sbit SDA = C51_SDA;
uchar code table[]="21-7-30 ";
uchar code table1[]="23:59:45 ";
static void delay_us()
{
;;
}
void delay(uchar n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<120;j++);
}
void IIC_Start()
{
SDA=1;
SCL=1;
delay_us();
SDA=0;
delay_us();
}
void IIC_Ack()
{
uchar i;
SCL=1;
delay_us();
while((SDA==1)&&(i<250))i++;
SCL=0;
delay_us();
}
void IIC_Write_Byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
delay_us();
SDA=CY;
delay_us();
SCL=1;
delay_us();
}
SCL=0;
delay_us();
SDA=1;
delay_us();
}
void IIC_Write_Comm_LCD(uchar comm)
{
uchar data_h = comm & 0xf0;
uchar data_l = (comm & 0x0f) << 4;
IIC_Write_Byte(0x00+data_h);
IIC_Ack();
IIC_Write_Byte(0x04+data_h);
IIC_Ack();
IIC_Write_Byte(0x00+data_h);
IIC_Ack();
delay(5);
IIC_Write_Byte(0x00+data_l);
IIC_Ack();
IIC_Write_Byte(0x04+data_l);
IIC_Ack();
IIC_Write_Byte(0x00+data_l);
IIC_Ack();
delay(5);
}
void IIC_Write_Date_LCD(uchar date)
{
uchar data_h = date & 0xf0;
uchar data_l = (date & 0x0f) << 4;
IIC_Write_Byte(0x01+data_h);
IIC_Ack();
IIC_Write_Byte(0x05+data_h);
IIC_Ack();
IIC_Write_Byte(0x01+data_h);
IIC_Ack();
delay(5);
IIC_Write_Byte(0x01+data_l);
IIC_Ack();
IIC_Write_Byte(0x05+data_l);
IIC_Ack();
IIC_Write_Byte(0x01+data_l);
IIC_Ack();
delay(5);
}
void LCD1602_Init()
{
uchar i = 0;
IIC_Start();
IIC_Write_Byte(ADDR);
IIC_Ack();
IIC_Write_Comm_LCD(0x02);
IIC_Write_Comm_LCD(0x28);
IIC_Write_Comm_LCD(0x08);
IIC_Write_Comm_LCD(0x0c);
IIC_Write_Comm_LCD(0x06);
IIC_Write_Comm_LCD(0x01);
for(i=0;i<String_len1;i++)
{
IIC_Write_Date_LCD(table[i]);
}
IIC_Write_Comm_LCD(0xc0);
for(i=0;i<String_len2;i++)
{
IIC_Write_Date_LCD(table1[i]);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
IIC_Write_Comm_LCD(0x80+0x40+add);
IIC_Write_Date_LCD(0x30+shi);
IIC_Write_Date_LCD(0x30+ge);
}
void main()
{
LCD1602_Init();
while(1);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(0,shi);
}
write_sfm(3,fen);
}
write_sfm(6,miao);
}
}
|