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

error reading analog pins

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



Joined: 06 Apr 2012
Posts: 21

View user's profile Send private message

error reading analog pins
PostPosted: Wed May 16, 2012 11:24 am     Reply with quote

ver4.057
pic18f452

I got error while reading analog pins of portA, there is always 5V present at the portA pins. Plz help.
I have initialized the adc as mentioned below:
Code:

///in main function
do{
   setup_port_a( ALL_ANALOG );
   setup_adc( ADC_CLOCK_div_32);
   adcread();
delay_ms(300);
}while(true);

///and then
void adcread(void)
{

set_adc_channel(0);
do{
    adcchk=adc_done();
}while(adcchk!=adc_done());

delay_ms(200);
adc=read_adc();
delay_ms(300);

lcd_putc("\f");
printf(lcd_putc,"adc_value1=%lu \n",adc);
delay_ms(900);
ch0V=(float)adc*5/1024;
presr=(float)70*(ch0V/5);

////channel 1 selected "temperature"

set_adc_channel(1);
      do{
            adcchk=adc_done();
         }while(adcchk!=adc_done());

delay_ms(200);
adc=read_adc();
delay_ms(300);

lcd_putc("\f");
printf(lcd_putc,"adc_value2=%lu \n",adc);
delay_ms(900);
ch1V=(float)adc*5/1024;
temp=(float)150*((ch1V/5)*3.424);
printf(lcd_putc,"Temp=%f",temp);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 16, 2012 12:11 pm     Reply with quote

Here is a demo program which shows how to read the analog voltage
(0 to +5v) on pin A0 and display it on an LCD:
Code:

#include <18F452.H>
#device adc=10
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=20M)

#include "flex_lcd.c"

//============================
void main()
{
int16 adc_value;
float volts;
 
lcd_init();

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(20);

while(1)
  {
   adc_value = read_adc();
   volts = (float)(adc_value * 5)/1023.0;   
   printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(500);
  }
}
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed May 16, 2012 12:13 pm     Reply with quote

If PCMs' program doesn't do the trick check the ADCON0/ADCON1 and
TRISA registers to see how they are set.

I just got through with a project using the 18F442 on version 4.132 and had
to set the ADCON registers manually.

See page 181 of the datasheet for the proper register settings.

// define the registers
#byte ADCON0 = 0xFC2
#byte ADCON1 = 0xFC1

// set the bits
ADCON0=xx
ADCON1=xx
_________________
Google and Forum Search are some of your best tools!!!!
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