|
|
View previous topic :: View next topic |
Author |
Message |
tapasparua
Joined: 13 Feb 2012 Posts: 1
|
Need help in pwm generation using CCS C |
Posted: Mon Feb 13, 2012 5:54 am |
|
|
I am trying to generate 2 pwm using ccp1 & ccp2 in pic16f877a. I am using the following instructions.
Code: |
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 5);
set_pwm1_duty(31);
|
Q1. I am getting pwm in both ccp1 & ccp2. in the 2nd instruction,i.e.,
setup_timer_2(T2_DIV_BY_1, 255, 5);
i.e., setup_timer_2(T2_DIV_BY_a, offset, postscaler);
T2_DIV is prescaler, 255 is offset, 5 is postscaler.
prescaler & offset have role in calculating period (i.e., pwm freq). But I found that postscaler has got no role. I have simulated this in Proteus. There if I change the value of postscaler in 1-16 (this range is given in ccs c manual), the freq of pwm did not change. Why?
Q2. Duty cycle is 10bit (found in datasheet). That means in the 3rd instruction i.e., set_pwm1_duty(x); x can be varied from 0 to 1023, where 1023 means almost 100% duty cycle, and x=(duty cycle in %) x 1023. But in some documents it is written x=0 to offset in instruction 2. Which one is right? While simulating in Proteus, I have seen x=(duty cycle in %) x 1023. This formula does not work. x=(duty cycle in %) x offset works. That means I can not have 10bit resolution if I use ccs c.
Can anybody please help me to solve this? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Mon Feb 13, 2012 6:20 am |
|
|
The postscaler has nothing whatsoever to do with the frequency generated. Hence does not affect the PWM. _READ THE CHIP DATA SHEET_.
It affects how often the timer generates an interrupt _only_.
The pwm on both outputs, is a Proteus bug.
Then, _search the forum, and read the manual_.
A Search on 'PWM 10bit', with 'search for all terms', will find the hundreds of threads explaining what you are seeing. Ore read the set_pwmx_duty section in the manual, and read the paragraph 'function'. Note what happens if an 8bit value is used with the function....
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|