forked from bhagman/SoftPWM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftPWM.cpp
More file actions
446 lines (367 loc) · 9.28 KB
/
SoftPWM.cpp
File metadata and controls
446 lines (367 loc) · 9.28 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
|| @author Brett Hagman <bhagman@wiring.org.co>
|| @contribution Paul Stoffregen (paul at pjrc dot com)
|| @url http://wiring.org.co/
|| @url http://roguerobotics.com/
||
|| @description
|| | A Software PWM Library
|| |
|| | Written by Brett Hagman
|| | http://www.roguerobotics.com/
|| | bhagman@roguerobotics.com, bhagman@wiring.org.co
|| |
|| | A Wiring (and Arduino) Library, for Atmel AVR8 bit series microcontrollers,
|| | to produce PWM signals on any arbitrary pin.
|| |
|| | It was originally designed for controlling the brightness of LEDs, but
|| | could be adapted to control servos and other low frequency PWM controlled
|| | devices as well.
|| |
|| | It uses a single hardware timer (Timer 2) on the Atmel microcontroller to
|| | generate up to 20 PWM channels (your mileage may vary).
|| |
|| #
||
|| @license Please see the accompanying LICENSE.txt file for this project.
||
|| @notes
|| | Minor modification by Paul Stoffregen to support different timers.
|| |
|| #
||
|| @name Software PWM Library
|| @type Library
|| @target Atmel AVR 8 Bit
||
|| @version 1.0.1
||
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "SoftPWM.h"
#include "SoftPWM_timer.h"
#if defined(WIRING)
#include <Wiring.h>
#elif ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#define HARDRESET(RST) if (RST) { SOFTPWM_TIMER_SET(0); _isr_softcount = 0xff; }
#if F_CPU
#define SOFTPWM_FREQ 60UL
#define SOFTPWM_OCR (F_CPU / (8UL * 256UL * SOFTPWM_FREQ))
#else
// 130 == 60 Hz (on 16 MHz part)
#define SOFTPWM_OCR 130
#endif
volatile uint8_t _isr_softcount = 0xff;
uint8_t _softpwm_defaultPolarity = SOFTPWM_NORMAL;
typedef struct soft_pwm_ch_
{
// hardware I/O port and pin for this channel
int8_t pin;
uint8_t polarity;
volatile uint8_t *outport;
uint8_t pinmask;
uint8_t pwmvalue;
uint8_t checkval;
uint8_t fadeuprate;
uint8_t fadedownrate;
struct soft_pwm_ch_ *next;
struct soft_pwm_ch_ *prev;
} softPWMChannel;
softPWMChannel _softpwm_channels[SOFTPWM_MAXCHANNELS];
struct soft_pwm_ch_ *g_used;
struct soft_pwm_ch_ *g_free;
// Here is the meat and gravy
#ifdef WIRING
void SoftPWM_Timer_Interrupt(void)
#else
ISR(SOFTPWM_TIMER_INTERRUPT)
#endif
{
// uint8_t i;
int16_t newvalue;
int16_t direction;
struct soft_pwm_ch_ *l_it = NULL;
if (++_isr_softcount == 0)
{
l_it = g_used;
while (l_it)
{
if (l_it->fadeuprate > 0 || l_it->fadedownrate > 0)
{
// we want to fade to the new value
direction = l_it->pwmvalue - l_it->checkval;
// we will default to jumping to the new value
newvalue = l_it->pwmvalue;
if (direction > 0 && l_it->fadeuprate > 0)
{
newvalue = l_it->checkval + l_it->fadeuprate;
if (newvalue > l_it->pwmvalue) {
newvalue = l_it->pwmvalue;
}
}
else if (direction < 0 && l_it->fadedownrate > 0)
{
newvalue = l_it->checkval - l_it->fadedownrate;
if (newvalue < l_it->pwmvalue) {
newvalue = l_it->pwmvalue;
}
}
l_it->checkval = newvalue;
}
else {
// just set the channel to the new value
l_it->checkval = l_it->pwmvalue;
}
// now set the pin low (checkval == 0x00) otherwise high
if (l_it->checkval == 0x00)
{
if (l_it->polarity == SOFTPWM_NORMAL)
*(l_it->outport) &= ~(l_it->pinmask);
else
*(l_it->outport) |= l_it->pinmask;
}
else
{
if (l_it->polarity == SOFTPWM_NORMAL)
*(l_it->outport) |= l_it->pinmask;
else
*(l_it->outport) &= ~(l_it->pinmask);
}
l_it = l_it->next;
}
return;
}
l_it = g_used;
while (l_it) {
// skip hit on checkval 0 and 255
if (l_it->checkval != 0x00 && l_it->checkval != 0xFF)
{
if (l_it->checkval == _isr_softcount) // if we have hit the width
{
// turn off the channel
if (l_it->polarity == SOFTPWM_NORMAL)
*(l_it->outport) &= ~(l_it->pinmask);
else
*(l_it->outport) |=l_it->pinmask;
}
}
l_it = l_it->next;
}
}
void ch_push(struct soft_pwm_ch_ *&p_head, struct soft_pwm_ch_ *p_item) {
p_item->next = p_head;
p_item->prev = NULL;
if (NULL == p_head) {
p_head = p_item;
return;
}
p_head->prev = p_item;
p_head = p_item;
};
void ch_remove(struct soft_pwm_ch_ *&p_head, struct soft_pwm_ch_ *p_item)
{
if (NULL == p_head)
{
return;
}
// remove head
if (p_head == p_item) {
p_head = p_item->next;
if (NULL != p_head) {
p_head->prev = NULL;
p_item->next = NULL;
}
return;
}
struct soft_pwm_ch_ *l_prev = p_item->prev;
l_prev->next = p_item->next;
// remove tail
if (NULL == p_item->next)
{
p_item->prev = NULL;
return;
}
// remove middle
p_item->next->prev = l_prev;
p_item->prev = NULL;
p_item->next = NULL;
return;
};
struct soft_pwm_ch_ *ch_pop(struct soft_pwm_ch_ *&p_head)
{
struct soft_pwm_ch_ *l_item = NULL;
if (NULL == p_head)
{
return NULL;
}
l_item = p_head;
p_head = l_item->next;
l_item->next = NULL;
return l_item;
};
struct soft_pwm_ch_ *ch_find(struct soft_pwm_ch_ *p_head, int8_t p_pin)
{
uint8_t i = 0;
struct soft_pwm_ch_ *l_it = p_head;
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{
if (NULL == l_it)
{
return NULL;
}
if (p_pin == l_it->pin)
{
return l_it;
}
l_it = l_it->next;
}
return NULL;
}
void ch_reset(struct soft_pwm_ch_ *p_ch)
{
p_ch->pin = -1;
p_ch->polarity = SOFTPWM_NORMAL;
p_ch->outport = 0;
p_ch->fadeuprate = 0;
p_ch->fadedownrate = 0;
// l_ch->next = NULL;
// l_ch->prev = NULL;
}
void SoftPWMBegin(uint8_t defaultPolarity)
{
// We can tweak the number of PWM period by changing the prescalar
// and the OCR - we'll default to ck/8 (CS21 set) and OCR=128.
// This gives 1024 cycles between interrupts. And the ISR consumes ~200 cycles, so
// we are looking at about 20 - 30% of CPU time spent in the ISR.
// At these settings on a 16 MHz part, we will get a PWM period of
// approximately 60Hz (~16ms).
_softpwm_defaultPolarity = defaultPolarity;
uint8_t i;
g_free = NULL;
g_used = NULL;
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{
struct soft_pwm_ch_ * l_ch = &_softpwm_channels[i];
ch_reset(l_ch);
ch_push(g_free, l_ch);
}
#ifdef WIRING
Timer2.setMode(0b010); // CTC
Timer2.setClockSource(CLOCK_PRESCALE_8);
Timer2.setOCR(CHANNEL_A, SOFTPWM_OCR);
Timer2.attachInterrupt(INTERRUPT_COMPARE_MATCH_A, SoftPWM_Timer_Interrupt);
#else
SOFTPWM_TIMER_INIT(SOFTPWM_OCR);
#endif
}
void SoftPWMSetPolarity(int8_t pin, uint8_t polarity)
{
if (polarity != SOFTPWM_NORMAL) {
polarity = SOFTPWM_INVERTED;
}
struct soft_pwm_ch_ * l_it = NULL;
// set individual pins
if (pin > 0) {
l_it = ch_find(g_used, pin);
if (NULL != l_it) {
l_it->polarity = polarity;
}
return;
}
// set all used pins
l_it = g_used;
while (l_it) {
l_it->polarity = polarity;
l_it = l_it->next;
}
return;
}
void SoftPWMSetPercent(int8_t pin, uint8_t percent, uint8_t hardset)
{
SoftPWMSet(pin, ((uint16_t)percent * 255) / 100, hardset);
}
void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset)
{
struct soft_pwm_ch_* l_it = NULL;
// set individual pins
if (pin > 0) {
l_it = ch_find(g_used, pin);
// not found
if (NULL == l_it) {
l_it = ch_pop(g_free);
if (NULL == l_it) {
// run out of pins
return;
}
l_it->pin = pin;
l_it->polarity = _softpwm_defaultPolarity;
l_it->outport = portOutputRegister(digitalPinToPort(pin));
l_it->pinmask = digitalPinToBitMask(pin);
l_it->pwmvalue = value;
l_it->next = NULL;
l_it->prev = NULL;
if (l_it->polarity == SOFTPWM_NORMAL) {
digitalWrite(pin, LOW);
} else {
digitalWrite(pin, HIGH);
}
pinMode(pin, OUTPUT);
ch_push(g_used, l_it);
HARDRESET(hardset)
return;
}
// found, just set the value
l_it->pwmvalue = value;
HARDRESET(hardset)
return;
}
// set all used pins
l_it = g_used;
while (l_it) {
l_it->pwmvalue = value;
l_it = l_it->next;
}
HARDRESET(hardset)
return;
}
void SoftPWMEnd(int8_t pin)
{
struct soft_pwm_ch_ *l_ch = ch_find(g_used, pin);
if (NULL == l_ch)
{
// pin not found
return;
}
ch_remove(g_used, l_ch);
digitalWrite(l_ch->pin, 1);
pinMode(l_ch->pin, INPUT);
ch_reset(l_ch);
ch_push(g_free, l_ch);
}
void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime)
{
int16_t fadeAmount;
uint8_t i;
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
{
fadeAmount = 0;
if (fadeUpTime > 0)
fadeAmount = 255UL * (SOFTPWM_OCR * 256UL / (F_CPU / 8000UL)) / fadeUpTime;
_softpwm_channels[i].fadeuprate = fadeAmount;
fadeAmount = 0;
if (fadeDownTime > 0)
fadeAmount = 255UL * (SOFTPWM_OCR * 256UL / (F_CPU / 8000UL)) / fadeDownTime;
_softpwm_channels[i].fadedownrate = fadeAmount;
if (pin >= 0) // we've set individual pin
break;
}
}
}