单片机万年历程序设计程序代码
时间: 2023-09-18 14:14:35 浏览: 712
以下是一个基于51单片机的简单万年历程序设计的代码:
```
#include<reg52.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴数码管0~9 a~f
uchar year, month, day;
uchar week; //定义年月日和星期
uchar dis_1,dis_2,dis_3,dis_4; //定义四个数码管的显示值
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void get_time() //获取时间函数
{
uchar temp;
temp = DS1302_Read(0x86);
year = DS1302_Read(0x86);
month = DS1302_Read(0x88);
day = DS1302_Read(0x8A);
week = DS1302_Read(0x8C);
year = year + 2000;
}
void main()
{
TMOD = 0x01; //定时器0工作模式,16位计数
TH0 = 0xFA; //赋初值
TL0 = 0xCD;
TR0 = 1; //开启定时器0
ET0 = 1; //开启定时器0中断
EA = 1; //开启总中断
while(1)
{
get_time(); //获取时间
dis_1 = year / 1000; //计算千位数码管的值
dis_2 = (year % 1000) / 100; //计算百位数码管的值
dis_3 = (year % 100) / 10; //计算十位数码管的值
dis_4 = year % 10; //计算个位数码管的值
P2 = 0x00; //清空P2口
P0 = table[dis_1]; //显示千位数码管的值
P2 = 0x01; //P2.0口输出高电平,点亮千位数码管
delay(2); //延时一段时间,防止闪烁
P2 = 0x00;
P0 = table[dis_2];
P2 = 0x02;
delay(2);
P2 = 0x00;
P0 = table[dis_3];
P2 = 0x04;
delay(2);
P2 = 0x00;
P0 = table[dis_4];
P2 = 0x08;
delay(2);
}
}
void T0_time() interrupt 1 //定时器0中断函数
{
TH0 = 0xFA; //重置定时器0初值
TL0 = 0xCD;
DS1302_Init(); //DS1302初始化
}
```
该代码使用了DS1302实时时钟芯片来获取时间。具体的接口和初始化函数在代码中没有给出,需要自行添加。在主函数中,通过计算年份的千位、百位、十位和个位数码管的值来实现万年历的显示效果。在定时器0中断函数中,调用了DS1302的初始化函数来更新时间。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)