ds1302带闹钟功能的单片机控制的时钟程序
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit nz=P2^3;//闹钟
sbit lcdrs=P3^5;
sbit lcdwr=P3^6;
sbit lcden=P3^4;//液晶使能
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit ajjd=P3^7;//按键
sbit clk=P2^1;
sbit io=P2^0;
sbit rst=P2^5;
void naozhongscan();
uchar num0,num1,num2,num3,num4;
void init();
void delay(uchar);
void keyscan();
void didi();
uchar lcd_date_add[7]={0x40+9,0x40+6,0x40+3,0xa,7,0xd,4};
uchar date_write_add[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//miao fen shi ri yue zhou nian
uchar date_read_add[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar date[7]={30,32,16,27,2,7,11};
uchar date1[7];
uchar naozhong[7]={35,32,16,27,2,7,11};
ucharflag,flag1,flag2;
uchar code table0[]=" - - ";
uchar code table1[]=" : : ";
void lcd_com(uchar);
void lcd_date(uchar);
void lcd_time(uchar,uchar);
void write_ds1302(uchar add,uchar date);
uchar read_ds1302(uchar add);
void read_rtc();
void set_ds1302(uchar add,uchar time);
void display();
void init_ds1302();
void main()
{
wela=1;
P1=0xff;
wela=0;
init();
init_ds1302();
while(1)
{
if(flag1==1)
naozhongscan();
read_rtc();
if(flag==0)
display();
keyscan();
}
}
void init()
{
flag=0;
flag1=0;
flag2=0;
lcden=0;
lcdwr=0;
num2=0;
num3=0;
num4=0;
lcd_com(0x38);//显示模式设置,16*2显示,5*7点阵,8位数据接口
lcd_com(0x01);//清屏,显示、指针清零
lcd_com(0x0c);//显示开关及光标设置,开显示,显示光标,光标闪烁
lcd_com(0x06);//读写后指针加一光标加一
lcd_com(0x80);//从第一个显示地址开始显示
for(num0=0;num0<16;num0++)
lcd_date(table0[num0]);
lcd_com(0x80+0x40);
for(num0=0;num0<11;num0++)
lcd_date(table1[num0]);
}
void init_ds1302()
{
uchar i,j;
uchar flag3;
flag3=read_ds1302(0x81);
if(flag3&0x80)//如果ch=1则进行初始化
{
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
j=date[i]/10;
date[i]=date[i]%10;
date[i]=date[i]+j*16;
write_ds1302(date_write_add[i],date[i]);
}
write_ds1302(0x8e,0x80);
}
}
void delay(uchar z)
{
uchar x,y;
for(x=110;x>0;x--)
for(y=z;y>0;y--);
}
void didi()
{
nz=0;
delay(50);
nz=1;
delay(100);
nz=0;
delay(50);
nz=1;
}
void lcd_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_date(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_time(uchar add,uchar Date)
{
uchar shi,ge;
shi=Date/10;
ge=Date%10;
lcd_com(0x80+add);
lcd_date(0x30+shi);
lcd_date(0x30+ge);
}
void keyscan()
{
ajjd=0;
if(num3!=2)
{
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1);
num2++;
if(num2==1)
{
flag=1;
lcd_com(0x80+lcd_date_add[0]);
lcd_com(0x0f);//显示开关及光标设置,开显示,显示光标,光标闪烁
}
if(num2==2)
{
lcd_com(0x80+lcd_date_add[1]);
}
if(num2==3)
{
lcd_com(0x80+lcd_date_add[2]);
}
if(num2==4)
{
lcd_com(0x80+lcd_date_add[3]);
}
if(num2==5)
{
lcd_com(0x80+lcd_date_add[4]);
}
if(num2==6)
{
lcd_com(0x80+lcd_date_add[5]);
}
if(num2==7)
{
lcd_com(0x80+lcd_date_add[6]);
}
if(num2==8)
{
lcd_com(0x0c);//关光标
num2=0;
flag=0;
}
}
}
if(num2!=0)
{
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
if(num2==1)
{
date[0]++;
if(date[0]==60)
date[0]=0;
_nop_();
lcd_time(lcd_date_add[0],date[0]);
lcd_com(0x80+lcd_date_add[0]);
set_ds1302(date_write_add[0],date[0]);
}
if(num2==2)
{
date[1]++;
if(date[1]==60)
date[1]=0;
_nop_();
lcd_time(lcd_date_add[1],date[1]);
lcd_com(0x80+lcd_date_add[1]);
set_ds1302(date_write_add[1],date[1]);
}
if(num2==3)
{
date[2]++;
if(date[2]==24)
date[2]=0;
_nop_();
lcd_time(lcd_date_add[2],date[2]);
lcd_com(0x80+lcd_date_add[2]);
set_ds1302(date_write_add[2],date[2]);
}
if(num2==4)
{
date[3]++;
if(date[3]==32)
date[3]=1;
_nop_();
lcd_time(lcd_date_add[3],date[3]);
lcd_com(0x80+lcd_date_add[3]);
set_ds1302(date_write_add[3],date[3]);
}
if(num2==5)
{
date[4]++;
if(date[4]==13)
date[4]=1;
_nop_();
lcd_time(lcd_date_add[4],date[4]);
lcd_com(0x80+lcd_date_add[4]);
set_ds1302(date_write_add[4],date[4]);
}
if(num2==6)
{
date[5]++;
if(date[5]==8)
date[5]=1;
_nop_();
lcd_time(lcd_date_add[5],date[5]);
lcd_com(0x80+lcd_date_add[5]);
set_ds1302(date_write_add[5],date[5]);
}
if(num2==7)
{
date[6]++;
if(date[6]==99)
date[6]=1;
_nop_();
lcd_time(lcd_date_add[6],date[6]);
lcd_com(0x80+lcd_date_add[6]);
set_ds1302(date_write_add[6],date[6]);
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
if(num2==1)
{
date[0]--;
if(date[0]==-1)
date[0]=59;
_nop_();
lcd_time(lcd_date_add[2],date[0]);
lcd_com(0x80+lcd_date_add[0]);
set_ds1302(date_write_add[0],date[0]);
}
if(num2==2)
{
date[1]--;
if(date[1]==-1)
date[1]=59;
_nop_();
lcd_time(lcd_date_add[1],date[1]);
lcd_com(0x80+lcd_date_add[1]);
set_ds1302(date_write_add[1],date[1]);
}
if(num2==3)
{
date[2]--;
if(date[2]==-1)
date[2]=23;
_nop_();
lcd_time(lcd_date_add[2],date[2]);
lcd_com(0x80+lcd_date_add[2]);
set_ds1302(date_write_add[2],date[2]);
}
if(num2==4)
{
date[3]--;
if(date[3]==0)
date[3]=31;
_nop_();
lcd_time(lcd_date_add[3],date[3]);
lcd_com(0x80+lcd_date_add[3]);
set_ds1302(date_write_add[3],date[3]);
}
if(num2==5)
{
date[4]--;
if(date[4]==0)
date[4]=12;
_nop_();
lcd_time(lcd_date_add[4],date[4]);
lcd_com(0x80+lcd_date_add[4]);
set_ds1302(date_write_add[4],date[4]);
}
if(num2==6)
{
date[5]--;
if(date[5]==0)
date[5]=7;
_nop_();
lcd_time(lcd_date_add[5],date[5]);
lcd_com(0x80+lcd_date_add[5]);
set_ds1302(date_write_add[5],date[5]);
}
if(num2==7)
{
date[6]--;
if(date[6]==0)
date[6]=99;
_nop_();
lcd_time(lcd_date_add[6],date[6]);
lcd_com(0x80+lcd_date_add[6]);
set_ds1302(date_write_add[6],date[6]);
}
}
}
}
}
if(k4==0)
{
delay(5);
if(k4==0)
{
while(!k4);
num3++;
}
}
if(num3==1)
{
flag1=1;
lcd_com(0x80+1);
lcd_date(0x41);
}
if(num3==2)
{
ajjd=0;
flag1=0;
if(num4==0)
{
for(num0=0;num0<7;num0++)
lcd_time(lcd_date_add[num0],naozhong[num0]);
lcd_com(0x80+1);
lcd_com(0x0c);//关光标
lcd_date(0x42);
}
//lcd_com(0x0f);
flag=1;
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1);
num4++;
if(num4==1)
{
lcd_com(0x80+lcd_date_add[0]);
lcd_com(0x0f);//显示开关及光标设置,开显示,显示光标,光标闪烁
}
if(num4==2)
{
lcd_com(0x80+lcd_date_add[1]);
}
if(num4==3)
{
lcd_com(0x80+lcd_date_add[2]);
}
if(num4==4)
{
lcd_com(0x80+lcd_date_add[3]);
}
if(num4==5)
{
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
lcd_com(0x80+lcd_date_add[5]);
}
if(num4==7)
{
lcd_com(0x80+lcd_date_add[6]);
}
if(num4==8)
{
lcd_com(0x0c);//关光标
num4=0;
flag=1;
}
}
}
if(num4!=0)
{
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
if(num4==1)
{
naozhong[0]++;
if(naozhong[0]==60)
naozhong[0]=0;
_nop_();
lcd_time(lcd_date_add[0],naozhong[0]);
lcd_com(0x80+lcd_date_add[0]);
}
if(num4==2)
{
naozhong[1]++;
if(naozhong[1]==60)
naozhong[1]=0;
_nop_();
lcd_time(lcd_date_add[1],naozhong[1]);
lcd_com(0x80+lcd_date_add[1]);
}
if(num4==3)
{
naozhong[2]++;
if(naozhong[2]==24)
naozhong[2]=0;
_nop_();
lcd_time(lcd_date_add[2],naozhong[2]);
lcd_com(0x80+lcd_date_add[2]);
}
if(num4==4)
{
naozhong[3]++;
if(naozhong[3]==32)
naozhong[3]=1;
_nop_();
lcd_time(lcd_date_add[3],naozhong[3]);
lcd_com(0x80+lcd_date_add[3]);
}
if(num4==5)
{
naozhong[4]++;
if(naozhong[4]==13)
naozhong[4]=1;
_nop_();
lcd_time(lcd_date_add[4],naozhong[4]);
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
naozhong[5]++;
if(naozhong[5]==8)
naozhong[5]=1;
_nop_();
lcd_time(lcd_date_add[5],naozhong[5]);
lcd_com(0x80+lcd_date_add[5]);
}
if(num4==7)
{
naozhong[6]++;
if(naozhong[6]==99)
naozhong[6]=1;
_nop_();
lcd_time(lcd_date_add[6],naozhong[6]);
lcd_com(0x80+lcd_date_add[6]);
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
if(num4==1)
{
naozhong[0]--;
if(naozhong[0]==-1)
naozhong[0]=59;
_nop_();
lcd_time(lcd_date_add[2],naozhong[0]);
lcd_com(0x80+lcd_date_add[0]);
}
if(num4==2)
{
naozhong[1]--;
if(naozhong[1]==-1)
naozhong[1]=59;
_nop_();
lcd_time(lcd_date_add[1],naozhong[1]);
lcd_com(0x80+lcd_date_add[1]);
}
if(num4==3)
{
naozhong[2]--;
if(naozhong[2]==-1)
naozhong[2]=23;
_nop_();
lcd_time(lcd_date_add[2],naozhong[2]);
lcd_com(0x80+lcd_date_add[2]);
}
if(num4==4)
{
naozhong[3]--;
if(naozhong[3]==0)
naozhong[3]=31;
_nop_();
lcd_time(lcd_date_add[3],naozhong[3]);
lcd_com(0x80+lcd_date_add[3]);
}
if(num4==5)
{
naozhong[4]--;
if(naozhong[4]==0)
naozhong[4]=12;
_nop_();
lcd_time(lcd_date_add[4],naozhong[4]);
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
naozhong[5]--;
if(naozhong[5]==0)
naozhong[5]=7;
_nop_();
lcd_time(lcd_date_add[5],naozhong[5]);
lcd_com(0x80+lcd_date_add[5]);
}
if(num4==7)
{
naozhong[6]--;
if(naozhong[6]==0)
naozhong[6]=99;
_nop_();
lcd_time(lcd_date_add[6],naozhong[6]);
lcd_com(0x80+lcd_date_add[6]);
}
}
}
}
}
if(num3==3)
{
flag=0;
flag1=0;
lcd_com(0x80+1);
lcd_date(20);//让lcd什么都不显示
num3=0;
num4=0;
}
}
void write_ds1302(ucharadd,uchar date)
{
uchar i,temp;
rst=0;
clk=0;
_nop_();
rst=1;
for(i=0;i<8;i++)
{
clk=0;
temp=add;
io=temp&0x01;
add>>=1;
clk=1;
_nop_();
}
for(i=0;i<8;i++)
{
clk=0;
temp=date;
io=temp&0x01;
date>>=1;
clk=1;
_nop_();
}
rst=0;
}
uchar read_ds1302(uchar add)
{
uchar i,temp,data1,j;
rst=0;
clk=0;
_nop_();;
rst=1;
for(i=0;i<8;i++)
{
clk=0;
temp=add;
io=temp&0x01;
add>>=1;
clk=1;
_nop_();
}
for(i=0;i<8;i++)
{
data1>>=1;
clk=0;
if(io)
data1=data1|0x80;
clk=1;
}
rst=0;
_nop_();
j=data1%16;
data1=data1/16;
data1=data1*10+j;
return(data1);
}
void set_ds1302(uchar add,uchar time)
{
uchar j;
write_ds1302(0x8e,0x00);
j=time/10;
time=time%10;
time=time+j*16;
write_ds1302(add,time);
write_ds1302(0x8e,0x80);
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
date[i]=read_ds1302(date_read_add[i]);
}
}
void display()
{
uchar i;
for(i=0;i<7;i++)
{
lcd_time(lcd_date_add[i],date[i]);
}
}
void naozhongscan()
{
uchar i=0;
uchar j=0;
while((j==0)&&(i<7))
{
j=naozhong[i]^date[i];
i++;
}
if(i==7)
didi();
}
ds1302闹钟功能单片机控 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)