我这里有个例程,你看看吧
#include<reg52.h> //52单片机头文件,一般不要改动,里面包含特殊功能寄存器的定义
#define uchar unsigned char //将unsigned char定义为uchar,简化输写。提高编程速度
#define uint unsigned int //将unsigned char定义为uint,简化输写。提高编程速度
uchar code duma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f, //共阴的数码管段选.将值送给P2口
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x80,0x00}; //数值''0-F' '-' '熄灭'
uchar code wema[]={0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7}; //位选,0-7位数码管
void delay(uint time);
void main()
{
uchar i; //定义一个无符号字符变量。
while(1) //做一个死循环,让程序永远在while下面的大括号里面运行。
{
for(i=0;i<8;i++) //for语句判断条
{
P1=wema[i]; //表达式为假时执行括号里面语句。为真时,执行下一句。
P2=duma[i+1]; //要显示1-8.取值从1开始
delay(270); //修改此处的时间,可以明显感觉到动态扫描的原理。时间定的太长会有明显闪烁。
} //参考值270,可以换成500试试。感觉一下。更改值后,要重新编译生成HEX文件才有效。
}
}
void delay(uint time)
{
while(--time); //当time的值为非0时,执行空语句。当time为0时,跳出while语句。
}
|