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 PortA.1 and reads a value.

1. Configure Port:
• Disable pin output driver
TRISAbits.TRISA1 = 1; //Make pin Input A.1

• Configure pin as analog
ANSELAbits.ANSA1 =1; //Enable Analog A.1

2. 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 – A.1 or AN1

• Turn on ADC module
ADCONbits.ADON = 1;

3. Start conversion by setting the GO/DONE bit.
ADCONbits.GO_nDONE = 1; //Start the conversion

4. Wait for ADC conversion to complete by Polling the GO/DONE bit:
while (ADCONbits.GO_nDONE == 1){
NOP();
}

5. 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 A.0 – 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 A.1 – which is configured as the analog input.

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

PIC10F322 XC8 Analog-to-Digital Converter (ADC) Getting Started

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.

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

 

#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 A.0
    ANSELAbits.ANSA0 =0;   //Disable Analog A.0
    LATAbits.LATA0 = 0;    //Make output low 
    
    TRISAbits.TRISA1 = 1;  //Make pin Input A.1
    ANSELAbits.ANSA1 =1;   //Enable Analog A.1
    
    //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 - A.1 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;
}

Something You Might Be Intrested In


 

10F32X Rapid Prototype Board

Make The PIC10F322 and other PIC10F Processors Breadboard Friendly for Quick and Easy Prototyping.

{Click to learn more}

PIC10F322 XC8 Analog-to-Digital Converter (ADC) Getting Started
PIC10F322 XC8 Analog-to-Digital Converter (ADC) Getting Started