Вы находитесь на странице: 1из 42

Les Microcontrôleurs

Chapitre 4
 Conversion analogique
numérique (CAN ou ADC)

Brahim Attia « Docteur en électronique et micro-électronique »


brahim.attia@yahoo.fr
Jaouher Ben Ali « Docteur en automatique et informatique industrielle »
benalijaouher@yahoo.fr
1
Ports et PINs utilisés pour le CAN

2
Registres Associés avec l’ADC
ADDR NAME BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0

0Bh INTCON GIE PEIE T0IE INTE RBIE T0IF INTF RBIF
0Ch PIR1 PSPIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF
8Ch PIE1 PSPIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE
1Eh ADRESH L’octet du plus fort poids du registre résultat du CAN
9Eh ADRESL L’octet du plus faible poids du registre résultat du CAN
1Fh ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/ - ADON
!DONE
9Fh ADCON1 ADFM - - - PCFG3 PCFG2 PCFG1 PCFG0
85h TRISA - - Registre de direction de données du PORTA
05h PORTA PORTA pin en cas de lecture, Bascule asynchrone de données
du PORTA en cas d’écriture.
89h TRISE IBF OBF IBOV PSP - Bits de direction de données
MODE du PORTE
09h PORTE - - - - - RE2 RE1 RE0

3
Registres Associés avec l’ADC
Vérifier la fin de conversion Activé ou désactivé
Analogique numérique l’interruption du CAN.

PIR1 0C PIE1 8C

Résultat du conversion
sur 10 bits ADRESH 1E ADRESL 9E
Mise en service ADCON0 1F ADCON1 9F Configuration
du CAN du CAN
Registres à 20 Registres à A0
usage usage
générale générale EF
7F
Bank 0 Bank 1

4
Registre ADCON0
ADCON0 (adresse : 1Fh ) Registre de configuration du convertisseur Analogique / Digital

5
Registre ADCON0
Au reset : ADCON0 = 00000000
Bit 7 et bit 6 : ADSC1 et ADSC0 = Clock Select bits.
Ces 2 bits permettent de choisir la vitesse de conversion :
00= Fosc/2.
01= Fosc/8.
10= Fosc/32.
11= Oscillateur RC interne.
Le temps de conversion d'un bit est TAD. Pour une conversion totale des 10 bits il faut : 12
TAD.
Pour que la conversion soit correcte il faut que TAD soit au minimum de 1,6μs.
Avec l'oscillateur interne RC on a : TAD = 4 μs typique ( entre 2 et 6 μs ).

6
Registre ADCON0
Temps de conversion TAD en fonction du Quartz et des bits du Clock select :
Quartz Clock TAD 12 TAD Ne convient pas si TAD< 1,6µs
4 MHz Fosc /2 = 2 MHz 0,5 µs 6µs Ne convient pas
Fosc /8 = 500 KHz 2 µs 24 µs OK
Fosc /32 = 125 KHz 8 µs 96 µs OK
8 MHz Fosc /2 = 4 MHz 0,25 µs 3 µs Ne convient pas
Fosc /8 = 1 MHz 1µs 12 µs Ne convient pas
Fosc /32 = 250 KHz 4 µs 48 µs OK
12 MHz Fosc /2 = 6 MHz 0,16 µs 1,92 µs Ne convient pas
Fosc /8 = 1,5 MHz 0,66 µs 8 µs Ne convient pas
Fosc /32 = 375 KHz 2,6 µs 32 µs OK
16 MHz Fosc /2 = 8 MHz 0,125 µs 1,5 µs Ne convient pas
Fosc /8 = 2 MHz 0,5 µs 6 µs Ne convient pas
Fosc /32 = 500 KHz 2 µs 24 µs OK
20 MHz Fosc /2 = 10 MHz 0,1 µs 1,2 µs Ne convient pas
Fosc /8 = 2,5 MHz 0,4µs 4,8 µs Ne convient pas
Fosc /32 = 625 KHz 1,6 ms 19,2 ms OK
7
Registre ADCON0
Bit 5 bit4 et bit 3 : CHS2 CHS1 et CHS0 = Channel Select bits.
Ces 3 bits permettent de choisir l'entrée analogique qui va être convertie.

8
Registre ADCON0
Bit 2 : bit d’état de la conversion (GO/DONE)
Ce bit indique la fin de la conversion et permet aussi de lancer la conversion. Il est
nommé ADGO dans le langage C HI-TECH .
La mise à 1 de ce bit par le programmeur permet de commencer la conversion.
Ce bit passe automatiquement à 0 (par le HARD) lorsque le CAN terminera la
conversion.

