Hier mal der Code für 2 Servos
Da auch noch SW-PWM dazumachen für die Motoren ist sicher auch noch eine knifflige Sache.Code:#include <avr/io.h> #include <stdbool.h> #include <stdlib.h> #include <avr/io.h> #include <AVR/iom8.h> #include <inttypes.h> //#include <util/delay.h> #define F_CPU 12000000 // clock // Timer1 initialisieren void timer1_init(void) { TCCR1A |= (1<<WGM11)|(1<<COM1A1)|(1<<COM1A0)|(1<<COM1B1)|(1<<COM1B0); // initalize mega8 Timer1 with 9Bit, inverted, Prescaler 256 TCCR1B = (1<<CS12); // this gives us a signal with 21.76ms at 12MHz OCR1A = 476; // pulse of 1.5ms 512- 1500*(F_CPU/256/1000000)/2 OCR1B = 476; } int main (void) { timer1_init(); DDRB = (1<<PB1)|(1<<PB2); /* Pin PB1 2 als Ausgang für Servo */ DDRD &= (~ (1<<PD2)|(1<<PD3)|(1<<PD0)|(1<<PD1)); /* Pin D als Eingang */ PORTD |= (1<<PD2)|(1<<PD3)|(1<<PD0)|(1<<PD1); /* Pull Up aktivieren */ while(1){ if (!( PIND & (1<<PIND2))) /* mache was wenn PinD2 low ist */ { OCR1A=450; } if (!( PIND & (1<<PIND3))) /* mache was wenn PinD3 low ist */ { OCR1A=499; } if (( PIND & (1<<PIND2))&&(PIND & (1<<PIND3))) { /* Mitte wenn keine Taste */ OCR1A = 476; } if (!( PIND & (1<<PIND0))) /* mache was wenn PinD0 low ist */ { OCR1B=450; } if (!( PIND & (1<<PIND1))) /* mache was wenn PinD1 low ist */ { OCR1B=499; } if (( PIND & (1<<PIND0))&&(PIND & (1<<PIND1))) { /* Mitte wenn keine Taste */ OCR1B=476; } } }
Edit: Welche Frequenz hast du dir für die PWM vorgestellt.







Zitieren

Lesezeichen