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 CCS Technical Support

Incorrect ADC conversion with 30F5015

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



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

Incorrect ADC conversion with 30F5015
PostPosted: Thu Feb 06, 2014 4:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 06, 2014 5:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 06, 2014 5:57 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 06, 2014 6:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 06, 2014 8:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 06, 2014 10:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 07, 2014 4:59 am     Reply with quote

I fixed it! Thanks!
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