/* * * Created on: July 02, 2011 * Author: Brian Redmond * This program controls an ATMEGA168 (other chips may be pin compatible) * with a servo on PB1 (pin 15) and 2 green LEDs on PD0 (pin 2) * The servo controls a skeleton jaw opening and closing * with delays at both ends and also cyclically brightens and dims * the LEDs for the eyes (ISR controlled) * The x values in the for loops control the range of motion for the jaw... * adjust the values as needed * */ #include #include #include #include #ifndef F_CPU #define F_CPU 1000000UL #endif int x; // For servo stepping static unsigned char b=20; // Initial dim value for eyes static uint8_t dir; // For eyes whether getting dimmer or brighter enum { UP, DOWN }; ISR(TIMER0_COMPA_vect) // Turns off both LEDs when TCNT0 matches OCR0A { PORTD=(0<<0); } ISR(TIMER0_OVF_vect) // Turns on both LEDs on TCNT0 overflow { PORTD=(1<<0); // Turns on LEDs switch (dir) // Increments b (LED brightness) to 250 then decrements to 20 { case UP: if(++b == 250) dir=DOWN; break; case DOWN: if(--b==20) dir=UP; break; } OCR0A=b; } int main(void) { DDRB=0xFF; // PORT B as output DDRD=0xFF; // PORT D as output OCR1A=1400; // Starting position for jaw ( half open) OCR0A=20; // Starting value for eyes (dim) PORTD |=(1<<0); // Turn on eyes ICR1=19999; // Top value for 50Hz PWM for servo control TCCR1A=((0<=1100;x-=100) // Slow movement of jaw from closed to open with 3 second delay at fully closed { OCR1A=x; _delay_ms(100); } } }