Suh*_*pta 0 c microcontroller avr atmega avr-gcc
我使用AVR作为微控制器,使用ATMEGA8作为处理器(在微控制器内部).具有微控制器的板有4个LED.我能够刻录程序并点亮LED.但我无法达到特定的目的.
L1 L2 L3 L4
Run Code Online (Sandbox Code Playgroud)
这些是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
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点.因为我设置一个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
Run Code Online (Sandbox Code Playgroud)
看起来你的序列是错误的.当您打开第二个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);
Run Code Online (Sandbox Code Playgroud)