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

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
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: 1924
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