Select Page

The PIC10F322 Analog-to-Digital Converter has three selectable external analog channels. This allows you to convert an analog signal (voltage) to an 8bit value (0 – 255) that you can use – to do whatever you dream up.

Here is the process that enables the ADC on Port.A1 and reads a value.

Configure Port:

  • Disable pin output driver
  • TRISAbits.TRISA1 = 1; //Make pin Input PORT.A1
  • Configure pin as analog
  • ANSELAbits.ANSA1 =1; //Enable Analog PORT.A1

Configure the ADC module:

  • Select ADC conversion clock
  • ADCONbits.ADCS = 0b010;
  • //Set Analog conversion clock FOSC/32, since we are running 16Mhz, we need to have a conversion time at or greater 1uS – FOSC/32 will give us 2uS – See Table 15-1 in the PIC10F322 Datasheet

Select ADC input channel

  • ADCONbits.CHS = 0b001; //Select the Analog channel – PORT.A1 or AN1

Turn on ADC module

  • ADCONbits.ADON = 1;

Start conversion by setting the GO/DONE bit.

  • ADCONbits.GO_nDONE = 1; //Start the conversion

Wait for ADC conversion to complete by Polling the GO/DONE bit:

while (ADCONbits.GO_nDONE == 1){
NOP();
}

Read ADC Result.


int adc_value = ADRES;

So, lets put that to code and get something to light a LED when the input voltage goes over a certain value.

The setup has a LED attached to PORT.A0 – which will light when the input voltage value is greater than 128. Anything below that will keep it off.

A 10K potentiometer is attached to PORT.A1 – which is configured as the analog input.

For a quick potentiometer refresher – see the image below. The output is changed by turning the shaft.

Potentiometer terminals

The Circuit

Turn the potentiometer will increase or decrease the output voltage – please note that this voltage should be only used for measurement and not to drive the circuit.

10f322 adc a1

The Code

The code has a 1 second delay before reading the ADC again and checking the value.

/*
 * File:   adc_example.c
 * Author: Jamie
 *
 * Created on 9/14/2021, 8:01 AM
 */


#include <xc.h>
#include <stdint.h>

//Device Configuration
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 16000000  //16Mhz

//Prototypes
void setup(void);
int ADC_Read(void);

void setup(void){    
    //Set the System Clock - You can change this to match the setting you are looking for
    OSCCONbits.IRCF = 0b111;  //Set System Clock to 16Mhz
    
    //Pin LED is connected to
    TRISAbits.TRISA0 = 0;  //Make pin Output Port.A0
    ANSELAbits.ANSA0 =0;   //Disable Analog Port.A0
    LATAbits.LATA0 = 0;    //Make output low 
    
    TRISAbits.TRISA1 = 1;  //Make pin Input Port.A1
    ANSELAbits.ANSA1 =1;   //Enable Analog Port.A1
    
    //Set Analog conversion clock FOSC/32, since we are running 16Mhz, we need to have a conversion time at or greater 1uS
    //FOSC/32 will give us 2uS
    ADCONbits.ADCS = 0b010; 
    
    //Select the Analog channel - Port.A1 or AN1
    ADCONbits.CHS = 0b001;
    
    //Turn on the ADC module
    ADCONbits.ADON = 1;      
}

void main(void)
{
    setup();
    
    while(1){
     
        int ADC_Value = 0;
        ADC_Value = ADC_Read();
        
        if (ADC_Value > 128){
            LATAbits.LATA0 = 1;  //Turn ON LED
        }
        else{
            LATAbits.LATA0 = 0;  //Turn OFF LED
        }
        
    __delay_ms(1000);  //Wait 1 second and do it again
        
    }
}

int ADC_Read(){
    //Starts the ADC read and waits until a conversion is complete before returning
    //Returns the ADC value
    ADCONbits.GO_nDONE = 1;  //Start the conversion
    
    while (ADCONbits.GO_nDONE == 1){
        NOP();
    }
    
    return ADRES;
}

Have a Project or Idea!?

I am Available for Freelance Projects

My skills are always primed and ready for new opportunities to be put to work, and I am ever on the lookout to connect with individuals who share a similar mindset.

If you’re intrigued and wish to collaborate, connect, or simply indulge in a stimulating conversation, don’t hesitate! Drop me an email and let’s begin our journey. I eagerly anticipate our interaction!

jamie@jamiestarling.com


Pin It on Pinterest

Share This