3
\$\begingroup\$

I am having trouble getting a simple PWM output to work at all on a PIC32MX775F512H. Looking online, this seems to be all I need to get something working:

int main(void)
{
    SYSTEMConfig(SYSTEM_FREQ_HZ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
    OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0xFFFF);
    SetDCOC1PWM(0x4000); // 50% duty cycle

    while(1)
    {
    }
}

However, the OC1 pin (pin 46 on the 64 QFN package) doesn't seem to be doing anything.

\$\endgroup\$
  • \$\begingroup\$ I think a lot of stuff is going in the "HardwareProfile.h" file. You did not set any of the pins' functions as I see. I don't know much about PIC32, but maybe the pin is shared with a digital input or an analog input or any other peripheral, and you may have to configure it to use as PWM output? \$\endgroup\$ – abdullah kahraman May 14 '13 at 12:32
  • \$\begingroup\$ The best way to start any Micro program is to define every pin to something that makes sense. I set everything to digital input or output unless it needs to be something else (even unused pins). Also setting all the timers and control registers helps stop "Strange" things from happening. \$\endgroup\$ – Spoon May 14 '13 at 17:33
4
+50
\$\begingroup\$

See page 54 in the oc datasheet There is one item in the list that the example code shown by the OP doesn't match, although that may not be the issue. #3 Write the OxCR register with the initial duty cycle.

Personally I do not care for macro usage because it is too generic and difficult to double check in code, as above, and sometimes the macros have bugs. If you are using MplabX, right click the macro and navigate-go to declaration to review the implementation.

Page 65 has a complete example except for a main subroutine, it could be copied and pasted quite easily.

Pic32 chips (and others in the MC family) don't hand hold very well. All it takes is one bit wrong somewhere and stuff just won't work. Every detail matters. Go through each one step by step and it will work. Also, I don't have enough reputation to comment, but could @RocketMagnet go into more detail about how he is checking the OC1 pin?

Also, 0xFFFF / 0x4000 = 33% duty cycle.

Added code --

#include <stdio.h>
#include <stdlib.h>
#include "p32xxxx.h"
#include "plib.h"
#define SYSTEM_FREQ_HZ 80000000
#pragma config FPLLODIV = DIV_1, FSOSCEN = OFF, FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FWDTEN = OFF, FPBDIV = DIV_1, POSCMOD = XT, FNOSC = PRIPLL, CP = OFF
#pragma config FMIIEN = ON, FETHIO = OFF, FUSBIDIO = OFF, FVBUSONIO = OFF   // external PHY in RMII/alternate configuration
#pragma config UPLLEN = ON,UPLLIDIV = DIV_2
/*
 * 
 */
int main(void)
{
    SYSTEMConfig(SYSTEM_FREQ_HZ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
    OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0xFFFF);
    unsigned short a;
    int b;
    while(1)
    {
        for(b=0;b<100;b++)
            Nop();
        SetDCOC1PWM(a++);
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You are providing some great general tips for working with the PIC32, but do you have a specific answer to the OP's question? \$\endgroup\$ – Jesse Craig Jun 12 '13 at 14:36
  • 1
    \$\begingroup\$ Without having the OP hardware in hand, it is going to be rather difficult to give a working example. I didn't see any problem with his example. Saying it "Doesn't seem to be doing anything" really isn't descriptive enough to do more. Thus the suggestion to follow every detail down, because that is usually where I have found these issues to be. \$\endgroup\$ – Erik Friesen Jun 12 '13 at 14:50
  • 1
    \$\begingroup\$ I tested the code the OP posted on an ethernet starter kit, added config bits and includes, and changed to OpenOC3, and it worked out of the box. So, its probably a hardware problem. \$\endgroup\$ – Erik Friesen Jun 12 '13 at 15:32
  • \$\begingroup\$ Can you post your code? Was the only change moving to OC3? Did you get the same result as the OP with OC1? \$\endgroup\$ – Jesse Craig Jun 12 '13 at 17:35
  • 1
    \$\begingroup\$ Added complete code. Slowly brightens the LED #1 on the starter kit. Ethernet Starter kit uses 795f512L. \$\endgroup\$ – Erik Friesen Jun 12 '13 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.