Servo.c


1
/*
2
 * ----------------------------------------------------------------------------
3
 * SERVO Testprogramm                        3.4.2006
4
 * ----------------------------------------------------------------------------
5
 */
6
7
#include <inttypes.h>
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#include <avr/signal.h>
11
12
#  define OC1 PB1
13
#  define DDROC DDRB
14
#  define OCR OCR1
15
16
#  define Servo_Pause 20000          /* entspricht 20000µs oder 20ms wg. 1Mhz=1µs... */
17
#  define Servo_Puls   1500          /* Wert zwischen 1000 und 2000 (µs)       */
18
19
//enum { UP, DOWN };
20
21
//volatile uint16_t Servo_Puls=1000;
22
23
24
//----------------------------------------------------------------------------
25
26
27
SIGNAL (SIG_OUTPUT_COMPARE1A)        /* Wenn der Wert im OCR mit dem Timer übereinstimmt */
28
{
29
  if (OCR == Servo_Puls){        /* Wechsel zwischen Pause und Puls           */
30
    OCR = Servo_Pause;
31
  }
32
  else{
33
    OCR = Servo_Puls;
34
  }
35
}
36
37
38
//----------------------------------------------------------------------------
39
40
41
void
42
ioinit (void) 
43
{
44
    DDRB = _BV (OC1);            /* OC1 ist PB1 beim AT90S4433. Pin B1 ist über 10k mit Servo verbunden    */
45
46
    TCCR1A = _BV (COM10);          /* Toggle PB1 beim Übereinstimmung OCR/Timer (Compare match)         */
47
  TCCR1B = _BV (CTC1) | _BV (CS11);    /* [1]Clear on Compare Match. [2]tmr1 läuft mit 1/8 des SysCLK (1Mhz)    */
48
    OCR = Servo_Pause;            
49
50
    timer_enable_int (_BV (OCIE1));      /* Interrupt Vektor=5 "Timer/Counter1 Compare Match              */
51
    sei ();
52
}
53
54
55
//----------------------------------------------------------------------------
56
57
58
int
59
main (void)
60
{
61
    ioinit ();
62
63
    for (;;) 
64
        ;
65
66
    return (0);
67
}