Sunday, August 15, 2021

PIC16F818 A/D Converter Bar-graph Example in XC8

 

Introduction

In previous article we have shown about A/D converter module of PIC16F818, and its programming example in MPLABX XC8. We are going to make more programming example using this analog input peripheral module inside this PIC device.

LED bar-graph is a popular LED display that show analog level status of any quantity – analog voltage, water level, and VU indicator. For example an bar-graph display indicating level of remaining energy of a smart phone power bank.

PIC16F818 A/D Converter Bar-graph Example in XC8
A simulation sample of this example

LED Bar-graph Programming Example With A/D Converter

PIC microcontroller will be programmed to display bar-graph output with nine value – from null to its maximum display on. Analog input connects to RA0/AN0 while its analog representation will be displayed on Port B.

Circuit Design

Resistor network and LED bar-graph are more convenience in this programming example. They are packed in one package making an ease of circuit wiring on breadboard.

PIC16F818 A/D Converter Bar-graph Example in XC8
Circuit Diagram

This controller operates from its 8MHz internal oscillator due to economics issue and ease of design. An external reset on MCLR is needed as an aid of controller being stuck.

XC8 Programming for A/D Converter

Most of programming source code is similar to previous example. It excepts an additional lines of code that process bar-graph LED display. C program convert A/D converter 8-bit result pair to its 10-bit resolution.

An additional step converts this 1024 decimal value to 9 value range to fit 8-LED bar-graph display.

/*
 * PIC16F818 A/D Converter Example 2
 * A/D reading bargraph display
 */
#include <xc.h>
#include "pic16f818_config.h"
/*Set oscillator to 8MHz*/
#define _XTAL_FREQ  8000000
void main(void){
    /*LED Bar Graph Representation*/
    char barGraph[9]={0,0b00000001,0b00000011,0b00000111,0b00001111,
        0b00011111,0b00111111,0b01111111,0b11111111};
    /*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){
        /*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 barValue=(_H<<8)+_L;
        /*Convert A/D result to 9 values*/
        barValue=(float)barValue*9.0/1024;
        /*Display bargraph on Port B*/
        PORTB = barGraph[barValue];
    }
}

Configuration bits keep in C header file “pic16f818_config.h” in the same project directory.

// PIC16F818 Configuration Bit Settings
// CONFIG
#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)

User can write these configuration codes within C main program.

Program Testing

This embedded controller program was primarily tested in software simulator.

PIC16F818 A/D Converter Bar-graph Example in XC8
Microcontroller simulation in Proteus 8


Click here to get zip file of this working example.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)