在普中51单片机主函数中写入一个程序,使静态数码管循环显示数字一到九
时间: 2024-02-15 13:02:32 浏览: 27
以下是基于普中51单片机,利用P0口控制静态数码管循环显示数字1-9的主函数代码:
```c
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
// 数码管段码表,共阴数码管
uchar code table[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
// 数码管位码表,P0.0-P0.7对应COM0-COM7
uchar code wtable[] = {
0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f
};
// 延时函数
void delay(uint ms)
{
uint i, j;
for(i=0; i<ms; i++)
for(j=0; j<125; j++);
}
// 数码管显示函数
void display(uchar num)
{
P0 = wtable[0]; // 选择第1个数码管
P2 = table[num]; // 显示数字
delay(1); // 延时一段时间
}
// 主函数
void main()
{
uchar i;
while(1) {
for(i=1; i<=9; i++) { // 显示数字1-9
display(i); // 调用显示函数
}
}
}
```
注:以上代码只是简单的示例,实际使用中还需要考虑到数码管的电流限制、保护等问题。同时,为了达到更好的显示效果,可以在数码管显示函数中增加一些延时和扫描间隔的控制。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)