5 LED点阵屏
LED点阵屏由若干个独立LED组成,以矩阵形式排列
![[Pasted image 20250120160748.png]]
![[Pasted image 20250120162044.png]]
![[Pasted image 20250121114613.png]]
类似于数码管和矩阵键盘,LED点阵同样需要进行逐行或逐列扫描
74HC595
通过74HC595,扩展I/O口
![[Pasted image 20250120162916.png]]
- SERCLK:在时钟信号上升沿时触发,将输入的串行数据一位位存入寄存器(压子弹)
- RCLK:时钟信号上升沿触发,当输入串行数据存至8位时,将这8位数据全部移入发送缓存中
- QH':用于级联下一个寄存器
sfr与sbit
![[Pasted image 20250120170545.png]]
^= :和1异或,相同是1不同是0
实现LED屏播放动画
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
unsigned char code animation[] = { // code关键字使数组存到flash中而不是RAM
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 但加入code关键字后变量不能被更改
0xFF,0x90,0x90,0x90,0x90,0x00,0x00,0x00,
0xFC,0x02,0x01,0x01,0x01,0x02,0xFC,0x00,
0x00,0x7E,0x81,0x81,0x81,0x81,0x00,0x00,
0x00,0xFF,0x18,0x24,0x42,0x81,0x00,0x00,
};
void main()
{
unsigned char i, offset = 0, count = 0; // offset为动画偏移量 count为偏移次数计数
matLEDInit();
while(1)
{
for(i = 0; i < 8; i++)
{
matLEDShowColumn(i,animation[i + offset]);
}
count++; // 每扫描一次count加一次
if(count > 24) // 扫描24次到下一帧(一帧扫描24次)
{
count = 0;
offset++;
// 防止偏移数组溢出
if(offset > 20)
{
offset = 0;
}
}
}
}
头文件部分
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
5; // RCLK
sbit SER = P3^4; // SER
sbit SRCLK = P3^6; // SERCLK
/**
* @brief 初始化LED
* @param N/A
* @param N/A
* @retval void
*/
void matLEDInit()
{
SRCLK = 0;
RCK = 0;
}
void __74HC595__WriteByte(unsigned char byte)
{
unsigned char i;
// 循环取出SER中每一位
// 0x80 = 1000 0000B
// 向右移动i位
// 第一次0x80 >> 0 -> 0x80 >> 1 -> 0x80 >> 2 -> 0x80 >> 3 -> 0x80 >> 4 ...
for(i = 0; i < 8;i++)
{
SER = byte & (0x80 >> i); // 取出SER中最高位
SRCLK = 1;
SRCLK = 0;
}
RCK = 1;
RCK = 0;
}
/**
* @brief 将数据显示到LED屏上
* @param 0~7列LED
* @param 74HC595传入数据
* @retval void
*/
void matLEDShowColumn(unsigned char column, inData)
{
__74HC595__WriteByte(inData);
P0 = ~(0x80 >> column);
// 延时后清零,去除残影
Delay(1);
P0 = 0xFF;
}
sbit RCK = P3^复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
void matLEDShowColumn(unsigned char column, inData);
void matLEDInit();