Bit 2 : GO/DONE
Si ADON =1
GO/DONE =1 ; Démarre la conversion A/D. Ce bit est remis à "0" par hard.
GO/DONE =0 ; La conversion A/D est terminée.

9
Registre ADCON0
Bit 0 : ADON : A/D on bit.
1= Convertisseur A/D en service.
0 = Convertisseur A/D à l'arrêt.

ADON: ce bit permet de « mettre en route » le convertisseur analogique numérique:


1: le convertisseur analogique numérique est en fonctionnement.
0: le convertisseur analogique numérique ne fonctionne pas, les broches d’entrées ne
consomment pas de courant.

C.-à-d. Le positionnement à “1” du bit ADON active le convertisseur A/N. Dans le cas
contraire le C.A.N. est inopérant.

10
Registre ADCON1
ADCON1 (adresse : 9Fh ) Registre de configuration du convertisseur Analogique / Digital

11
Registre ADCON1
ADFM= Sélection du Format du résultat de la conversion:
Le résultat de la conversion sur 10 bits peut se présenter de 2 façons :
- ADFM = 0 justification à gauche (dans ce cas ADRESH donne un résultat sur 8 bits,
correspondant à la partie la plus significative = poids fort). Ce mode est surtout utilisé
lorsque l’on se contente d’un résultat sur 8 bits (variable de type “unsigned char”).
Left justified

- ADFM = 1 justification à droite. Le résultat correspond directement à un entier de type


“unsigned int” en langage C. Ce mode est donc tout indiqué lorsque l’on souhaite utiliser le
résultat sur 10 bits.
Right justified

12
Registre ADCON1

13
Registre ADCON1
Configuration des PORTS en fonctions des 4 bits PCFG:

Bit 3 bit 2 bit 1 et bit 0 : PCFG3 PCFG2 PCFG1 et PCFG0


Bits de contrôle de la configuration des Ports.
Ces bits permettent de choisir le partage entre entrées analogiques et digitales sur les PORTS A et
E.
Ils permettent également de choisir pour VREF+ entre VDD et PA3 et pour et VREF - entre VSS et
PA2.

ATTENTION :
Au reset le registre ADCON1 est initialisé à h'00'.
Cela signifie que les 5 bits du Port A et les 3 bits du Port E sont configurés en entrées
analogiques.
Pour récupérer le 5 bits du Port A et les 3 bits de Port E en tant que I/O digitales il faut écrire la
valeur h'06' dans ADCON1.

A = Entrée Analogique.
D = I/O Digitale.
14
Architecture interne de l’ADC

15
Programmation de l’ADC
Ces options permettent à l'utilisateur de configurer et utiliser le module convertisseur
analogique / numérique. Ils ne sont disponibles que sur les PICs avec ADC intégré. Les options
pour les fonctions et les directives varient en fonction du type du PIC et ils sont répertoriés
dans le fichier d'en-tête du PIC utilisé. Sur certains PICs, il ya deux modules ADC indépendants,
pour ces PICs, le second module est configuré en utilisant les fonctions de configuration
secondaire ADC (Ex. setup_ADC2).
setup_adc(mode) Sets up the a/d mode like off, the adc clock etc.
setup_adc_ports(value) Sets the available adc pins to be analog or digital.
set_adc_channel(channel) Specifies the channel to be use for the a/d call.
read_adc(mode) Starts the conversion and reads the value. The mode can also
control the functionality.
adc_done() Returns 1 if the ADC module has finished its conversion.
Relevant Preprocessor:
#DEVICE ADC=xx Configures the read_adc return size. For example, using a PIC with a 10
bit A/D you can use 8 or 10 for xx- 8 will return the most significant byte, 10 will return the
full A/D reading of 10 bits.

16
setup_adc(mode)
Syntax: setup_adc (mode); setup_adc2(mode);
Paramètre: mode: Analog to digital mode. Les options valides varient selon le PIC
utilisé. Voire le fichier 16f877.h file pour toutes les options. Quelques options typiques sont:

////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 0x100
#define ADC_CLOCK_DIV_8 0x40
#define ADC_CLOCK_DIV_32 0x80
#define ADC_CLOCK_INTERNAL 0xc0 // Internal 2-6us

