View previous topic :: View next topic |
Author |
Message |
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
Problem CCP! |
Posted: Wed Mar 21, 2012 4:59 pm |
|
|
Hello Everybody!
I write this code so depending on the analog voltage on pin_a0 in 16f877a the two ccp pins lights two led. And from the code you can note that if the analog input voltage is 5+ the first ccp is +5 and second is 0.
My question is why I don't obtain 2.5 volt on each ccp when the analog voltage is 2.5 ????
Code: |
#include <16F877a.h>
#device ADC=8// ADC=x, Where x is the number of bits read_adc() should return
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)
#byte port_a = 5
#byte port_b = 6
#byte port_c = 7
#byte port_d = 8
#byte port_e = 9
void main()
{
int value; // Long integer type is enough for analog data of 10 bits.
int volt;
setup_adc_ports(ALL_ANALOG);
// Configures the analog to digital converter.
setup_adc(ADC_CLOCK_DIV_32); // Fosc/32 full speed
setup_ccp1(CCP_PWM); // configure CCP1 as a PWM
setup_ccp2(CCP_PWM);
set_tris_a(0xFF); // PORTA as input
set_tris_c(0x00); // PORTC<2> as PWM1 output
while(1)
{
set_adc_channel(0); //select chanel PA0
delay_us(10); // In general 10us is good for most applications.
volt = read_adc();
set_pwm1_duty(volt);
set_pwm2_duty(255-volt);
setup_timer_2(T2_DIV_BY_16,255,1);
delay_ms(50);
}
}
|
Thanks 4 reading ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 21, 2012 5:38 pm |
|
|
The CCP pins will output a digital rectangular waveform, not an analog
voltage. Are you converting this waveform to analog ? How ?
Are you expecting a 50% duty cycle waveform to light up the LED at
50% intensity ? It's not linear. You need to use a formula or a lookup
table to correct for it.
Also, you don't need to have the setup_timer_2() function inside the loop.
It should be placed above the loop, with the rest of your CCP setup code. |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Wed Mar 21, 2012 6:07 pm |
|
|
Thanks. Yes you are right for the timer2.
I know that ccp is not a analog voltage but I intend that 50% duty cycle of 5 volt is 2.5 volt (the average voltage) the voltage which you can measure with any dc voltmeter. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 21, 2012 6:23 pm |
|
|
It works on my voltmeter. With that test program, with a PWM duty value
of 127, I measure 2.50v with meter. I have it set on the 20v DC setting.
(Not the AC setting). It's a B&K 2706A meter.
I don't have any external circuits connected to Pin C1, which is the pin
that I'm measuring with the voltmeter. So the voltage is about 0 to 5v
peak to peak. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Mar 22, 2012 3:35 am |
|
|
Without an external integrator, the result you get, will depend totally on the meter you use.
Probably 80% of meters will give the 'average' voltage as PCM_programmer sees. These use the traditional 'dual slope integrating' ADC, and will therefore 'integrate' the waveform. However some won't.
Stuck it on my best meter Fluke true RMS, and it displays alternately 4.98v, and 0.002v, with the warning flag up on the display to say the waveform is AC. Switch it to 'mean' mode, and it gives 2.498v. Select 'peak', and it displays 4.9821v. etc. etc..
Add an integrator.
Best Wishes |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Thu Mar 22, 2012 10:20 am |
|
|
but my question is without an integrator and using this code do i obtain the desired velocity of a dc motor, i mean using duty (127) do i obtain the half speed of a the motor.. or i have to add an integrator in this case too.?!?!
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Mar 22, 2012 10:58 am |
|
|
No, you won't get half speed. The response will be non linear.
What happens will depend totally on the nature of your drive to the motor, and the motor itself.
Your PWM frequency is probably too low for comfort. Only:
20000000/(4*16*256)Hz = 1220Hz
Typically something more like 5KHz, is typically used. There is a balancing act. Low frequencies > more noise, but less losses in the switcher, and less problems from the motor inductance. Individual motors may also have resonant frequencies that need to be avoided.
Anything under perhaps 2KHz, is likely to be quite irritating.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Mar 22, 2012 11:58 am |
|
|
Quote: |
but my question is without an integrator and using this code do i obtain the desired velocity of a dc motor, i mean using duty (127) do i obtain the half speed of a the motor.. or i have to add an integrator in this case too.?!?!
|
This is your first mention of a motor.
Depending on how you do your drive, the motor can perform the integration.
How, exactly, do you intend to drive the motor?
You've got a 20MHz clock. Without the prescaler you could get 0.1% resolution and a ~20kHz switching frequency.
20kHz switching should be inaudible.
Most modern switch mode power supplies work at way above 50kHz.
Mike |
|
|
|