Advertisement
defineSN

blinky.c

Jan 4th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. /* function to read value from one port, and control the led blinking on another
  2. port by transferring data of the read port to the controlling port
  3. data read from port B, and led controlled from port D */
  4. // ATmega8, clock freq=1 MHz.
  5. #include <avr/io.h>
  6. #include<util/delay.h>
  7. #include<math.h>
  8. int main(void) {
  9. unsigned int polarity=0;
  10. unsigned int speed=0;
  11. double speed_n;
  12. int i;
  13. DDRB=0x00;  //port B set as input
  14. DDRD=0xFF;  //port D set as output
  15. PORTD=PINB;//port B input transferred to port D, PINB is register storing input data for port B
  16. while(1)
  17. {
  18. // polarity will be 0 or 1 based on whether B7 is low or high
  19. PINB>127?(polarity=1):(polarity=0); // for PINB to be > 127, B7 must be high
  20. speed=(PINB)&(0x7F);                    //masking B7, thus B0 to B6 value will result in speed control
  21. speed_n=(log10(speed)/log10(2));        //thus speed_n contains which(nth) multiple of 2 is the data read at Port B
  22. for(i=1;i<=128;i*=2)
  23. {
  24. if(polarity)
  25. PORTD=~i;           //if B7 is high, then polarity=1, and Port D displays inverse of i
  26. else
  27. PORTD=i;
  28. _delay_ms(100+(speed_n*50));
  29. }
  30. for(i=128;i>1;i-=i/2)
  31. {
  32. if(polarity)
  33. PORTD=~i;
  34. else
  35. PORTD=i;
  36. _delay_ms(100+(speed_n*50));
  37. }
  38. }return(1);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement