Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_pwm.h
Go to the documentation of this file.
1 
88 #ifndef _NZDEFAULT_PWM_H_
89 #define _NZDEFAULT_PWM_H_
90 
91 #if defined(HAS_NZ_PWM)
92 
93 #if (defined(__PIC24F__)) && defined(__C30__) // Microchip C30 compiler
94  #include "PPS.h"
95 #elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler
96 #elif defined(__PIC32MX__) // Microchip C32 compiler
97 #else
98  #error Unknown processor or compiler. See Compiler.h
99 #endif
100 
101 
102 //The PWM clock is configured by this module to be 16MHz, giving a 62.5ns period
103 #define PWM_CLOCK_FREQ (GetInstructionClock())
104 #define PWM_CLOCK_PERIOD (1/GetInstructionClock())
105 
106 typedef struct {
107  WORD OCxCON1;
108  WORD OCxCON2;
109  WORD OCxRS; //Period, pwm will count from 0 to OCxRS
110  WORD OCxR; //Duty Cycles
111  WORD OCxTMR;
112 } REGS_OC_;
113 
114 //For other chips, check OC1CON1 is at address 0x0190, and all OC registers follow each other from 0x0190 to 0x01E8
115 #if defined(__PIC24FJ128GB106__) || defined(__PIC24FJ256GB106__) || defined(__PIC24FJ256GB206__)
116  extern volatile REGS_OC_ __attribute__ ((sfr(0x0190))) regs_oc_[9];
117 #elif defined(__PIC32MX__) // Microchip C32 compiler
118  // TODO for PIC32MX - this code WILL NOT WORK!!!! Just here so it compiles & for testing!
119  extern volatile REGS_OC_ regs_oc_[9];
120 #else
121  #error "nz_pwm no processor defined!"
122 #endif
123 
128 #define PWM_FREQ_320HZ 49999
129 
134 #define PWM_FREQ_1600HZ 9999
135 
140 #define PWM_FREQ_3200HZ 4999
141 
146 #define PWM_FREQ_8KHZ 1999
147 
152 #define PWM_FREQ_16KHZ 999
153 
158 #define PWM_FREQ_80KHZ 199
159 
164 #define PWM_FREQ_160KHZ 99
165 
166 #ifndef PWM1_FREQ
167 #define PWM1_FREQ PWM_FREQ_16KHZ
168 #endif
169 
170 #ifndef PWM2_FREQ
171 #define PWM2_FREQ PWM_FREQ_16KHZ
172 #endif
173 
174 #ifndef PWM3_FREQ
175 #define PWM3_FREQ PWM_FREQ_16KHZ
176 #endif
177 
178 #ifndef PWM4_FREQ
179 #define PWM4_FREQ PWM_FREQ_16KHZ
180 #endif
181 
182 #ifndef PWM5_FREQ
183 #define PWM5_FREQ PWM_FREQ_16KHZ
184 #endif
185 
186 #ifndef PWM6_FREQ
187 #define PWM6_FREQ PWM_FREQ_16KHZ
188 #endif
189 
190 #ifndef PWM7_FREQ
191 #define PWM7_FREQ PWM_FREQ_16KHZ
192 #endif
193 
194 #ifndef PWM8_FREQ
195 #define PWM8_FREQ PWM_FREQ_16KHZ
196 #endif
197 
198 #ifndef PWM9_FREQ
199 #define PWM9_FREQ PWM_FREQ_16KHZ
200 #endif
201 
202 
232 #define PWM_CALCULATE_PERIOD_FOR_FREQUENCY(f) ((PWM_CLOCK_FREQ/f)-1)
233 
234 
253 #define pwm1OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC1; pwmOpenDefault(1, PWM1_FREQ, inverted);}
254 
273 #define pwm2OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC2; pwmOpenDefault(2, PWM2_FREQ, inverted);}
274 
293 #define pwm3OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC3; pwmOpenDefault(3, PWM3_FREQ, inverted);}
294 
313 #define pwm4OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC4; pwmOpenDefault(4, PWM4_FREQ, inverted);}
314 
333 #define pwm5OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC5; pwmOpenDefault(5, PWM5_FREQ, inverted);}
334 
353 #define pwm6OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC6; pwmOpenDefault(6, PWM6_FREQ, inverted);}
354 
373 #define pwm7OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC7; pwmOpenDefault(6, PWM7_FREQ, inverted);}
374 
393 #define pwm8OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC8; pwmOpenDefault(8, PWM8_FREQ, inverted);}
394 
413 #define pwm9OpenDefault(ppsOut, inverted) {ppsOut = OUT_FN_PPS_OC9; pwmOpenDefault(9, PWM9_FREQ, inverted);}
414 
441 void pwmOpenDefault(BYTE chan, WORD period, BYTE inverted);
442 
443 
466 #define pwm1SetPeriod(period) pwmSetPeriod(1, period)
467 #define pwm2SetPeriod(period) pwmSetPeriod(2, period)
468 #define pwm3SetPeriod(period) pwmSetPeriod(3, period)
469 #define pwm4SetPeriod(period) pwmSetPeriod(4, period)
470 #define pwm5SetPeriod(period) pwmSetPeriod(5, period)
471 #define pwm6SetPeriod(period) pwmSetPeriod(6, period)
472 #define pwm7SetPeriod(period) pwmSetPeriod(7, period)
473 #define pwm8SetPeriod(period) pwmSetPeriod(8, period)
474 #define pwm9SetPeriod(period) pwmSetPeriod(9, period)
475 
476 
501 void pwmSetPeriod(BYTE chan, WORD period);
502 
503 
513 WORD pwmGetPeriod(BYTE chan);
514 
515 
520 #define pwm1GetPeriod() (OC1RS)
521 #define pwm2GetPeriod() (OC2RS)
522 #define pwm3GetPeriod() (OC3RS)
523 #define pwm4GetPeriod() (OC4RS)
524 #define pwm5GetPeriod() (OC5RS)
525 #define pwm6GetPeriod() (OC6RS)
526 #define pwm7GetPeriod() (OC7RS)
527 #define pwm8GetPeriod() (OC8RS)
528 #define pwm9GetPeriod() (OC9RS)
529 
530 
560 void pwmSetFreqAndDutyCycle(BYTE chan, float freq, float dc);
561 
562 
571 float pwmGetFrequency(BYTE chan);
572 
573 
583 #define pwm1SetDutyCycle(dc) pwmSetDutyCycle(1, dc)
584 #define pwm2SetDutyCycle(dc) pwmSetDutyCycle(2, dc)
585 #define pwm3SetDutyCycle(dc) pwmSetDutyCycle(3, dc)
586 #define pwm4SetDutyCycle(dc) pwmSetDutyCycle(4, dc)
587 #define pwm5SetDutyCycle(dc) pwmSetDutyCycle(5, dc)
588 #define pwm6SetDutyCycle(dc) pwmSetDutyCycle(6, dc)
589 #define pwm7SetDutyCycle(dc) pwmSetDutyCycle(7, dc)
590 #define pwm8SetDutyCycle(dc) pwmSetDutyCycle(8, dc)
591 #define pwm9SetDutyCycle(dc) pwmSetDutyCycle(9, dc)
592 
603 void pwmSetDutyCycle(BYTE chan, WORD dc);
604 //#define pwmSetDutyCycle(chan,dc) {if(chan==1) OC1R = dc; else if(chan==2) OC2R = dc; else if .....} //Alternative method
605 
606 
630 #define pwm1SetPercent(period, percent) (OC1R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
631 #define pwm2SetPercent(period, percent) (OC2R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
632 #define pwm3SetPercent(period, percent) (OC3R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
633 #define pwm4SetPercent(period, percent) (OC4R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
634 #define pwm5SetPercent(period, percent) (OC5R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
635 #define pwm6SetPercent(period, percent) (OC6R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
636 #define pwm7SetPercent(period, percent) (OC7R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
637 #define pwm8SetPercent(period, percent) (OC8R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
638 #define pwm9SetPercent(period, percent) (OC9R = ((((period+1)/100) * percent)==0) ? 0 : ((((period+1)/100) * percent)-1))
639 
640 
649 //#define pwmSetPercent(chan, percent) {if((chan>0)&&(chan<10)) {regs_oc_[chan-1].OCxR = ((((PWM2_FREQ+1)/100) * percent)==0) ? 0 : ((((PWM2_FREQ+1)/100) * percent)-1);}}
650 //void pwmSetPercent(BYTE ch, WORD p);
651 
652 #endif //#if defined(HAS_NZ_PWM)
653 #endif