一段代码写完了,编译下载都ok。
但是没有看到想要的结果?怎么排错???
借一个别人正常的板子是一种方法,但是镜像提供了全仿真工具,试一下吧。
新建项目:
添加time.c到工程中!
添加复用数码管。
当然程序要做微微调整。
单片机课堂思考题-2000秒倒计时?
参考上例程序:
先分配管脚和端口
代码语言:javascript复制#include<8052.h>
unsigned char smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x0f8,0x80,0x90};
unsigned char T0RH = 0;
unsigned char T0RL = 0;
unsigned int cnt;
int sec;
int sec_all=6666;
int count=0;
void LedScan(unsigned char pos,unsigned char dispcode);
void EXINT0() __interrupt 0;
void Timer0() __interrupt 1;
void EXINT1() __interrupt 2;
void ConfigTimer0(unsigned int);
void main()
{
cnt = 0;
sec = sec_all;
ConfigTimer0(2);
IT0=1;
EX0=1;
IT1=1;
EX1=1;
PX1=1;
TR0=1;
while(1)
{
LedScan(0,smgduan[sec]);
LedScan(1,smgduan[sec/10]);
LedScan(2,smgduan[sec/100]);
LedScan(3,smgduan[sec/1000]);
if(count%2==0)
{
TR0 = 1;
}
else
{
TR0 = 0;
}
}
}
void EXINT0() __interrupt 0
{
count ;
}
void EXINT1() __interrupt 2
{
sec_all=0;
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp;
tmp = 11059200 / 12;
tmp = (tmp * ms) / 1000;
tmp = 65536 - tmp;
tmp = tmp 18;
T0RH = (unsigned char)(tmp>>8);
T0RL = (unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
}
void Timer0() __interrupt 1
{
TH0 = T0RH;
TL0 = T0RL;
cnt ;
if (cnt >= 5)
{
cnt = 0;
sec--;
if (sec < 0)
{
sec = sec_all;
}
}
}
void LedScan(unsigned char pos,unsigned char dispcode)
{
unsigned char i;
for(i=0;i<=pos;i )
{
switch(pos) {
case(0):
P1_0=0;P1_1=1;P1_2=1;P1_3=1; break;
case(1):
P1_0=1;P1_1=0;P1_2=1;P1_3=1; break;
case(2):
P1_0=1;P1_1=1;P1_2=0;P1_3=1; break;
case(3):
P1_0=1;P1_1=1;P1_2=1;P1_3=0; break;
}
P0=dispcode;
}
}
代码语言:javascript复制#include<8052.h>
unsigned char smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x0f8,0x80,0x90};
unsigned char T0RH = 0;
unsigned char T0RL = 0;
unsigned int cnt;
int sec;
int sec_all=6666;
int count=0;
void LedScan(unsigned char pos,unsigned char dispcode);
void EXINT0() __interrupt 0;
void Timer0() __interrupt 1;
void EXINT1() __interrupt 2;
void ConfigTimer0(unsigned int);
void main()
{
cnt = 0;
sec = sec_all;
ConfigTimer0(1);
IT0=1;
EX0=1;
IT1=1;
EX1=1;
PX1=1;
TR0=1;
while(1)
{
LedScan(0,smgduan[sec]);
LedScan(1,smgduan[sec/10]);
LedScan(2,smgduan[sec/100]);
LedScan(3,smgduan[sec/1000]);
if(count%2==0)
{
TR0 = 1;
}
else
{
TR0 = 0;
}
}
}
void EXINT0() __interrupt 0
{
count ;
}
void EXINT1() __interrupt 2
{
sec_all=0;
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp;
tmp = 11059200 / 12;
tmp = (tmp * ms) / 1000;
tmp = 65536 - tmp;
tmp = tmp 18;
T0RH = (unsigned char)(tmp>>8);
T0RL = (unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
}
void Timer0() __interrupt 1
{
TH0 = T0RH;
TL0 = T0RL;
cnt ;
if (cnt >= 2)
{
cnt = 0;
sec--;
if (sec < 0)
{
sec = sec_all;
}
}
}
void LedScan(unsigned char pos,unsigned char dispcode)
{
unsigned char i;
for(i=0;i<=pos;i )
{
switch(pos) {
case(0):
P1_0=0;P1_1=1;P1_2=1;P1_3=1; break;
case(1):
P1_0=1;P1_1=0;P1_2=1;P1_3=1; break;
case(2):
P1_0=1;P1_1=1;P1_2=0;P1_3=1; break;
case(3):
P1_0=1;P1_1=1;P1_2=1;P1_3=0; break;
}
P0=dispcode;
}
}