stc89c52单片机点阵模块MAX7219实现时钟代码
时间: 2024-05-16 11:12:43 浏览: 17
以下是STC89C52单片机通过MAX7219点阵模块实现时钟的代码:
```c
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Delay 1
#define blank 0x00
sbit CLK = P2^0;
sbit DIN = P2^1;
sbit CS = P2^2;
uchar disCode[10]={0x7e,0x12,0xb6,0xb2,0xd2,0xe4,0xf4,0x32,0xfe,0xf2};//显示0~9的编码
void Write_Max7219_Clock(uchar address,uchar dat)
{
CS=0;
CLK=0;
DIN=0;
CLK=1;
for(uint i=0;i<8;i++)
{
DIN=dat&0x80;
CLK=0;
CLK=1;
dat<<=1;
}
for(uint i=0;i<8;i++)
{
DIN=address&0x80;
CLK=0;
CLK=1;
address<<=1;
}
CS=1;
}
void Init_MAX7219_Clock()
{
Write_Max7219_Clock(0x09,0x00);
Write_Max7219_Clock(0x0a,0x0f);
Write_Max7219_Clock(0x0b,0x07);
Write_Max7219_Clock(0x0c,0x01);
Write_Max7219_Clock(0x0f,0x00);
}
void Display_Digital(uchar dat)
{
Write_Max7219_Clock(0x01,disCode[dat%10]);//个位数码管
Write_Max7219_Clock(0x02,disCode[dat/10]);//十位数码管
Write_Max7219_Clock(0x03,blank); //空位
Write_Max7219_Clock(0x04,blank); //空位
}
void main()
{
uchar seconds=0,minutes=0,hours=0;
Init_MAX7219_Clock();
while(1)
{
Delay=1000;
seconds++;
if(seconds>=60)
{
seconds=0;
minutes++;
if(minutes>=60)
{
minutes=0;
hours++;
if(hours>=24)
{
hours=0;
}
}
}
Display_Digital(seconds+minutes*100+hours*10000);
}
}
```
这段代码中,我们通过MAX7219点阵模块的控制,实现了一个简单的时钟功能。其中,我们利用了STC89C52单片机的定时器功能,每隔1秒钟更新一次时钟的显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)