Author Topic: PIC12F1571 PWM problems  (Read 2845 times)

0 Members and 1 Guest are viewing this topic.

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 332
  • Country: bg
    • Microlab.info
PIC12F1571 PWM problems
« on: November 11, 2016, 08:05:24 pm »
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: [Select]
  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 RA5
  PWM1CLKCON = 0x00;
  //PWM1CLKCONbits.PS = 0;
  //PWM1CLKCONbits.CS = 0b01;
  PWM1INTE = 0;
  PWM1CONbits.EN = 1;

  PWM1DC = 0x003F;
  PWM1LDCONbits.LDT = 1;
 

Offline Paul Price

  • Super Contributor
  • ***
  • Posts: 1443
Re: PIC12F1571 PWM problems
« Reply #1 on: November 11, 2016, 08:35:10 pm »
In short, RTFM:

22.4 Reload Operation
Four of the PWM module control register pairs and one
control bit are double-buffered so that all can be
updated simultaneously. These include:
• PWMxPHH:PWMxPHL register pair
• PWMxDCH:PWMxDCL register pair
• PWMxPRH:PWMxPRL register pair
• PWMxOFH:PWMxOFL register pair

• OFO control bit
When written to, these registers do not immediately
affect the operation of the PWM. By default, writes to
these registers will not be loaded into the PWM Operating
Buffer registers until after the arming conditions
are met. The arming control has two methods of
operation:
• Immediate
• Triggered
The LDT bit of the PWMxLDCON register controls the
arming method. Both methods require the LDA bit to be
set. All four buffer pairs will load simultaneously at the
loading event.
22.4.1 IMMEDIATE RELOAD
When the LDT bit is clear, then the immediate mode is
selected and the buffers will be loaded at the first period
event after the LDA bit is set. Immediate reloading is
used when a PWM module is operating stand-alone or
when the PWM module is operating as a master to
other slave PWM modules.
22.4.2 TRIGGERED RELOAD
When the LDT bit is set, then the Triggered mode is
selected and a trigger event is required for the LDA bit
to take effect. The trigger source is the buffer load
event of one of the other PWM modules in the device.
The triggering source is selected by the LDS<1:0> bits
of the PWMxLDCON register. The buffers will be
loaded at the first period event following the trigger
event. Triggered reloading is used when a PWM
module is operating as a slave to another PWM and it
is necessary to synchronize the buffer reloads in both
modules.
« Last Edit: November 11, 2016, 09:20:35 pm by Paul Price »
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 332
  • Country: bg
    • Microlab.info
Re: PIC12F1571 PWM problems
« Reply #2 on: November 12, 2016, 08:23:13 am »
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 mode
PWM1OF - 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 initially
and I update the period like this:

Code: [Select]
PWM1LDCONbits.LDT = 0; // make sure previous update does not take place between the setting of PWM1DCH and PWM1DCL
PWM1DC = xxx;
PWM1LDCONbits.LDT = 1; // that's Immediate mode, right? on next period new settings should be active, but it pwm duty cycle never changes.
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 332
  • Country: bg
    • Microlab.info
Re: PIC12F1571 PWM problems
« Reply #3 on: November 13, 2016, 08:46:09 am »
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.
 

Offline npelovTopic starter

  • Frequent Contributor
  • **
  • Posts: 332
  • Country: bg
    • Microlab.info
Re: PIC12F1571 PWM problems
« Reply #4 on: November 13, 2016, 02:13:32 pm »
And I found my mistake:

Code: [Select]
PWM1LDCONbits.LDT = 1;
I thought I'm setting load bit, but that's the trigger bit. What I actually meant is:
Code: [Select]
PWM1LDCONbits.LDA = 1;
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->