使用定时器中断实现8个LED亮1000MS灭1000MS
我的写了一个程序,但是没有实现要求,哪里出错了呢?
程序和原理图如下:
#include<reg52.h>
#define uchar unsigned char
uchar count;
/*定时器中断初始化*/
void inint()
{
TMOD = 0x10;//选择定时器1
TH1 = 0x4b;
TL1 = 0xfd;//定时50MS
TR1 = 1;
ET1 = 1;
EA = 1;
}
int main(void)
{
inint();
while(1)
{
P1 = 0;
}
return 0;
}
/*定时器1中断服务程序*/
void timer1(void)interrupt 3
{
TH1 = 0x4b;
TL1 = 0xfd;
count++;
if (count == 20)
{
P1 = 1;
count = 0;
}
}
![](../imgqa/ele/mcu/mcu-3721rd.com-280255oyb11tth4s.png)
中断会被主程序刷新,所以应该吧while里的P1=0去掉,把中断中的P1=1改为P1=~P1
while(1)
{
P1 = 0;
}
死循环,
#include<reg52.h>
#define uchar unsigned char
uchar count;
/*定时器中断初始化*/
void inint()
{
TMOD = 0x10;//选择定时器1
TH1 = 0x4b;
TL1 = 0xfd;//定时50MS
TR1 = 1;
ET1 = 1;
EA = 1;
}
void main(void)
{
inint();
while(1);
}
/*定时器1中断服务程序*/
void timer1() interrupt 3
{
TH1 = 0x4b;
TL1 = 0xfd;
count++;
if (count == 20)
{
P1 =~P1;
count = 0;
}
}
#include<reg52.h>
#define uchar unsigned char
uchar count;
/*定时器中断初始化*/
void inint()
{
TMOD = 0x10;//选择定时器1
TH1 = 0x4b;
TL1 = 0xfd;//定时50MS
TR1 = 1;
ET1 = 1;
EA = 1;
}
void main(void)
{
inint();
while(1);
}
/*定时器1中断服务程序*/
void timer1() interrupt 3
{
TH1 = 0x4b;
TL1 = 0xfd;
count++;
if (count == 20)
{
P1 =~P1;
count = 0;
}
}
中断会被主程序刷新,是什么意思?可以给我讲讲吗?
对啊,我的理解是while(1)中的P1 = 0;,程序先运行当中断出现时,转去运行中断服务程序中的P1 = 1,完了再回到主程序运行P1 = 0;。哪里错了吗?