本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
程序显示 如附件显示
程序如下:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^5;
sbit rw=P2^6;
sbit en=P2^7;
sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;
sbit k1=P3^2;
sbit k2=P3^3;
sbit k3=P3^4;
sbit k4=P3^5;
sbit clock_clk = P3 ^ 0;//ds1302_clk(时钟线)
sbit clock_dat = P3 ^ 1;//ds1302_dat(数据线)
sbit clock_Rst = P3 ^ 2;//ds1302_Rst(复位线)
char idata sett; //模式计数变量
char idata disp_buf1[16];//1602显示缓冲区1(即上排16个字符)
char idata disp_buf2[16];//1602显示缓冲区2(即下排16个字符)
char idata timedata[7];//存放时间值变量
uchar code table[]="Temperature ok !";
uchar i;
uchar code menuyear[]="Set year";
uchar code menumon[]="Set month";
uchar code menudate[]="Set date";
uchar code menuweek[]="Set week";
uchar code menuhour[]="Set hour";
uchar code menumin[]="Set minute";
uchar code menusec[]="Set second";
uchar code menunext[]="Next";
uchar code menuok[]="OK";
uchar *p;
uchar code self_dat[]={
0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00, //一,代码 0x04
0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00, //二,代码 0x05
0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00, //三,代码 0x06
0x00,0x00,0xff,0xf5,0xfb,0xf1,0xff,0x00, //四,代码 0x07
0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00, //五,代码 0x08
0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00, //六,代码 0x09
0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00}; //日,代码 0x0a};//自定义字符
//*********************************************************//
//************LCD相关函数**********************************//
void delay(uint n) //延时时间为n*1ms
{ uchar x=0;
for(;n>0;n--)
{
for(;x<250;x++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void check_busy() //LCD忙检测函数
{
P0=0xff;
rs=0;
rw=1;
en=1;
while(P0&0x80);
en=0;
}
void lcd_wcom(uchar com) //写命令
{ check_busy();
rw=0;
rs=0;
P0=com;
en=1;
en=0;
}
void lcd_wdat(uchar dat) //写数据
{ check_busy();
rw=0;
rs=1;
P0=dat;
en=1;
en=0;
}
void self_data() //写入自定义字符
{
uchar i=0;
lcd_wcom(0x06);//设定地址指针加一
lcd_wcom(0x48);//装入自定义字符的起始地址(CGRAM)
for(;i<64;i++)
lcd_wdat(self_dat[i]);
}
void lcd_init() //初始化
{
lcd_wcom(0x38); //8位,双列,5×
lcd_wcom(0x0c); //开显示,关光标,光标所在字符不闪烁
lcd_wcom(0x06); //显示地址递增
lcd_wcom(0x01); //清屏,光标移到左上角
self_data();
}
void int_display() //初始显示
{
uchar m=0;
lcd_init();
lcd_wcom(0x80); //显示地址
for(m=0;m<16;m++)
{
lcd_wdat(table[m]); //将数组中字符显示出来
delay(1000);
}
delay(10);
lcd_wcom(0x01);
}
void display() //显示缓冲区数据函数
{
uchar i;
lcd_wcom(0x80);
for(i=0;i<16;i++)
lcd_wdat(disp_buf1[i]);
lcd_wcom(0xc0);
for(i=0;i<16;i++)
lcd_wdat(disp_buf2[i]);
}
//**************DS1302相关子函数**************//
//DS1302写入字节(底层协议)
uchar clock_in(){
clock_dat=1;
a0=clock_dat;
clock_clk=1; clock_clk=0; a1=clock_dat;
clock_clk=1; clock_clk=0; a2=clock_dat;
clock_clk=1; clock_clk=0; a3=clock_dat;
clock_clk=1; clock_clk=0; a4=clock_dat;
clock_clk=1; clock_clk=0; a5=clock_dat;
clock_clk=1; clock_clk=0; a6=clock_dat;
clock_clk=1; clock_clk=0; a7=clock_dat;
return(ACC);
}
//1302驱动程序(底层协议)
void clock_out(uchar dd){
ACC=dd;
clock_dat=a0; clock_clk=1; clock_clk=0;
clock_dat=a1; clock_clk=1; clock_clk=0;
clock_dat=a2; clock_clk=1; clock_clk=0;
clock_dat=a3; clock_clk=1; clock_clk=0;
clock_dat=a4; clock_clk=1; clock_clk=0;
clock_dat=a5; clock_clk=1; clock_clk=0;
clock_dat=a6; clock_clk=1; clock_clk=0;
clock_dat=a7; clock_clk=1; clock_clk=0;
}
//DS1302读数据(底层协议)
uchar read_clock(uchar ord){
uchar dd=0;
clock_clk=0;
clock_Rst=0;
clock_Rst=1;
clock_out(ord);
dd=clock_in();
clock_Rst=0;
clock_clk=1;
return(dd);
}
//DS1302写数据(底层协议)
void write_clock(uchar ord, uchar dd){
clock_clk=0;
clock_Rst=0;
clock_Rst=1;
clock_out(ord);
clock_out(dd);
clock_Rst=0;
clock_clk=1;
}
//设置1302的初始时间
void Init_1302(){//(2008年1月1日00时00分00秒星期二)
if(read_clock(0xc1) != 0xf0)
{
write_clock(0x8e,0x00);//允许写操作
write_clock(0x8c,0x08);//年
write_clock(0x8a,0x03);//星期
write_clock(0x88,0x05);//月
write_clock(0x86,0x28);//日
write_clock(0x84,0x00);//小时
write_clock(0x82,0x33);//分钟
write_clock(0x80,0x40);//秒
write_clock(0x90,0xa5);//充电
write_clock(0xc0,0xf0); //判断是否初始化一次标识写入
write_clock(0x8e,0x80);//禁止写操作
}
}
//常用时钟数据读取
void read_clocks()
{
timedata[0] = read_clock(0x81);//读取秒数据
timedata[1] = read_clock(0x83);//读取分钟数据
timedata[2] = read_clock(0x85);//小时
timedata[3] = read_clock(0x87);//日
timedata[4] = read_clock(0x89);//月
timedata[5] = read_clock(0x8b);//星期
timedata[6] = read_clock(0x8d);//年
}
void set_display(uchar com, uchar dat)
{
lcd_wcom(com);
lcd_wdat(dat/16+0x30);
lcd_wdat(dat%16+0x30);
}
void set_selfdisplay(uchar com, uchar dat)
{
lcd_wcom(com);
lcd_wdat(dat);
}
/*void lcd_menu(uchar com,uchar menuaddr) //调时时菜单界面显示
{
uchar length ,i=0;
switch(menuaddr)
{
case 1: {p=menuyear; length=8;lcd_wcom(0xc6);
lcd_wdat(disp_buf1[0]);
lcd_wdat(disp_buf1[1]);
lcd_wdat(disp_buf1[2]);
lcd_wdat(disp_buf1[3]);}break;
case 2: {p=menumon; length=9;
lcd_wcom(0xc8);
lcd_wdat(disp_buf1[5]);
lcd_wdat(disp_buf1[6]);}break;
case 3: {p=menudate; length=8;
lcd_wcom(0xc8);
lcd_wdat(disp_buf1[8]);
lcd_wdat(disp_buf1[9]);}break;
case 4: {p=menuweek;length=8;
lcd_wcom(0xc9);
lcd_wdat(disp_buf1[12]);} break;
case 5: {p=menuhour;length=8;
lcd_wcom(0xc8);
lcd_wdat(disp_buf2[0]);
lcd_wdat(disp_buf2[1]);}break;
case 6: {p=menumin; length=10;
lcd_wcom(0xc8);
lcd_wdat(disp_buf2[3]);
lcd_wdat(disp_buf2[4]);}break;
case 7: {p=menusec; length=10;
lcd_wcom(0xc8);
lcd_wdat(disp_buf2[6]);
lcd_wdat(disp_buf2[7]);}break;
default :break;
}
lcd_wcom(com);
for(;length>0;length--)
{
lcd_wdat(*p);
p++;
}
lcd_wcom(0xc0);
p=menunext; //显示NEXT
for(;i<4;i++)
{
lcd_wdat(*p);
p++;
}
lcd_wcom(0xce);
p=menuok;
for(i=0;i<2;i++)
{
lcd_wdat(*p);
p++;
}
lcd_wcom(0xc9);
}
void int_checkbutton() //初始化按键
{
k1=1;
k2=1;
k3=1;
k4=1;
}
void check_ok() //按下OK键后的处理程序
{
if(k4==0)
{
delay(30);
if(k4==0)
{
while(!k4);
sett=0;
lcd_wcom(0x0c);
TR0=1;
}
}
} */
void int_t0() interrupt 1 using 1
{
TH0=0x3c;
TL0=0xb0;
i++;
if(i==10)
{
i=0; //更新显示数据
display();
}
}
//********************************************//
//**************主程序************************//
void main()
{
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
int_display();
// int_checkbutton();
Init_1302();
while(1)
{
/* if(k1==0)
{
delay(30);
if(k1==0)
{
while(!k1);
sett++;
TR0=0;
if(sett==1)
{
lcd_wcom(0x0f);
lcd_wcom(0x01);
lcd_menu(0x84,1);
}
if(sett==2)
{
lcd_wcom(0x01);
lcd_menu(0x84,2);
}
if(sett==3)
{
lcd_wcom(0x01);
lcd_menu(0x84,3);
}
if(sett==4)
{
lcd_wcom(0x01);
lcd_menu(0x84,4);
}
if(sett==5)
{
lcd_wcom(0x01);
lcd_menu(0x84,5);
}
if(sett==6)
{
lcd_wcom(0x01);
lcd_menu(0x84,6);
}
if(sett==7)
{
lcd_wcom(0x01);
lcd_menu(0x84,7);
}
if(sett==8)
{
sett=0;
lcd_wcom(0x0c);
TR0=1;
}
}
}if(sett!=0)
{
if(k2==0)
{
delay(30);
if(k2==0)
{
while(!k2);
if(sett==1)
{ uchar item ;
item=timedata[6];
item=(item/16)*10+item%16;
item++;
if(item==100)item=0;
item=(item/10)*16+item%10;
set_display(0xc8,item);
write_clock(0x8e,0x00);//允许写操作
write_clock(0x8c,item);
lcd_wcom(0xc9);
}
if(sett==2)
{ uchar item ;
item=timedata[4];
item=(item/16)*10+item%16;
item++;
if(item==13)item=1;
item=(item/10)*16+item%10;
set_display(0xc8,item);
write_clock(0x8e,0x00);//允许写操作
write_clock(0x88,item);
lcd_wcom(0xc9);
}
if(sett==3)
{ uchar item ;
item=timedata[3];
item=(item/16)*10+item%16;
item++;
if(item==32)item=1;
item=(item/10)*16+item%10;
set_display(0xc8,item);
write_clock(0x8e,0x00);//允许写操作
write_clock(0x86,item);
lcd_wcom(0xc9);
} |