Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ATtiny85_Eyes.c
- *
- * Created: 11/25/2014 7:36:41 PM
- * Author: grist.carrigafoyl
- Some code to make 2 LEDs behave kind of like eyes.
- Want periodic blinking and the occasional wink.
- */
- #define F_CPU 8000000UL // 8Mhz
- #define LEFT_EYE PB0 // pin5
- #define RIGHT_EYE PB1 // pin6
- // macros to set eye brightness
- #define SET_RIGHT_EYE_LEVEL(x) OCR0A = (255 - x)
- #define SET_LEFT_EYE_LEVEL(x) OCR0B = (255 - x)
- #include <avr/io.h>
- #include <avr/interrupt.h>
- // volatile because it's changed in the interrupt routine.
- volatile unsigned long counter = 0;
- // function prototypes
- void wink(uint8_t eye);
- void blink();
- void delay(unsigned long time);
- void fade_up(int delay_length);
- int main(void)
- {
- // Set the LED pins as output
- DDRB = _BV(LEFT_EYE) | _BV(RIGHT_EYE);
- // Enable ALL the Timer0 output compare interrupts!
- TIMSK = _BV(OCIE0A) | _BV(OCIE0B) | _BV(TOIE0);
- // Settings for the delay timer
- TIMSK |= _BV(TOIE1); // overflow timer 1
- // start the delay timer
- TCCR1 = _BV(CS12); // prescaler 8
- // start the PWM timer, prescaler 64, for around 485Hz
- TCCR0B = _BV(CS01) | _BV(CS00);
- // Enable global interrupts
- sei();
- while(1)
- {
- fade_up(200);
- delay(10000); // change to some random time
- wink(RIGHT_EYE);
- delay(10000);
- wink(LEFT_EYE);
- delay(10000);
- blink();
- delay(10000);
- }
- }
- void blink()
- // Blink the eyes
- {
- int i; // brightness
- int delay_length = 2; // delay between changing brightness levels.
- for (i=255;i>0;i--) {
- SET_LEFT_EYE_LEVEL(i);
- SET_RIGHT_EYE_LEVEL(i);
- delay(delay_length);
- }
- delay(1000);
- for (i=0;i<255;i++) {
- SET_LEFT_EYE_LEVEL(i);
- SET_RIGHT_EYE_LEVEL(i);
- delay(delay_length);
- }
- }
- void wink(uint8_t eye)
- // Wink an eye
- {
- int i; // brightness
- int delay_length = 2; // delay between changing brightness levels.
- for (i=255;i>0;i--) {
- if (eye == RIGHT_EYE) {
- SET_RIGHT_EYE_LEVEL(i);
- } else {
- SET_LEFT_EYE_LEVEL(i);
- }
- delay(delay_length);
- }
- delay(1000);
- for (i=0;i<255;i++) {
- if (eye == RIGHT_EYE) {
- SET_RIGHT_EYE_LEVEL(i);
- } else {
- SET_LEFT_EYE_LEVEL(i);
- }
- delay(delay_length);
- }
- }
- void fade_up(int delay_length)
- // Fade both eyes up slowly.
- {
- int i; // brightness
- for (i=1;i<255;i++) {
- TCNT0 = 0; // reset the hardware counter
- SET_LEFT_EYE_LEVEL(i);
- SET_RIGHT_EYE_LEVEL(i);
- delay(delay_length);
- }
- }
- void delay(unsigned long time)
- // an arbitrary blocking delay
- {
- TCNT1 = 0; // reset the hardware counter
- counter = 0; // this is incremented by the overflow interrupt
- while (counter < time) {
- // dum de doo, twiddle twiddle.
- }
- }
- // Interrupt routines
- ISR(TIMER0_OVF_vect)
- // Timer 0 overflow, so end of the pulse
- {
- PORTB = 0; // might as well turn 'em all off in one hit. no other output pins in this application.
- }
- ISR(TIMER0_COMPA_vect)
- // Start of the pulse for the left eye
- {
- PORTB |= _BV(LEFT_EYE);
- }
- ISR(TIMER0_COMPB_vect)
- // Start of the pulse for the right eye
- {
- PORTB |= _BV(RIGHT_EYE);
- }
- ISR(TIMER1_OVF_vect)
- // Overflow for timer 1. Used for the delay timer
- {
- counter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement