kingst 51开发板,在中断调用LedShow 和在主程序调用LedShow,效果不同,为什么?
时间:10-02
整理:3721RD
点击:
kingst 51开发板,自制红绿灯程序,在中断调用LedShow 和在主程序调用LedShow,效果不同,为什么?
STC59C52
---------------------------
请大家帮我看看,下面的程序,在中断调用LedShow 和在主程序调用LedShow,效果不同,为什么?
在中断调用时:LED和发光二极管目测下,一闪一闪的。
在主程序调用时: 发光正常.
是因为调用LedShow时,LedShow 没有执行完毕,而产生新的中断吗? 我是菜鸟,庆大侠赐教
#include<reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void ConfigerTimer0(unsigned int ms);
static unsigned char timer = 0;
static unsigned char LedBuff[]= {
0xFF,0xFF,0xFF
};
unsigned char code LedChar[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
static unsigned char T0TH = 0;
static unsigned char T0TL = 0;
static unsigned int counter = 0;
void LedShow();
void main()
{
unsigned char color = 0;
EA = 1;
ENLED = 0;
ConfigerTimer0(1);
while(1)
{
if(timer == 0)
{
switch(color)
{
case 0: timer = 9; color = 1; LedBuff[2] = 0x3F; break;
case 1: timer = 15; color = 2; LedBuff[2] = 0xE7; break;
case 2: timer = 5; color = 0; LedBuff[2] = 0xFC; break;
default:break;
}
}
LedBuff[0] = LedChar[timer%10];
LedBuff[1] = LedChar[timer/10];
LedShow();
};
}
void ConfigerTimer0(unsigned int ms)
{
unsigned long tmp;
tmp = 11059200/12;
tmp = (tmp*ms)/1000;
T0TH =(unsigned char)(tmp>>8);
T0TL =(unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
ET0 = 1;
TR0 = 1;
}
void LedShow()
{
static unsigned char i = 0;
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 = 1;ADDR1 = 1;ADDR0 = 0; i=0;P0 = LedBuff[2]; break;
default: break;
}
}
void InterruptTimer0() interrupt 1
{
TH0 = T0TH;
TL0 = T0TL;
counter++;
if(counter>=20)
{
counter = 0;
timer--;
}
}
STC59C52
---------------------------
请大家帮我看看,下面的程序,在中断调用LedShow 和在主程序调用LedShow,效果不同,为什么?
在中断调用时:LED和发光二极管目测下,一闪一闪的。
在主程序调用时: 发光正常.
是因为调用LedShow时,LedShow 没有执行完毕,而产生新的中断吗? 我是菜鸟,庆大侠赐教
#include<reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void ConfigerTimer0(unsigned int ms);
static unsigned char timer = 0;
static unsigned char LedBuff[]= {
0xFF,0xFF,0xFF
};
unsigned char code LedChar[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
static unsigned char T0TH = 0;
static unsigned char T0TL = 0;
static unsigned int counter = 0;
void LedShow();
void main()
{
unsigned char color = 0;
EA = 1;
ENLED = 0;
ConfigerTimer0(1);
while(1)
{
if(timer == 0)
{
switch(color)
{
case 0: timer = 9; color = 1; LedBuff[2] = 0x3F; break;
case 1: timer = 15; color = 2; LedBuff[2] = 0xE7; break;
case 2: timer = 5; color = 0; LedBuff[2] = 0xFC; break;
default:break;
}
}
LedBuff[0] = LedChar[timer%10];
LedBuff[1] = LedChar[timer/10];
LedShow();
};
}
void ConfigerTimer0(unsigned int ms)
{
unsigned long tmp;
tmp = 11059200/12;
tmp = (tmp*ms)/1000;
T0TH =(unsigned char)(tmp>>8);
T0TL =(unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
ET0 = 1;
TR0 = 1;
}
void LedShow()
{
static unsigned char i = 0;
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 = 1;ADDR1 = 1;ADDR0 = 0; i=0;P0 = LedBuff[2]; break;
default: break;
}
}
void InterruptTimer0() interrupt 1
{
TH0 = T0TH;
TL0 = T0TL;
counter++;
if(counter>=20)
{
counter = 0;
timer--;
}
}
顶下自己的问题