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 support@ccsinfo.com

Problem with ADC input

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







Problem with ADC input
PostPosted: Wed Jun 22, 2005 2:02 am     Reply with quote

I am inputting a 10k potentiometer on a 16F819. This chip is a 10 bit ADC, so am I correct in saying that I am basically reading in a number between 0 and 1024? Anyways, I store the input as a long and pass it to a function to output to two, 7 segment displays. I want to limit the adjustability of the potentiometer to about 60, which is why I am dividing the long by 17.0 (1024/17~=60).

My output on the display only goes between 1 and 14. Can any of you see what I am doing wrong?



Also, is there any other frequency I can set this chip to run at? or will I always want to do 4 MHz?

Thanks



Code:
#include <16F819.h>

#FUSES INTRC_IO,NOPROTECT,NOWDT,PUT,BROWNOUT, NOMCLR, NOLVP
#use delay(clock=4000000)

#define VALVE_OUTPUT PIN_A2

#define LED_DISPLAY_LS_0 PIN_B0
#define LED_DISPLAY_LS_1 PIN_B1
#define LED_DISPLAY_LS_2 PIN_B2
#define LED_DISPLAY_LS_3 PIN_B3

#define LED_DISPLAY_MS_0 PIN_B4
#define LED_DISPLAY_MS_1 PIN_B5
#define LED_DISPLAY_MS_2 PIN_B6
#define LED_DISPLAY_MS_3 PIN_B7

long read_adc_average(int channel) {
   long value;
   int i;

   set_adc_channel(channel);
   value=0;
   for ( i=0 ; i<8 ; i++ ) {
      value += read_adc();
      delay_ms(2);
   }
   /* divide by 8 */
   value = value / 8;

   return value;
}

void init_adc(void) {
   setup_adc( ADC_CLOCK_INTERNAL );
   setup_adc_ports( RA0_RA1_RA3_ANALOG );
}

void write_display(long led_num){
      if(led_num != 0){

         if(led_num%10 == 0){
            output_low(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 1){
            output_high(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 2){
            output_low(LED_DISPLAY_LS_0);
            output_high(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 3){
            output_high(LED_DISPLAY_LS_0);
            output_high(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 4){
            output_low(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_high(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 5){
            output_high(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_high(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 6){
            output_low(LED_DISPLAY_LS_0);
            output_high(LED_DISPLAY_LS_1);
            output_high(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 7){
            output_high(LED_DISPLAY_LS_0);
            output_high(LED_DISPLAY_LS_1);
            output_high(LED_DISPLAY_LS_2);
            output_low(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 8){
            output_low(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_high(LED_DISPLAY_LS_3);
         }
         else if(led_num%10 == 9){
            output_high(LED_DISPLAY_LS_0);
            output_low(LED_DISPLAY_LS_1);
            output_low(LED_DISPLAY_LS_2);
            output_high(LED_DISPLAY_LS_3);
         }
         
         if(led_num <10){
            output_float(LED_DISPLAY_MS_0);
            output_float(LED_DISPLAY_MS_1);
            output_float(LED_DISPLAY_MS_2);
            output_float(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 1){
            output_high(LED_DISPLAY_MS_0);
            output_low(LED_DISPLAY_MS_1);
            output_low(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 2){
            output_low(LED_DISPLAY_MS_0);
            output_high(LED_DISPLAY_MS_1);
            output_low(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 3){
            output_high(LED_DISPLAY_MS_0);
            output_high(LED_DISPLAY_MS_1);
            output_low(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 4){
            output_low(LED_DISPLAY_MS_0);
            output_low(LED_DISPLAY_MS_1);
            output_high(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 5){
            output_high(LED_DISPLAY_MS_0);
            output_low(LED_DISPLAY_MS_1);
            output_high(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }   
         else if(led_num/10 == 6){
            output_low(LED_DISPLAY_MS_0);
            output_high(LED_DISPLAY_MS_1);
            output_high(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 7){
            output_high(LED_DISPLAY_MS_0);
            output_high(LED_DISPLAY_MS_1);
            output_high(LED_DISPLAY_MS_2);
            output_low(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 8){
            output_low(LED_DISPLAY_MS_0);
            output_low(LED_DISPLAY_MS_1);
            output_low(LED_DISPLAY_MS_2);
            output_high(LED_DISPLAY_MS_3);
         }
         else if(led_num/10 == 9){
            output_high(LED_DISPLAY_MS_0);
            output_low(LED_DISPLAY_MS_1);
            output_low(LED_DISPLAY_MS_2);
            output_high(LED_DISPLAY_MS_3);
         }
         
      }
}

void main(void) {

   long user_input_adc_value;
   long sensor_input_adc_value;
   long display_value = 0;

   init_adc();

   while ( 1 ) {

      user_input_adc_value = read_adc_average(0);
      sensor_input_adc_value = read_adc_average(1);
         
   
      delay_ms(50);

      output_high(VALVE_OUTPUT);


      delay_ms(50);      
      
   //   output_low(VALVE_OUTPUT);

   display_value = user_input_adc_value/17.0;
         
      write_display(display_value);
      
   }
}

[/quote]
Stewie82
Guest







PostPosted: Wed Jun 22, 2005 2:11 am     Reply with quote

After some testing, I am thinking that the ADC is actually only 2^8 or 256.....what am I confused about here?
Paolino



Joined: 19 Jan 2004
Posts: 42

View user's profile Send private message

PostPosted: Wed Jun 22, 2005 2:51 am     Reply with quote

Add
Code:
#device adc=10

if you want to read 10 bit analog signals.
Best regards.

Paolo.
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Jun 23, 2005 2:10 am     Reply with quote

The ADC is being read too soon after the channel is selected. You need to wait for the output to "settle". I use 25us on an 18F452, I don't know what settling time your processor requires.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
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