ADC on Pic 18f2420 - Page 1
Forum section: electronics / general

author: kolbep
author_rank: Frequent Contributor
timestamp: October 27, 2013, 07:21:26 pm
content: Hi.I am playing around with the ADC on a Pic Micro (18f2420), using C18 Compiler.I have an LCD using RA1 to RA3 for the Control Lines,and PortC is the Data for the LCD.After many hours, I can now use the LCD fine, using the XLCD libraries.The problem I have is now,I have a 10K preset pot between VCC and GND, and that is connected through a resistor into the RA0 port.When I turn the pot, I only get 2 outputs from the adc, namely 0, or 1023, no intermediate values.I did accidently have the RA0 set as an output, which was low, whilst I was trying to use it as an adc input (I hope I havent blown something in the uC).I have since fixed that, but still have the same problems (Only giving 0 or 1023)Can one of you knowledgeable folks please give a brief look at my code, and see if you can spot a mistake.The ultimate goal for this project is going to be a logging Volt and Ammeter, so my code is sort of going in that direction.Muchas Gracias (I suck with that spelling, i guess)ThanksPeterCode:/** File: voltlogger.c* Author: Peter** Created on 26 October 2013, 2:31 PM*/#include <stdio.h>#include <stdlib.h>#include <xlcd.h>#include <p18f2420.h>#include <timers.h>#include <delays.h>#include <adc.h>//char text="text";int result;char textresult="";//-------------------------------------------------------------// CONFIGURATION FUSES#pragma config OSC = INTIO67#pragma config WDT = OFF#pragma config LVP = OFF//#pragma config PBADEN = OFFvoid DelayFor18TCY( void ){Delay10TCYx(2);}void DelayPORXLCD( void ){Delay1KTCYx(10);}void DelayXLCD( void ){Delay10TCYx(5);}int data;char buffer;void main(void) {//OSCCONbits.IRCF0=0; //Lower Clock Speed//OSCCONbits.IRCF1=0;////OSCCONbits.IRCF2=0;//// ADCON1=0xFF; //TURN OFF ADC's TO GET DIGITAL IO// CMCON=0xFF; //TURN OFF ADC's TO GET DIGITAL IOTRISC=0;LATC=0;TRISA=0x01; // ALL OUT EXCEPT RA0LATA=0;while( BusyXLCD() );OpenXLCD( FOUR_BIT & LINES_5X7);WriteCmdXLCD( BLINK_OFF & CURSOR_OFF);WriteCmdXLCD( SHIFT_DISP_LEFT );// unsigned char x = 'A';// putrsXLCD(text);//while(!BusyXLCD() ){//putsXLCD( text );// }putrsXLCD(" VLog V1.0");// WriteCmdXLCD( SHIFT_CUR_LEFT );WriteCmdXLCD((0x80+0x47)); //Move Cursor PositionputrsXLCD("V");WriteCmdXLCD ((0x80+0x42));//putrsXLCD ("0");while(1) {OpenADC( ADC_FOSC_RC &ADC_RIGHT_JUST &ADC_VREFPLUS_VDD &ADC_VREFMINUS_VSS &ADC_12_TAD, ADC_CH0 &ADC_INT_OFF,0);Delay10TCYx( 5 ); // Delay for 50TCYConvertADC(); // Start conversionwhile( BusyADC() ); // Wait for completion//result=0;//textresult="hello";result = ReadADC(); // Read resultCloseADC(); // Disable A/D converteritoa(result,textresult);while(BusyXLCD());WriteCmdXLCD ((0x80+0x42));while(BusyXLCD());putrsXLCD(" ");while(BusyXLCD());WriteCmdXLCD ((0x80+0x42));while(BusyXLCD());putsXLCD (textresult);};while(1);}

author: Clint
author_rank: Regular Contributor
timestamp: October 27, 2013, 08:43:03 pm
content: I am going blind but I can see where you set it as an analogue input ANSELA

author: kolbep
author_rank: Frequent Contributor
timestamp: October 27, 2013, 09:08:16 pm
content: It might be a bit late at night, so I am mybe not thinking clearly,But please rephrase your last comment,Also what is ansela?

author: Clint
author_rank: Regular Contributor
timestamp: October 27, 2013, 09:14:20 pm
content: Sorry should have been clearer;ANSEL = Analogue Select - otherwise it thinks it digital 0 or 1 0 or 1023This is what I do to set this up:Clear the portPORTA=0x00Select Port as analogueANSEL=0x01Select all outputs except RA0TRISA=0x01

author: kolbep
author_rank: Frequent Contributor
timestamp: October 27, 2013, 09:26:49 pm
content: Thanks, will give it another shot tomorrow eve.

author: kolbep
author_rank: Frequent Contributor
timestamp: October 28, 2013, 05:04:22 pm
content: Using ANSEL was only giving errors,but I searched a bit more, and if I set my openadc to :OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD,ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,ADC_5ANA & 0xf /*patch di g.dar*/);Now it works fine.Dunno why, but it works