您的位置 首页 主动

ds1302带闹钟功用的单片机操控的时钟程序

端口自己修改一下用的1602液晶显示includereg52h>includeintrinsh>defineucharunsignedchardefineuintunsignedintsb

端口自己修正一下 用的1602液晶显现

#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,0×40+6,0×40+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]);
}
}
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/zhudong/318200.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部