C
59
leds8
Guest on 8th July 2022 05:30:44 PM
#include <avr/io.h>
#include <util/delay.h>
void longdelay(uint8_t ms)
{
while (ms!=0){
_delay_loop_2(0); // delays 262 ms on a 1Mhz CPU (from avr libc manual)
--ms;
}
}
void ledon(uint8_t curled) // function to turn on, delay and turn off the current led
{
PORTB= curled; // bind curled
longdelay(4); // we want a delay of 262 msecs on a 4 MHZ CPU
PORTB=0; // turn off all LEDs
}
int main(void)
{
uint8_t curled=0x80; // code of the led to be turned on
DDRB=0xff; // program all pins on port B to output
ledon(curled); // initially turn on the LED on pin 8
while (1){ // let's cyclically turn on each of the leds
while (curled != 0x01){
curled= curled>>1; // move the LED to be turned on to the right
ledon(curled);
}
while(curled != 0x80){
curled= curled<<1; // move the LED to be turned on to the left
ledon(curled);
}
}// close the infinite loop
}