只能定义为char型,否则下载到单片机无法运行?
#include <reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { //数码管显示缓冲区,初值0xFF确保启动时都不亮
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char i = 0; //动态扫描的索引
unsigned int cnt = 0; //记录T1中断次数
void main()
{
char j; //循环变量
unsigned long sec = 1000000; //记录经过的秒数,倒计时初值为999999+1
unsigned char buf[6]; //中间转换缓冲区
EA = 1; //使能总中断
ENLED = 0; //使能U3
ADDR3 = 1; //因为需要动态改变ADDR0-2的值,所以不需要再初始化了
TMOD = 0x10; //设置T1为模式1
TH1 = 0xFC; //为T1赋初值0xFC67,定时1ms
TL1 = 0x67;
ET1 = 1; //使能T1中断
TR1 = 1; //启动T1
while (1)
{
if (cnt >= 1000) //判断T1溢出是否达到1000次
{
cnt = 0; //达到1000次后计数值清零
sec--; //秒计数自减1
//将sec按十进制位从低到高依次提取到buf数组中
buf[0] = sec%10;
buf[1] = sec/10%10;
buf[2] = sec/100%10;
buf[3] = sec/1000%10;
buf[4] = sec/10000%10;
buf[5] = sec/100000%10;
//从最高为开始,遇到0不显示,遇到非0退出循环
for (j=5; j>=1; j--)
{
if (buf[j] == 0)
LedBuff[j] = 0xFF;
else
break;
}
//将剩余的有效数字位如实转换
for ( ; j>=0; j--) //for()起始未对j操作,j即保持上个循环结束时的值
{
LedBuff[j] = LedChar[buf[j]];
}
}
}
}
/* 定时器1中断服务函数 */
void InterruptTimer1() interrupt 3
{
TH1 = 0xFC; //重新加载初值
TL1 = 0x67;
cnt++; //中断次数计数值加1
//以下代码完成数码管动态扫描刷新
P0 = 0xFF; //显示消隐
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5]; break;
default: break;
}
}
|