刚开始学51
单片机,买了个
开发板,做LED实验,开发板的8个LED接的STC89C52RC的P2口@P2^0-P2^7分别对应1-8个LED)
想实现的效果是1-4个LED和5-8个LED两两交替闪烁,但是下载到单片机后的结果却是5-8四个LED可以实现LED4,LED6和LED5,LED7交替闪烁,但是LED0,LED2常亮,LED1和LED3一起快速闪一下后灭,然后又快速闪一下灭如此闪烁,删除下列程序中的if(b ==2)和if(b ==4)中的对应的任意一个函数,都能实现两两交替闪烁,求解!函数如下:
#include<reg52.h>
typedef unsigned char uint8;
uint8 a,b;
sbit LED0 = P2^0;
sbit LED1 = P2^1;
sbit LED2 = P2^2;
sbit LED3 = P2^3;
sbit LED4 = P2^4;
sbit LED5 = P2^5;
sbit LED6 = P2^6;
sbit LED7 = P2^7;
main()
{
TMOD = 0x01;
TF0 = 0;
TH0 = 0xb8;
TL0 = 0x00;
TR0 = 1;
P2 = 0xaa;
a = 0; b = 0;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xb8;
TL0 = 0x00;
a++;
}
if(a == 10)
{
a = 0;
b++;
}
if(b == 2)
{
LED4 = ~LED4;
LED5 = ~LED5;
LED6 = ~LED6;
LED7 = ~LED7;
}
if(b == 4)
{
b = 0;
LED0 = ~LED0;
LED1 = ~LED1;
LED2 = ~LED2;
LED3 = ~LED3;
}
}
}
EA = 1;
其次,定时器0在01模式下的中断标志位是自动清零的。
还有定时器0的中断使能也没有开
ET0 =1;
typedef unsigned char uint8;
uint8 a,b;
sbit LED0 = P2^0;
sbit LED1 = P2^1;
sbit LED2 = P2^2;
sbit LED3 = P2^3;
sbit LED4 = P2^4;
sbit LED5 = P2^5;
sbit LED6 = P2^6;
sbit LED7 = P2^7;
main()
{
P2 = 0xaa;
TMOD = 0x01;
TH0 = 0xb8;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
EA = 1;
a = 0;
b = 0;
while(1)
{
if(a == 10)
{
a = 0;
b++;
}
if(b == 2)
{
b++;
LED7 = ~LED7;
LED4 = ~LED4;
LED6 = ~LED6;
LED5 = ~LED5;
}
if(b == 4)
{
b = 0;
LED0 = ~LED0;
LED1 = ~LED1;
LED2 = ~LED2;
LED3 = ~LED3;
}
}
}
void time0Isr() interrupt 1
{
TH0 = 0xb8;
TL0 = 0x00;
a++;
}
谢谢回复,我现在还没学到外部中断,我的想法是让定时器0工作在01模式,然后开始定时,当定时器溢出时,TF = 1,然后TF = 0清零,同时a++,当a = 10时,b++,当b = 2时,P2^4到P2^7输出电位反相,当b = 4时,P2^0到P2^3输出电位反相,同时b = 0,清零重新计数。
P2的初始值时10101010,按照我的理解其真值表如下:
当=0时,对应的LED应该为亮状态,按如下应该能实现P2口的8个LED4个一组以相同的间隔不同的时间交替闪烁,但是把程序下载的单片机运行后的结果是P2^4到P2^7对应的LED能实现相同的功能,P2^1和P2^3也能闪烁,但是P2^0和P2^1几乎是常亮状态,只能隐约看到稍微暗一点点。如果把b = 2或者b = 4那一段去掉的话就能实现对应的闪烁效果
b P2
0 10101010 //初始值
1 10101010
2 10100101
3 10100101
4 01010101
0 01010101
1 01010101
2 01011010
3 01011010
4 10101010
0 10101010
请明示!!!
一周热门 更多>