Statistics
| Branch: | Revision:

root / paintboard / code / servo.c @ 1226c007

History | View | Annotate | Download (1 KB)

1
#include <avr/io.h>
2
#include "servo.h"
3
#include <stdlib.h>
4

    
5
/*
6

7
servo controller. Timer0 is set to phase-correct PWM mode, with 32 prescaler.
8

9
SERVO_PWM1: PD6 (Timer0)
10

11
SERVO_PWM2: PD5 (Timer0)
12

13
*/
14

    
15
int8_t servo1_angle;
16
int8_t servo2_angle;
17

    
18
void servo_init()
19
{
20

    
21
  // WGM0 0b001 (phase-correct PWM, 8-bit)
22
  // COM0A 0b10 (clear OCR0A on compare match, set on BOTTOM)
23
  // COM0B 0b10 (clear OCR0B on compare match, set on BOTTOM)
24
  // CS0 0b011 (32 prescaler)
25
  TCCR0A = _BV(WGM00) | _BV(COM0A1) |_BV(COM0B1);
26
  TCCR0B = _BV(CS01) | _BV(CS00);
27
  OCR0A = 0;
28
  OCR0B = 0;
29
  
30
  // set output pins
31
  DDRD |= _BV(PD5) | _BV(PD6);
32

    
33
  set_servo1(0);
34
  set_servo2(0);
35
}
36

    
37
void set_servo1(int8_t angle)
38
/* angle must be between -128 and 127 */
39
{
40
  servo1_angle = angle;
41
  OCR0A = (uint8_t)(angle + 128);
42
}
43

    
44
void set_servo2(int8_t angle)
45
/* angle must be between -128 and 127 */
46
{
47
  servo2_angle = angle;
48
  OCR0B = (uint8_t)(angle + 128);
49
}
50

    
51
int8_t get_servo1()
52
{
53
  return servo1_angle;
54
}
55

    
56
int8_t get_servo2()
57
{
58
  return servo2_angle;
59
}
60