Pic16f188xx ADC with fvr
#include <xc.h>
// Configuration bits
#pragma config FOSC = INTOSC // Internal Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disable
#pragma config PWRTE = OFF // Power-up Timer Disable
#pragma config MCLRE = ON // MCLR Pin Function Select
#pragma config CP = OFF // Flash Program Memory Code Protection
#pragma config BOREN = ON // Brown-out Reset Enable
#pragma config CLKOUTEN = OFF // Clock Out Disable
#pragma config IESO = OFF // Internal/External Switchover
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Disable
// Define system frequency
#define _XTAL_FREQ 32000000 // 32 MHz internal oscillator
void setup(void) {
// Initialize oscillator
OSCCON1 = 0x60; // Set OSCCON1 - HFINTOSC with 1x divider
OSCFRQ = 0x06; // Set OSCFRQ - 32MHz HFINTOSC
// Initialize FVR to 2.048V for ADC
FVRCON = 0b11000010; // FVREN enabled, ADC FVR Buffer Gain is 2x (2.048V)
// Set RA0/AN0 as analog input
TRISAbits.TRISA0 = 1; // Configure RA0 as input
ANSELAbits.ANSELA0 = 1; // Enable analog functionality on RA0
// ADC configuration
ADCON0 = 0x01; // ADON enabled, channel AN0
ADCON1 = 0b11010000; // ADFM right justified, ADPREF FVR, ADCS FOSC/64
ADREF = 0b00110000; // ADNREF VSS, ADPREF FVR (2.048V)
}
uint16_t readADC(void) {
__delay_us(5); // Acquisition time delay
ADCON0bits.GO = 1; // Start ADC conversion
while (ADCON0bits.GO); // Wait for conversion to complete
return ((uint16_t)((ADRESH << 8) + ADRESL)); // Return right-justified 10-bit result
}
void main(void) {
setup();
while(1) {
uint16_t adcResult = readADC(); // Read ADC value from AN0
// Implement your application logic here
__delay_ms(1000); // Example delay, modify as per application requirement
}
}
Comments
Post a Comment