Exemples:
Returns: undefined
setup_adc_ports( ALL_ANALOG );
Function: Configures the analog to digital converter.
setup_adc(ADC_CLOCK_INTERNAL );
Disponibilité: Pour les PICs ayant des ADCs intégré. set_adc_channel( 0 );
value = read_adc();
Nécessite: Constantes définies dans le fichier setup_adc( ADC_OFF ); 17
16f877.h.
setup_adc_ports( )
Syntaxe: setup_adc_ports (value)
Returns: undefined
Paramètres: value - une constante défini dans le fichier 16f877.h
Fonction: configure les pins de l’ ADC comme entrée analogique, numérique, ou comme
combinaison et la tension de reference à utiliser quand on calcule la valeur fournit par l’ADC.

Examples:
// All pins analog (that can be)
setup_adc_ports( ALL_ANALOG );
// Pins A0, A1 and A3 are analog and all others are digital. The +5v is used as a reference.
setup_adc_ports( RA0_RA1_RA3_ANALOG );
// Pins A0 and A1 are analog. Pin RA3 is used for the reference voltage and all other pins
// are digital.
setup_adc_ports( A0_RA1_ANALOGRA3_REF );
18
// Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 7 // None
#define ALL_ANALOG 0 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF 1 // A0 A1 A2 A5 E0 E1 E2 VRefh=A3
#define AN0_AN1_AN2_AN3_AN4 2 // A0 A1 A2 A3 A5
#define AN0_AN1_AN2_AN4_VSS_VREF 3 // A0 A1 A2 A4 VRefh=A3
#define AN0_AN1_AN3 4 // A0 A1 A3
#define AN0_AN1_VSS_VREF 5 // A0 A1 VRefh=A3
#define AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF 0x08 // A0 A1 A5 E0 E1 E2 VRefh=A3 VRefl=A2
#define AN0_AN1_AN2_AN3_AN4_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_AN1_AN2_AN4_AN5_VSS_VREF 0x0A // A0 A1 A2 A5 E0 VRefh=A3
#define AN0_AN1_AN4_AN5_VREF_VREF 0x0B // A0 A1 A5 E0 VRefh=A3 VRefl=A2
#define AN0_AN1_AN4_VREF_VREF 0x0C // A0 A1 A4 VRefh=A3 VRefl=A2
#define AN0_AN1_VREF_VREF 0x0D // A0 A1 VRefh=A3 VRefl=A2
#define AN0 0x0E // A0
#define AN0_VREF_VREF 0x0F // A0 VRefh=A3 VRefl=A2
#define ANALOG_RA3_REF 0x1 //!old only provided for compatibility
#define A_ANALOG 0x2 //!old only provided for compatibility
#define A_ANALOG_RA3_REF 0x3 //!old only provided for compatibility
#define RA0_RA1_RA3_ANALOG 0x4 //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_REF 0x5 //!old only provided for compatibility
#define ANALOG_RA3_RA2_REF 0x8 //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2 0x9 //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2_REF_RA3 0xA //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2_REF_RA3_RA2 0xB //!old only provided for compatibility
#define A_ANALOG_RA3_RA2_REF 0xC //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_RA2_REF 0xD //!old only provided for compatibility
#define RA0_ANALOG 0xE //!old only provided for compatibility
#define RA0_ANALOG_RA3_RA2_REF 0xF //!old only provided for compatibility

19
set_adc_channel( )
Syntaxe: set_adc_channel (chan)
Paramèters: chan est le numéro de canal à sélectionner. Les numéros des canaux commencent
à 0 et ils sont mentionnés dans le datasheet comme AN0, AN1, …,AN7.
Returns: indéfini.

Fonction: Elle indique le canal à utiliser pour le prochain appel de READ_ADC. Soyez conscient
que vous devez attendre un peu de temps après avoir changer le canal avant de pouvoir obtenir
une lecture valide. La durée varie en fonction de l'impédance de la source d'entrée.
En général, 10 μs sont suffisantes pour la plupart des applications.
Vous n’avez pas besoin de changer le canal avant chaque lecture si le canal ne change pas.
Disponibilité: Cette fonction n’est disponible que sur les PICs avec un convertisseur AN.

