蓝桥杯单片机第七届省赛,打开定时器中断后,数码管疯狂闪烁,而且值不会变,
其中onewire用的是官方的底层代码,其中延时已经扩大了12倍
#include"reg52.h"
#include"intrins.h"
#include"onewire.h"
void smg();
sfr AUXR=0x8e;
sbit Fen=P3^4;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
int K4=1,K5=0,K7=0;
int last_time=60;
int count=0;
int count_1=0;
int pwm_num=0;
unsigned int temper;
char code xianshi[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6};
void choose573(int n)
{
switch(n)
{
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
case(0):P2=(P2&0x1f)|0x00;break;
}
}
void clean()
{
choose573(4);
P0=0xff;
choose573(5);
P0=0x00;
choose573(0);
P0=0xff;
}
//----------------------------------------------wendu
void read_temper()
{
int HBS,LBS;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LBS=Read_DS18B20();
HBS=Read_DS18B20();
temper=HBS<<4;
temper=temper|(LBS>>4);
}
//----------------------------------------------
//----------------------------------------------pwm
void Timer0Init(void) //10微秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x91; //设置定时初始值
TH0 = 0xFF; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void serverTimer0Init()interrupt 1
{
TL0 = 0x91; //设置定时初始值
TH0 = 0xFF; //设置定时初始值
count++;
count_1++;
if(last_time!=0)
{
if(count_1<=100000)
{
if(count<=pwm_num)
{
Fen=1;
}
else if(count>pwm_num&&count<100)
{
Fen=0;
}
else if(count==100)
{
count=0;
}
}
else if(count_1==100000)
{
last_time--;
count_1=0;
}
}
}
//----------------------------------------------
//----------------------------------------------anjian
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{smg();
while (--j);
} while (--i);
}
void key_board()
{
if(S4==0)
{
Delay5ms();
if(S4==0)
{
switch(K4)
{
case(1):pwm_num=20;K4=2;break;
case(2):pwm_num=30;K4=3;break;
case(3):pwm_num=70;K4=1;break;
}
}
while(!S4);
}
if(S5==0)
{
Delay5ms();
if(S5==0)
{
switch(K5)
{
case(0):last_time=60;K5=1;break;
case(1):last_time=120;K5=2;break;
case(2):last_time=0;K5=0;break;
}
}
while(!S5);
}
if(S6==0)
{
Delay5ms();
if(S6==0)
{
last_time=0;
}
while(!S6);
}
if(S7==0)
{
Delay5ms();
if(S7==0)
{
if(K7==1)
K7=0;
else
K7=1;
}
while(!S7);
}
}
//----------------------------------------------
//----------------------------------------------SMG
void Delay400us() //@11.0592MHz
{
unsigned char i, j;
i = 5;
j = 74;
do
{
while (--j);
} while (--i);
}
void SMG_display(int wei,int dat)
{
choose573(6);
P0=0x80>>(wei-1);
choose573(7);
P0=xianshi[dat];
}
void smg()
{
if(K7==0)
{
SMG_display(1,last_time%10);
Delay400us();
SMG_display(2,last_time/10-(last_time/100)*10);
Delay400us();
SMG_display(3,last_time/100);
Delay400us();
SMG_display(4,0);
Delay400us();
SMG_display(5,10);
Delay400us();
SMG_display(6,11);
Delay400us();
SMG_display(7,K4);
Delay400us();
SMG_display(8,11);
Delay400us();
}
if(K7==1)
{
SMG_display(1,12);
Delay400us();
SMG_display(2,temper%10);
Delay400us();
SMG_display(3,temper/10);
Delay400us();
SMG_display(4,10);
Delay400us();
SMG_display(5,10);
Delay400us();
SMG_display(6,11);
Delay400us();
SMG_display(7,4);
Delay400us();
SMG_display(8,11);
Delay400us();
}
}
//----------------------------------------------
void main()
{
clean();
Timer0Init();
while(1)
{
smg();
key_board();
read_temper();
}
}
关闭中断以后,数码管显示正常,而且温度也显示正常
想要知道为什么打开中断以后数码管闪烁的原因,以及解决的方案
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。