Author Topic: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)  (Read 6034 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
I have carefully RTFM about PWM yet still seem to not get "it", how to get my PWM to work.
I am trying to get the RC2 CCP1 pin to work with TMR4 for  approx 20KHz 10-bit PWM operation with internal clock set at 64MHz

I get ziltch at the output.

Attached is the Microchip specsheet diagram of PWM operation..I cannot seem to get to understand how PWM works from studying this carefully.

Here's what I think I know, it is not enough to get this !#@^@& PWM to work:

1)I fully understand PWM as a dutycycle controlling method using a timer to generate a fixed frequency free-running oscillator that has its high(on) time (dutycycle) controlled by PWM register settings.
2)I fully understand how PWM might work:
 A fixed frequency oscillation is created with by using a  (usually 8-bit) clocked counter to reach a full count at some period(freq., i.e. say approx 20KHz) and this counter is setting an output pin high on at count zero and at overflow.
If there is no match with a comparator a fixed value(dutycycle setting) the output will remain high indefinitely.
If the counter does count up to a value that matches the comparison value(aka duty cycle), then the output pin is set low for the remainder of the count until a counter overflow sets the output pin high again at counter overflow.

To increase the resolution of a PWM, the counter overflow increments a a few bits of a concatenated secondary counter to increase the 8-bit's counter overflow period to something like 10 bits. In this case the counter then must overflow as many times as necessary to cause and allow setting a 10-bit match to cause the PWM output to be reset and set high again at overflow.

A prescaler or postscaler for the 8-bit PWM counter can lower the PWM freq. as needed by dividing the system clock by some binary ratio.

By allowing the 8-bit counter to only reach some fraction of the max 256(8-bit) overflow, the 8-bit counter count can be fed to a comparator whose purpose is increase the PWM frequency by having it reach a max count <overflow at some value. like only 64 or 128 counts, thus decreasing the period(count) of overflow, at the expense of PWM resolution. 

Therefore resolution is set only by the binary ratio of the comparison clock to the 8-bit clock?

