• AVR Freaks

AnsweredHot!PWM with PIC16F15323

Author
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
2018/02/19 07:44:27 (permalink)
0

PWM with PIC16F15323

Hi,
 
I started using 8-bit PIC's after using 16-bit until now,
And I can't seem to use the PWM in my pic16f15323(and Pickit3),
 
This is the code I wrote according to paragraph 29.1.9, page 314 in the datasheet:
 
#include <xc.h>
 

void main()
{
    ANSELCbits.ANSC1 = 0;
    TRISCbits.TRISC1 = 1; // temporarily input
    PR2 = 199; //Period=25us, Prescaler=1:1, Tosc=1.25e-7 (Fosc=32Mhz)
    PWM3DCH = 0;
    PWM3DCL = 0;
    PIR4bits.TMR2IF = 0;
    //PIE4bits.TMR2IE = 1; // it's not written in the datasheet, but I tried with it and without
    T2CONbits.CKPS = 0; // 1:1 Prescaler
    T2CONbits.ON = 1;
    while (PIR4bits.TMR2IF == 0);
    TRISCbits.TRISC1 = 0; // output
    PMD3bits.PWM3MD = 1; // enable PWM3
    RC1PPS = 0x0b; // output => PWM3
    PWM3CONbits.EN = 1;
    
    while (1)
    {
        PWM3DCL = 190;
    }
}

 
the problem seen in debug is that it gets stuck in line 
while (PIR4bits.TMR2IF == 0);

I tried to enable TMR2IE before, no luck.
I tried changing this line with a delay, also no luck.
 
Thanks.
post edited by popperd - 2018/02/19 09:12:29
#1
davekw7x
Entropy++
  • Total Posts : 1852
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 08:38:39 (permalink) ☄ Helpfulby popperd 2018/02/21 08:01:14
+1 (1)
For starters...
Consider the Data Sheet: Look at T2CLKCON.  What is the clock source for Timer2?
 
