四个数码管,前两位显示两个数字,后两位计数。分别按按键,前两位两个数字会加减,后两位两个数字也会加减。我用两个外中断和两个计数器做。编译无错误无警告。实物输入程序后无显示,请问各位前辈,我该怎么修改程序,谢谢大家,辛苦!劳驾了!
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar code duanma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //段码
uchar code weima[4]={0xef,0xdf,0xbf,0x7f}; //位码
uchar disp[4]={0,0,0,0}; //显示数字
static uchar second=44; //时间显示
static uchar count=53; //计数显示
uchar f=0;
sbit duan=P2^6; //段锁
sbit wei=P2^7; //位锁
void int0_init(void) //外中断P3.2初始化
{
EA =1; //全局中断允许
IT0 =1;
EX0 =1;
}
void int1_init(void) //外中断P3.3初始化
{
EA=1; //全局中断允许
IT1=1;
EX1=1;
}
void duan_lock(void) //段锁
{
duan=1;
_nop_();
_nop_();
duan=0;
}
void wei_lock(void) //位锁
{
wei=1;
_nop_();
_nop_();
wei=0;
}
void time_init(void) //定时器初始化
{
TMOD=0x01; //16位定时器1
TH0=(65536-2500)/256; //定时器高八位
TL0=(65536-2500)%256; //定时器低八位
TF0=0; //定时器没启动前清零
TR0=1; //定时器启动
}
void timer0_init() //计数器0 P3.4脚初始化
{
TMOD = 0x05; //采用计数器
TH0 = 0x00;
TL0 = 0x00;
EA = 1; //全局中断允许
ET0 = 1; //定时器0中断允许
TR0 = 1; //开启定时器0
}
void timer1_init() //计数器0 P3.5脚初始化
{
TMOD = 0x07; //采用计数器
TH0 = 0x00;
TL0 = 0x00;
EA = 1; //全局中断允许
ET0 = 1; //定时器0中断允许
TR0 = 1; //开启定时器0
}
void suzi(void) //数字取码
{
disp[0]=second/10;
disp[1]=second%10;
disp[2]=count/10;
disp[3]=count%10;
}
void main()
{
time_init(); //定时器初始化
int0_init(); //外中断0 P3.2脚初始化
int1_init(); //外中断1 P3.3脚初始化
timer0_init();
timer1_init(); //计数器0 P3.4脚初始化
while(1)
{
suzi(); //数字取码初始化
if(TF0==1) //如果定时器0溢出
{
TF0=0; //定时器0未溢出
TH0=(65536-2500)/256;
TL0=(65536-2500)%256;
// for(f=0;f<4;f++) 这里如果用条件语句for的话,就会出现8位数码管前面几个暗,最后一位亮的情况
// {
P0=duanma[disp[f]];
duan_lock();
P0=weima[f];
wei_lock();
f=f+1;
if(f>3)
{
f=0;
}
// }
}
}
}
void ISR_int0 (void) interrupt 0 using 3 //外中断P3.2
{
second =second+1;
if(second==100)
{
second=0;
}
}
void ISR_int1 (void) interrupt 2 using 2 //外中断P3.3
{
second =second-1;
if(second==0)
{
second=99;
}
}
void Timer0(void) interrupt 1 //计数器0中断P3.4
{
count=count+1;
if(count==100)
{
count=0 ;
}
}
void Timer1(void) interrupt 3 //计数器0中断P3.5
{
count=count+1;
if(count==100)
{
count=0 ;
}
}
一周热门 更多>