Hi.
I got stuck and maybe blind. I have an LED connected to RA2, and a POT connected to RC3/AN7. The idea is that the LED will flash at different rates according the to ADC reading.
When I run the code on the chip, the LED flashes at the fastest rate, regardless of the POT.
I need some advice.
I got stuck and maybe blind. I have an LED connected to RA2, and a POT connected to RC3/AN7. The idea is that the LED will flash at different rates according the to ADC reading.
Rich (BB code):
// ADC test program
#include <htc.h>
#define _XTAL_FREQ 4000000
#define led RA2
unsigned int adc_raw;
// Configuration
__CONFIG(FOSC_XT & // Crystal/resonator on RA4 & RA5
WDTE_OFF & // Watchdog timer off
PWRTE_OFF & // POwer up timer enable off
MCLRE_OFF & // MCLR pin function is digital input, MCLR internally tied to VDD
CP_OFF & // Code protection off
CPD_OFF & // Data code protection off
BOREN_OFF & // Brown out off
IESO_OFF & // Internat switchover off
FCMEN_ON); // Fail-Safe clock monitor on
void adc_init(void)
{
ADCON0bits.ADFM = 0; // Left justified
ADCON0bits.VCFG = 0; // VDD as reference
ADCON0bits.CHS = 0b111; // Analog channel set to AN7
ADCON0bits.ADON = 1; // ADC is enabled
ADCON1bits.ADCS = 0b001; // ADC Conversion Clock Select bit
} // End adc_init
void adc_read(void)
{
ADCON0bits.GO = 1;
__delay_us(10);
while(ADCON0bits.nDONE);
} // End adc_read
void main (void)
{
CMCON0 = 0x07; // Turn off comparators
ANSEL = 0b00001000; // Analog input on RC3/AN7
ADCON1 = 0b01000000; // Conversion Clock
TRISA = 0b00000000;
TRISC = 0b00001000;
led = 0;
adc_init();
while (1)
{
adc_read();
adc_raw = ADRESH << 8;
adc_raw |= ADRESL;
if (adc_raw <= 512)
{
led = 1;
__delay_ms(500);
led = 0;
__delay_ms(500);
}
else
{
led = 1;
__delay_ms(100);
led = 0;
__delay_ms(100);
}
}
}
I need some advice.