-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTimers.cpp
708 lines (664 loc) · 24.5 KB
/
SimpleTimers.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/****************************************************************************************/
/* */
/* SimpleTimers.cpp */
/* */
/* This library provides a simple interface to control the timers on */
/* the PIC32, as well as timer interrupts and PWM */
/* */
/***************************************************************************************/
/* Authors: Thomas Kappenman */
/* Copyright 2014, Digilent Inc. */
/***************************************************************************************/
/* Module Description: */
/* */
/* This library provides functions that set up the timers on the PIC32 */
/* microcontroller. It also provides functions to link timer interrupts to */
/* other functions, and provides an easy way to set up a basic PWM */
/* signal using output compare modules. */
/* */
/**************************************************************************************/
/* Revision History: */
/* */
/* 8/7/2014(TommyK): Created */
/* */
/***************************************************************************************/
/* */
/* This module was adapted from the PIC32 core library WInterrupts.c */
/* by Mark Sproul and Gene Apperson. */
/* */
/***************************************************************************************/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SIMPLETIMERS_cpp
#define SIMPLETIMERS_cpp
#include "SimpleTimers.h"
//Global ISR function array
volatile static voidFuncPtr intFunc[5];
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* startTimer()
**
** Parameters:
** timerNum: The timer to initialize <TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
** microseconds: The period, in microseconds, to set the timer to.
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Starts the specified timer with a period specified by the user in microseconds.
**
** Example:
** startTimer(TIMER23, 10000000); Starts a 32 bit timer (TIMER2 & TIMER3) with a period of 10 seconds
*/
void startTimer(uint8_t timerNum, long microseconds){
unsigned long cycles = (F_CPU / 1000000) * microseconds; //Number of cycles = Cycles per second * Seconds
if (timerNum < 7){
switch (timerNum){
case(TIMER1):
if(cycles < MAX16BIT) T1CON=(~T_ON & PS_1_1), PR1=cycles-1, T1CONSET=T_ON; // No prescale
else if((cycles >>= 3) < MAX16BIT) T1CON=(~T_ON & PS_1_8), PR1=cycles-1, T1CONSET=T_ON; // Prescale by /8
else if((cycles >>= 3) < MAX16BIT) T1CON=(~T_ON & PS_1_64), PR1=cycles-1, T1CONSET=T_ON; // Prescale by /64
else if((cycles >>= 2) < MAX16BIT) T1CON=(~T_ON & PS_1_256), PR1=cycles-1, T1CONSET=T_ON; // Prescale by /256
else T1CON=(~T_ON & PS_1_256), PR1=MAX16BIT-1, T1CONSET=T_ON; // Max period
break;
case(TIMER2):
if(cycles < MAX16BIT) T2CON=(~T_ON & PS_1_1), PR2=cycles-1, T2CONSET=T_ON; // No prescale
else if((cycles >>= 3) < MAX16BIT) T2CON=(~T_ON & PS_1_8), PR2=cycles-1, T2CONSET=T_ON; // Prescale by /8
else if((cycles >>= 3) < MAX16BIT) T2CON=(~T_ON & PS_1_64), PR2=cycles-1, T2CONSET=T_ON; // Prescale by /64
else if((cycles >>= 2) < MAX16BIT) T2CON=(~T_ON & PS_1_256), PR2=cycles-1, T2CONSET=T_ON; // Prescale by /256
else T2CON=(~T_ON & PS_1_256), PR2=MAX16BIT-1, T2CONSET=T_ON; // Max period
break;
case(TIMER3):
if(cycles < MAX16BIT) T3CON=(~T_ON & PS_1_1), PR3=cycles-1, T3CONSET=T_ON; // No prescale
else if((cycles >>= 3) < MAX16BIT) T3CON=(~T_ON & PS_1_8), PR3=cycles-1, T3CONSET=T_ON; // Prescale by /8
else if((cycles >>= 3) < MAX16BIT) T3CON=(~T_ON & PS_1_64), PR3=cycles-1, T3CONSET=T_ON; // Prescale by /64
else if((cycles >>= 2) < MAX16BIT) T3CON=(~T_ON & PS_1_256), PR3=cycles-1, T3CONSET=T_ON; // Prescale by /256
else T3CON=(~T_ON & PS_1_256), PR3=MAX16BIT-1, T3CONSET=T_ON; // Max period
break;
case(TIMER4):
if(cycles < MAX16BIT) T4CON=(~T_ON & PS_1_1), PR4=cycles-1, T4CONSET=T_ON; // No prescale
else if((cycles >>= 3) < MAX16BIT)T4CON=(~T_ON & PS_1_8), PR4=cycles-1, T4CONSET=T_ON; // Prescale by /8
else if((cycles >>= 3) < MAX16BIT)T4CON=(~T_ON & PS_1_64), PR4=cycles-1, T4CONSET=T_ON; // Prescale by /64
else if((cycles >>= 2) < MAX16BIT)T4CON=(~T_ON & PS_1_256), PR4=cycles-1, T4CONSET=T_ON; // Prescale by /256
else T4CON=(~T_ON & PS_1_256), PR4=MAX16BIT-1, T4CONSET=T_ON; // Max period
break;
case(TIMER5):
if(cycles < MAX16BIT) T5CON=(~T_ON & PS_1_1), PR5=cycles-1, T5CONSET=T_ON; // No prescale
else if((cycles >>= 3) < MAX16BIT)T5CON=(~T_ON & PS_1_8), PR5=cycles-1, T5CONSET=T_ON; // Prescale by /8
else if((cycles >>= 3) < MAX16BIT)T5CON=(~T_ON & PS_1_64), PR5=cycles-1, T5CONSET=T_ON; // Prescale by /64
else if((cycles >>= 2) < MAX16BIT)T5CON=(~T_ON & PS_1_256), PR5=cycles-1, T5CONSET=T_ON; // Prescale by /256
else T5CON=(~T_ON & PS_1_256), PR5=MAX16BIT-1, T5CONSET=T_ON; // Max period
break;
case(TIMER23):
if(cycles < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_1), PR2=cycles-1, T2CONSET=T_ON; //No prescale
else if((cycles >>= 3) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_8), PR2=cycles-1, T2CONSET=T_ON; //Prescale by /8
else if((cycles >>= 3) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_64), PR2=cycles-1, T2CONSET=T_ON; //Prescale by /64
else if((cycles >>= 2) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_256), PR2=cycles-1, T2CONSET=T_ON; //Prescale by /256
else T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_256), PR2=MAX16BIT-1, T2CONSET=T_ON; //Max period possible
break;
case(TIMER45):
if(cycles < MAX32BIT) T4CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_1), PR4=cycles-1, T4CONSET=T_ON; //No prescale
else if((cycles >>= 3) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_8), PR4=cycles-1, T4CONSET=T_ON; //Prescale by /8
else if((cycles >>= 3) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_64), PR4=cycles-1, T4CONSET=T_ON; //Prescale by /64
else if((cycles >>= 2) < MAX32BIT) T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_256), PR4=cycles-1, T4CONSET=T_ON; //Prescale by /256
else T2CON=(~T_ON & T_32_BIT_MODE_ON | PS_1_256), PR4=MAX16BIT-1, T4CONSET=T_ON; //Max period possible
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* stopTimer()
**
** Parameters:
** timerNum: The timer to stop <TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Stops the specified timer
**
** Example:
** stopTimer(TIMER23) Stops the 32 bit timer (TIMER2 & TIMER3)
*/
void stopTimer(uint8_t timerNum){
if (timerNum < 7){
switch(timerNum){
case TIMER1:IEC0CLR = _IEC0_T1IE_MASK, T1CON = 0x0;
break;
case TIMER2:IEC0CLR = _IEC0_T2IE_MASK, T2CON = 0x0;
break;
case TIMER3:IEC0CLR = _IEC0_T3IE_MASK, T3CON = 0x0;
break;
case TIMER4:IEC0CLR = _IEC0_T4IE_MASK, T4CON = 0x0;
break;
case TIMER5:IEC0CLR = _IEC0_T5IE_MASK, T5CON = 0x0;
break;
case TIMER23:IEC0CLR = _IEC0_T3IE_MASK, T2CON = 0x0,T3CON = 0x0;
break;
case TIMER45:IEC0CLR = _IEC0_T5IE_MASK, T4CON = 0x0, T5CON = 0x0;
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* setTimerPeriod()
**
** Parameters:
** timerNum: The timer to set the period of <TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** At the moment, just calls startTimer(), this also enables the timer. This function is
** mainly for the name.
**
** Description:
** Starts the specified timer with the specified period in microseconds
**
** Example:
** startTimer(TIMER4, 100000); Starts a 16 bit timer (TIMER4) with a period of 100 milliseconds.
*/
void setTimerPeriod(uint8_t timerNum, long microseconds){
startTimer(timerNum, microseconds);
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* timerReset()
**
** Parameters:
** timerNum: The timer to reset <TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Resets the value of the timer counter to 0.
**
** Example:
** timerReset(TIMER23); Resets the 32 bit timer 2-3 count to 0.
*/
void timerReset(uint8_t timerNum){
switch(timerNum){
case TIMER1: TMR1=0;
break;
case TIMER2: TMR2=0;
break;
case TIMER3: TMR3=0;
break;
case TIMER4: TMR4=0;
break;
case TIMER5: TMR5=0;
break;
case TIMER23: TMR2=0;TMR3=0;
break;
case TIMER45: TMR4=0;TMR5=0;
break;
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* attachTimerInterrupt()
**
** Parameters:
** timerNum: The timer interrupt to set <TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
** userfunc: The interrupt service routine to jump to when the interrupt is triggered
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Specifies a function as an interrupt service routine for a timer.
**
** Example:
** attachTimerInterrupt(TIMER23, toggleLED); Attaches the 32 bit timer 2-3 to the function toggleLED();
*/
void attachTimerInterrupt(uint8_t timerNum, void (*userFunc)(void))
{
if (timerNum < 7)
{
if (timerNum==TIMER23) timerNum=TIMER3;
else if (timerNum==TIMER45) timerNum=TIMER5;
intFunc[timerNum] = userFunc;
switch (timerNum)
{
case TIMER1:
IEC0bits.T1IE = 0;
IFS0bits.T1IF = 0;
setIntVector(_TIMER_1_VECTOR, (isrFunc) Timer1IntHandler);
IFS0CLR = _IFS0_T1IF_MASK;
IPC1CLR = _IPC1_T1IP_MASK, IPC1SET = ((_T1_IPL_IPC) << _IPC1_T1IP_POSITION);
IPC1CLR = _IPC1_T1IS_MASK, IPC1SET = ((_T1_SPL_IPC) << _IPC1_T1IS_POSITION);
IEC0SET = (1 << _IEC0_T1IE_POSITION);
break;
case TIMER2:
IEC0bits.T2IE = 0;
IFS0bits.T2IF = 0;
setIntVector(_TIMER_2_VECTOR, (isrFunc) Timer2IntHandler);
IFS0CLR = _IFS0_T2IF_MASK;
IPC2CLR = _IPC2_T2IP_MASK, IPC2SET = ((_T2_IPL_IPC) << _IPC2_T2IP_POSITION);
IPC2CLR = _IPC2_T2IS_MASK, IPC2SET = ((_T2_SPL_IPC) << _IPC2_T2IS_POSITION);
IEC0SET = (1 << _IEC0_T2IE_POSITION);
break;
case TIMER3:
IEC0bits.T3IE = 0;
IFS0bits.T3IF = 0;
setIntVector(_TIMER_3_VECTOR, (isrFunc) Timer3IntHandler);
IFS0CLR = _IFS0_T3IF_MASK;
IPC3CLR = _IPC3_T3IP_MASK, IPC3SET = ((_T3_IPL_IPC) << _IPC3_T3IP_POSITION);
IPC3CLR = _IPC3_T3IS_MASK, IPC3SET = ((_T3_SPL_IPC) << _IPC3_T3IS_POSITION);
IEC0SET = (1 << _IEC0_T3IE_POSITION);
break;
case TIMER4:
IEC0bits.T4IE = 0;
IFS0bits.T4IF = 0;
setIntVector(_TIMER_4_VECTOR, (isrFunc) Timer4IntHandler);
IFS0CLR = _IFS0_T4IF_MASK;
IPC4CLR = _IPC4_T4IP_MASK, IPC4SET = ((_T4_IPL_IPC) << _IPC4_T4IP_POSITION);
IPC4CLR = _IPC4_T4IS_MASK, IPC4SET = ((_T4_SPL_IPC) << _IPC4_T4IS_POSITION);
IEC0SET = (1 << _IEC0_T1IE_POSITION);
break;
case TIMER5:
IEC0bits.T5IE = 0;
IFS0bits.T5IF = 0;
setIntVector(_TIMER_5_VECTOR, (isrFunc) Timer5IntHandler);
IFS0CLR = _IFS0_T5IF_MASK;
#if defined(__PIC32MX__)
IPC5CLR = _IPC5_T5IP_MASK, IPC5SET = ((_T5_IPL_IPC) << _IPC5_T5IP_POSITION);
IPC5CLR = _IPC5_T5IS_MASK, IPC5SET = ((_T5_SPL_IPC) << _IPC5_T5IS_POSITION);
#else if defined(__PIC32MZ__)
IPC6CLR = _IPC6_T5IP_MASK, IPC5SET = ((_T5_IPL_IPC) << _IPC6_T5IP_POSITION);
IPC6CLR = _IPC6_T5IS_MASK, IPC5SET = ((_T5_SPL_IPC) << _IPC6_T5IS_POSITION);
#endif
IEC0SET = (1 << _IEC0_T1IE_POSITION);
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* detachTimerInterrupt()
**
** Parameters:
** timerNum: The timer interrupt to detach<TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Detaches the interrupt from the previously specified ISR and disables the timer interrupt
**
** Example:
** detachTimerInterrupt(TIMER23); Detaches timer 2-3 from its ISR,
*/
void detachTimerInterrupt(uint8_t timerNum)
{
if (timerNum < 7)
{
if (timerNum==TIMER23) timerNum=TIMER3;
else if (timerNum==TIMER45) timerNum=TIMER5;
switch (timerNum){
case TIMER1:
IEC0bits.T1IE = 0;
clearIntVector(_TIMER_1_VECTOR);
break;
case TIMER2:
IEC0bits.T2IE = 0;
clearIntVector(_TIMER_2_VECTOR);
break;
case (TIMER3):
IEC0bits.T3IE = 0;
clearIntVector(_TIMER_3_VECTOR);
break;
case TIMER4:
IEC0bits.T4IE = 0;
clearIntVector(_TIMER_4_VECTOR);
break;
case (TIMER5):
IEC0bits.T5IE = 0;
clearIntVector(_TIMER_5_VECTOR);
break;
}
intFunc[timerNum] = 0;
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* disableTimerInterrupt()
**
** Parameters:
** timerNum: The timer interrupt to disable<TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Turns off the timer interrupt without detaching the ISR
**
** Example:
** disableTimerInterrupt(TIMER4); Disables timer 4
*/
void disableTimerInterrupt(uint8_t timerNum)
{
if (timerNum < 7)
{
if (timerNum==TIMER23) timerNum=TIMER3;
else if (timerNum==TIMER45) timerNum=TIMER5;
switch (timerNum){
case TIMER1:
IEC0bits.T1IE = 0;
break;
case TIMER2:
IEC0bits.T2IE = 0;
break;
case (TIMER3):
IEC0bits.T3IE = 0;
break;
case TIMER4:
IEC0bits.T4IE = 0;
break;
case (TIMER5):
IEC0bits.T5IE = 0;
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* enableTimerInterrupt()
**
** Parameters:
** timerNum: The timer interrupt to enable<TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER23, TIMER45>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Turns on the timer interrupt
**
** Example:
** enableTimerInterrupt(TIMER4); Enables timer 4
*/
void enableTimerInterrupt(uint8_t timerNum)
{
if (timerNum < 7)
{
if (timerNum==TIMER23) timerNum=TIMER3;
else if (timerNum==TIMER45) timerNum=TIMER5;
switch (timerNum){
case TIMER1:
IEC0bits.T1IE = 1;
break;
case TIMER2:
IEC0bits.T2IE = 1;
break;
case (TIMER3):
IEC0bits.T3IE = 1;
break;
case TIMER4:
IEC0bits.T4IE = 1;
break;
case (TIMER5):
IEC0bits.T5IE = 1;
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* startPWM
**
** Parameters:
** timerNum: The timer to use, with the period of the PWM <TIMER2, TIMER3, TIMER23>
** OCnum: The output compare module to use <OC1, OC2, OC3, OC4, OC5>
** dutycycle: The duty cycle (out of 100%) of the PWM
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Enables one of the output compare modules as a PWM with the specified duty cycle,
** and the period of the specified timer.
**
** Example:
** startPWM(TIMER2, OC1, 80); Initializes OC1 as a PWM output with 80% duty cycle, and TIMER2's period
*/
void startPWM(uint8_t timerNum, uint8_t OCnum, uint8_t dutycycle){
unsigned long outputCompareValue;
if (dutycycle<=100){
switch(timerNum){
case TIMER2:
outputCompareValue = (PR2 * dutycycle) / 100;
switch(OCnum){
case OC1:
OC1RS = (outputCompareValue), OC1R = (outputCompareValue), OC1CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC2:
OC2RS = (outputCompareValue), OC2R = (outputCompareValue), OC2CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC3:
OC3RS = (outputCompareValue), OC3R = (outputCompareValue), OC3CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC4:
OC4RS = (outputCompareValue), OC4R = (outputCompareValue), OC4CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC5:
OC5RS = (outputCompareValue), OC5R = (outputCompareValue), OC5CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
}
break;
case TIMER3:
outputCompareValue = (PR3 * dutycycle) / 100;
switch(OCnum){
case OC1:
OC1RS = (outputCompareValue), OC1R = (outputCompareValue), OC1CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC2:
OC2RS = (outputCompareValue), OC2R = (outputCompareValue), OC2CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC3:
OC3RS = (outputCompareValue), OC3R = (outputCompareValue), OC3CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC4:
OC4RS = (outputCompareValue), OC4R = (outputCompareValue), OC4CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC5:
OC5RS = (outputCompareValue), OC5R = (outputCompareValue), OC5CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE);
break;
}
break;
case TIMER23:
outputCompareValue = (PR2 * dutycycle) / 100;
switch(OCnum){
case OC1:
OC1RS = (outputCompareValue), OC1R = (outputCompareValue), OC1CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE32 | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC2:
OC2RS = (outputCompareValue), OC2R = (outputCompareValue), OC2CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE32 | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC3:
OC3RS = (outputCompareValue), OC3R = (outputCompareValue), OC3CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE32 | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC4:
OC4RS = (outputCompareValue), OC4R = (outputCompareValue), OC4CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE32 | OC_PWM_FAULT_PIN_DISABLE);
break;
case OC5:
OC5RS = (outputCompareValue), OC5R = (outputCompareValue), OC5CON = (OC_ON | OC_IDLE_CON | OC_TIMER_MODE32 | OC_PWM_FAULT_PIN_DISABLE);
break;
}
break;
}
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* stopPWM()
**
** Parameters:
** OCnum: The output compare module to turn off <OC1, OC2, OC3, OC4, OC5>
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Turns off the specified output compare module.
**
** Example:
** stopPWM(OC1); Turns off the PWM signal being output by OC1
*/
void stopPWM(uint8_t OCnum){
switch(OCnum){
case OC1: IEC0CLR = _IEC0_OC1IE_MASK;
OC1CONCLR = _OC1CON_ON_MASK;
break;
case OC2: IEC0CLR = _IEC0_OC2IE_MASK;
OC2CONCLR = _OC2CON_ON_MASK;
break;
case OC3: IEC0CLR = _IEC0_OC3IE_MASK;
OC1CONCLR = _OC1CON_ON_MASK;
break;
case OC4: IEC0CLR = _IEC0_OC4IE_MASK;
OC1CONCLR = _OC1CON_ON_MASK;
break;
case OC5: IEC0CLR = _IEC0_OC5IE_MASK;
OC1CONCLR = _OC1CON_ON_MASK;
break;
}
}
/* --------------------------------------------------------------------------------------------------------------------------------------- */
/* setDutyCycle()
**
** Parameters:
** OCnum: The output compare module to turn off <OC1, OC2, OC3, OC4, OC5>
** dutycycle: The duty cycle percent (0-100) to set the PWM to
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** Sets the duty cycle of the specified output compare module to a certain percent of the current timer's period.
**
** Example:
** setDutyCycle(OC3, 90); Sets the PWM signal on OC3 to 90% duty cycle
*/
void setDutyCycle(uint8_t OCnum, float dutycycle){
unsigned long outputCompareValue;
bool timerSrc;
if (dutycycle<=100){
switch(OCnum){
case OC1:
timerSrc = (OC1CON>> _OC1CON_OCTSEL_POSITION) & 1; //Src = Timer2 if 0, Timer3 if 1
outputCompareValue = (PR2 * !timerSrc + PR3 * timerSrc) * dutycycle / 100;
OC1RS=outputCompareValue;
break;
case OC2:
timerSrc = (OC2CON>> _OC2CON_OCTSEL_POSITION) & 1; //Src = Timer2 if 0, Timer3 if 1
outputCompareValue = (PR2 * !timerSrc + PR3 * timerSrc) * dutycycle / 100;
OC2RS=outputCompareValue;
break;
case OC3:
timerSrc = (OC3CON>> _OC3CON_OCTSEL_POSITION) & 1; //Src = Timer2 if 0, Timer3 if 1
outputCompareValue = (PR2 * !timerSrc + PR3 * timerSrc) * dutycycle / 100;
OC3RS=outputCompareValue;
break;
case OC4:
timerSrc = (OC4CON>> _OC4CON_OCTSEL_POSITION) & 1; //Src = Timer2 if 0, Timer3 if 1
outputCompareValue = (PR2 * !timerSrc + PR3 * timerSrc) * dutycycle / 100;
OC4RS=outputCompareValue;
break;
case OC5:
timerSrc = (OC5CON>> _OC5CON_OCTSEL_POSITION) & 1; //Src = Timer2 if 0, Timer3 if 1
outputCompareValue = (PR2 * !timerSrc + PR3 * timerSrc) * dutycycle / 100;
OC5RS=outputCompareValue;
break;
}
}
}
//Interrupt Service Routines
//************************************************************************
// Timer1 ISR
void __attribute__((interrupt(),nomips16)) Timer1IntHandler(void)
{
if (intFunc[TIMER1] != 0)
{
(*intFunc[TIMER1])();
}
IFS0bits.T1IF = 0;
}
//************************************************************************
// Timer2 ISR
void __attribute__((interrupt(),nomips16)) Timer2IntHandler(void)
{
if (intFunc[TIMER2] != 0)
{
(*intFunc[TIMER2])();
}
IFS0bits.T2IF = 0;
}
//************************************************************************
// Timer3 ISR
void __attribute__((interrupt(),nomips16)) Timer3IntHandler(void)
{
if (intFunc[TIMER3] != 0)
{
(*intFunc[TIMER3])();
}
IFS0bits.T3IF = 0;
}
//************************************************************************
// Timer4 ISR
void __attribute__((interrupt(),nomips16))Timer4IntHandler(void)
{
if (intFunc[TIMER4] != 0)
{
(*intFunc[TIMER4])();
}
IFS0bits.T4IF = 0;
}
//************************************************************************
// Timer5 ISR
void __attribute__((interrupt(),nomips16)) Timer5IntHandler(void)
{
if (intFunc[TIMER5] != 0)
{
(*intFunc[TIMER5])();
}
IFS0bits.T5IF = 0;
}
//************************************************************************
#endif