Examples:
set_adc_channel(2);
delay_us(10);
value = read_adc();
20
read_adc( )
Syntax: value = read_adc ([mode])
Parameters: mode est un paramètre facultatif. S’il est utilisé, les valeurs peuvent être:
ADC_START_AND_READ (continually takes readings, this is the default)
ADC_START_ONLY (starts the conversion and returns)
ADC_READ_ONLY (reads last conversion result)
Returns: soit un entier int8 ou int16E en fonction de la directive #DEVICE ADC= directive.
Function: Cette fonction va lire la valeur numérique du convertisseur analogique-numérique..
Les appels à setup_adc(), setup_adc_ports() et set_adc_channel() doit être faite un peu avant
que cette fonction est appelée.
Examples:
setup_adc( ADC_CLOCK_INTERNAL );
#DEVICE 8 bit 10 bit 11 bit 12 bit 16 bit
setup_adc_ports( ALL_ANALOG );
ADC=8 00-FF 00-FF 00-FF 00-FF 00-FF set_adc_channel(1);
ADC=10 x 0-3FF x 0-3FF x while ( input(PIN_B0) ) {
ADC=11 x x 0-7FF x x delay_ms( 5000 );
ADC=16 0-FF00 0-FFC0 0-FFEO 0-FFF0 0-FFFF value = read_adc();
printf("A/D value = %2x\n\r", value);
}
read_adc(ADC_START_ONLY);
sleep();
value=read_adc(ADC_READ_ONLY);
21
adc_done( )
Syntax: value = adc_done();
Parameters: None
Returns: A short int. TRUE if the A/D converter is done with conversion, FALSE if it is still busy.
Function: Can be polled to determine if the A/D has valid data.
Availability: Only available on devices with built in analog to digital converters
Requires: None

Examples:
int16 value;
setup_adc_ports(AN0_AN1_VSS_VREF);
setup_adc(ADC_CLOCK_DIV_4|ADC_TAD_MUL_8);
set_adc_channel(0);
read_adc(ADC_START_ONLY);

int1 done = adc_done();


while(!done) {
done = adc_done();
}
value = read_adc();
printf(“A/C value = %LX\n\r”, value);
} 22
23
PIC16F877A ADC example with CCS PIC C compiler
The microcontroller PIC16F877A has 8 ADC (Analog to Digital Converter) channels. The resolution
of the PIC16F877A ADC is 10-bit, which means the analog value after conversion is stored as a 10-
bit number that varies from 0 to 1023 (0x3FF). The A/D module has high and low-voltage reference
input that is software selectable to some combination of VDD, VSS, RA2 or RA3.
PIC16F877A ADC example with CCS C:
This is an example to start with the ADC module, example circuit schematic is shown below:

The LCD used to display the analog value


after conversion where channel 0 (RA0) is
the analog input and analog signal can be
changed from the potentiometer. The
output analog voltage of the potentiometer
varies from 0V to 5V.
The reference voltage is set to VDD (+5V)
and GND.
24
25
26
27
Unipolar stepper motor control using PIC16F877A microcontroller
Interfacing unipolar stepper motor with PIC16F877A: This post shows how to control
speed and rotation direction of unipolar stepper motor using PIC16F877A
microcontroller and CCS PIC C compiler.
Usually the unipolar stepper motor has 5 wires one for motor supply and the other for
coils. This motor has 4 coils and they are connected as shown in the figure below:

The unipolar stepper motor can be controlled in full step mode or half step mode, the
usual method is the full step driving mode. The following table shows driving sequences:
STEP A B C D

1 0 0 1 1

2 0 1 1 0

3 1 1 0 0

4 1 0 0 1
28
Interfacing PIC16F877A with unipolar stepper motor circuit
 In the circuit there are 2 pushbuttons which are connected to RB0 and RB1 pins, they are used to
choose motor rotation direction.
 To control the stepper motor speed a potentiometer (10K) is used and it is connected to analog
channel 0(AN0).
 ULN2003 (or ULN2004) chip is used to energize the stepper motor coils.
 The ULN2003(ULN2004) is a Darlington transistor array which contains seven open collector
Darlington pairs with common emitters. For stepper motor controller we need 4 transistors form
this chip which means 4 inputs and 4 outputs are needed.

29
Interfacing PIC16F877A with unipolar stepper motor circuit
 Instead of the ULN2003 chip, another chip can be used which is L293D dual H-bridge circuits as
shown in the circuit schematic below.
 For the L293D chip VS voltage always +5V and VSS voltage is the same as the motor voltage for
example if the motor voltage is 12V VSS should be connected to +12V power supply.

30
31
Bipolar stepper motor control with PIC16F877A microcontroller
Cd-Rom bipolar stepper motor control using PIC16F877A microcontroller and CCS PIC C compiler
 Most of cd-rom or dvd-rom drives has a bipolar stepper motor, this motor has two windings and
