Introduction
Analog-to-Digital Converter (ADC) operates with analog signal input to microcontroller. Input signal from any device is converted to analog voltage before it’s fed to analog input pin.
Typical reference voltage of ADC is 5V. Maximum direct input voltage to analog input channel must not exceed +5V. For a 10-bit resolution ADC the step voltage is 0.048V.
In this example the controller is programmed to read and show input voltage to ADC input channel 0 – AN0 of PIC16F818. The result will show on a single common anode 7-Segments display.
Programming in MPLABX XC8
There are some additional codes over previous post that convert ADC result to analog voltage, and displaying. A display is one inch in digit size.
One inch red common anode seven-segments display |
Circuit Design
System runs without external crystal oscillator as it comes with internal oscillator with maximum 8MHz frequency.
Schematic Design |
AN0 reads input voltage while Port B display the result. Input voltage is varied due to the adjustment of RV1 POT.
Programming
Main program loop keeps track of ADC reading and result updating. Most of source code here is often seen in some previous posts relate to PIC16F818 ADC programming in MPLABX XC8.
* PIC16F818 Analog Voltage Reading
*/
#include <xc.h>
// PIC16F818 Configuration Bit Settings
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2 // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void readVoltage(void){
/*Seven-Segment output data*/
char displayAnode[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
/*Start the conversion*/
ADCON0bits.GO_nDONE=1;
/*When GO_nDONE is clear A/D conversion is completed*/
while(GO_nDONE);
/*Wait for some microseconds*/
for(int i=0;i<1000;i++);
/*Make a 10-bit A/D converter result*/
int _L=ADRESL;
int _H=ADRESH;
unsigned int adcResult=(_H<<8)+_L;
/*convert to voltage with +0.1V offset*/
adcResult=(float)adcResult*5.1/1024;
/*Show voltage reading*/
PORTB=displayAnode[adcResult];
}
void main(void){
/*Select 8MHz internal oscillator*/
OSCCONbits.IRCF=0x07;
/*Clear Port A*/
PORTA=0x00;
/*Clear Port B*/
PORTB=0x00;
/*RA0 analog input*/
TRISA=0x01;
/*Port B digital output*/
TRISB=0x00;
/*Select internal RC oscillator of A/D converter*/
ADCON0bits.ADCS=0x01;
/*Select analog channel 0 - AN0*/
ADCON0bits.CHS=0x00;
/*RA0-AN0 analog input, AVDD and AVSS voltage references*/
ADCON1bits.PCFG=14;
/*Result is right justified*/
ADCON1bits.ADFM=1;
/*Turn on A/D converter module*/
ADCON0bits.ADON=1;
/*Main Program Loop*/
while(1){
readVoltage();
}
}
Click here to download zip file of this working example.
No comments:
Post a Comment