PIC12F1571 PWM problems - Page 1
Forum section: electronics / projects

author: npelov
author_rank: Frequent Contributor
timestamp: November 11, 2016, 08:05:24 pm
content: I have troubles making PWM work. I had problems starting the PWM. I fixed them by changing all the registers while PWM1CONbits.EN = 0 and then set EN to 1. So it now works but I can't change duty cycle without turning the module off. The code below should set duty cycle to 50% after the initialization to about 1.5%, but the duty cycle stays to the first value set. I don't want to turn the module off to set duty cycle. I want it updated on next period match. Any ideas?Code:PWM1CON = 0x40;//PWM1CON = 0;//PWM1CONbits.MODE = 0b00;//PWM1CONbits.OE = 1;PWM1LDCONbits.LDA = 0;PWM1PR = 0x007F;PWM1DC = 0x0002;PWM1PH = 0;PWM1LDCONbits.LDT = 1;APFCONbits.P1SEL = 1; // 1 => pwm1 function is on RA5PWM1CLKCON = 0x00;//PWM1CLKCONbits.PS = 0;//PWM1CLKCONbits.CS = 0b01;PWM1INTE = 0;PWM1CONbits.EN = 1;PWM1DC = 0x003F;PWM1LDCONbits.LDT = 1;

author: Paul Price
author_rank: Super Contributor
timestamp: November 11, 2016, 08:35:10 pm
content: In short, RTFM:22.4 Reload OperationFour of the PWM module control register pairs and onecontrol bit are double-buffered so that all can beupdated simultaneously. These include:• PWMxPHH:PWMxPHL register pair• PWMxDCH:PWMxDCL register pair• PWMxPRH:PWMxPRL register pair• PWMxOFH:PWMxOFL register pair• OFO control bitWhen written to, these registers do not immediatelyaffect the operation of the PWM. By default, writes tothese registers will not be loaded into the PWM OperatingBuffer registers until after the arming conditionsare met. The arming control has two methods ofoperation:• Immediate• TriggeredThe LDT bit of the PWMxLDCON register controls thearming method. Both methods require the LDA bit to beset. All four buffer pairs will load simultaneously at theloading event.22.4.1 IMMEDIATE RELOADWhen the LDT bit is clear, then the immediate mode isselected and the buffers will be loaded at the first periodevent after the LDA bit is set. Immediate reloading isused when a PWM module is operating stand-alone orwhen the PWM module is operating as a master toother slave PWM modules.22.4.2 TRIGGERED RELOADWhen the LDT bit is set, then the Triggered mode isselected and a trigger event is required for the LDA bitto take effect. The trigger source is the buffer loadevent of one of the other PWM modules in the device.The triggering source is selected by the LDS<1:0> bitsof the PWMxLDCON register. The buffers will beloaded at the first period event following the triggerevent. Triggered reloading is used when a PWMmodule is operating as a slave to another PWM and itis necessary to synchronize the buffer reloads in bothmodules.

author: npelov
author_rank: Frequent Contributor
timestamp: November 12, 2016, 08:23:13 am
content: Hi Paul,I did read this section, but apparently I'm missing something important. About the double buffered registers:PWM1PH - I set this to 0. Although, not sure if it matters when I'm using only one PWM in independent modePWM1OF - I don't set this register because I read it only affects offset relative to another PWM module. I only use one.PWM1PR - I set it initiallyand I update the period like this:Code:PWM1LDCONbits.LDT = 0; // make sure previous update does not take place between the setting of PWM1DCH and PWM1DCLPWM1DC = xxx;PWM1LDCONbits.LDT = 1; // that's Immediate mode, right? on next period new settings should be active, but it pwm duty cycle never changes.

author: npelov
author_rank: Frequent Contributor
timestamp: November 13, 2016, 08:46:09 am
content: Well I simplified the code into a separate project and it works. So I'm probably doing something else wrong. I'll check my code again.

author: npelov
author_rank: Frequent Contributor
timestamp: November 13, 2016, 02:13:32 pm
content: And I found my mistake:Code:PWM1LDCONbits.LDT = 1;I thought I'm setting load bit, but that's the trigger bit. What I actually meant is:Code:PWM1LDCONbits.LDA = 1;