View previous topic :: View next topic |
Author |
Message |
Daniel123 Guest
|
Need help with set_pwm1_duty() |
Posted: Tue Aug 11, 2009 2:14 pm |
|
|
I'm having problems with the function set_pwm1_duty(). When I set set_pwm1_duty(0) the output should be 0V, but it's not. The configuration I did for timer2 and pwm is below.
Code: | setup_timer_2(T2_DIV_BY_1,254,1);
setup_ccp1(ccp_pwm); |
Could someone help me?
Thank you. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Aug 11, 2009 2:39 pm |
|
|
That looks okay, Now how about a working test program? |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 11, 2009 2:46 pm |
|
|
Also remember that the output will not ever be really '0v', if there is any load on the pin. Vol, is normally specified as 0.6v, into something like a 8mA load. Not '0v'. To get a genuine 0v, requires a buffer capable of driving to 0v.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 11, 2009 4:36 pm |
|
|
re getting ZERO volts
There is a trick way to get a neat 10 bit D/A virtual DC source form the PWM output , using a rail to rail op amp( but with +/- 5 v rails) and configuring it as a sallen key second order LOW pass filter - with the PWM output driving the filter input.
I routinely get about 20 mV above ground with pwm duty cycle of 0
on 16f886/7 and the series resistors of the SK filter set to 100K
and the caps at .1uf ( after settling time that is ).
Instead of unity gain setup - to get a span of zero to +5 v, use a feedback network of 100K from output to (-) input and from (-) input, another 100k to the same +5v rail as the PIC. for best result linearity - I use 100K .1% for the latter two 100ks for the series input pair 1% ok.
Moral of story:
With light enuf load and some integration time
Very near zero IS doable
I have done it |
|
|
Daniel123 Guest
|
set_pwm1_duty() |
Posted: Tue Aug 11, 2009 5:43 pm |
|
|
mkuang,
The program I'm using is this:
Code: |
void main()
{
set_tris_a(0b00001011);
set_tris_c(0x00);
set_tris_b(0x00);
SETUP_ADC(ADC_CLOCK_INTERNAL);
SETUP_ADC_PORTS(AN0_AN1_AN3);
setup_timer_2(T2_DIV_BY_1,254,1);
setup_ccp1(ccp_pwm);
while(true)
{
set_adc_channel(1);
delay_us(10);
valor2=read_adc();
if(valor2<=218)
{
set_adc_channel(0);
delay_us(10);
valor=read_adc();
set_pwm1_duty(valor);
}
else
{
valor=218;
set_pwm1_duty(valor);
}
}
} |
This program is for a DC motor control. I would like to control the speed and don't let the voltage exceed 13V.
I'm reading from channel 1 the voltage sensor (resistive divider), if the voltage is under 218 (218=4.333V=13V) I can read the channel 0 (potentiometer) and increase the speed.
It's working, but the motor speed is never zero, this is my problem.
Thank you, Ttelmah and asmboy for the tips!! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 11, 2009 5:53 pm |
|
|
1- Is valor an unsigned INT16 ?
Since you look like you have 10 bit resolution here
that would nicely match the 1023 bits you can READ with the ADC - if it is a PIC with a 10 bit adc
2- Post a link to PDF of your motor modulator circuit schematic ?
A HARDWARE problem with your DC modulation approach can
explain that too.
3- What PIC r U using ( tris_a trouble ?) ? |
|
|
Daniel123 Guest
|
set_pwm1_duty() |
Posted: Tue Aug 11, 2009 8:00 pm |
|
|
i´m using PIC16F870 (AD 10bits ; PWM 10 bits:
#include<16f870.h>
#device ADC=8
#use delay(clock=20000000)
#fuses hs, nowdt, protect, put, brownout
valor is an unsigned int8, and read_adc() is returning an 8 bit value.
I decided to use 8 bits.I could try to use 10 bits of AD, but i don´t know if it could solve the problem.
As i´m using an unsigned int8 and pwm has 10 bits, the two less significant bits are zero. I hope i´m not doing anything wrong . |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 11, 2009 8:31 pm |
|
|
how about your HARDWARE schematic that the PWM is suppsosed to control ?
i have a deep suspicion that IT is linked to your problem
try using the 10 bit capability your AtoD has
its way better
and also using an unsigned int16 var to control your PWM -
until i see the schematic of the motor PWM DC modulator -
i think i can't help much more
you really ought to show the motor driver SCHEMATIC HARDWARE |
|
|
Daniel123 Guest
|
set_pwm1_duty() |
Posted: Tue Aug 11, 2009 9:26 pm |
|
|
How can I show you the hardware schematic?
I can send it to your e-mail. Here is my e-mail and msn, you can add me:
[email protected]
Thanks! |
|
|
Daniel123 Guest
|
|
Posted: Thu Aug 13, 2009 11:21 am |
|
|
Asmboy, thank you for your attention!
we discovered what was happening!!Now the DC motor is working well.
Daniel |
|
|
|