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

Newb to CCS compiler A2D conversion ? 0-255 <> 0-5.00V

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








Newb to CCS compiler A2D conversion ? 0-255 <> 0-5.00V
PostPosted: Fri Dec 19, 2008 10:05 am     Reply with quote

Hi, Yep I am new to PCD from CCS.
Could someone lend a hand.....?

The following code is blinking some leds and reading AN0 port of my protoboard and then sending the reading to the computer via RS-232.

Everything work so far except for the conversion....
0-255 resolution to 0-5.00 volts output.
The pot fprint to the screen yields 0.00 - 0.04

Am I on the right track?

Code:

#define yellowled PIN_B4
#define redled PIN_B1
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Preprocessor directive that includes RS232 libraries

main (){
 unsigned int8 i, value, min, max;
 int pot;
 setup_adc_ports(AN0);             //USES DEFAULT VDD,VSS REFERENCE VOLTAGES
 setup_adc(ADC_CLOCK_INTERNAL);    //THIS SETS UP THE ADC mode,SPEED, & SAMPLE TIME
 set_adc_channel(0);
   
//   while (TRUE){
printf(" LEDS BLINKING\n\r");
      output_low (greenled);
      output_low (redled);
      output_low (yellowled);
      delay_ms(1000);
      output_high (greenled);
      output_high (redled);
      output_high (yellowled);
      delay_ms(1000);
     
      for (i=1;i<3;i++){
      output_low (greenled);
      delay_ms(100);
      output_low (yellowled);
      delay_ms(100);
      output_low (redled);
      delay_ms(100);
     
      output_high (greenled);
      delay_ms(100);
      output_high (yellowled);
      delay_ms(100);
      output_high (redled);
      delay_ms(100);
      }
     
//               }

      for(i=0;i<20;i++){
      output_low (greenled);
      output_low (redled);
      output_low (yellowled);
      delay_ms(50);
      output_high (greenled);
      output_high (redled);
      output_high (yellowled);
      delay_ms(50);   
      }
     
   printf(" A2D CONVERSION BEGINNING\n\r");
   delay_ms(2000);
   setup_adc(ADC_CLOCK_INTERNAL);            // Built-in A/D setup function
   set_adc_channel(0);                       // Built-in A/D setup function

 

      for(i=1; i<=10; ++i) {
      min=255;
      max=0;
         delay_ms(2);                      // Built-in delay function
         value = read_adc();                 // Built-in A/D read function
         pot = (value*100)/51;
         if(value<min)
            min=value;
         if(value>max)
            max=value;
     
     
      printf("\r\nMin: %u  Max: %u\n\r",min,max);
      printf("\r\nPot: %3.2w ",pot);
      delay_ms(3000);
      }
 
}
Guest








PostPosted: Fri Dec 19, 2008 11:10 am     Reply with quote

Hi

In CCS int8 and int is only 8 bit, think your "pot" must be int16.

Code:
int16 pot;
Guest








PostPosted: Fri Dec 19, 2008 11:25 am     Reply with quote

Hi,
I changed the format to %Lu in printf and I changed to int16 pot;
still not working properly....

I am only seeing 0-3 volts

the min and max are working fine 0-255 when I adjust the pot so I know that pin AN0 is seeing 0 -5V....
Ttelmah
Guest







PostPosted: Fri Dec 19, 2008 2:07 pm     Reply with quote

To get 10 bits from the ADC, requires two things:
int16 for the variable, and
#device ADC=10

Otherwise the default is 8bit operation.

Two more comments:
Don't use 'ADC_CLOCK_INTERNAL'. Read the data sheet. This is not recommended above 1MHz.
Then, your arithmetic will have problems. You have an int16. This can hold numbers up to 65536. You are taking the ADC value (0 to 1023), and multiplying this by 1000. This gives numbers from 0 to 102300. These _wont fit_ into an int16. You need to use an int32 value for this operation.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Dec 19, 2008 3:12 pm     Reply with quote

Where are you getting this math from?
Code:
       pot = (value*100)/51;

If you want to turn A/D counts into voltage you want something like

volts = (counts * fullscalevolts)/fullscalecounts

or in your case
Code:
       pot = (value*5000)/1023;

to give an output in integer millivolts for a 10 bit A/D with a 5.000V reference.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah
Guest







PostPosted: Fri Dec 19, 2008 3:47 pm     Reply with quote

But will need to be:
pot = ((int32)value*5000)/1023;

to work.

Best Wishes
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