CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM frequency for Timer2

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ProgPath



Joined: 19 Sep 2020
Posts: 4

View user's profile Send private message

PWM frequency for Timer2
PostPosted: Sun Sep 20, 2020 2:43 am     Reply with quote

Hello,

It's about PIC18F46K22 running at 64 MHz.

I was trying to understand a code which says:

Code:
setup_timer_2(T2_DIV_BY_4, 140, 1); // PWM frequency is 28.369 kHz


I think that Timer2 is 8 bit. I don't understand the PWM frequency is 28.369 kHz.

CCS Compiler Reference Manual says:

Quote:
setup_timer_2 (mode, period, postscale)

mode may be one of: T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16. Period is a int 0-255 that determines when the clock value is reset. Postscale is a number 1-16 that determines how many timer overflows before an interrupt: (1 means once, 2 means twice, and so on)


Can you please help me to figure out how 28.369 kHz is calculated?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 20, 2020 3:49 am     Reply with quote

This is the PWM formula:
Code:

                      Crystal Frequency
PWM Freq  = -----------------------------------------
                  (PR2 + 1) * (Timer2 Prescaler) * 4
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sun Sep 20, 2020 3:58 am     Reply with quote

The key things to note are the '+1', and that it is Fosc/4, that feeds the
timer prescaler (this is the '*4 in the calculation).
Gives 28368.794 Hz for the example you post.
Rounded to the nearest Hz 28369.
ProgPath



Joined: 19 Sep 2020
Posts: 4

View user's profile Send private message

PostPosted: Sun Sep 20, 2020 5:28 pm     Reply with quote

Thanks very much!

PWM Freq = (64*10^6) / {(140+1)*(4)*4} = 28368.9 Hz

Code:

                      Crystal Frequency
PWM Freq  = -----------------------------------------
                  (PR2 + 1) * (Timer2 Prescaler) * 4


In the formula posted by @PCM programmer "PR2" is period, "Timer2 Prescalar" is prescalar, what's the purpose of "4"?

The reference manual says that for the Timer2 period is a int 0-255 that determines when the clock value is reset. I think that an interrupt is generated when clock value is reset. Please correct me if I'm wrong.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 20, 2020 7:17 pm     Reply with quote

ProgPath wrote:
what's the purpose of "4"?

Did you read Ttelmah's post ? He gave the reason for the '4'.

ProgPath wrote:
I think that an interrupt is generated when clock value is reset.

The mid-range PIC reference manual says:
Quote:

Timer2 always resets to zero when it equals PR2 and flag bit TMR2IF
always gets set at this time. By putting FFh into PR2, you will get an
interrupt on overflow at FFh.

Setting TMR2IF generates an interrupt. This occurs when it hits the
period count of 140 in your posted code.

See Question 6 on page 15:
http://ww1.microchip.com/downloads/en/DeviceDoc/31014a.pdf
ProgPath



Joined: 19 Sep 2020
Posts: 4

View user's profile Send private message

PostPosted: Sun Sep 20, 2020 8:43 pm     Reply with quote

PCM programmer wrote:
ProgPath wrote:
what's the purpose of "4"?

Did you read Ttelmah's post ? He gave the reason for the '4'.


Hello,

Yes, I read what Ttelmah wrote: "The key things to note are the '+1', and that it is Fosc/4, that feeds the timer prescaler (this is the '*4 in the calculation)."

So, "4" is in a way predefined 'prescalar' which divides the oscillator frequency by 4 by default. I was confused before.

Thanks very much!
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Sep 21, 2020 12:18 am     Reply with quote

Exactly.

It's actually the 'instruction clock'. Lot's of things in the PIC run off Fosc/4
rather than Fosc. This is the clock rate of a single processor instruction.
In fact using Fosc, is the unusual rate, Fosc/4 is the commonest rate.
This is the clock that is fed 'out' of the chip if you enable a clock output,
and if you look at the timer block diagrams (so for example Fig 13-1for
Timer2/4/6), you will see that the clock being fed in on the left is 'Fosc/4'.
Very Happy
ProgPath



Joined: 19 Sep 2020
Posts: 4

View user's profile Send private message

PostPosted: Mon Sep 21, 2020 1:55 am     Reply with quote

Thanks very much! Very nice of you guys.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group