MAX7219点阵模块四合一显示函数 基于51单片机
时间: 2023-12-30 15:06:56 浏览: 37
以下是基于51单片机的MAX7219点阵模块四合一显示函数的示例代码:
```c
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit CS = P3^5;
sbit CLK = P3^6;
sbit DIN = P3^4;
void Delay(uint i) // 延时函数
{
uint j, k;
for (j = i; j > 0; j--)
for (k = 110; k > 0; k--);
}
void Write_Max7219(uchar address, uchar data) // 写入函数
{
CS = 0;
Delay(1);
CLK = 0;
Delay(1);
for (uchar i = 8; i >= 1; i--)
{
CLK = 0;
DIN = address & 0x80;
address <<= 1;
if (DIN)
DIN = 1;
Delay(1);
CLK = 1;
Delay(1);
}
for (uchar i = 8; i >= 1; i--)
{
CLK = 0;
DIN = data & 0x80;
data <<= 1;
if (DIN)
DIN = 1;
Delay(1);
CLK = 1;
Delay(1);
}
CS = 1;
Delay(1);
}
void Init_Max7219() // 初始化函数
{
Write_Max7219(0x09, 0x00); // 译码方式:BCD码
Write_Max7219(0x0a, 0x03); // 亮度:4/16
Write_Max7219(0x0b, 0x07); // 扫描位数:8位
Write_Max7219(0x0c, 0x01); // 关闭掉电模式
Write_Max7219(0x0f, 0x00); // 测试模式:关闭
}
void Display_Max7219(uchar max_col, uchar max_row, uchar (*p)[8]) // 显示函数
{
uchar i, j, k, col, row, data;
for (i = 0; i < max_col; i++)
{
for (k = 0; k < 8; k++)
{
col = i * 8 + k + 1;
for (j = 0; j < max_row; j++)
{
row = j + 1;
data = p[j][i];
Write_Max7219(col, data);
}
}
Delay(80); // 延时一定时间再切换列,防止过快导致闪烁
}
}
void main()
{
uchar a[8][8] = { // 显示的8组数据,每组表示8行数据
{0x3C, 0x42, 0x81, 0x81, 0x81, 0x81, 0x42, 0x3C}, // A
{0xFE, 0x92, 0x92, 0x92, 0x92, 0x92, 0x82, 0x82}, // B
{0x7C, 0x82, 0x81, 0x81, 0x81, 0x81, 0x82, 0x7C}, // C
{0xFE, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0xFE}, // D
{0xFE, 0x92, 0x92, 0x92, 0x92, 0x81, 0x81, 0x81}, // E
{0xFE, 0x90, 0x90, 0x90, 0x90, 0x80, 0x80, 0x80}, // F
{0x7C, 0x82, 0x81, 0x81, 0x89, 0x89, 0x8A, 0x7C}, // G
{0xFE, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xFE} // H
};
Init_Max7219(); // 初始化MAX7219
while (1)
{
Display_Max7219(8, 8, a); // 显示8列,每列8行数据
}
}
```
在上述代码中,可以看到:
- `Write_Max7219()` 函数用于向 MAX7219 写入数据,其中 `address` 表示要写入的地址,`data` 表示要写入的数据。
- `Init_Max7219()` 函数用于初始化 MAX7219,设置译码方式、亮度、扫描位数等参数。
- `Display_Max7219()` 函数用于显示数据,其中 `max_col` 表示要显示的列数,`max_row` 表示每列要显示的行数,`p` 表示要显示的数据,是一个二维数组,每行表示一组数据,每组数据有 `max_col` 个字节。
- 在 `main()` 函数中,先初始化 MAX7219,然后不断调用 `Display_Max7219()` 函数来显示数据。