C   59

leds8

Guest on 8th July 2022 05:30:44 PM

  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3.  
  4. void longdelay(uint8_t ms)     
  5. {
  6.   while (ms!=0){
  7.       _delay_loop_2(0); // delays 262 ms on a 1Mhz CPU (from avr libc manual)
  8.       --ms;
  9.   }
  10. }
  11.  
  12. void ledon(uint8_t curled) // function to turn on, delay and turn off the current led
  13. {
  14.   PORTB= curled; // bind curled
  15.   longdelay(4); // we want a delay of 262 msecs on a 4 MHZ CPU
  16.   PORTB=0; // turn off all LEDs
  17. }
  18.  
  19. int main(void)
  20. {
  21.   uint8_t curled=0x80; // code of the led to be turned on
  22.   DDRB=0xff; // program all pins on port B to output   
  23.   ledon(curled); // initially turn on the LED on pin 8
  24.   while (1){ // let's cyclically turn on each of the leds
  25.     while (curled != 0x01){            
  26.            curled= curled>>1; // move the LED to be turned on to the right
  27.            ledon(curled);
  28.         }
  29.     while(curled != 0x80){  
  30.        curled= curled<<1; // move the LED to be turned on to the left
  31.            ledon(curled);
  32.     }
  33.  
  34.   }// close the infinite loop
  35. }

Raw Paste


Login or Register to edit or fork this paste. It's free.