Revision 1095
Test code to clarify timer operation
Created data structures
branches/library_refactor/behaviors/library_test/main.c | ||
---|---|---|
84 | 84 |
//orb_enable (); |
85 | 85 |
|
86 | 86 |
orb_init (); |
87 |
delay_ms (1000); |
|
88 |
while (1) |
|
87 |
//delay_ms (1000);
|
|
88 |
while (1);
|
|
89 | 89 |
{ |
90 | 90 |
orb1_set (1,0,0); |
91 | 91 |
orb2_set (0,1,0); |
branches/library_refactor/projects/libdragonfly/lights.c | ||
---|---|---|
46 | 46 |
bugs, unnecessary synchronized code, memory waste |
47 | 47 |
*/ |
48 | 48 |
|
49 |
/* |
|
50 |
Operation: |
|
51 |
On timer overflow: |
|
52 |
- switch on LEDs (where value>0, according to a pre-determined mask) |
|
53 |
- load the first output compare value |
|
54 |
At compare match: |
|
55 |
- switch off LEDs (according to mask) |
|
56 |
- load the next output compare value |
|
57 |
|
|
58 |
*/ |
|
59 |
|
|
49 | 60 |
#include "lights.h" |
50 | 61 |
#include <avr/interrupt.h> |
51 | 62 |
#include "dragonfly_lib.h" |
... | ... | |
93 | 104 |
|
94 | 105 |
// Mask for the individual LEDs, organized as an array for programmatic access. |
95 | 106 |
// The layout of this array is orb_mask[orb_num, color_num] |
96 |
const uint8_t orb_mask[2][3]=
|
|
107 |
const uint8_t orb_mask[NUM_ORBS][NUM_COLORS]=
|
|
97 | 108 |
{ |
98 | 109 |
{ orb1_red_mask, orb1_green_mask, orb1_blue_mask }, |
99 | 110 |
{ orb2_red_mask, orb2_green_mask, orb2_blue_mask } |
100 | 111 |
}; |
101 | 112 |
|
102 | 113 |
|
114 |
// *********** |
|
115 |
// ** Types ** |
|
116 |
// *********** |
|
117 |
|
|
118 |
struct pwm_channel |
|
119 |
{ |
|
120 |
uint8_t time; |
|
121 |
uint8_t mask; |
|
122 |
}; |
|
123 |
|
|
124 |
|
|
125 |
// *************** |
|
126 |
// ** Variables ** |
|
127 |
// *************** |
|
128 |
|
|
129 |
struct pwm_channel channels[NUM_ORBS*NUM_COLORS]; |
|
130 |
|
|
131 |
|
|
103 | 132 |
// ******************** |
104 | 133 |
// ** Initialization ** |
105 | 134 |
// ******************** |
... | ... | |
117 | 146 |
// Set all orbs to "off" |
118 | 147 |
orb_set (0, 0, 0); |
119 | 148 |
|
149 |
// Set up the timer |
|
150 |
// Prescaler 8 is 66 ms or 15 Hz (8 MHz/prescaler 8/2^16) |
|
151 |
TCCR3A=0; |
|
152 |
TCCR3B=_BV(CS31); // Prescaler 8 |
|
153 |
TCCR3B=_BV(CS31) | _BV(CS30); // Prescaler 64 |
|
154 |
TCCR3C=0; |
|
155 |
ETIMSK |= _BV(TOIE3) | _BV(OCIE3C); // Enable Overflow Interrupt and Output compare 3 interrupt |
|
156 |
|
|
157 |
// Clear Timer on Compare match: |
|
158 |
// Either from OCR3A (WGM3 3:0=4) or from ICR3 (WGM3 3:0=12 |
|
159 |
|
|
160 |
TCCR3B |= _BV(WGM33); |
|
161 |
ICR3=511; |
|
162 |
|
|
163 |
OCR3C=127; |
|
164 |
|
|
165 |
// Debug |
|
166 |
DDRF=2; |
|
167 |
|
|
120 | 168 |
// uint16_t i; |
121 | 169 |
// uint8_t mysreg; |
122 | 170 |
// |
... | ... | |
145 | 193 |
} |
146 | 194 |
// |
147 | 195 |
|
196 |
|
|
197 |
// **************** |
|
198 |
// ** Timer ISRs ** |
|
199 |
// **************** |
|
200 |
|
|
201 |
volatile uint8_t x; |
|
202 |
|
|
203 |
SIGNAL (SIG_OVERFLOW3) |
|
204 |
{ |
|
205 |
// Overflow: set debug pin |
|
206 |
PORTF=2; |
|
207 |
|
|
208 |
orb1_set (1,0,0); |
|
209 |
orb2_set (1,0,0); |
|
210 |
x=0; |
|
211 |
OCR3C=64<<1; |
|
212 |
} |
|
213 |
|
|
214 |
SIGNAL (SIG_OUTPUT_COMPARE3C) |
|
215 |
{ |
|
216 |
// First match: clear debug pin |
|
217 |
// Second match: spike on debug pin |
|
218 |
if (x==0) |
|
219 |
{ |
|
220 |
PORTF=0; |
|
221 |
|
|
222 |
x=1; |
|
223 |
orb1_set (0,0,0); |
|
224 |
OCR3C=192<<1; |
|
225 |
} |
|
226 |
else if (x==1) |
|
227 |
{ |
|
228 |
PORTF=2; PORTF=0; |
|
229 |
PORTF=2; PORTF=0; |
|
230 |
PORTF=2; PORTF=0; |
|
231 |
PORTF=2; PORTF=0; |
|
232 |
PORTF=2; PORTF=0; |
|
233 |
PORTF=2; PORTF=0; |
|
234 |
PORTF=2; PORTF=0; |
|
235 |
PORTF=2; PORTF=0; |
|
236 |
x=2; |
|
237 |
orb2_set (0,0,0); |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
|
|
148 | 242 |
// ************************ |
149 | 243 |
// ** Setting RGB colors ** |
150 | 244 |
// ************************ |
Also available in: Unified diff