#include
#define uint unsigned int
#define uchar unsigned char
/************声明*************/
void ds1302_init();
/*************LCD12864*************/
sbit beer=P2^0;
#define P0 P0
uchar co
uchar co
uchar co
sbit lcd_rs=P1^0;//定义LCD引脚
sbit lcd_rw=P1^1;
sbit lcd_en=P1^2;
sbit lcd_psb=P2^4;
sbit lcd_rst=P2^3;
void delay(uint z) //延时
{
uint x,y;
for(x=110;x>0;x--);
for(y=z;y>0;y--);
}
void lcd_w_com(uchar com)//写命令
{
lcd_rs=0;
lcd_rw=0;
lcd_en=0;
P0=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void lcd_w_da
{
lcd_rs=1;
lcd_rw=0;
lcd_en=0;
P0=date;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
void lcd_init()//液晶初始化
{
lcd_rst=0;
delay(10);
lcd_rst=1;
delay(10);
lcd_psb=1;
P3=0xff;
key5=1;
key6=1;
lcd_w_com(0x30);
lcd_w_com(0x0c);
lcd_w_com(0x14);
lcd_w_com(0x01);
}
void lcd_pos(uchar x,uchar y)//选择写入位置
{
uchar pos;
if(x==0)
{x=0x80;}
else if(x==1)
{x=0x90;}
else if(x==2)
{x=0x88;}
else if(x==3)
{x=0x98;}
pos=x+y;
lcd_w_com(pos);
}
void clr_screen()//清屏
{
lcd_w_com(0x34);
delay(5);
lcd_w_com(0x30);
delay(5);
lcd_w_com(0x01);
}
void disp_chinese(uchar co
{
while(*s>0)
{
lcd_w_da
s++;
delay(500);
}
}
void disp_title()
{
uchar i;
lcd_pos(0,2);
i=0;
while(table1[i]!='\0')
{
lcd_w_da
i++;
}
lcd_pos(1,0);
i=0;
while(table2[i]!='\0')
{
lcd_w_da
i++;
}
lcd_pos(2,0);
i=0;
while(table3[i]!='\0')
{
lcd_w_da
i++;
}
lcd_pos(3,0);
i=0;
}
/***********DS1302************/
sbit ds1302_sclk=P3^4;// 时钟线引脚
sbit ds1302_io=P3^5; //数据线引脚
sbit ds1302_rst=P3^6; //复位线引脚
uchar year,month,day,week,hour,minute,second;
uchar time[20];
uchar ds1302_r_da
{
uchar i,j,k;
ds1302_rst=0;
ds1302_sclk=0;
ds1302_rst=1;
for(i=0x01,j=0;j<8;j++)
{
if(addr&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<<1;
ds1302_sclk=0;
}
k=0;
for(i=0;i<8;i++)
{
k>>=1;
ds1302_sclk=0;
if(ds1302_io)
k|=0x80;
ds1302_sclk=1;
}
ds1302_rst=0;
ds1302_sclk=1;
return(k);
}
void ds1302_w_da
{
uchar i,j;
ds1302_rst=0;
ds1302_sclk=0;
ds1302_rst=1;
for(i=0x01,j=0;j<8;j++)
{
if(addr&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<<1;
ds1302_sclk=0;
}
for(i=0x01,j=0;j<8;j++)
{
if(dat&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<<1;
ds1302_sclk=0;
}
ds1302_rst=0;
ds1302_sclk=1;
}
void ds1302_init()//DS1302初始化 (如果有初始化,每次上电都将
// 重新设置时间,无法让1302断电保持!)
{
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
ds1302_w_da
}
void dotime()//时间处理
{
time[0]=ds1302_r_da
time[1]=ds1302_r_da
time[2]=ds1302_r_da
time[3]=ds1302_r_da
time[4]=ds1302_r_da
time[5]=ds1302_r_da
time[6]=ds1302_r_da
second=((time[0]&0x70)>>4)*10+(time[0]&0x0f);
minute=((time[1]&0x70)>>4)*10+(time[1]&0x0f);
hour=((time[2]&0x70)>>4)*10+(time[2]&0x0f);
day=((time[3]&0x70)>>4)*10+(time[3]&0x0f);
month=((time[4]&0x70)>>4)*10+(time[4]&0x0f);
year=((time[5]&0x70)>>4)*10+(time[5]&0x0f);
week=((time[6]&0x70)>>4)*10+(time[6]&0x0f);
}
void disp_week()//显示星期
{
dotime();
switch(week)
{
case 1:
lcd_pos(3,0);
disp_chinese("星期一 ");
break;
case 2:
lcd_pos(3,0);
disp_chinese("星期二");
break;
case 3:
lcd_pos(3,0);
disp_chinese("星期三");
break;
case 4:
lcd_pos(3,0);
disp_chinese("星期四");
break;
case 5:
lcd_pos(3,0);
disp_chinese("星期五");
break;
case 6:
lcd_pos(3,0);
disp_chinese("星期六");
break;
case 7:
lcd_pos(3,0);
disp_chinese("星期日");
break;
}
}
void disp_da
{
dotime();
lcd_pos(1,3);
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_pos(2,3);
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
lcd_w_da
}
void main()
{
lcd_init();//液晶初始化
dotime();
while(1)
{
dotime();//处理时间
disp_title();//显示表头
disp_week();//显示星期
disp_da
keyscan(); //扫描键盘
}
}
上一篇:LCD12864测试程序
下一篇:DS18B20测试程序
推荐阅读最新更新时间:2024-03-16 15:06