我使用 AVR 作为微控制器, ATMEGA8 作为处理器(在微控制器内)。具有微控制器的板有4个LED。我能够刻录程序并点亮LED。但我无法达到特定的目标。
L1 L2 L3 L4
这些是4个LED。在第一轮中,每个LED在3秒的间隙后亮起。最后一个LED(L4)在第一轮之后保持点亮。当第三轮开始时,每个LED以3秒的间隙点亮,当L4也是L3时,L3保持点亮照明,它继续......直到L1。
L1 L2 L3 L4
On
On On
On On On
On On On On
但我无法做到这一点。因为我设置一个LED ON其他关闭。我甚至尝试添加一个小的时间间隔为10毫秒。我该怎么做呢 ?这就是我现在所拥有的:
#include<avr/io.h>
#include<util/delay.h>
DDRB = 0xFF; // input
//PORTB = 0xFF;
// ob00011110 --> on all --> binary
int i=0;
while(i<1) {
PORTB = 0b00010000; // first led on
_delay_ms(3000);
PORTB = 0b00001000; // second led on
_delay_ms(3000);
PORTB = 0b00000100; // third on
_delay_ms(3000);
PORTB = 0b00000010; // fourth on
_delay_ms(3000);
i += 1;
}
PORTB = 0b00000010; // keep the 4th on and start all over again and reach til 3rd LED
答案 0 :(得分:5)
看起来您的序列错了。当您打开第二个LED时,您将关闭第一个LED。顺序应为:
PORTB = 0b00010000; // first led only
_delay_ms(3000);
PORTB = 0b00011000; // first and second led on
_delay_ms(3000);
PORTB = 0b00011100; // first, second, and third on
_delay_ms(3000);
PORTB = 0b00011110; // first, second, third, and fourth on
_delay_ms(3000);
答案 1 :(得分:2)
您可以使用以下内容:
while (1){
PORTB = 0b00010000;
_delay_ms(3000);
PORTB |= 0b00001000;
_delay_ms(3000);
PORTB |= 0b00000100;
_delay_ms(3000);
PORTB |= 0b0000010;
_delay_ms(3000);
它将关闭每个循环开始时的所有LED,然后逐个打开......