View previous topic :: View next topic |
Author |
Message |
ferkar
Joined: 14 Jul 2007 Posts: 38
|
pwm 10 bit? |
Posted: Wed Apr 02, 2008 4:25 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 4:29 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 4:34 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 5:15 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 7:31 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 7:40 am |
|
|
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
|
set_pwm_duty('x'L) |
Posted: Wed Apr 02, 2008 7:52 am |
|
|
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
|
sorry |
Posted: Wed Apr 02, 2008 8:11 am |
|
|
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
|
|
Posted: Wed Apr 02, 2008 8:44 am |
|
|
For a variable, read what I say in the last line of my original posting...
Best Wishes |
|
|
Matro Guest
|
|
Posted: Wed Apr 02, 2008 8:44 am |
|
|
If you use a variable to set the pwm value, just declare it as int16.
Matro. |
|
|
ferkar
Joined: 14 Jul 2007 Posts: 38
|
pwm 10bit code body |
Posted: Thu Apr 03, 2008 4:30 am |
|
|
ı 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
|
|
Posted: Thu Apr 03, 2008 5:24 am |
|
|
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 |
|
|
|