So why does the specsheet offer  this silly(difficult to understand)  formula to calculate resolution?
Resolution in bits = ( LOG(4 * (PRX+1) )/2   


It would seem to me that to get 10-bit resolution of duty cycle using an 8-bit PWM freq, the comparison would require using a clock running 4x as fast, synchronizing the start of the 8-bit PWM counter to the 10-bit comparsion 4x as fast counter.

That being said, I don't quite understand the diagram taken from the PIC18Fx5K2x specsheet.

It shows the output of the 8-bit concatenated counter toggling the RS flipflop, but RS flipflops don't toggle, they do set/reset. I also don't understand what that line from the lower comparator to the TMRX block and S pin of the S/R FF.
I also don't understand why two bits of the system clock are used for comparison.

But in any case, either by not enough RTFM or by a coding error, I do not have all the settings required or correctly set to get the ball rolling.


I can choose from three clocks and a handful of output or multiple bridge output pins.

I only want one PWM output pin, RC2 CCP1
Is it just me or I am missing something?
« Last Edit: June 22, 2015, 09:25:46 am by SuzyC »
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 1605
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #1 on: June 22, 2015, 10:23:25 am »
To increase the resolution of a PWM, the counter overflow increments a a few bits of a concatenated secondary counter to increase the 8-bit's counter overflow period to something like.
Can I suggest the following interpretation of Figure 14-4: The two extra bits are "concatenated" at the low end of TMRx, not at the overflow end. I.e. the value in TMRx represents the high-order bits of the count, the two low-order bits come from either Fosc or the prescaler.

Quote
So why does the specsheet offer  this silly(difficult to understand)  formula to calculate resolution?
Resolution in bits = ( LOG(4 * (PRX+1) )/2   

You only have 8 bits to define PRx. As stated above, these bits represent the high-order bits of the total period. I.e you can only define the repetition frequency to a resolution of 8 bits.

Quote
It shows the output of the 8-bit concatenated counter toggling the RS flipflop, but RS flipflops don't toggle, they do set/reset.
Er ... not on the diagram that I can see!
I see the lower comparator causing the timer (TMRx) to be reset to zero (I assume) and the flip-flop to be SET. I think the line that extends up to the arrow that links CCPRxL to CCPRxH is indicating that the value in CCPRxL is loaded into CCPRxH when the lower comparator trips.

The upper comparator RESETs the flip-flop when the value in TMRx and the extra two low bits match the 10 bits defined in CCPRxL plus CCPxCON<5:4>

In summary: The value in PRx defines the upper 8 bits of the PWM period (frequency). The value in CCPRxL+ CCPxCON<5:4> defines the ON duty period. TMRx is clocked at a quarter of whatever clock source you've selected.
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #2 on: June 22, 2015, 11:07:37 am »
Hmmm, my dear Watson, I still don't get it, if they concatenate at the lsb or msb it still creates the same bit-length counter that feeds into a comparator.

 I wish Microchip would have included a waveform diagram that showed the simultaneous behavior of clocks and counters and comparators and logic in PWM operation, or even better, a C-code example on how to setup at least one of the many PWM possible pin outputs.
« Last Edit: June 22, 2015, 12:55:22 pm by SuzyC »
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 604
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #3 on: June 22, 2015, 06:43:39 pm »
if they concatenate at the lsb or msb it still creates the same bit-length counter that feeds into a comparator.
The total number of bits is the same either way, but you need to stuff the bits into the correct places or else you won't get the PWM ratio you were expecting. If you want 8 bit PWM then just load the 8 bit value into CCPRxL. For 10 bit PWM you must put bits 1-0 into CCPxCON 5-4, and bits 9-2 into CCPRxL.   

Quote
I wish Microchip would have included a waveform diagram that showed the simultaneous behavior of clocks and counters and comparators and logic in PWM operation, or even better, a C-code example on how to setup at least one of the many PWM possible pin outputs.
This is the standard PWM block used in most PIC chips, so it shouldn't be hard to find code examples.

Basic operation is quite simple; 8 bit TMRx counts up until it matches the value in PRx. This is the PWM period. The output pulse is set high at the start of the cycle, and reset low when the value loaded into CCPRxL matches the count in TMRx. If CCPRxL = 0 then the output latch is held in reset and the pulse never starts. If CCPRxL is larger than PRx then a match never occurs and the output stays high throughout the PWM cycle.

10 bit resolution is achieved by adding 2 extra bits from CCPxCON, which is compared to 2 bits from the prescaler. This makes the PWM pulse 'step' 4 times faster, quadrupling the resolution without effecting the period.
 
Quote
I get ziltch at the output.

If you have loaded reasonable values into CCPRxL and PRx then either you aren't in PWM mode or you haven't configured the output module or I/O pins correctly.


 
 

Online H.O

  • Frequent Contributor
  • **
  • Posts: 576
  • Country: se
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #4 on: June 22, 2015, 06:57:30 pm »
Quote
So why does the specsheet offer  this silly(difficult to understand)  formula to calculate resolution?
Resolution in bits = ( LOG(4 * (PRX+1) )/2
Now that formula isn't quoted quite correct, it should be Resolution in bits = LOG [4*(PRX+1)] / LOG(2). So if you set PRX to, lets say, 103 then that turns out to something like 8.7bits which isn't quite 9 bits but still more than 8.....
In reallity it means that the dutycycle register(s) can be set to anything between 0 and 415 for a 0 to 100% dutycycle.

Quote
It would seem to me that to get 10-bit resolution of duty cycle using an 8-bit PWM freq, the comparison would require using a clock running 4x as fast, synchronizing the start of the 8-bit PWM counter to the 10-bit comparsion 4x as fast counter.
Correct, and that's exactly what's happening due to the timbase beeing concatenated by the two bits of the internal Fosc/4 divider making it 10bits wide. The RS Flipflop ain't it.

EDIT:
Quote
I also don't understand why two bits of the system clock are used for comparison.
That IS where the 2 bits of extra resolution is comming from.
« Last Edit: June 22, 2015, 07:03:41 pm by H.O »
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 1685
  • Country: gb
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #5 on: June 22, 2015, 08:37:28 pm »
So why does the specsheet offer  this silly(difficult to understand)  formula to calculate resolution?
Resolution in bits = ( LOG(4 * (PRX+1) )/2   


Calling functions you don't understand as 'silly' is hardly constructive.

The PWM resolution is simply determined by the maximum value the internal counter reaches before being reset (i.e. 1 PWM period) which is calculated by 4*(PRx+1).

Two additional bits are added to PRx within the PIC to improve the resolution, which is why the register value is multiplied by 2^2  = 4.  A value of one is first added to the PRx value, since by design a PRx register value of zero yields a period of 4 clock cycles.   

To determine the number of bits required to represent an arbitrary number, you simply take LOG base 2 of that number.

Resolution = LOG2(4*(PRx+1))

Since pocket calculators typical don't have a LOG Base 2 function, the LOG identity LOGy(x) = LOGb(x)/LOGb(y) has been added to convert the result of an arbitrary base LOG (e.g. base 10 for a pocket calculator) to a base 2 LOG, i.e. LOG2(x) = LOGb(x)/LOGb(2).

So:

Resolution = LOG(4*(PRx+1))/LOG(2)

simply calculates the number of clock cycles in one PWM period, and then calculates the number of bits required to hold that value, which yields the resolution in bits.
 

Offline android

  • Regular Contributor
  • *
  • Posts: 131
  • Country: au
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #6 on: June 22, 2015, 09:17:35 pm »
Hi SuzyC,

You may find this PWM calculator spreadsheet that I put together a while back helpful. It displays a chart that illustrates the various interactions between the MCU freq, PWM freq and prescaler settings that you select. I used LibreOffice Calc - I'm not sure what it looks like in Excel.
Lecturer: "There is no language in which a double positive implies a negative."
Student:  "Yeah...right."
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #7 on: June 22, 2015, 10:44:54 pm »
Thanks to all for your help, but I am not yet quite getting it, CCP1 RC2 output is still steady at Vss.

For instance, suppose I have battery-operated device and I want to know what is the lowest system clock freq. usable and still have 10-bit resolution.

I can see that in all cases, at any freq, the PRx value must be 0xFF for 10-bit resolution, so this is no secret, but why I call the specsheet formula silly is that it doesn't help get the answer for the selecting the lowest practical PWM freq.

I realize as clock speeds get slower and slower, PWM freq. falls to a freq.  that fails to be blink'n practical for LED MTPX'ing, motor control or PWM voice synthesis.
Microchip fails helping a designer to get this coding task done quickly, and this is due just to poor technical writing.

What is simply required in the specsheet is simply a chart or graph of System Clock v. PWM Freq. v. Bit Resolution.


Thanks Android, you seem fully qualified for and should seriously try to apply for a tech writer position at Microchip.
I downloaded the .odb file and it loads successfully into Excel 2007 and yet yields a blank spreadsheet.

« Last Edit: June 23, 2015, 02:25:51 am by SuzyC »
 

Online H.O

  • Frequent Contributor
  • **
  • Posts: 576
  • Country: se
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #8 on: June 23, 2015, 01:51:55 am »
Quote
For instance, suppose I have battery-operated device and I want to know what is the lowest system clock freq. usable and still have 10-bit resolution.
There is no lower limit. You can run your PIC at 32kHz and STILL get a PWM signal with 10 bit resolution out of it.

The highest PWM frequency, while maintaining 10 bit resolution will always be Fosc / 2^10. So at 32.768kHz the PWM frequency can not be higher than 32Hz in order to maintain 10bit resolution, or 128Hz to maintain 8bit resolution and so on.
 

Offline picandmix

  • Frequent Contributor
  • **
  • Posts: 374
  • Country: gb
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #9 on: June 23, 2015, 02:23:47 am »
To help you prove things are still working ok on your system here is a .hex file  with a pwm example that puts out approx  25% duty on RC2 and 75% on RC1

 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #10 on: June 23, 2015, 02:50:07 am »
Thanks picandmix for the help and .hex file, but I already know my HW is working correctly from previous test programs and a .hex file gives me no C-code insight of what I might be coding wrong.

Thanks, H.0, this gives idea what the lower system clock freq. limits are and PWM freq. at the very lowest of Fosc.

This is what I have so far:

void interrupt low_priority lp(void)
if (TMR0IF)             //TMR0 overflow flag, if set triggers IRQ  TMR0Clk=Clk/2
 { TMR0=63903;   //Comp needed for 64Mhz, calibrates timer for 100uS/IRQ.
   TMR0IF=0;         //64736 -(13 to enter isr + 2 cnts dly) = 64721 expected but 63903 gives rock-solid 100uS
//--------------------SET PWM Duty Cycle-------------------------------
    CCP1CONbits.DC1B0 = DCyc & 1; //set low bit
    CCP1CONbits.DC1B1 = (DCyc >> 1) & 1;  //set second lowest
    CCPR1L = (DCyc >> 2); //sets DCycH hi 8 hi-bits
    //DC1B0=n; syntax works as well in HT18
 }

void PWM1Setup(void)//CCP1 output on RC2 
{
TRISC2=0; //PWM  requires the PWM pin TRIS'd as output
//                                       
//Select 8-bit Timer: Choices TMR2/TMR4/TMR6,use CxTSEL<1:0> bits of CCPTMRS0
//C1TSEL=0x01; //TMR4 selected   options are: 10=TMR6 00=TMR2 01=TMR4
CCPTMRS0=(CCPTMRS0 & 0xFC); //Mask to leave C3TSEL and C2TSEL unchanged
CCPTMRS0=(CCPTMRS0 | 0x01); //C1TSEL=0x01 TMR4 selected
/* Bit  7      6      5     4      3                    2             1    0
 ------------------------------------------------------------------------
         x       0      0     0      0                    1              0   0
                  TxOUTPS<3:0>
                POSTSCALER 1:1 to 1:16     TMxOn        PRESCALER
                                                                              1:1=00 01=1:4 1x1:/16
*/
T4CON=0b00000100;
//CCP1CON holds duty cycle LSb in bits<5:4>LSbs, but CCPR1L=MSbits
CCP1CON=00000000; //DCyc=0 off PWM1 channel
CCPR1L=(CCPR1L & 0xC0); //Mask P1M bits, MSbits of DCyc=0, CPR1H not used
PR4=0xFF; //overflow count 0XFF for 10-bit resolution for any PWM freq.
CCP1IE=0; // Disable CCP1 IRQ enable not used for PWM only mode

TMR4IE = 1;    // enable TIMER4 interrupt
PEIE   = 1;    // enable peripheral interrupts
TMR4ON = 1;    //Turn on TMR4  used for duty cycle period
}
//====   End of PWM1Setup   ===================================

void main(void)
{
  Init_PIC();
  Init_TMR0();
  PWM1Setup();//Setup CCP1 cbit2 PWM

  IPEN  =1;   //Priority interrupts enabled
              //RCONbits.IPEN = 1; //also works 9.80

  TRISB =1;   //INT0IE sw on bbit0 ready with pullup to Vdd
              //INT0IF=0;
              //INT0IE=1;
  TMR0IE=1;   
  TMR0ON=1;
  TMR0IP=0;
  TMR4IP=0;
  TMR4ON=1;
  TMR4IP=0; 
  TMR4IE=0; 
  CCP1IE=0; 
  CCP1IP=0; 
//  PIE1=1;   //Not Needed for TMRx
//  PIE2=1;   //Not Needed for TMRx
    PLLEN =1;
    PEIE  =1;   //Needed for TMRx interrupts and A2D?
    GIEL=1;   //Enables Low-Priority if GIEH/GIE=1
    GIEH=1;   //Ok to use INTCONbits.GIE = 1; to enable all interrupts     
 
//======================== START =======================================
  Start:

  DCyc=50;
  goto Start;

« Last Edit: June 23, 2015, 04:34:51 am by SuzyC »
 

Offline picandmix

  • Frequent Contributor
  • **
  • Posts: 374
  • Country: gb
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #11 on: June 23, 2015, 03:48:03 am »
Rather lost in all your C code above.

This is how its done in Assembly, am sure you could readily convert it to run in C;  believe you can also run Assembly code within C, though no idea how.

Code: [Select]
CONFIG3H  CCP2MX = PORTC1

pwm_setup ; sequence from datasheet

         movlw   b'11111001'           ; period = 2000 hz at 4mhz with tm2 prescaler 4
         movwf PR2     

clrf CCPR1L ; set pwm1 duty to zero
        clrf CCPR2L        ; set pwm2 duty to zero

bcf TRISC,2        ; ensure /set ccp1/2 ports to output
bcf TRISC,1

  movlw   b'01111100'        ; tmr2=ON,pre=1:4
    movwf T2CON   
    bsf T2CON,TMR2ON

    movlw b'00111100'    ; set ccp1 to pwm mode
        movwf   CCP1CON         

       movlw   B'00111100'    ; set ccp2to pwm mode
       movwf   CCP2CON 
   
   
   
 pwm_run
 
  movlw 0x4F ; raise LED1 PWM to start value of 0xnn
movwf CCPR1L
movlw 0xAF ; raise LED2 PWM to start value of 0xnn
movwf CCPR2L

 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #12 on: June 23, 2015, 04:08:01 am »
Thanks picandmix, but I am totally lost in assembly, it is hard enough to troubleshoot complex operations in C, I would imagine going to even a lower level would be an horror.

Update:  I put a test bit in main() to blink a LED if the TMR4>0 (LED on), if TMR0>250 (LED on RC3 off) 
             
               Test LED turns on and briefly off, Means TMR4 is resetting on 256.. PR4, but no CCP1 pin output
« Last Edit: June 23, 2015, 05:09:32 am by SuzyC »
 

Offline picandmix

  • Frequent Contributor
  • **
  • Posts: 374
  • Country: gb
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #13 on: June 23, 2015, 05:44:41 am »
Thanks picandmix, but I am totally lost in assembly, it is hard enough to troubleshoot complex operations in C, I would imagine going to even a lower level would be an horror.

Update:  I put a test bit in main() to blink a LED if the TMR4>0 (LED on), if TMR0>250 (LED on RC3 off) 
             
               Test LED turns on and briefly off, Means TMR4 is resetting on 256.. PR4, but no CCP1 pin output

Thats all the code needed, added to the flasking led program,  to set up and run pwm in assembly, do not think it could be much less.

As I say, I do not know C, but to me it looks like you are investigating every bit of every register and including them all in your code.
Probably many not needed as you can simply rely on the register default values.

Forgive asking, but have you tried using a C tutorial to get you going more easily, there are a few still left around, heres one.

http://www.electro-tech-online.com/articles/nigel-goodwins-tutorials-in-c.467/


 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #14 on: June 23, 2015, 05:58:29 am »
Thanks picandmix, I have written 100's of complicated C-language programs that work, I think I understand C well-enough.
Its this chip that's the problem.
 

Offline lewis

  • Frequent Contributor
  • **
  • Posts: 697
  • Country: gb
  • Nullius in verba
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #15 on: June 23, 2015, 06:35:21 am »
Here's some abridged code that I've used on the 18F45K22 in a product.

The errata mentions the PWM does not work with CCP3, 4 or 5: http://ww1.microchip.com/downloads/en/DeviceDoc/80498B.pdf

The 10us delay in the main() function is very important for some reason. The ECCP module would not start up without it and I never figured out why. Could be a software bug, but I pared everything down to try to understand it, no joy.

The pwm_init() function configures the ECCP module to use auto-shutdown triggered by the comparator. It also uses pulse steering. This is single output PWM, not half-bridge or full-bridge.

It's all in the datasheet! (except the 10us delay bit, as I say, my PWM wouldn't work without it for some unknown [to me] reason).

Code: [Select]
//outputs
sbit        MOT                  at LATD5_bit;                         //PWM output for motor
sbit        MOT_TRIS             at TRISD5_bit;                        //TRIS bit for PWM output
#define     MOT_PWM              0b0010


//sets PWM duty cycle. Takes unsigned int as duty value, splits off LSBs to DC1B bits, the rest goes into CCPR1L
void pwm_set_duty(unsigned int duty) {
     if(duty >= 1023) duty = 1023;           //ensure duty cycle does not go too far
     CCP1CON.B4 = lo(duty).B0;               //set lsbs in ccp1con register
     CCP1CON.B5 = lo(duty).B1;
     CCPR1L = (unsigned char)(duty>>2);      //lop off LSBs, MSBs to register.
}

//initialise pwm. requires comparator to have been configured previously
//comparator config not shown in code below
void pwm_init() {
     pwm_set_duty(0);                              //default zero duty cycle
     MOT = 0;                                      //drive output latches low
     MOT_TRIS = 1;                                 //set outputs as inputs to prevent spurious signals, we have an external pulldown
     T2CON = 0;                                    //turn off timer
     PR2 = PR2_CONFIG;                             //set period register
     TMR2 = 0;                                     //reset TMR2
     PSTR1CON = 0b00010000 | MOT_PWM;              //steer on next pwm period, set correct output to drive
     CCPTMRS0 = 0;                                 //all pwm uses tmr2
     CCPTMRS1 = 0;                                 // "   "   "    "
     ECCP1AS = 0b10110000;                         //auto shutdown enabled, comparator 1 source, drive all pins low on fault
     PWM1CON = 0;                                  //auto restart disbaled for now, 0Tosc delay after fault.
     CCP1CON = 0b00001100;                         //Enhanced PWM, single output, P1A (steerable) active high
     CCPR1L = 0;                                   //definitely zero duty cycle
     MOT = 0;                                      //ensure output latches are low ready for TRIS
     MOT_TRIS = 0;                                 //set outputs
     T2CON = T2CON_CONFIG;                         //start the timer
     PWM1CON.P1RSEN = 1;                           //start the PWM - enable auto-restart after shutdown (hardware clears CCP1ASE [bit 7] of ECCP1AS register)
}


void main() {

     OSCCON = 0b01110010;                     //16MHz Primary oscillator (PLL disabled in CONFIG word);                       /*go fast*/
     OSCCON2 = 0;
     OSCTUNE = 0;
     LATA = 0;                                     //all outputs default to low
     LATB = 0;
     LATC = 0;
     LATD = 0;
     LATE = 0;
     TRISA = TRISA_CONFIG;
     ANSELA = ANSELA_CONFIG;
     TRISB = TRISB_CONFIG;
     ANSELB = ANSELB_CONFIG;
     WPUB = WPUB_CONFIG;
     TRISC = TRISC_CONFIG;
     ANSELC = ANSELC_CONFIG;
     TRISD = TRISD_CONFIG;
     ANSELD = ANSELD_CONFIG;
     TRISE = TRISE_CONFIG;
     ANSELE = ANSELE_CONFIG;
     INTCON = 0;                                   //ensure no interrupts are enabled
     WDTCON = 0;

     delay_us(10);                                 //wait for oscillator to stabilise. VERY IMPORTANT FOR ECCP MODULE for some reason

     PMD0 = PMD0_CONFIG;
     PMD1 = PMD1_CONFIG;
     PMD2 = PMD2_CONFIG;

     RCON = RCON_CONFIG;

   //peripheral initialisations
     pwm_init();                           //initialise PWM after delay

   // MAIN LOOP
     while(1) {

           //stuff

     }
}



« Last Edit: June 23, 2015, 06:56:19 am by lewis »
I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 604
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #16 on: June 23, 2015, 07:59:53 am »
//CCP1CON holds duty cycle LSb in bits<5:4>LSbs, but CCPR1L=MSbits
CCP1CON=00000000; //DCyc=0 off PWM1 channel
Forgot the '0b'?

To select PWM mode you must set CCPxM<3:2>, like this:-

 CCP1CON=0b00001100;

Quote
CCPR1L=(CCPR1L & 0xC0); //Mask P1M bits, MSbits of DCyc=0, CPR1H not used
Not sure what you wanted to do there, but CCPR1L just holds the upper 8 bits of the PWM value.

 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #17 on: June 23, 2015, 10:24:09 am »
Thanks Lewis,  I read the errata link you posted and can see that the problem with PWM only applies to chip revision earlier than then 4.
In fact in order to RTFM, I downloaded the newer, latest errata and it doesn't show the problem. Also the chips I have are quite new out of the oven, the are all revision 5.
 

Offline android

  • Regular Contributor
  • *
  • Posts: 131
  • Country: au
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #18 on: June 23, 2015, 06:45:03 pm »
I downloaded the .odb file and it loads successfully into Excel 2007 and yet yields a blank spreadsheet.

hmmm, kind of expected I guess. Try this Excel 2003 version of the PWM calculator. ...created by LibreOffice so could still be flaky  ;D

Edit: Here's a version created from my son's copy of MS Excel 2007: Third time lucky.
« Last Edit: June 23, 2015, 10:07:44 pm by android »
Lecturer: "There is no language in which a double positive implies a negative."
Student:  "Yeah...right."
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #19 on: June 23, 2015, 08:04:17 pm »
Thanks Again Android, this .xls opens, but it is still unusable, flaky.
« Last Edit: June 23, 2015, 08:19:30 pm by SuzyC »
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #20 on: June 23, 2015, 09:03:19 pm »
Thanks for the .hex file, picandmix, but it will not program on my programmer because you set some CONFIG write protects in your code that disables verification after programming.
 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #21 on: June 23, 2015, 09:08:16 pm »
Thanks Bruce Abbot, but according to the 25K22 specsheet bits 6 and 7 of CCP1CON are used for P1M advanced output configuration bits and I did not think I should change the default settings for these two bits. In my isr, I set the dutycycle when the PWM is started.
I tried just setting the CCP1CON to 0 before PWM startup, but same results. I can see that PR4 is counting but the CCP1 pin remains constantly at Vss.
« Last Edit: June 23, 2015, 09:30:06 pm by SuzyC »
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 604
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #22 on: June 24, 2015, 02:00:51 am »
Thanks Bruce Abbot, but according to the 25K22 specsheet bits 6 and 7 of CCP1CON are used for P1M advanced output configuration bits
Bits 6 and 7 can be ignored, but bits 2 and 3 must be set. See datasheet page 205:-

"REGISTER 14-1: CCPxCON: STANDARD CCPx CONTROL REGISTER
bit 3-0 CCPxM<3:0>: ECCPx Mode Select bits
11xx =: PWM mode"

Here's my working test code for MPLAB X. You may recognize some of it... ;)

#include <xc.h>

void Init_TMR0()
{
 T0CON = 0b01000111; // 8bit, internal clock, prescale 1:256
}

int DCyc;

void interrupt low_priority lp(void)
{
if (TMR0IF)      //TMR0 overflow flag, if set triggers IRQ  TMR0Clk=Clk/2
 { TMR0=63903;   //Comp needed for 64Mhz, calibrates timer for 100uS/IRQ.
   TMR0IF=0;     //64736 -(13 to enter isr + 2 cnts dly) = 64721 expected but 63903 gives rock-solid 100uS
//--------------------SET PWM Duty Cycle-------------------------------
    CCP1CONbits.DC1B0 = DCyc & 1; //set low bit
    CCP1CONbits.DC1B1 = (DCyc >> 1) & 1;  //set second lowest
    CCPR1L = (DCyc >> 2); //sets DCycH hi 8 hi-bits
    //DC1B0=n; syntax works as well in HT18
 }
}

void PWM1Setup(void)//CCP1 output on RC2
{
TRISC2=0; //PWM  requires the PWM pin TRIS'd as output
//
//Select 8-bit Timer: Choices TMR2/TMR4/TMR6,use CxTSEL<1:0> bits of CCPTMRS0
//C1TSEL=0x01; //TMR4 selected   options are: 10=TMR6 00=TMR2 01=TMR4
CCPTMRS0=(CCPTMRS0 & 0xFC); //Mask to leave C3TSEL and C2TSEL unchanged
CCPTMRS0=(CCPTMRS0 | 0x01); //C1TSEL=0x01 TMR4 selected
/* Bit  7      6      5     4      3                    2             1    0
 ------------------------------------------------------------------------
         x       0      0     0      0                    1              0   0
                  TxOUTPS<3:0>
                POSTSCALER 1:1 to 1:16     TMxOn        PRESCALER
                                                                              1:1=00 01=1:4 1x1:/16
*/
T4CON=0b00000100;
//CCP1CON holds duty cycle LSb in bits<5:4>LSbs, but CCPR1L=MSbits
CCP1CON=0b00001100; //DCyc=0 off PWM1 channel, mode = PWM
// CCPR1L=(CCPR1L & 0xC0); //Mask P1M bits, MSbits of DCyc=0, CPR1H not used
PR4=0xFF; //overflow count 0XFF for 10-bit resolution for any PWM freq.
CCP1IE=0; // Disable CCP1 IRQ enable not used for PWM only mode

TMR4IE = 1;    // enable TIMER4 interrupt
PEIE   = 1;    // enable peripheral interrupts
TMR4ON = 1;    //Turn on TMR4  used for duty cycle period
}
//====   End of PWM1Setup   ===================================

void main(void)
{
//  Init_PIC();
  Init_TMR0();
  PWM1Setup();//Setup CCP1 cbit2 PWM

  IPEN  =1;   //Priority interrupts enabled
              //RCONbits.IPEN = 1; //also works 9.80

  TRISB =1;   //INT0IE sw on bbit0 ready with pullup to Vdd
              //INT0IF=0;
              //INT0IE=1;
  TMR0IE=1;
  TMR0ON=1;
  TMR0IP=0;
  TMR4IP=0;
  TMR4ON=1;
  TMR4IP=0;
  TMR4IE=0;
  CCP1IE=0;
  CCP1IP=0;
//  PIE1=1;   //Not Needed for TMRx
//  PIE2=1;   //Not Needed for TMRx
  PLLEN =1;
  PEIE  =1; //Needed for TMRx interrupts and A2D?
  GIEL=1;   //Enables Low-Priority if GIEH/GIE=1
  GIEH=1;   //Ok to use INTCONbits.GIE = 1; to enable all interrupts

//======================== START =======================================
  DCyc=50;
  while(1);
}

 

Offline SuzyC

  • Frequent Contributor
  • **
  • Posts: 492
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #23 on: June 24, 2015, 02:33:14 am »
Bruce Abbot, a thousand thanks!

What is so much a trap here in the spec sheet (of which I thought I  printed out all the pages I carefully thought to be relevant) is that there is actually two pages about setting up PWM giving the same register name different configuration information.
So, all this time I printed out and was using page 206 when the page required was 205 and I didn't see the alternate. I was working with the Enhanced instead of the standard because when I was using Acrobat  Reader and i was at that time first looking in the index then used a search in reverse to find the configuration page for the register corresponding to CCP1CON. So I found the wrong page first, printed it out and to this moment assumed it had all the possible info for CCPxCON.

I changed CCP1CON to 0b00001100 and bingo!
« Last Edit: June 24, 2015, 02:37:47 am by SuzyC »
 

Offline lewis

  • Frequent Contributor
  • **
  • Posts: 697
  • Country: gb
  • Nullius in verba
Re: Please Help Me Understand PWM Operation Diagram (PIC18F25K22)
« Reply #24 on: June 24, 2015, 03:38:55 am »
I changed CCP1CON to 0b00001100 and bingo!

* cough cough *
I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf