// // hello.servo.44.c // // servo motor hello-world // // set lfuse to 0x7E for 20 MHz xtal // // Neil Gershenfeld---modification by Jose Figueroa 19/03/2013 // 4/8/12 // // (c) Massachusetts Institute of Technology 2012 // Permission granted for experimental and personal use; // license for commercial sale available from MIT. // #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define input(directions,pin) (directions &= (~pin)) // set port direction for input #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define position_delay() _delay_ms(1000) #define PWM_port PORTB #define PWM_pin (1 << PB2) #define PWM_direction DDRB #define input_port PORTA #define input_direction DDRA #define input_pin (1 << PA3) #define input_pins PINA int main(void) { // // main // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // set up timer 0 // TCCR0A = (1 << COM0A1) | (0 << COM0A0) | (0 << WGM01) | (1 << WGM00); // clear OC0A on compare match TCCR0B = (0 << CS02) | (1 << CS01) | (0 << CS00) | (0 << WGM02); // prescaler /8, phase and frequency correct PWM, 0xFF TOP // // set PWM pin to output // clear(PWM_port, PWM_pin); output(PWM_direction, PWM_pin); set(input_port, input_pin); // turn on pull-up input(input_direction, input_pin); // // main loop // while (1) { while (0 == pin_test(input_pins,input_pin)) ; OCR0A = 50; position_delay(); OCR0A = 250; position_delay(); } }