Hallo,
da ich den Timer 1 meines Atmega für die input capture Funktion brauche,
möchte ich nun mit dem 8bit Timer mein Servo ansteuern.
Meine grundlegenden Überlegungen dazu waren, dass ich alle 0,004ms einen
Interrupt auslöse und dabei eine Zählvariable hochzähle mit der ich den
1-2ms Impuls erzeuge und nach 20ms erneut starte.
Leider bewegt sich das Servo nicht, habe testweise auch schon den Wert
des Winkels in main geändert, bewegt sich trotzdem nicht.
Wäre toll wenn mir hier jemand meinen Fehler aufzeigen könnte.
Hier mal der Code:
1 | #define outputPin PIND7
|
2 | #define outputPort PORTD
|
3 | #define outputDDR DDRD
|
4 |
|
5 | #define F_CPU 16000000UL
|
6 |
|
7 | #include <avr/io.h>
|
8 | #include <avr/interrupt.h>
|
9 | #include <util/delay.h>
|
10 |
|
11 | volatile uint16_t angle=90; //Winkel in Grad
|
12 |
|
13 | void output_init(void){
|
14 | outputDDR |= (1<<outputPin);
|
15 | outputPort &= ~(1<<outputPin);
|
16 | }
|
17 |
|
18 | void timer_init(void){
|
19 |
|
20 | TCCR0B |= (1<<CS01) | (1<<CS00); //Normal mode F_CPU/64
|
21 | TIMSK0 |= (1<<TOIE0);
|
22 | }
|
23 |
|
24 | ISR(TIMER0_OVF_vect){ //Interrupt alle 0,004ms
|
25 | static uint16_t ms_counter=0;
|
26 |
|
27 | if (ms_counter==5000) ms_counter=0; // 20ms Interval erzeugen 5000*0,004=20ms
|
28 | else ms_counter++;
|
29 |
|
30 | if (ms_counter<((1+(angle/180))/0.004)) //Winkel in Interval umrechnen
|
31 | { outputPort |= (1<<outputPin); //Pin so lange einschalten bis gewünschte Dauer erreicht
|
32 | }
|
33 |
|
34 | else {outputPort &= ~(1<<outputPin);
|
35 | }
|
36 | }
|
37 |
|
38 | int main(void)
|
39 | {
|
40 | output_init();
|
41 | timer_init();
|
42 |
|
43 | sei();
|
44 |
|
45 | while(1)
|
46 | {
|
47 |
|
48 |
|
49 | }
|
50 | }
|