-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwmTest.c
72 lines (61 loc) · 1.35 KB
/
pwmTest.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "utils/pinFunc.h"
static volatile unsigned char AB;
static volatile unsigned char count;
void init_pci(void) {
// When PCIE1 bit set = PCINT14:8 interrupts enabled
PCICR |= _BV(PCIE1);
// Enable PCINT8 and PCINT9 pins (B and A respectively)
PCMSK1 |= _BV(PCINT8) | _BV(PCINT9);
}
void init_encoder(void) {
count = 0;
PORTC |= _BV(PC0) | _BV(PC1);
}
void init_pwm(void) {
DDRD |= _BV(PD3);
TCCR2A |= _BV(COM2B0) | _BV(COM2B1) | _BV(WGM20) | _BV(WGM21);
TCCR2B |= _BV(CS21);
}
void incr_count(void) {
if(count < 255) {
count++;
OCR2B = 255 - count;
} else {
toggle_error_led();
}
}
void decr_count(void) {
if(count > 0) {
count--;
OCR2B = 255 - count;
} else {
toggle_error_led();
}
}
ISR(PCINT1_vect) {
// get value of encoder
unsigned char BA_new = PINC;
// use Rene Sommer's algorithm to get XOR sum to determine forward or backward
unsigned char sum = AB^BA_new;
// forward = 1, backward = 2
if(sum == 1) {
incr_count();
} else if (sum == 2) {
decr_count();
}
// swap A and B bits
AB = (BA_new >> 1 & 1) | (BA_new << 1 & 2);
}
int main(void)
{
init_pwm();
init_pci();
init_encoder();
init_leds();
// PWM inverted in code => 0 = high, 255 = low
//OCR2B = 100;
while(1);
}