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)
Thanks
Peter
/*
* 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[5]="text";
int result;
char textresult[6]="";
//-------------------------------------------------------------
// CONFIGURATION FUSES
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
//#pragma config PBADEN = OFF
void DelayFor18TCY( void )
{
Delay10TCYx(2);
}
void DelayPORXLCD( void )
{
Delay1KTCYx(10);
}
void DelayXLCD( void )
{
Delay10TCYx(5);
}
int data;
char buffer[16];
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 IO
TRISC=0;
LATC=0;
TRISA=0x01; // ALL OUT EXCEPT RA0
LATA=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 Position
putrsXLCD("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 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
//result=0;
//textresult="hello";
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
itoa(result,textresult);
while(BusyXLCD());
WriteCmdXLCD ((0x80+0x42));
while(BusyXLCD());
putrsXLCD(" ");
while(BusyXLCD());
WriteCmdXLCD ((0x80+0x42));
while(BusyXLCD());
putsXLCD (textresult);
};
while(1);
}