-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwmgenerate.cpp
46 lines (40 loc) · 1.01 KB
/
pwmgenerate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Arduino.h"
#include "debug.h"
#include "pwmgenerate.h"
/**
* Generates PWM signal for servo control
*/
PWMGenerate::PWMGenerate(uint8_t pin) {
pinMode(pin, OUTPUT);
}
/**
* Initalizes Timer. Used to generate the PWM signal to control the X - Y axis
* of the camera
*/
void PWMGenerate::initTimer() {
DPRINTLNF("Initializing timer");
// COM1A0 = 1; COM1A1 = 0 Toggle OC1A on Compare Match
// OC1A corresponds to pin 9
TCCR1A |= _BV(COM1A1);
// CS10-CS12 Set the pre-scaler to 8 (010)
// WGM10-WGM13 Set Fast PWM with ICR1 controlling top (0111)
TCCR1A &= ~_BV(WGM10);
TCCR1A |= _BV(WGM11);
TCCR1B = _BV(CS11) | _BV(WGM13) | _BV(WGM12);
ICR1 = this->TOP;
OCR1A = this->compare;
}
/**
* Set duty cycle
* Position can range from -6 to +6
*/
void PWMGenerate::setPosition(int position) {
this->compare = this->DEFAULT_COMPARE + position;
this->updateCompare();
}
/**
* Update compare register
*/
void PWMGenerate::updateCompare() {
OCR1A = this->compare;
}