51单片机数码管倒计时程序,从999999开始倒计时,用定时器T1的中断完成
**************************/#include<reg52.h>sbitADDR0=P1^0;sbitADDR1=P1^1;sbitADDR2=P...
*************************
*/
#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中断次数
unsigned char flag1s = 0; //1秒定时标志
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 (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
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退出循环
}
}
}
/* 定时器1中断服务函数 */
为什么倒计时初始值是999999+1而不是999999 展开
*/
#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中断次数
unsigned char flag1s = 0; //1秒定时标志
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 (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
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退出循环
}
}
}
/* 定时器1中断服务函数 */
为什么倒计时初始值是999999+1而不是999999 展开
1个回答
展开全部
这个和代码顺序有关系:
while (1)
{
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
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退出循环
}
}
如果初值要改为999999,代码可以改成如下:
while (1)
{
if (flag1s == 1) //判断1秒定时标志
{
flag1s = 0; //1秒定时标志清零
// 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;
sec--; //秒计数自减1【这里是先送显示,后面再减一】
//从最高为开始,遇到0不显示,遇到非0退出循环
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询