51单片机点阵屏渐变显示,详细的代码及注释
时间: 2023-12-21 08:04:06 浏览: 40
下面是51单片机点阵屏渐变显示的详细代码及注释。本代码使用Keil C编译器,使用的点阵屏为8x8的共阴极点阵屏。
```
#include<reg52.h> //头文件
#define uchar unsigned char //定义uchar类型
#define uint unsigned int //定义uint类型
uchar code table[] = { //定义渐变表
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
void delay(uint t) //延时函数
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}
void main()
{
uchar i,j,k;
while(1)
{
for(i=0;i<8;i++) //循环渐变显示
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
P0 = 0x00; //关闭所有阴极
P2 = 0x01<<k; //打开对应的阳极
P0 = table[i*8+j]; //输出对应的渐变值
delay(1); //延时
}
}
}
}
}
```
代码注释:
1. 定义`uchar`和`uint`类型,分别代表无符号字符和无符号整数。
```
#define uchar unsigned char //定义uchar类型
#define uint unsigned int //定义uint类型
```
2. 定义一个渐变表,包含了0到255的渐变值。
```
uchar code table[] = { //定义渐变表
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
```
3. 定义延时函数`delay`,使用嵌套循环实现延时。
```
void delay(uint t) //延时函数
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}
```
4. 主函数中进行循环渐变显示。外层循环控制渐变值的变化,内层两个循环分别控制阳极和阴极的变化。
```
void main()
{
uchar i,j,k;
while(1)
{
for(i=0;i<8;i++) //循环渐变显示
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
P0 = 0x00; //关闭所有阴极
P2 = 0x01<<k; //打开对应的阳极
P0 = table[i*8+j]; //输出对应的渐变值
delay(1); //延时
}
}
}
}
}
```
注意事项:
1. 在点阵屏上,共阴极和共阳极的接口是不同的。本代码使用的是共阴极点阵屏,需要注意接口的连接。
2. 延时函数的参数`t`代表延时的时间,具体的延时时间需要根据实际情况进行调整。
3. 渐变表中的渐变值可以根据需要进行修改,以实现不同的效果。
相关推荐
![](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)