View previous topic :: View next topic |
Author |
Message |
riz179
Joined: 06 Apr 2012 Posts: 21
|
error reading analog pins |
Posted: Wed May 16, 2012 11:24 am |
|
|
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
|
|
Posted: Wed May 16, 2012 12:11 pm |
|
|
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
|
|
Posted: Wed May 16, 2012 12:13 pm |
|
|
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!!!! |
|
|
|