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 on 16F877
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 24, 2007 2:05 pm     Reply with quote

Put an upper case 'L' after the 500. ie., make it be 500L.
jksor1234



Joined: 18 Jan 2007
Posts: 9

View user's profile Send private message

PostPosted: Wed Jan 24, 2007 3:18 pm     Reply with quote

What does the upper case L do?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 24, 2007 3:31 pm     Reply with quote

It tells the compiler that the number is a 'long' integer (greater than
8-bits). When the compiler sees a 'long' value as the duty cycle
parameter, it switches to use 10-bit PWM mode instead of 8-bit.
Here are several threads that discuss this:
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
http://www.ccsinfo.com/forum/viewtopic.php?t=9927
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 10, 2007 10:23 am     Reply with quote

PWMWizard tells me to write :

set_pwm1_duty(500l);

for the above example...

but from what you said, I should call

set_pwm1_duty(124);

I have posted a new thread to this effect, could you (PCM) have a look at it ?
jksor1234



Joined: 18 Jan 2007
Posts: 9

View user's profile Send private message

PostPosted: Mon Feb 12, 2007 8:01 am     Reply with quote

The L is needed to tell the compiler you want a 10-bit PWM... otherwise it will assume 8-bit.

From the equations I posted earlier, a 50% duty cycle needs a value of 125... set_pwm1_duty(125);

125 Dec = 0111 1101 Binary (an 8-bit number)
500 Dec = 01 1111 0100 Binary (a 10-bit number)

The 125 is just shifted left two, to make 500. Both values would set the PWM to 50%, except 500 would give the PWM 10-bits of accuracy and 125 would only give 8-bits.
Aquila



Joined: 25 Feb 2007
Posts: 1
Location: Turkiye

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

PostPosted: Mon Mar 19, 2007 1:47 pm     Reply with quote

Hi I'm a student from mechanical engineering department. I don't know a lot of things about electronics. Could you tell me how can I build control circuit for two DC motor. I have to use 16f877; Motors are 12 V 550 rpm and one of them use 9-10 Ampere in start and full load. Must I use transistor or relay in this situation? I can't find any useful links:sad:

I'm sorry that I took your precious time



thanks...
Ttelmah
Guest







PostPosted: Mon Mar 19, 2007 3:39 pm     Reply with quote

What are you going to 'control'?. Just on/off, or speed?. Forward/reverse?. Braking?. The 'odds' are that you want all of these. In which case, the 'answer' is an 'H-bridge' drive.
Most of the robotics sites, will offer some information about these. Remember that the trap diodes are essential if you are not going to destroy electronics when the drives go 'off'. Look at the 'robotpower' site, for some off the shelf drives, in particular, the Scorpion XL, is commonly used in a lot of robotic designs this sort of size.

Best Wishes
disenteako
Guest







PWM
PostPosted: Mon Jun 23, 2008 9:00 am     Reply with quote

jksor1234 wrote:
The L is needed to tell the compiler you want a 10-bit PWM... otherwise it will assume 8-bit.

From the equations I posted earlier, a 50% duty cycle needs a value of 125... set_pwm1_duty(125);

125 Dec = 0111 1101 Binary (an 8-bit number)
500 Dec = 01 1111 0100 Binary (a 10-bit number)

The 125 is just shifted left two, to make 500. Both values would set the PWM to 50%, except 500 would give the PWM 10-bits of accuracy and 125 would only give 8-bits.


If i were to use a long for the pwm duty cycle, should i also now set the middle value of the

setup_timer_2(T2_DIV_BY_1, value, 1);

to 1000. Or leave it as it is with 250 and still be able to get a 10 bit resolution? Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 23, 2008 11:41 am     Reply with quote

Quote:

Should i also now set the middle value of setup_timer_2() to 1000 ?

It can't be larger than 255. The hardware register is only 8 bits.


Quote:

Or leave it as it is with 250 and still be able to get a 10 bit resolution?

Keep it at 250.


Quote:
[will I] still be able to get 10-bit resolution?

Yes.

Read the links in this thread that explain 10-bit mode.
http://www.ccsinfo.com/forum/viewtopic.php?t=17729&start=12
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Wed Feb 22, 2012 3:56 pm     Reply with quote

I am using PIC16F848A and I want to get 25kHz through PWM. How can I do that?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 22, 2012 5:17 pm     Reply with quote

1. There is no PIC with that part number. What's the real part number ?

2. What is the oscillator frequency of your PIC ?
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 23, 2012 2:19 am     Reply with quote

I am sorry. I am using PIC16F648A and my oscillator frequency is 4MHz. I would like to generate a 25 kHz PWM signal.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Feb 23, 2012 2:43 am     Reply with quote

Data sheet. Data sheet. Data sheet.
Table 9-3 (just divide the frequencies by 5, since you are clocking at 4MHz, and the values in this are for 20MHz), and paragraph 9.3.1.
Yes you can get 25Khz. PR2=39. You will have a PWM range of 0 to 159. 7.3bits.

Best Wishes
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 23, 2012 3:29 am     Reply with quote

So I should write like that, right? Please refer to the bold part.

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

main()
{
output_low(PIN_B3); // Set CCP1 output low

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

setup_timer_2(T2_DIV_BY_5, 39, 1); // 25kHz
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 !)
}

Thanks for your help. Sorry, I am the beginner for PIC user.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Feb 23, 2012 4:19 am     Reply with quote

No. 'T2_DIV_BY_5'. no such setting. Would give 5KHz anyway.
The point about the division by five I mention, is when _looking at the frequencies given by table 9-3_, the frequencies _you_ will get, will be 1/5 of the shown values, because the table is calculated for a 20MHz master clock.
T2_DIV_BY_1

Your 50%, and 25% values are a fraction high. 0 to 159. What is 1/2 and 1/4 of this?.

Also remember as has already been said in this thread several times to use the full resolution of the PWM generate, you _must_ have 'L' on the numbers if they are less than 255.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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