Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Generate 200KHz PWM from PIC MCU

Status
Not open for further replies.

sairfan1

Full Member level 1
Joined
Jun 12, 2010
Messages
97
Helped
4
Reputation
8
Reaction score
7
Trophy points
1,288
Location
Regina, Canada
Activity points
2,373
Hi,

i want to generate 200KHz PWM from 16F877A or 16F887, kindly advise how is it possible, which crystal i have to use, no very high accuracy is required.

thanking you.
 

20 MHz crystal gives 5 MHz timer clock, duty cycle of 200 kHz PWM can be set iin 4% steps.
 

thanks for reply, can you please explain how can i calculate initial values for PWN registers,
 

calculate the value which need to load into PR2 and adjust the duty cycle by:

duty cycle=(value of PR2)x(% you need)

and remember to set the CCP1CON to pwm mode and then set the t2con register to on timer 2.

it is available in datasheet,have a try=)
 

thanks for help, as i'm new to understand PWM, i don't know how to calculate downwards to get 200KHz frequency, for example in datasheet it is giver how to calculate PWM period, and resolution and calculating values for timer2 register, but i'm unable to relate it with frequency i require, where to start from
 

As the formula f=1/T,where T is the period...Tosc is the oscillator period for example u using 20Mhz crystal oscillator,the period will be 1/20M..
 
20 MHz isn't the timer clock, see post #2. Everything is explained pretty detailed in the processor data sheets.
 

Sorry,my mistake,Tosc should be 1/5M,thank for correction,
 

thanks, its the breakthrough what i was looking for, if crystal used is 20 MHz, then why we calculate frequency f = 1/5MHz
 

By default it a divided by 4 internally,whatever oscillator u used,just divide by 4 that's all(if prescaler is 1:1) prescaler rate is to slow the frequency...
 
step to create pwm in pic 16f877a :

assuming Fosc=20MHZ

1) calculate required period register value (between 0 to 255) using PR2= ( Fosc / ( 4*Fpwm*TMR2PS ) -1 ) this formula .

2)calculate dutycycle value which is 10 bit so value varies from 0 to 1023 using this formula pwm duty cycle = ( Fosc*duty cycle ) / ( Fpwm*TMR2PS )
assign 8 msb bit to CCPR1L and 2 lsb bit to CCP1CON<5,4> register .

3)select pwm mode in CCP1CON register

4)set timer2 prescale in T2CON register . then turn on timer2 by setting TMRON=1;

so if you want higher resolution use higher oscillating frequency , reduce timer2 prescale .

here sample 200khz pwm
 

Attachments

  • 200khz.rar
    452 bytes · Views: 98
thanks alot for code example, i want to discuss formula for better understanding,
Fosc = Oscillator clock
Fpwm = PWM frequency in my case its 200KHz, (200,000)
pwm duty cycle = ( Fosc*duty cycle ) / ( Fpwm*TMR2PS ) how did you know "duty cycle"

(if i further explain my question how do we calculate period and duty cycle if required PWM is 200KHz)
 
Last edited:

for the duty cycle,this is the formula mentioned in datasheet:

PWM Duty Cycle =(CCPR1L:CCP1CON<5:4>) •TOSC • (TMR2 Prescale Value)

as i said,you can treat this formula as this if you don't understand this:

value need to write into CCPR1L= PR2 x (duty cycle)

for example PR2 = 100, if you want duty cycle to be 50%,

CCPR1L= 50.
 
pwm duty cycle = ( Fosc*duty cycle ) / ( Fpwm*TMR2PS ) how did you know "duty cycle"

(if i further explain my question how do we calculate period and duty cycle if required PWM is 200KHz)

Hi , we have to assume at starting any value (between 0 to 1) and if you want to increment it do it in code through a conditional statement by example duty cycle increments when a key is pressed .

PR2 is the period register .If you use the formula which i posted in previous post you will get a value between 0 to 255 . If you get value greater than 255 then increase timer 2 available (refer datasheet) prescale .

In pic 16f877a pwm is 10 bit resolution . so we have to set this 10 bit value into duty cycle register which how i said it in previous post .
 
Hi sairfan,
Look the datasheet deeply, there is explanation like to generate the 208.3kHz in
page:68,
1. run the pic16f877a in 20MHz speed(use only HS mode see page:145, in other modes it is limited to 4MHz and 200KHz)
2. set duty cycle as you required, say 50%.
3. set Timer prescalar at 1:1
4. set the PR2 = 0x17h, then the maximum resolution would be 5.5, if you are testing with oscilloscope then change the values of PR2 and check it with oscilloscope then you will get the 200KHz pulses.
 

Attachments

  • 200khz.png
    200khz.png
    8.3 KB · Views: 108
thank you very much, chippevijaya, gobi1990,khangzing, i understand calculations

if i'm looking for 200KHz 1/200000 = 0.000005us is PWM Period that we want to obtain by configuring Timer2 and PWM registers, this is our starting point, next step are as you people stated above to obtain this PWM period we calculate, value for PR2 register, duty cycle for exapmle 50% or what ever we want, if pre-scalar 1:1 Tosc = 1/(20Mhz/4)
These are the major calculation that should be done, what ever left is to properly initialize Timer2 registers and configure PWM, after setting up we should use oscilloscope to fine tune the obtained frequency, that's it.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top