Sunday, 31 May 2015

YetAnotherPWM.c with the Pic32MX250F128B


During one of my tests, i was scratching my head for ages about why the PWM signal wasnt coming out on one of the remappable pins PPS. Went through examples, family datasheet, etc...
 Only to come to the conclusion that it would never work without disabling the JTAG ports in the configuration bits.

anotherpwm.c
YetAnotherpwm.X\anotherpwm.c
 1 /* 
 2  * File:   YetAnotherpwm.c
 3  * Author: Ras B.
 4  *
 5  * Created on 26 May 2015, 19:20
 6  */
 7 
 8 #include <plib.h>
 9 #include <p32xxxx.h>
10 
11 #pragma config   JTAGEN    = OFF // // disable the JTAG port  DDPCONbits.JTAGEN = 0;
12 #pragma config   FNOSC     = FRCPLL
13 #pragma config   FPLLIDIV  = DIV_2
14 #pragma config   FPLLMUL   = MUL_20
15 #pragma config   FPLLODIV  = DIV_2
16 #pragma config   FPBDIV    = DIV_1
17 #pragma config   POSCMOD   = OFF
18 #pragma config   FWDTEN    = OFF
19 //
20 #define GetSystemClock()        (40000000ul)
21 #define GetPeripheralClock()    (GetSystemClock()/(1<<OSCCONbits.PBDIV))// #define GetPeripheralClock()        (GetSystemClock()/(1<<OSCCONbits.PBDIV))
22 #define PWM_FREQ                33250
23 
24 volatile unsigned int incCount = 0; /*
25  * 
26  */
27 
28 void initAudio(void) {
29     // configures peripherals for Audio playback  
30     // Activate the PWM module
31     // OC1 in PWM mode, TMR2 based
32     OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
33     // timebase   
34     // enable TMR2, prescale 1:1, internal clock, period  
35     OpenTimer2(T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0);
36     mT2SetIntPriority(4);
37     // set TMR2 interrupt priority     
38 } // initAudio
39 
40 void startAudio(int bitrate) { // begins the audio playback
41     //  set the period for the given bitrate
42     PR2 = GetPeripheralClock() / bitrate - 1; //fpb
43     //  enable the interrupt state machine
44     mT2ClearIntFlag();
45     // clear interrupt flag   
46     mT2IntEnable(1);
47     // enable TMR2 interrupt      
48 } // startAudio
49 
50 void haltAudio(void) {
51     // stops playback state machine
52     mT2IntEnable(0);
53 } // halt audio
54 
55 main() {
56     SYSTEMConfig(GetPeripheralClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
57     INTEnableSystemMultiVectoredInt();
58     //
59     TRISBbits.TRISB7 = 0; //Set port as output
60     RPB7Rbits.RPB7R = 0x0005;
61     mPORTAClearBits(BIT_0); //Clear bits to ensure light is off.
62     mPORTASetPinsDigitalOut(BIT_0); //Set port as output
63     //mPORTAClearBits(BIT_0); //Clear bits to ensure light is off.
64     // Set OC1 to pin RB7 with peripheral pin select
65     RPB7Rbits.RPB7R = 0x0005;
66     initAudio();
67     OC1RS = (PR2 + 1) * ((float) 50 / 100); // pwm duty cycle can be updated here on in interrupt
68     startAudio (PWM_FREQ);
69     //timer init
70     //mT2ClearIntFlag();
71     // clear interrupt flag
72     //mT2IntEnable(1);
73     //
74     while (1) {
75         if (incCount >= (PWM_FREQ/2)) {
76             incCount = 0;
77             mPORTAToggleBits(BIT_0);
78         }
79     }//while 1
80 }//main
81 
82 void __ISR(_TIMER_2_VECTOR, ipl4) T2Interrupt(void) {
83     // 
84     incCount++;
85     // 1. load the new samples for the next cycle  
86     OC1RS = (PR2 + 1) * ((float) 50 / 100);
87     // 2. clear interrupt flag and exit
88     mT2ClearIntFlag();
89 } // T2Interrupt  
90 

No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless