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

I got question about the PWM

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



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

I got question about the PWM
PostPosted: Sat Jun 18, 2011 11:02 am     Reply with quote

I copy few example from other forum
I'm newbie, forgive me if i ask stupid question =.=

1.
Quote:
Here is an example of the code the spreadsheet produces for a PWM frequency of 16KHz on a midrange PIC running at 8MHz.
Code:
setup_timer_2(T2_DIV_1, 124, 1 );
setup_ccp1( CCP_PWM );
set_pwm1_duty( 250L ); // square wave output - 50% duty


2.
Quote:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

main()
{
output_low(PIN_C1); // Set CCP2 output low
output_low(PIN_C2); // Set CCP1 output low

setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM

setup_timer_2(T2_DIV_BY_16, 124, 1); // 500 Hz
set_pwm1_duty(31); // 25% duty cycle on pin C2
set_pwm2_duty(62); // 50% duty cycle on pin C1

while(1); // Prevent PIC from going to sleep (Important !)
}



3.
Quote:
setup_timer_2(t2_div_by_16,0xff,16); The timing works like this. 1/clock frequency gives you your period (4/4000000=1us) . Then, I have a pre-scale of 16 (.000001*16=16us) Then I have a post-scale of 16 (.000016*16=256us). The period is 0xff or 255. This is how many times the timer can loop before an overflow (.000256*255=65.28ms or 65,280us). 1/.06528=15.32Hz. This is the slowest we can make this timer.

set_pwm1_duty(512); The duty is the ratio of high to low. A duty of 1 is about 1% high and 99% low. This makes an LED very dim. 512 is 50% high and 50% low. It’s the equivalent of this:
output_high(LED); delay_ms(8); output_low(LED); delay_ms(8); //loop this forever
A duty cycle of 1023 is 1% high and 99% low. The LED is very bright!

set_timer_2(0); This is to start the timer, and where to start it.



4.
Quote:
#use delay(clock=20000000)

