Advertisement
grist

ATtiny85 Eyes

Nov 27th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. /*
  2.  * ATtiny85_Eyes.c
  3.  *
  4.  * Created: 11/25/2014 7:36:41 PM
  5.  *  Author: grist.carrigafoyl
  6.  
  7.  Some code to make 2 LEDs behave kind of like eyes.
  8.  
  9.  Want periodic blinking and the occasional wink.
  10.  
  11.  
  12.  */
  13. #define F_CPU 8000000UL // 8Mhz
  14.  
  15. #define LEFT_EYE PB0  // pin5
  16. #define RIGHT_EYE PB1 // pin6
  17.  
  18. // macros to set eye brightness
  19. #define SET_RIGHT_EYE_LEVEL(x)  OCR0A = (255 - x)
  20. #define SET_LEFT_EYE_LEVEL(x)   OCR0B = (255 - x)
  21.  
  22. #include <avr/io.h>
  23. #include <avr/interrupt.h>
  24.  
  25. // volatile because it's changed in the interrupt routine.
  26. volatile unsigned long counter = 0;
  27.  
  28.  
  29. // function prototypes
  30. void wink(uint8_t eye);
  31. void blink();
  32. void delay(unsigned long time);
  33. void fade_up(int delay_length);
  34.  
  35. int main(void)
  36. {
  37.     // Set the LED pins as output
  38.     DDRB = _BV(LEFT_EYE) | _BV(RIGHT_EYE);
  39.    
  40.     // Enable ALL the Timer0 output compare interrupts!
  41.     TIMSK = _BV(OCIE0A) | _BV(OCIE0B) | _BV(TOIE0);
  42.    
  43.     // Settings for the delay timer
  44.     TIMSK |= _BV(TOIE1); // overflow timer 1
  45.    
  46.     // start the delay timer
  47.     TCCR1 = _BV(CS12); // prescaler 8
  48.    
  49.     // start the PWM timer, prescaler 64, for around 485Hz
  50.     TCCR0B = _BV(CS01) | _BV(CS00);
  51.    
  52.     // Enable global interrupts
  53.     sei();
  54.    
  55.     while(1)
  56.     {
  57.         fade_up(200);
  58.         delay(10000); // change to some random time
  59.         wink(RIGHT_EYE);
  60.         delay(10000);
  61.         wink(LEFT_EYE);
  62.         delay(10000);
  63.         blink();
  64.         delay(10000);
  65.  
  66.     }
  67. }
  68.  
  69.  
  70. void blink()
  71. // Blink the eyes
  72. {
  73.     int i; // brightness
  74.     int delay_length = 2; // delay between changing brightness levels.
  75.  
  76.     for (i=255;i>0;i--) {
  77.         SET_LEFT_EYE_LEVEL(i);
  78.         SET_RIGHT_EYE_LEVEL(i);
  79.         delay(delay_length);
  80.     }
  81.     delay(1000);
  82.     for (i=0;i<255;i++) {
  83.         SET_LEFT_EYE_LEVEL(i);
  84.         SET_RIGHT_EYE_LEVEL(i);
  85.         delay(delay_length);
  86.     }
  87.  
  88.  
  89. }
  90.  
  91.  
  92. void wink(uint8_t eye)
  93. // Wink an eye
  94. {
  95.     int i; // brightness
  96.     int delay_length = 2; // delay between changing brightness levels.
  97.    
  98.     for (i=255;i>0;i--) {
  99.         if (eye == RIGHT_EYE) {
  100.             SET_RIGHT_EYE_LEVEL(i);
  101.         } else {
  102.             SET_LEFT_EYE_LEVEL(i);
  103.         }
  104.         delay(delay_length);
  105.     }  
  106.     delay(1000);   
  107.     for (i=0;i<255;i++) {
  108.         if (eye == RIGHT_EYE) {
  109.             SET_RIGHT_EYE_LEVEL(i);
  110.         } else {
  111.             SET_LEFT_EYE_LEVEL(i);
  112.         }
  113.         delay(delay_length);
  114.     }
  115.  
  116.    
  117. }
  118.  
  119. void fade_up(int delay_length)
  120. // Fade both eyes up slowly.
  121. {
  122.     int i; // brightness
  123.     for (i=1;i<255;i++) {
  124.         TCNT0 = 0; // reset the hardware counter
  125.         SET_LEFT_EYE_LEVEL(i);
  126.         SET_RIGHT_EYE_LEVEL(i);
  127.         delay(delay_length);
  128.     }
  129. }
  130.  
  131. void delay(unsigned long time)
  132. // an arbitrary blocking delay
  133. {
  134.     TCNT1 = 0; // reset the hardware counter
  135.     counter = 0; // this is incremented by the overflow interrupt
  136.     while (counter < time) {
  137.         // dum de doo, twiddle twiddle.
  138.     }
  139. }
  140.  
  141.  
  142. // Interrupt routines
  143. ISR(TIMER0_OVF_vect)
  144. // Timer 0 overflow, so end of the pulse
  145. {
  146.     PORTB = 0; // might as well turn 'em all off in one hit. no other output pins in this application.
  147. }
  148.  
  149. ISR(TIMER0_COMPA_vect)
  150. // Start of the pulse for the left eye
  151. {
  152.     PORTB |= _BV(LEFT_EYE);
  153. }
  154.  
  155. ISR(TIMER0_COMPB_vect)
  156. // Start of the pulse for the right eye
  157. {
  158.     PORTB |= _BV(RIGHT_EYE);
  159. }
  160.  
  161. ISR(TIMER1_OVF_vect)
  162. // Overflow for timer 1. Used for the delay timer
  163. {
  164.     counter++;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement