Vous êtes sur la page 1sur 7

Utilisation de ADC de PIC Microcontroller - MPLAB XC8

Dans ce tutoriel, nous allons apprendre comment utiliser le module ADC d'un microcontrleur PIC
l'aide du compilateur MPLAB XC8. Pour la dmonstration, nous utiliserons le microcontrleur PIC
16F877A communment disponible.

Chaque grandeur physique trouve dans la nature comme la temprature, l'humidit, la pression, la force
est analogique. Nous devons convertir ces quantits analogiques en numriques pour la traiter l'aide
d'un ordinateur numrique ou d'un microcontrleur. Cela se fait en utilisant des convertisseurs
analogiques numriques. Un convertisseur analogique ou numrique ou ADC est un dispositif qui
convertit une quantit analogique continue (ici: tension) en valeurs numriques discrtes
correspondantes.

Le microcontrleur PIC 16F877A possde 8 entres ADC et convertit les entres analogiques en un
nombre numrique de 10 bits . Par souci d'explication, apportez ADC Lower Reference comme 0V et
Higher Reference comme 5V.

Vref- = 0V
Vref+ = 5V
n = 10 bits
Resolution = (Vref+ Vref-)/(2n 1) = 5/1023 = 0.004887V

La rsolution ADC est donc de 0,00487V, ce qui correspond la tension minimale requise pour changer
un peu. Voir les exemples ci-dessous.

Digital output
Analog Input
Binary Hex Decimal
0 0b0000000000 0x000 0
0.004887V 0b0000000001 0x001 1
0.009774V 0b0000000010 0x002 2
0.014661V 0b0000000011 0x003 3
4.999401V 0b1111111111 0x3FF 1023

Le module ADC de PIC 16F877A dispose de 4 registres.


ADRESH A/D Result High Register
ADRESL A/D Result Low Register
ADCON0 A/D Control Register 0
ADCON1 A/D Control Register 1

Diagramme de bloc ADC

partir de ce diagramme, vous pouvez facilement comprendre le fonctionnement de la slection de


canal ADC et de la slection de la tension de rfrence.

Le registre de contrle ADCON0

Le registre ADCON0 contrle les oprations de la fonction conversion analogique numrique. Il permet
de :

slectionner la frquence dhorloge du convertisseur ;


slectionner le canal ou lentre analogique sur laquelle doit tre effectue la conversion ;
lancer la conversion ;
savoir lorsque celle-ci est acheve.
Bit 7 ~ 6 : ADCS1 & ADCS0, A/D Conversion clock selection bits. These bits are used in
combination with ADCS2 of ADCON1 register. (See the ADC Clock Selection Table)
Bit 5 ~ 3 : CH2, CH1 & CH0 are analog channel selection bits which will select a particular
channel out of 8.
Bit 2 : Go_nDone is the A/D conversion status bit. Setting this bit initiates the A/D conversion
and it will be automatically cleared when the conversion completes.
Bit 1 : Unimplemented, Read as 0.
Bit 0 : ADON, A/D Module On bit. Setting this bit will turn on the ADC module.

Le registre de contrle ADCON1

Le registre de contrle ADCON1 permet de :

choisir la justification droite ou gauche du rsultat ;


slectionner les entres analogiques utilises ;
slectionner les entres analogiques AN2 et AN3 pour rgler lchelle de conversion par des
tensions extrieures (tensions de rfrences)

Bit 7 : ADFM, A/D Result Format bit. Bits of result will be right justified if this bit is set
otherwise it will be left justified.
Bit 6 : ADCS2, A/D Conversion clock selection bit. This bit is used in combination with
ADCS0 and ADCS1 of ADCON0 register. (See the ADC Clock Selection Table)
Bit 5 ~ 4 : Unimplemented. Read as 0.
Bit 3 ~ 0 : PCFG3 ~ PCFG0, A/D Port Configuration Bits. Status of these bits determine
whether the pin is Analog or Digital as per the table below.
Note : : Certains circuits intgrs utilisent un registre spcial ANSEL pour dfinir chaque broche comme
analogique ou numrique.

A/D Temps d'acquisition

Le condensateur de maintien (CHOLD) doit tre charg sur la tension d'entre pour rpondre la prcision
spcifie par la fiche technique. Nous devons donc fournir un dlai suprieur au temps d'acquisition
minimum requis pour charger le condensateur. 19.72s est le temps minimum spcifi dans la fiche
technique. Veuillez vous rfrer la fiche technique pour plus de dtails.

Slection de l'horloge A/D

A/D conversion clock must be selected to ensure minimum TAD. TAD is the conversion time per bit, which
is 1.6s. Please refer the datasheet for more details.

L'horloge de conversion A/D doit tre slectionne pour assurer un minimum de TAD. TAD est le
temps de conversion par bit, qui est de 1,6 s. Veuillez vous rfrer la fiche technique pour plus de
dtails.
Schma de circuit

You can easily understand the circuit if you already go through our first tutorials, PIC Microcontroller MPLAB
XC8 Tutorials. The reference voltages for A/D conversion is set to VDD (5V) and VSS (GND) in the software
(see the code below). Analog input to Channel 0 is provided using a potentiometer such that we can vary the input
voltage from 0 ~ 5V. A/D conversion will generate a 10 bit digital value (0 ~ 1023) corresponding to the analog
input. This digital value is displayed using 10 LEDs connected PORTB and PORTC of the microcontroller.
MPLAB XC8 Code

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming
Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code p
rotection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection o
ff; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection
off)

#include <xc.h>
#include <pic16f877a.h>

#define _XTAL_FREQ 8000000

void ADC_Init()
{
ADCON0 = 0x81; // Allumer ADC et Slection de l'horloge
ADCON1 = 0x00; // Toutes les broches comme entre analogique et rglage
// Tensions de rfrence
}

unsigned int ADC_Read(unsigned char channel)


{
if(channel > 7) // La plage de canaux est 0 ~ 7
return 0;

ADCON0 &= 0xC5; //Effacer les bits de slection de canal


ADCON0 |= channel<<3; //Setting channel selection bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D conversion
while(GO_nDONE); //Waiting for conversion to complete
return ((ADRESH<<8)+ADRESL); //Return result
}

void main()
{
unsigned int a;
TRISA = 0xFF; //Analog pins as Input
TRISB = 0x00; //Port B as Output
TRISC = 0x00; //Port C as Output
ADC_Init(); //Initialize ADC

do
{
a = ADC_Read(0); //Read Analog Channel 0
PORTB = a; //Write Lower bits to PORTB
PORTC = a>>8; //Write Higher 2 bits to PORTC
__delay_ms(100); //Delay
}while(1); //Infinite Loop
}

Vous aimerez peut-être aussi