Hi I'm trying to test the adc on a picf161939. I have followed the adc procedure but its not working. I am measuring an analog value from a pot and sending the adresh and adresl to port c and d with led's on it.
At the outputs of portc and d I am getting a random sequence.
I assume the code is wrong but can't see where I've gone wrong.
At the outputs of portc and d I am getting a random sequence.
I assume the code is wrong but can't see where I've gone wrong.
Code:
#include <pic16f1939.h>
#include <xc.h>
#define _XTAL_FREQ 500000 //1ms delay
void main()
{
PORTC = 0; PORTD = 0;
LATC = 0; LATD = 0;
TRISAbits.TRISA0 = 1;
ANSELAbits.ANSA0 = 1;
TRISC = 0x00;
TRISD = 0x00;
ADCON1bits.ADFM = 1;
ANSELD = 0x00;
ADCON1bits.ADPREF = 0b00;
ADCON1bits.ADNREF = 0;
ADCON0bits.CHS = 0b00000;
ADCON1bits.ADCS = 0b000;
while(1)
{
ADCON0bits.ADON = 1;
__delay_ms(10);
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE==1);
PORTC = ADRESH; //Also tried LATC and LATD
PORTD = ADRESL;
__delay_ms(250);
}
}