void main(void)
{
setup_timer_2(T2_DIV_BY_1,24,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(20); //80%
set_pwm2_duty(20); //80%



i confuse
2 and 4 the duty cycle

Quote:
setup_timer_2(T2_DIV_BY_16, 124, 1);
set_pwm1_duty(31); 124*25% = 31
set_pwm2_duty(62); 124*50% = 62



Quote:
setup_timer_2(T2_DIV_BY_1,24,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(20); //80%
set_pwm2_duty(20); //80%




compare with 1 and 3




Quote:
setup_timer_2(T2_DIV_1, 124, 1 );
setup_ccp1( CCP_PWM );
set_pwm1_duty( 250L ); 50%?? why not 124*50%=62??


Quote:

setup_timer_2(t2_div_by_16,0xff,16);
set_pwm1_duty(512); 50%?? why not 0xff = 255, 255*50% = 128??




I doing a project about the three axis accelerometer to control the servo motor, so i need know how to use the PWm or timer.....confusing
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 18, 2011 12:07 pm     Reply with quote

Here are some threads that discuss 10-bit PWM vs. 8-bit:
http://www.ccsinfo.com/forum/viewtopic.php?t=26751
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
http://www.ccsinfo.com/forum/viewtopic.php?t=22119

If that's not enough, there are many more.
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 18, 2011 1:21 pm     Reply with quote

PCM programmer wrote:
Here are some threads that discuss 10-bit PWM vs. 8-bit:
http://www.ccsinfo.com/forum/viewtopic.php?t=26751
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
http://www.ccsinfo.com/forum/viewtopic.php?t=22119

If that's not enough, there are many more.


o??
just now 1,2,3,4 have some different is because of the bits??
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 18, 2011 1:26 pm     Reply with quote

Here is a Microchip tutorial on PWM which discusses 8-bit and 10-bit mode:

An539 - Frequency and Resolution Options for PWM Outputs
http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 18, 2011 1:29 pm     Reply with quote

PCM programmer wrote:
Here are some threads that discuss 10-bit PWM vs. 8-bit:
http://www.ccsinfo.com/forum/viewtopic.php?t=26751
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
http://www.ccsinfo.com/forum/viewtopic.php?t=22119

If that's not enough, there are many more.


http://www.ccsinfo.com/forum/viewtopic.php?t=26751
Quote:
So if you use the same parameter value in both 8-bit and 10-bit mode,
then the perceived effect will be that the duty cycle is divided by 4
when you use a 16-bit parameter. If that's controlling a motor, the
maximum speed will be only 1/4 of what it was. (Assuming a 1:1
relationship between duty cycle and speed).


question1
mean that the case 2 and 4 is 8bits, case 1 and 3 is 10bits?

question2
Quote:
setup_timer_2(T2_DIV_1, 124, 1 );
setup_ccp1( CCP_PWM );
set_pwm1_duty( 250L ); // square wave output - 50% duty

124*4 = 496
496*50% = 248
why it is 250L?
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 18, 2011 2:29 pm     Reply with quote

PCM programmer wrote:
Here is a Microchip tutorial on PWM which discusses 8-bit and 10-bit mode:

An539 - Frequency and Resolution Options for PWM Outputs
http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf


ok i learned about it thx


in case i want to control a servo motor
period = 20ms, pull up 2ms, pulldown 18ms, how to write the code?
..........2ms
...........___........18ms..............___............................
_____|......|______________|......|_____________
..........<------------20ms-------->
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 18, 2011 3:02 pm     Reply with quote

There are many, many posts on this forum with servo code.
Use the forum's search page to find them:
http://www.ccsinfo.com/forum/search.php

Here are a few of the servo posts:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22
http://www.ccsinfo.com/forum/viewtopic.php?t=37807&start=3
http://www.ccsinfo.com/forum/viewtopic.php?t=44328&start=3
There are many more.
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 18, 2011 9:40 pm     Reply with quote

PCM programmer wrote:
There are many, many posts on this forum with servo code.
Use the forum's search page to find them:
http://www.ccsinfo.com/forum/search.php

Here are a few of the servo posts:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22
http://www.ccsinfo.com/forum/viewtopic.php?t=37807&start=3
http://www.ccsinfo.com/forum/viewtopic.php?t=44328&start=3
There are many more.


wohooo..thx
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sun Jun 19, 2011 1:45 am     Reply with quote

setup_adc(ADC_CLOCK_DIV_32);

for example crystal= 20Mhz

TAD(time for Analog to Digital???) = 1/20Mhz * 32 = 0.16 microseconds
Requires min. 12 TAD for 10 bit...Why ned 12 in 10bit?? how about 8bit??
Min. conversion time approx. 2 microsecond?? Mean 12 * 0.16microseconds = 1.92 = 2microseconds? convert A/D need 2 microseconds? only in 10 bit? how about 8 bit?

why we want to put DIV_32?
can we put DIV_1,2,3,4,........100????
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Jun 19, 2011 2:51 am     Reply with quote

Data sheet, data sheet, data sheet.

The ADC has a maximum clock rate _it_ can run at. Depends on the chip, but typically 500KHz for the older chips up to a couple of MHz for the later chips. The division brings the clock rate from your CPU down to a rate the ADC can use.

Division ratios allowed _are in the data sheet_ typically limited range of binary values only, so perhaps 1,2,4,8,16,32,64 - again _read the data sheet_. For your chip, only /2, /8, and /32 are available.

The ADC performs a 'successive approximation' conversion. Effectively 'is the value greater than half range', then select this half of the range, and ask if the value is in the top/bottom half of this range, then repeat again and again. One test for each bit in the output value. Two more clock cycles, one to disconnect the input source, and set everything up, and one to reconnect at the end. Hence 12 clocks for a 10bit conversion. Generally, the converter performs a complete (10bit) conversion, even if you are only reading 8bits of the result. Read up on successive approximation ADC's.

The 'min conversion time' being referred to in the data sheet, is _per bit_. It is the minimum value for Tad - so for the example you give, 2uSec per bit = 500KHz. However it is _not_ actually 2uSec for your chip.
From the 16F877 data sheet:
"For correct A/D conversions, the A/D conversion clock (Tad) must be selected to ensure a minimum Tad time of 1.6uSec.". 625000Hz

Now step to 20MHz. /8, gives 20000000/8 = 2500000Hz. Too fast.
20000000/32 = 625000Hz. OK

Tad is then 1.6uSec, and a conversion takes 19.2uSec

A repeated conversion takes longer than this. First, the capacitor used for the conversion needs to recharge - Tacq. 19.72uSec for your chip, and also the value needs to be read out of the registers. Also on this chip there is a 2*Tad delay between completing the reading, and the input capacitor being reconnected. So the fastest 'repeated' conversion, would probably be about 19.2+19.72+3.2uSec per conversion. Just over 42uSec/conversion. About 23500 conversions/second. Trying to get better than perhaps 20000conversions per second on this chip would probably be a waste of effort - latter chips have faster ADC's...

You need to look at some of the Microchip application notes about the ADC, and read the data sheet.

Best Wishes
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sun Jun 19, 2011 3:20 am     Reply with quote

ok thx bro =)
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