编写ds1302的单片机电子时钟C语言程序,高分悬赏,30分全给您了,下面是要求 30
采用AT89C52为主控芯片,实现电子时钟的设计,实现的功能如下:1.启动时显示制作的年、月、日、制作者的学号等信息;2.24小时计时功能(精确到秒);3.整点报时功能;...
采用AT89C52为主控芯片,实现电子时钟的设计,实现的功能如下:
1. 启动时显示制作的年、月、日、制作者的学号等信息;
2. 24小时计时功能(精确到秒);
3. 整点报时功能;
4.对时、分、秒进行校准。 展开
1. 启动时显示制作的年、月、日、制作者的学号等信息;
2. 24小时计时功能(精确到秒);
3. 整点报时功能;
4.对时、分、秒进行校准。 展开
2个回答
展开全部
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit IO = P1^0;
sbit SCLK = P1^1;
sbit RST = P1^2;
sbit RS = P0^0;
sbit RW = P0^1;
sbit EN = P0^2;
sbit KEY1=P3^4;
sbit KEY2=P3^5;
sbit KEY3=P3^6;
uchar *WEEK[]={"SUN","***","MON","TUS","WEN","THU","FRI","SAT"};
uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00 "};
uchar LCD_DSY_BUFFER2[]={"TIME 00:00:00 "};
uchar DateTime[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //秒,分,时,日,月,周,年
uchar flag,flag_1,i,miao,fen,shi,ri,yue,zhou,nian;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/*****************************************************/
//函数名称: Write_A_Byte_TO_DS1302(uchar x)
//函数功能: 向1302写入一个字节
//入口参数: x
//出口参数: 无
//调用子程序: 无
/*****************************************************/
void Write_A_Byte_TO_DS1302(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
IO=x&0x01;
SCLK=1;
SCLK=0;
x>>=1;
}
}
void Write_DS1302(uchar add,uchar num)
{
SCLK=0;
RST=0;
RST=1;
Write_A_Byte_TO_DS1302(add);
Write_A_Byte_TO_DS1302(num);
RST=0;
SCLK=1;
}
/*****************************************************/
//函数名称: Get_A_Byte_FROM_DS1302()
//函数功能: 从1302读一个字节
//入口参数: 无
//出口参数: b/16*10+b%16
//调用子程序: 无
/*****************************************************/
uchar Get_A_Byte_FROM_DS1302()
{
uchar i,b=0x00;
for(i=0;i<8;i++)
{
b |= _crol_((uchar)IO,i);
SCLK=1;SCLK=0;
}
return b/16*10+b%16;
}
/*****************************************************/
//函数名称: Read_Data(uchar addr)
//函数功能: 指定位置读数据
//入口参数: addr
//出口参数: dat
//调用子程序: Write_Abyte_1302(addr)
/*****************************************************/
uchar Read_Data(uchar addr)
{
uchar dat;
RST = 0;SCLK=0;RST=1;
Write_A_Byte_TO_DS1302(addr);
dat = Get_A_Byte_FROM_DS1302();
SCLK=1;RST=0;
return dat;
}
/*****************************************************/
//函数名称: GetTime()
//函数功能: 读取时间
//入口参数: 无
//出口参数: 无
//调用子程序: 无
/*****************************************************/
void GetTime()
{
uchar i,addr = 0x81;
for(i=0;i<7;i++)
{
DateTime[i]=Read_Data(addr);addr+=2;
}
}
uchar Read_LCD_State()
{
uchar state;
RS=0;RW=1;EN=1;DelayMS(1);
state=P2;
EN = 0;DelayMS(1);
return state;
}
void LCD_Busy_Wait()
{
while((Read_LCD_State()&0x80)==0x80);
DelayMS(5);
}
void Write_LCD_Data(uchar dat) //写数据到1602
{
LCD_Busy_Wait();
RS=1;RW=0;EN=0;P2=dat;EN=1;DelayMS(1);EN=0;
}
void Write_LCD_Command(uchar cmd) //写命令
{
LCD_Busy_Wait();
RS=0;RW=0;EN=0;P2=cmd;EN=1;DelayMS(1);EN=0;
}
void Init_LCD() //1602 初始化
{
Write_LCD_Command(0x38);
DelayMS(1);
Write_LCD_Command(0x01);
DelayMS(1);
Write_LCD_Command(0x06);
DelayMS(1);
Write_LCD_Command(0x0c);
DelayMS(1);
}
void Set_LCD_POS(uchar p)
{
Write_LCD_Command(p|0x80);
}
void Display_LCD_String(uchar p,uchar *s) //1602显示
{
uchar i;
Set_LCD_POS(p);
for(i=0;i<16;i++)
{
Write_LCD_Data(s[i]);
DelayMS(1);
}
}
void write_com(uchar com)
{
RS=0;
P2=com;
DelayMS(5);
EN=1;
DelayMS(5);
EN=0;
}
void write_date(uchar date)
{
RS=1;
P2=date;
DelayMS(5);
EN=1;
DelayMS(5);
EN=0;
}
void display(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void display1(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void Format_DateTime(uchar d,uchar *a)
{
a[0]=d/10+'0';
a[1]=d%10+'0';
}
uchar ZH(uchar dat)
{
uchar tmp;
tmp=dat/10;
dat=dat%10;
dat=dat+tmp*16;
return dat;
}
void Keyscan()
{
flag_1=1;
while(flag_1)
{
if(KEY1==0)
{
DelayMS(5);
while(!KEY1);
flag=(flag+1)%8;
switch(flag)
{
case(1): Write_LCD_Command(0x0f);
Write_LCD_Command(0x80+0x40+11);
break;
case(2): Write_LCD_Command(0x80+0x40+8);
break;
case(3): Write_LCD_Command(0x80+0x40+5);
break;
case(4): Write_LCD_Command(0x80+13);
break;
case(5): Write_LCD_Command(0x80+11);
break;
case(6): Write_LCD_Command(0x80+8);
break;
case(7): Write_LCD_Command(0x80+5);
break;
case(0): flag_1=0;
Write_LCD_Command(0x0c);
//miao
Write_DS1302(0x8e,0x00);
Write_DS1302(0x80,ZH(DateTime[0]));
Write_DS1302(0x8e,0x80);
//fen
Write_DS1302(0x8e,0x00);
Write_DS1302(0x82,ZH(DateTime[1]));
Write_DS1302(0x8e,0x80);
//shi
Write_DS1302(0x8e,0x00);
Write_DS1302(0x84,ZH(DateTime[2]));
Write_DS1302(0x8e,0x80);
//ri
Write_DS1302(0x8e,0x00);
Write_DS1302(0x86,ZH(DateTime[3]));
Write_DS1302(0x8e,0x80);
// yue
Write_DS1302(0x8e,0x00);
Write_DS1302(0x88,ZH(DateTime[4]));
Write_DS1302(0x8e,0x80);
//nian
Write_DS1302(0x8e,0x00);
Write_DS1302(0x8c,ZH(DateTime[6]));
Write_DS1302(0x8e,0x80);
break;
}
}
if(flag!=0)
{
if(KEY2==0)
{
DelayMS(5);
if(KEY2==0)
while(!KEY2);
if(flag==1) //miao
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==2) //fen
{
DateTime[1]++;
if(DateTime[1]==60)
DateTime[1]=0;
write_com(0x80+0x40+8);
display(8,DateTime[1]);
}
if(flag==3) //shi
{
DateTime[2]++;
if(DateTime[2]==24)
DateTime[2]=0;
write_com(0x80+0x40+5);
display(5,DateTime[2]);
}
if(flag==4) //zhou
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==5) // ri
{
DateTime[3]++;
if(DateTime[3]==30)
DateTime[3]=0;
write_com(0x80+11);
display1(11,DateTime[3]);
}
if(flag==6) //yue
{
DateTime[4]++;
if(DateTime[4]==13)
DateTime[4]=0;
write_com(0x80+8);
display1(8,DateTime[4]);
}
if(flag==7) //nian
{
DateTime[6]++;
if(DateTime[6]==100)
DateTime[6]=0;
write_com(0x80+5);
display1(5,DateTime[6]);
}
}
}
if(flag!=0)
{
if(KEY3==0)
{
DelayMS(5);
if(KEY3==0)
while(!KEY3);
if(flag==1) //miao
{
DateTime[0]--;
if(DateTime[0]==-1)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==2) //fen
{
DateTime[1]--;
if(DateTime[1]==-1)
DateTime[1]=0;
write_com(0x80+0x40+8);
display(8,DateTime[1]);
}
if(flag==3) //shi
{
DateTime[2]--;
if(DateTime[2]==-1)
DateTime[2]=0;
write_com(0x80+0x40+5);
display(5,DateTime[2]);
}
if(flag==4) //zhou
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==5) // ri
{
DateTime[3]--;
if(DateTime[3]==-1)
DateTime[3]=0;
write_com(0x80+11);
display1(11,DateTime[3]);
}
if(flag==6) //yue
{
DateTime[4]--;
if(DateTime[4]==-1)
DateTime[4]=0;
write_com(0x80+8);
display1(8,DateTime[4]);
}
if(flag==7) //nian
{
DateTime[6]--;
if(DateTime[6]==-1)
DateTime[6]=0;
write_com(0x80+5);
display1(5,DateTime[6]);
}
}
}
}
}
void main()
{
Init_LCD();
while(1)
{
EA=1;
EX0=1;
SCLK = 0;
RST = 0;
Write_DS1302(0x80,0x00); //关时间
Write_DS1302(0x80,0x00);
Write_DS1302(0x80,0x00);
Write_DS1302(0x82,0x02);
Write_DS1302(0x84,0x12);
Write_DS1302(0x86,0x11);
Write_DS1302(0x88,0x02);
Write_DS1302(0x8a,0x00);
Write_DS1302(0x8c,0x11);
Write_DS1302(0x8e,0x00);
Write_DS1302(0x8e,0x80);
GetTime();
Format_DateTime(DateTime[6],(LCD_DSY_BUFFER1+5)); //年
Format_DateTime(DateTime[4],(LCD_DSY_BUFFER1+8)); //月
Format_DateTime(DateTime[3],(LCD_DSY_BUFFER1+11)); //日
//strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]); //周 串拷贝 包含于STRING.H
Format_DateTime(DateTime[2],(LCD_DSY_BUFFER2+5)); //时
Format_DateTime(DateTime[1],(LCD_DSY_BUFFER2+8)); //分
Format_DateTime(DateTime[0],(LCD_DSY_BUFFER2+11)); //秒
Display_LCD_String(0x00,LCD_DSY_BUFFER1);
Display_LCD_String(0x40,LCD_DSY_BUFFER2);
}
}
void int0() interrupt 0
{
Keyscan();
}
可能对你有用,试试看,我的是自己焊的板子,调试出来了,不知道你的运气怎样,呵呵
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit IO = P1^0;
sbit SCLK = P1^1;
sbit RST = P1^2;
sbit RS = P0^0;
sbit RW = P0^1;
sbit EN = P0^2;
sbit KEY1=P3^4;
sbit KEY2=P3^5;
sbit KEY3=P3^6;
uchar *WEEK[]={"SUN","***","MON","TUS","WEN","THU","FRI","SAT"};
uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00 "};
uchar LCD_DSY_BUFFER2[]={"TIME 00:00:00 "};
uchar DateTime[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //秒,分,时,日,月,周,年
uchar flag,flag_1,i,miao,fen,shi,ri,yue,zhou,nian;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/*****************************************************/
//函数名称: Write_A_Byte_TO_DS1302(uchar x)
//函数功能: 向1302写入一个字节
//入口参数: x
//出口参数: 无
//调用子程序: 无
/*****************************************************/
void Write_A_Byte_TO_DS1302(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
IO=x&0x01;
SCLK=1;
SCLK=0;
x>>=1;
}
}
void Write_DS1302(uchar add,uchar num)
{
SCLK=0;
RST=0;
RST=1;
Write_A_Byte_TO_DS1302(add);
Write_A_Byte_TO_DS1302(num);
RST=0;
SCLK=1;
}
/*****************************************************/
//函数名称: Get_A_Byte_FROM_DS1302()
//函数功能: 从1302读一个字节
//入口参数: 无
//出口参数: b/16*10+b%16
//调用子程序: 无
/*****************************************************/
uchar Get_A_Byte_FROM_DS1302()
{
uchar i,b=0x00;
for(i=0;i<8;i++)
{
b |= _crol_((uchar)IO,i);
SCLK=1;SCLK=0;
}
return b/16*10+b%16;
}
/*****************************************************/
//函数名称: Read_Data(uchar addr)
//函数功能: 指定位置读数据
//入口参数: addr
//出口参数: dat
//调用子程序: Write_Abyte_1302(addr)
/*****************************************************/
uchar Read_Data(uchar addr)
{
uchar dat;
RST = 0;SCLK=0;RST=1;
Write_A_Byte_TO_DS1302(addr);
dat = Get_A_Byte_FROM_DS1302();
SCLK=1;RST=0;
return dat;
}
/*****************************************************/
//函数名称: GetTime()
//函数功能: 读取时间
//入口参数: 无
//出口参数: 无
//调用子程序: 无
/*****************************************************/
void GetTime()
{
uchar i,addr = 0x81;
for(i=0;i<7;i++)
{
DateTime[i]=Read_Data(addr);addr+=2;
}
}
uchar Read_LCD_State()
{
uchar state;
RS=0;RW=1;EN=1;DelayMS(1);
state=P2;
EN = 0;DelayMS(1);
return state;
}
void LCD_Busy_Wait()
{
while((Read_LCD_State()&0x80)==0x80);
DelayMS(5);
}
void Write_LCD_Data(uchar dat) //写数据到1602
{
LCD_Busy_Wait();
RS=1;RW=0;EN=0;P2=dat;EN=1;DelayMS(1);EN=0;
}
void Write_LCD_Command(uchar cmd) //写命令
{
LCD_Busy_Wait();
RS=0;RW=0;EN=0;P2=cmd;EN=1;DelayMS(1);EN=0;
}
void Init_LCD() //1602 初始化
{
Write_LCD_Command(0x38);
DelayMS(1);
Write_LCD_Command(0x01);
DelayMS(1);
Write_LCD_Command(0x06);
DelayMS(1);
Write_LCD_Command(0x0c);
DelayMS(1);
}
void Set_LCD_POS(uchar p)
{
Write_LCD_Command(p|0x80);
}
void Display_LCD_String(uchar p,uchar *s) //1602显示
{
uchar i;
Set_LCD_POS(p);
for(i=0;i<16;i++)
{
Write_LCD_Data(s[i]);
DelayMS(1);
}
}
void write_com(uchar com)
{
RS=0;
P2=com;
DelayMS(5);
EN=1;
DelayMS(5);
EN=0;
}
void write_date(uchar date)
{
RS=1;
P2=date;
DelayMS(5);
EN=1;
DelayMS(5);
EN=0;
}
void display(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void display1(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void Format_DateTime(uchar d,uchar *a)
{
a[0]=d/10+'0';
a[1]=d%10+'0';
}
uchar ZH(uchar dat)
{
uchar tmp;
tmp=dat/10;
dat=dat%10;
dat=dat+tmp*16;
return dat;
}
void Keyscan()
{
flag_1=1;
while(flag_1)
{
if(KEY1==0)
{
DelayMS(5);
while(!KEY1);
flag=(flag+1)%8;
switch(flag)
{
case(1): Write_LCD_Command(0x0f);
Write_LCD_Command(0x80+0x40+11);
break;
case(2): Write_LCD_Command(0x80+0x40+8);
break;
case(3): Write_LCD_Command(0x80+0x40+5);
break;
case(4): Write_LCD_Command(0x80+13);
break;
case(5): Write_LCD_Command(0x80+11);
break;
case(6): Write_LCD_Command(0x80+8);
break;
case(7): Write_LCD_Command(0x80+5);
break;
case(0): flag_1=0;
Write_LCD_Command(0x0c);
//miao
Write_DS1302(0x8e,0x00);
Write_DS1302(0x80,ZH(DateTime[0]));
Write_DS1302(0x8e,0x80);
//fen
Write_DS1302(0x8e,0x00);
Write_DS1302(0x82,ZH(DateTime[1]));
Write_DS1302(0x8e,0x80);
//shi
Write_DS1302(0x8e,0x00);
Write_DS1302(0x84,ZH(DateTime[2]));
Write_DS1302(0x8e,0x80);
//ri
Write_DS1302(0x8e,0x00);
Write_DS1302(0x86,ZH(DateTime[3]));
Write_DS1302(0x8e,0x80);
// yue
Write_DS1302(0x8e,0x00);
Write_DS1302(0x88,ZH(DateTime[4]));
Write_DS1302(0x8e,0x80);
//nian
Write_DS1302(0x8e,0x00);
Write_DS1302(0x8c,ZH(DateTime[6]));
Write_DS1302(0x8e,0x80);
break;
}
}
if(flag!=0)
{
if(KEY2==0)
{
DelayMS(5);
if(KEY2==0)
while(!KEY2);
if(flag==1) //miao
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==2) //fen
{
DateTime[1]++;
if(DateTime[1]==60)
DateTime[1]=0;
write_com(0x80+0x40+8);
display(8,DateTime[1]);
}
if(flag==3) //shi
{
DateTime[2]++;
if(DateTime[2]==24)
DateTime[2]=0;
write_com(0x80+0x40+5);
display(5,DateTime[2]);
}
if(flag==4) //zhou
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==5) // ri
{
DateTime[3]++;
if(DateTime[3]==30)
DateTime[3]=0;
write_com(0x80+11);
display1(11,DateTime[3]);
}
if(flag==6) //yue
{
DateTime[4]++;
if(DateTime[4]==13)
DateTime[4]=0;
write_com(0x80+8);
display1(8,DateTime[4]);
}
if(flag==7) //nian
{
DateTime[6]++;
if(DateTime[6]==100)
DateTime[6]=0;
write_com(0x80+5);
display1(5,DateTime[6]);
}
}
}
if(flag!=0)
{
if(KEY3==0)
{
DelayMS(5);
if(KEY3==0)
while(!KEY3);
if(flag==1) //miao
{
DateTime[0]--;
if(DateTime[0]==-1)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==2) //fen
{
DateTime[1]--;
if(DateTime[1]==-1)
DateTime[1]=0;
write_com(0x80+0x40+8);
display(8,DateTime[1]);
}
if(flag==3) //shi
{
DateTime[2]--;
if(DateTime[2]==-1)
DateTime[2]=0;
write_com(0x80+0x40+5);
display(5,DateTime[2]);
}
if(flag==4) //zhou
{
DateTime[0]++;
if(DateTime[0]==60)
DateTime[0]=0;
write_com(0x80+0x40+11);
display(11,DateTime[0]);
}
if(flag==5) // ri
{
DateTime[3]--;
if(DateTime[3]==-1)
DateTime[3]=0;
write_com(0x80+11);
display1(11,DateTime[3]);
}
if(flag==6) //yue
{
DateTime[4]--;
if(DateTime[4]==-1)
DateTime[4]=0;
write_com(0x80+8);
display1(8,DateTime[4]);
}
if(flag==7) //nian
{
DateTime[6]--;
if(DateTime[6]==-1)
DateTime[6]=0;
write_com(0x80+5);
display1(5,DateTime[6]);
}
}
}
}
}
void main()
{
Init_LCD();
while(1)
{
EA=1;
EX0=1;
SCLK = 0;
RST = 0;
Write_DS1302(0x80,0x00); //关时间
Write_DS1302(0x80,0x00);
Write_DS1302(0x80,0x00);
Write_DS1302(0x82,0x02);
Write_DS1302(0x84,0x12);
Write_DS1302(0x86,0x11);
Write_DS1302(0x88,0x02);
Write_DS1302(0x8a,0x00);
Write_DS1302(0x8c,0x11);
Write_DS1302(0x8e,0x00);
Write_DS1302(0x8e,0x80);
GetTime();
Format_DateTime(DateTime[6],(LCD_DSY_BUFFER1+5)); //年
Format_DateTime(DateTime[4],(LCD_DSY_BUFFER1+8)); //月
Format_DateTime(DateTime[3],(LCD_DSY_BUFFER1+11)); //日
//strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]); //周 串拷贝 包含于STRING.H
Format_DateTime(DateTime[2],(LCD_DSY_BUFFER2+5)); //时
Format_DateTime(DateTime[1],(LCD_DSY_BUFFER2+8)); //分
Format_DateTime(DateTime[0],(LCD_DSY_BUFFER2+11)); //秒
Display_LCD_String(0x00,LCD_DSY_BUFFER1);
Display_LCD_String(0x40,LCD_DSY_BUFFER2);
}
}
void int0() interrupt 0
{
Keyscan();
}
可能对你有用,试试看,我的是自己焊的板子,调试出来了,不知道你的运气怎样,呵呵
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询