View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
Incorrect ADC conversion with 30F5015 |
Posted: Thu Feb 06, 2014 4:24 am |
|
|
Greatings! I`m using 30F5015. So I have 10 ohms resistor between GRD and pin 15 (Vref-) and another 10 ohms resistor between Vcc and pin 16(Vref+). I have 100n and 10n on Vref+ as the datasheet says. I tested channels 2 and 3 (pins 13 and 12). When I have GRD (0V) on pin 13, for example, and 10 bit mode sellected the adc returns 5-7 as result. When I pass Vcc (5V) it returns 511. This is OK, but when I try another value - for example 2V, the adc returns 451! Which is wrong!
Where is the problem?!
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Feb 06, 2014 5:07 am |
|
|
Why is '511' OK?. This is a ten bit ADC, should return 1023.
Now you don't tell us your clock rate, but 'KISS'. Just try something like:
Code: |
#include <30F5015.h>
#device ADC=10
//fuses, clock etc. here
//RS232 etc.
void main(void)
{
int16 value;
setup_adc(ADC_CLOCK_DIV_16| ADC_TAD_MUL_8);
//timings here will depend on your clock rate - read the data sheet
//and change as needed
setup_adc_ports(sAN2 | VREF_VREF);
//since you are using the Vref pins
set_adc_channel(2);
while (TRUE)
{
value=read_adc(); //don't need delay since Tad setup
printf("count = %ld\n\r",value);
delay_ms(500); //keep update reasonable
}
}
|
You should see 1023 when you get to 5v, not '511'. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Thu Feb 06, 2014 5:57 am |
|
|
Here are parts of my program:
Code: |
#include <30F5015.h>
#FUSES HS2_PLL16,NOWDT,NOPUT
#device ADC=10
#use delay(clock=80M)
//power_pwm_period = 800 (40us)
#INT_PWM1
void SetPvm()
{
set_motor_pwm_duty(1,1,nextpulse);
set_motor_pwm_duty(1,2,nextpulse);
set_motor_pwm_duty(1,3,nextpulse);
adcValue=read_adc();
}
void main()
{
setup_adc_ports(sAN2 | sAN3, VREF_VREF);
setup_adc(ADC_CLOCK | ADC_TAD_MUL_0);
set_adc_channel(2);
/* code ....*/
}
|
I don`t think I`m doing something different .... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Feb 06, 2014 6:06 am |
|
|
What is the minimum Tad?. What is ADC_CLOCK selecting?. You are trying to clock at least 4* faster than is legal..... |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Thu Feb 06, 2014 8:13 am |
|
|
I wasn`t sure about this settings, so I set the lowest possible and the compiler accepted it so I thought it`s OK.
As the datasheet says I need 2 Tad. What is the minimun setting for ADC_CLOCK?!
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Feb 06, 2014 10:23 am |
|
|
stoyanoff wrote: | I wasn`t sure about this settings, so I set the lowest possible and the compiler accepted it so I thought it`s OK.
As the datasheet says I need 2 Tad. What is the minimun setting for ADC_CLOCK?!
Thanks! |
That is not the 'lowest possible', it's the highest!... Remember these are divisions. You are trying to clock the ADC at 40MHz.
Look at 20.6 in the data sheet. Use ADC_CLOCK_DIV_4, which is OK at your clock rate.
The compiler does not check for things like this, since they differ for every chip. Hence the data sheet. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Fri Feb 07, 2014 4:59 am |
|
|
I fixed it! Thanks! |
|
|
|