单片机max7219实验编程
时间: 2023-07-08 08:55:53 浏览: 57
好的,单片机和MAX7219是一种常用的数字管显示方案,下面是一个简单的示例程序,可以实现数字的显示和滚动。
```
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Din = P1^0;
sbit CS = P1^1;
sbit CLK = P1^2;
void delay(uint t)
{
while(t--);
}
void write_Max7219_byte(uchar dat)
{
uchar i;
CS = 0;
for(i=8; i>=1; i--)
{
CLK = 0;
Din = dat & 0x80;
dat <<= 1;
CLK = 1;
}
}
void write_Max7219(uchar address, uchar dat)
{
CS = 0;
write_Max7219_byte(address);
write_Max7219_byte(dat);
CS = 1;
}
void init_Max7219()
{
write_Max7219(0x09, 0x00);
write_Max7219(0x0a, 0x03);
write_Max7219(0x0b, 0x07);
write_Max7219(0x0c, 0x01);
write_Max7219(0x0f, 0x00);
}
void display(uchar *p)
{
uchar i;
for(i=1; i<=8; i++)
{
write_Max7219(i, *(p+i-1));
}
}
void scroll_left(uchar *p, uint count)
{
uint i,j;
uchar t;
for(j=0; j<count; j++)
{
for(i=0; i<7; i++)
{
t = *(p+i);
*(p+i) = *(p+i+1);
*(p+i+1) = t;
}
display(p);
delay(10000);
}
}
void main()
{
uchar num[8] = {0x81,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
init_Max7219();
while(1)
{
scroll_left(num, 10);
}
}
```
这个程序使用了MAX7219的SPI接口,通过单片机的GPIO实现了SPI通信。程序中主要包含了以下几个函数:
1. `write_Max7219_byte`:用于向MAX7219写入一个字节的数据;
2. `write_Max7219`:用于向MAX7219的指定寄存器写入数据;
3. `init_Max7219`:用于初始化MAX7219,配置显示模式等参数;
4. `display`:用于显示一个数字,将数字的8个字节依次写入到MAX7219的8个寄存器中;
5. `scroll_left`:用于实现数字的滚动,将数字的每个字节向左移动一位,然后重新显示。
在main函数中,我们首先初始化MAX7219,然后将一个数字存储在num数组中,不断调用scroll_left函数实现数字的滚动。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)