Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Created May 14, 2019 11:10
Show Gist options
  • Save avr-programmierung/f422668a45ff3cac7fea2c98d0d38b84 to your computer and use it in GitHub Desktop.
Save avr-programmierung/f422668a45ff3cac7fea2c98d0d38b84 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz Servo 02
/* 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