-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimmer.c
105 lines (91 loc) · 2.09 KB
/
dimmer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//#define POLLING
#define INTERRUPT
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "utils/pinFunc.h"
#include "dimmer.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 display_count(unsigned char num) {
//PB6 PB7 PD5 PD6 PD7
PORTD = num << PD7 | ((num >> 1) & 1) << PD6 | ((num >> 2) & 1) << PD5;
PORTB = ((num >> 3) & 1) << PB7 | ((num >> 4) & 1) << PB6;
}
void incr_count(void) {
if(count < 255) {
count++;
display_count(count);
OCR2B = 255 - count;
} else {
toggle_error_led();
}
}
void decr_count(void) {
if(count > 0) {
count--;
display_count(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_encoder();
init_leds();
init_pwm();
#ifdef POLLING
while(1) {
// 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);
}
#endif
#ifdef INTERRUPT
init_pci();
// set interrupts
sei();
//OCR2B = 100;
while(1) {};
#endif
return 0;
}*/