ATmega88 @ 8MHz Servo 02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* servo_02.c ATmega88 @ 8MHz */ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
int main(void) | |
{ | |
DDRB |= (1<<PB1); // OC1A = output | |
DDRC = 0xFC; // PC0 und PC1 = input | |
PORTC = 0x03; // PC0 und PC1 Pullup | |
ICR1 = 19999; // Top Value = 19999 (20ms) PWM-Frequenz = 50Hz | |
OCR1A = 1500; // Compare Match bei 1,5ms (Servoposition Mitte) | |
// Clear OC1A on Compare Match, Mode 14, Fast PWM, ICR1 = TOP, Prescaler = 1 | |
TCCR1A = (1 << COM1A1) + (1 << WGM11); | |
TCCR1B = (1 << WGM13) + (1 << WGM12) + (1 << CS10); | |
while(1) | |
{ | |
if (!(PINC & (1<<PINC0))) // Impulszeit verlängern (Servo dreht nach rechts) | |
{ | |
if (OCR1A <= 2000) // Max. Servowinkel | |
{ | |
OCR1A = OCR1A + 10; // OCR um 10 erhöhen | |
_delay_ms(20); // 20ms warten | |
} | |
} | |
if (!(PINC & (1<<PINC1))) // Impulszeit verkürzen (Servo dreht nach links) | |
{ | |
if (OCR1A >= 1000) // Min. Servowinkel | |
{ | |
OCR1A = OCR1A - 10; // OCR um 10 vermindern | |
_delay_ms(20); // 20ms warten | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment