PWM and PPS on a PIC32MX

PWM and PPS on a PIC32MX

Postby yubyub » Tue Jul 29, 2014 8:40 pm

Hello again.

Being a bit new to MPLAB X and Harmony, I was wondering if anyone has any advice for getting the PPS and PWM working on a PIC32MX470F512H.

I've cut and pasted a bit of code from another example to configure the PWM, which looks okay to me. Although, saying that, the PPS code looks okay, too, and it obviously isn't as I get no output.

The aim is this: get a PWM output OC module 3, output via RB14.

Here's the relevant code I'm using:

Code: Select all
    PLIB_DEVCON_SystemUnlock(PORTS_ID_0);
    PLIB_DEVCON_DeviceRegistersUnlock(DEVCON_ID_0, DEVCON_ALL_REGISTERS);
    PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_OC3, OUTPUT_PIN_RPB14);
    PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_14 );
    PLIB_DEVCON_DeviceRegistersLock(DEVCON_ID_0, DEVCON_ALL_REGISTERS);

    PLIB_TMR_ClockSourceSelect(TMR_ID_2, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK);
    PLIB_TMR_PrescaleSelect(TMR_ID_2, TMR_PRESCALE_VALUE_1);
    PLIB_TMR_Mode16BitEnable(TMR_ID_2);
    PLIB_TMR_Counter16BitClear(TMR_ID_2);
    PLIB_TMR_Period16BitSet(TMR_ID_2, 2000);

    PLIB_OC_TimerSelect(OC_ID_3, OC_TIMER_16BIT_TMR2);
    PLIB_OC_ModeSelect(OC_ID_3, OC_TOGGLE_CONTINUOUS_PULSE_MODE);
    PLIB_OC_FaultInputSelect(OC_ID_3, OC_FAULT_DISABLE);
    PLIB_OC_BufferSizeSelect(OC_ID_3, OC_BUFFER_SIZE_16BIT);
    PLIB_OC_Buffer16BitSet(OC_ID_3, 0);
    PLIB_OC_PulseWidth16BitSet(OC_ID_3, 500);

     /* Enable the OCMP module and start the timer */
    PLIB_OC_Enable(OC_ID_3);
    PLIB_TMR_Start(TMR_ID_2);


The PIC itself is running fine - a 16MHz crystal is driving it to 80MHz, and the ever famous "blink and LED" test works great. The above code compiles just fine, but my favorite scope is just showing me flat lines. This is in a very recent copy of MPLAB X, along with a very recent copy of XC32.

I can't tell what may be going on here, as most of the functions return void (no error codes)... before I went ahead and started going through what I can, into the assembler, etc, I thought I'd ask here to see if I've done anything obviously wrong.

Thanks for your patience, and any advice.
yubyub
 
Posts: 10
Joined: Tue Jul 29, 2014 11:36 am
PIC experience: Professional 1+ years with MCHP products

Re: PWM and PPS on a PIC32MX

Postby yubyub » Wed Jul 30, 2014 1:44 pm

I finally did get this working.

My issue was that I was looking at the wrong specs for the port remapping (i.e. what OC3/etc can be mapped to). I ended up using OC4 outputting on RG7 with the following code:

Code: Select all
    PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_OC4, OUTPUT_PIN_RPG7);
    PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7);

    PLIB_TMR_ClockSourceSelect(TMR_ID_2, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK);
    PLIB_TMR_PrescaleSelect(TMR_ID_2, TMR_PRESCALE_VALUE_1);
    PLIB_TMR_Mode16BitEnable(TMR_ID_2);
    PLIB_TMR_Counter16BitClear(TMR_ID_2);
    PLIB_TMR_Period16BitSet(TMR_ID_2, 10000);

    PLIB_OC_TimerSelect(OC_ID_4, OC_TIMER_16BIT_TMR2);
    PLIB_OC_ModeSelect(OC_ID_4, OC_TOGGLE_CONTINUOUS_PULSE_MODE);

    PLIB_OC_Enable(OC_ID_4);
    PLIB_TMR_Start(TMR_ID_2);


This generates a perfect 4KHz square wave provided a system peripheral clock of 80MHz, as expected (80MHz/10000 clock ticks yields a timer cycle of 8KHz, and each timer reset toggles the output, so we get a 4KHz square wave).

In the future, I've gotta remember to RTCFM (Read The Correct F***** Manual).
yubyub
 
Posts: 10
Joined: Tue Jul 29, 2014 11:36 am
PIC experience: Professional 1+ years with MCHP products

Re: PWM and PPS on a PIC32MX

Postby yubyub » Wed Jul 30, 2014 1:54 pm

Just a visual for those who love pictures. Excuse the slight ringing/overshoot, it's my fault, not the circuit nor the scope.

scope_1.png
4KHz square wave working.
scope_1.png (21.37 KiB) Viewed 2864 times
yubyub
 
Posts: 10
Joined: Tue Jul 29, 2014 11:36 am
PIC experience: Professional 1+ years with MCHP products


Return to PIC32 topics

Who is online

Users browsing this forum: No registered users and 1 guest