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 support@ccsinfo.com

pwm 10 bit?

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



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

pwm 10 bit?
PostPosted: Wed Apr 02, 2008 4:25 am     Reply with quote

hi,

i would like to get 10bit pwm output . to see if the pwm 10 bit or not, i use the code like this:

set_pwm1_duty(250);//tmr2 4,255,1

if the code is 10 bit i should get 25% output
if the code is 8 bit i should get 100%output(nearly)

when i connect a scope ı see 100% output. so the pwm output is 8 bit.

my question is :

how could i generate 10 bit pwm output?
Matro
Guest







PostPosted: Wed Apr 02, 2008 4:29 am     Reply with quote

The pwm duty cycle depends on your timer2 configuration.
And if you want to use 10 bit pwm value, you shall had a "L" behind the value.

Please post your timer2 setup for further help.

Matro.
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 4:34 am     Reply with quote

A search here, would have found the answer. Been posted many dozens of times...
The key is in the manual.
Note what it says about 'if an 8bit value is used'.
To work in 10bit mode, you have to ensure that the value fed to the set_pwm_duty function, is a 16bit value.
So:
Code:

   set_pwm1_duty(250L);//tmr2 4,255,1

Note the letter 'L', which says to the compiler "treat this as a 'long' (16bit)" value.
Similarly, when driving using a variable, if you use an 'int', the unit will run in 8bit mode, but if you use an int16, it'll switch to 10bit mode.

Best Wishes
Matro
Guest







PostPosted: Wed Apr 02, 2008 5:15 am     Reply with quote

Even "set_pwm1_duty(250L);" will only give a 25% duty cycle if your timer 2 is setup like that:
setup_timer_2(T2_DIV_BY_1,124,XXX);

Matro.
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 7:31 am     Reply with quote

No Matro.

The maximum value usable in the set_pwm_duty, is 4* the timer value+1.
As originally shown, his timer was set to the maximum (255, resets at 256), so 250L, will give 250/1024. 24.4%. Near enough to 25%, unless he wants great accuracy.

With the timer setting you have now posted, 250L, will give 250/((124+1)*4) = 50%. Not even close.

He had posted his timer setups in the original post.

Best Wishes
Matro
Guest







PostPosted: Wed Apr 02, 2008 7:40 am     Reply with quote

My mistake. I did the calculation for 250L =50%
Correct parameters are :
setup_timer_2(T2_DIV_BY_1,249,XXX);

Matro.
ferkar



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

set_pwm_duty('x'L)
PostPosted: Wed Apr 02, 2008 7:52 am     Reply with quote

thanks for the solutions and informations but i would like to use duty values as changing. so if i could use set_pwm1_duty(xL) probably generates an error.

in one of my previous code about pwm there is such an expression at the end of the duty values,that is 'L'
best wishes...
Ferhat
ferkar



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

sorry
PostPosted: Wed Apr 02, 2008 8:11 am     Reply with quote

in one of my previous code about pwm there is no such an expression at the end of the duty values,that is 'L'

but it worked.
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 8:44 am     Reply with quote

For a variable, read what I say in the last line of my original posting...

Best Wishes
Matro
Guest







PostPosted: Wed Apr 02, 2008 8:44 am     Reply with quote

If you use a variable to set the pwm value, just declare it as int16.

Matro.
ferkar



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

pwm 10bit code body
PostPosted: Thu Apr 03, 2008 4:30 am     Reply with quote

ı tried what you suggested but there should be wrong with my code....

maybe timer interrupt or function calls or something like that...

is it necessary anything else?

progarm body is:

////
////

timer0_interrupt();
read_adc();
find_errors();
test_limits();
drive_pwm();///////////////16 bit duty cycle value
show_lcd(); /////////////// show the datas on LCD
Ttelmah
Guest







PostPosted: Thu Apr 03, 2008 5:24 am     Reply with quote

Impossible to tell without knowing more.
However, as a comment, there should not be anything at all to do with 'interrupts' in the main program body.
What is 'timer0_interrupt'?
You don't say what your chip is.
Look at some of PCM programmers posts in particular, for how to isolate a problem. I'd suggest that something in the order of a 20line program, just setting the fuses, initialising the I/O ports, and setting the PWM value, should be your target, and will then give you the ability to actually see how it works. Similar programs testing the ADC, and performing the LCD I/O, can then be got working on their own, and finally combined to make the whole.

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
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