Furthermore...
Since you are polling TMR2IF (and especially since you don't have an Interrupt Service Routine), your first impression was correct---don't enable the interrupt!!
 
Regards,

Dave
post edited by davekw7x - 2018/02/19 08:55:45

Sometimes I just can't help myself...
#2
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 08:56:50 (permalink)
0
Thanks davekw7x,
 
I deleted
//PIE4bits.TMR2IE = 1;

and added
T2CLKCONbits.CS = 0b0010; // clock is Fosc

now this is the whole code:
#include <xc.h>
 

void main()
{
    ANSELCbits.ANSC1 = 0;
    TRISCbits.TRISC1 = 1; // temporarily input
    PR2 = 199; //Period=25us, Prescaler=1:1, Tosc=1.25e-7 (Fosc=32Mhz)
    PWM3DCH = 0;
    PWM3DCL = 0;
    PIR4bits.TMR2IF = 0;
    T2CONbits.CKPS = 0; // 1:1 Prescaler
    T2CLKCONbits.CS = 0b0010; // clock is Fosc
    T2CONbits.ON = 1;
    while (PIR4bits.TMR2IF == 0);
    TRISCbits.TRISC1 = 0; // output
    PMD3bits.PWM3MD = 1; // enable PWM3
    RC1PPS = 0x0b; // output => PWM3
    PWM3CONbits.EN = 1;
    
    while (1)
    {
        PWM3DCL = 190;
    }
}

Now it doesn't get stuck, but also nothing in the RC1 output pin.

Thanks.
post edited by popperd - 2018/02/19 09:15:04
#3
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 09:11:21 (permalink)
0
I mistakenly set TRISC1 instead of clearing it in the end, fixed it.
still output is 0v.
#4
davekw7x
Entropy++
  • Total Posts : 1852
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 10:36:17 (permalink) ☄ Helpfulby popperd 2018/02/21 08:00:59
+1 (1)
popperd

    T2CLKCONbits.CS = 0b0010; // clock is Fosc


You have a little more reading to do
 
DataSheet DS40001853: Box in Section 29.12
Note: PWM operation requires that the timer
          used as the PWM time base has the
          FOSC/4 clock source selected.

 
[Edit]
Just noticed this:
    PMD3bits.PWM3MD = 1; // enable PWM3

 
That disables the PWM3 module!
[/Edit]
 
 
Regards,
 
Dave
post edited by davekw7x - 2018/02/19 10:53:36

Sometimes I just can't help myself...
#5
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 11:48:52 (permalink)
0
Thanks Dave,
 
Indeed I missed these two things.
It's very frustrating that instead of just writing a note, Microchip can't write it as one of the stages of the PWM setup(section 29.1.9 in datashhet DS40001897A), as it seems that it's a must stage.
 
I'll check it tomorow,
 
Thanks a lot.
#6
davekw7x
Entropy++
  • Total Posts : 1852
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 12:43:26 (permalink) ☼ Best Answerby popperd 2018/02/21 08:00:47
0
popperd
...
I'll check it tomorow,
...

You might want to check your duty cycle calculations.  You have put 0b10 into the upper two bits of DCL and zero into DCH.
 
By my calculations (and measurements on my PIC16F15355 breadboard) you will end up with a duty cycle ratio of 0.25%  That's a pulse width of 62.5 ns in a period of 25 us, assuming a 32 MHz system clock.  Is that what you really had in mind?
 
Regards,

Dave
 
post edited by davekw7x - 2018/02/19 13:37:01

Sometimes I just can't help myself...
#7
RISC
Super Member
  • Total Posts : 5392
  • Reward points : 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/19 13:30:24 (permalink) ☄ Helpfulby popperd 2018/02/21 08:00:40
+1 (1)
Hi,
The latest PIC16 devices have lots of registers to initialize.
I suggest you use MCC plugin, as it will make this process much more easier.
Regards
 
#8
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/21 07:57:14 (permalink)
0
Thanks RISC,
 
Eventually You helped me because the MCC saved me, and did the work.
Thanks a lot.
#9
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/02/21 07:59:59 (permalink)
0
Thanks a lot Dave,
For all the time you spent on me,
Indeed the calculations was wrong, I have no power to check why,
but I listened to RISC and used MCC plugin, the new calculations did the work.
 
Thanks again.
#10
davekw7x
Entropy++
  • Total Posts : 1852
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: PWM with PIC16F15323 2018/02/21 09:57:38 (permalink)
+2 (2)
popperd
...
Indeed the calculations was wrong, I have no power to check why,
...


Ref: Data PIC15313/23 Data Sheet, DS40001897A, Section 29.1.4

A 10-bit DC Register is defined as follows:

The eight most significant bits of DC_Reg are contained in PWMDCH<7:0>
The  two least significant bits of DC_Reg are contained in PWMDCL <7:6>

I visualize it (I think in terms of ASCII Graphics) like this:


                    DC_Reg<9:0>
                       |
                       ^
 _____________________/ \__________________
/                                           \
7    6    5    4    3    2    1    0  | 7    6
\______________ __________________/     \_ _/
               V                          V
               |                          |
          PWMDCH<7:0>                PWMDCL<7:6>


Calculations:

(1) From my visualization  : DC_Reg             = (PWMDCH << 2) | (PWMDCL >> 6)
(2) From Equation 29-3     : Duty Cycle Ratio   = DC_Reg / (4*(PR2+1))
(3) More familiarly        : Percent Duty Cycle = (100*DC_Reg) / (4*(PR2+1))

Note that DC_Reg value of 0 gives 0% duty cycle and DC_reg value greater than or equal to 4*(PR1+1) gives 100% duty cycle.

To calculate DCH and DCL from Percent Duty Cycle, start from equation 3 and work backwards.

See attached file for DCH and DCL values and duty cycle for given DC_Reg values with PR2 = 199, as in your example.

Also note that...
MCC (version 3.45.1, in MPLABX version 4.10) gives the wrong value for 50% duty cycle .  It's off by 1, as I have found to be the case for a lot (a whole lot) of MCC-supplied counter values for various target devices.  When I specify 50% duty cycle, MCC tells me PWMDC = 399, but actually 50% requires PWMDC = 400.  The value of 400 is consistent with the data sheet formula and has been verified by my direct measurement.  It's off by 1 for all other duty cycles (except for 0%) as well, but I cite 50% since that's easiest for me to measure precisely.

Maybe that's not important to for a particular example (actual duty cycle using MCC is 49.875% instead of 50%), but it bugs me.  And, in some applications it might be important.

Anyhow...
I have not been using MCC very much because I end up having to check the code against the data sheet anyhow.  That's not to say that I think others shouldn't use MCC, but I really feel that it's important for users to be familiar with peripheral characteristics and proper usage before diving in.

Otherwise, how the heck do you debug your program in case MCC doesn't get it quite right (or in some cases, not even close)?
 
Bottom line...
Chacun à son goût!


Regards,

Dave
post edited by davekw7x - 2018/02/21 10:21:52

Sometimes I just can't help myself...
#11
woolie
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2018/02/06 12:05:57
  • Location: Portland, OR
  • Status: offline
Re: PWM with PIC16F15323 2018/04/10 21:42:27 (permalink)
0
Would you mind posting what you ended up with?
#12
popperd
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2013/08/11 07:00:35
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2018/04/11 04:50:22 (permalink)
0
Of course,
 
My duty cycle calculations was wrong, as davekw7x said, I had no power to check why,
But I listened to RISC and used MCC plugin, which did the calculations for me, the new calculations did the work.
If you want to see the right calculations without MCC, check davekw7x's reply to this post from Wednesday, February 21, 2018 7:57 PM.
 
Good luck!
#13
Ravikiran_H
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2020/03/20 05:14:34
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2020/03/23 22:06:09 (permalink)
0
HI guys, tried using MCC plugin for configuring the PIC16F15323 IC ,i am able to generate PWM but i am getting frequency of the signal is 3 to 4 times less than the actual configured frequency.
if anybody know what may be the problem. please give me a solution.
thank you.
#14
Ravikiran_H
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2020/03/20 05:14:34
  • Location: 0
  • Status: offline
Re: PWM with PIC16F15323 2020/03/23 23:04:12 (permalink) ☄ Helpfulby abhishekbana 2020/03/27 03:07:28
+1 (1)
HI guys, tried using MCC plugin for configuring the PIC16F15323 IC ,i am able to generate PWM but i am getting frequency of the signal is 3 to 4 times less than the actual configured frequency.
if anybody know what may be the problem. please give me a solution.
thank you.
#15
Jump to:
© 2020 APG vNext Commercial Version 4.5