each winding has 2 inputs which means that this type of motor has 4 wires.
 This topic shows circuit schematic and C code for controlling the bipolar
stepper motor speed and direction using PIC16F877A microcontroller.
 The stepper motor can be controlled in full step mode or half step mode.
 The full step mode is a little bit easier than the half step control mode. In this
topic the full step control mode is used.
 To control the bipolar stepper motor we need two H-bridge circuits and for that L293D motor
driver chip is used, this cheap chip can work as a dual H-bridge drivers.
 In the full step control mode always both windings are energized according to the following two
tables:

 where table1 shows the driving sequence for  And the following table shows driving
rotation direction 1: sequence for the other rotation direction

32
Interfacing PIC16F877A with bipolar stepper motor circuit
 The following image shows circuit schematic diagram of this project where two pushbuttons are
used to choose motor rotation direction and a potentiometer to control motor speed.
 PortB internal pull-ups are enabled in the C code.

 A pot connected to AN0 is used to control the speed of the stepper motor.
 The microcontroller reads the analog data from AN0 and uses the digital value to change the
delay between motor driving phases and when the delay increases the motor speed decreases and
vise versa.
 The two pushbuttons which are connected to RB0 and RB1 are used to choose motor rotation
direction and when RB0 button pressed the driving sequence follows the first table above and when
RB1 button is pressed the driving sequence follows the second table. 33
 8-bit ADC resolution is used.
34
35
36
http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html
https://www.youtube.com/watch?v=qr84YO2hMtw

37
http://320volt.com/en/ccs-lm35-temperature-sensor-example-with-pic16f877-lcd/

38
39
MPLAB PROGRAMMING USING HI-TECH PIC16 COMPILER while(1)
#include<htc.h> {
#define _XTAL_FREQ 4000000 t=(adc()*500)/1024;
int h; a=t/10;
int adc(); b=t%10;
int adc() if(t>=25)//this is for buzzer
{ {
ADCON1=0x80; PORTD=0x02;
ADCON0=0x01; }
ADGO=1; PORTC=0x02;
while(ADGO==1); PORTB=arr[a];
h=ADRESH; __delay_ms(5);
h<<8; PORTB=0xff;
h=h+ADRESL; PORTC=0x01;
return(h); PORTB=arr[b];
} __delay_ms(5);
main() PORTB=0xff;
{ }
int }
i,a,x,t,b,arr[]={0x03,0x9f,0x25,0x0d,0X99,0X49,0Xc1,0x1f,0x0
1,0x19};
TRISB=0x00;
TRISC=0x00;
TRISD=0x00;
PORTB=0xff;
PORTD=0x00;

40
http://exeltronic.blogspot.com/2016/03/detecteur-dobjet-distance-sensor.html
http://exeltronic.blogspot.com/2015/01/capteur-temperature-tc1047a-et.html
https://www.youtube.com/watch?v=hcxOs4dKE_E&list=PLwX8VyNMoKQ5uWLt6fkuyfQmxd9W3T
https://www.youtube.com/watch?v=XPtTGaOB3no&list=PLwX8VyNMoKQ5uWLt6fkuyfQmxd9W3T
https://www.youtube.com/watch?v=d2xkvs7Fi-k&index=6&list=PL4YGUrOWs0NQTR11bc7AU35rs

https://www.youtube.com/watch?v=qOO8Kggzz1E&list=PLF-qcfymUY4VvPaoquGoQ5auW_n968P4
https://www.youtube.com/watch?v=We0O2gsx4Bw
https://www.youtube.com/watch?v=uByfh_VDpRw&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZ
https://www.youtube.com/watch?v=eYDQLs5iD2A&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZK
https://www.youtube.com/watch?v=rmuK6bY-aiI&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZK&
https://www.youtube.com/watch?v=iyCSJsZNm9E&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZK
https://www.youtube.com/watch?v=yCDCaPQU_Xw&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNk
https://www.youtube.com/watch?v=rRi6oCehEPM&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZK
https://www.youtube.com/watch?v=Zr5g2jTRk0Q&list=PLMFrcR3zyPe_BUvi1y3nzE4GNEqkUNkZK
https://www.youtube.com/watch?v=P8rcSEd4Q3k&index=9&list=PLF-qcfymUY4VvPaoquGoQ5auW

41
42

Вам также может понравиться