Microcontrôleurs : Formation Continue
Microcontrôleurs : Formation Continue
LES MICROCONTRÖLEURS
INTERRUPTIONS
1- PRESENTATIONS DU PIC
PIC 16F628A
Une interruption est un programme qui a une totale priorité sur le déroulement global d’un programme.
PROGRAM SQUELETTE_INTERRUPTION
Begin
…………
Mise à zéro de l’indicateur correspondant à la source d’interruption utilisée
End ;
Begin
…………
While true do
Begin
………..
End;
End.
1
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Port A
Port B
2-2 CARACTERISTIQUES
2
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PIE2 N.I. Réservé N.I. EEIE BCLIE N.I. N.I. CCP2IE -r-0 0--0
PIR2 N.I. Réservé N.I. EEIF BCLIF N.I. N.I. CCP2IF -r-0 0--0
EECON1 EEPGD — — — WRERR WREN WR RD x--- x000
TXSTA CSRC TX9 TXEN SYNC — BRGH TRMT TX9D 0000 -010
RCSTA SPEN RX9 SREN CREN ADDEN FERR OERR RX9D 0000 000x
CCPxCON — — DCxB1 DCxB0 CCPxM3 CCPxM2 CCPxM1 CCPxM0 --00 0000
T1CON — — T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON --00 0000
T2CON — TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0 -000 0000
SSPCON WCOL SSPOV SSPEN CKP SSPM3 SSPM2 SSPM1 SSPM0 0000 0000
SSPCON2 GCEN ACKSTAT ACKDT ACKEN RCEN PEN RSEN SEN 0000 0000
SSPSTAT SMP CKE D/A P S R/W UA BF 0000 0000
CCP1CON — — CCP1X CCP1Y CCP1M3 CCP1M2 CCP1M1 CCP1M0 --00 0000
RCSTA SPEN RX9 SREN CREN ADDEN FERR OERR RX9D 0000 000x
CCP2CON — — CCP2X CCP2Y CCP2M3 CCP2M2 CCP2M1 CCP2M0 --00 0000
ADCON0 ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE — ADON 0000 00-0
ADCON1 ADFM — — — PCFG3 PCFG2 PCFG1 PCFG0 0--- 0000
TRISx 1111 1111
3
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
3-LES INTERRUPTIONS
Sur ce modèle ils sont au nombre de 10 (voir data sheet)
D’après le data sheet Il existe plusieurs registre ayant un rapport avec l’interruption exemple le
registre INTCON
4
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Bit 0: RBIF = RB Port Change Interrup Flag bit. Ce drapeau doit être remis à zéro par le
programme.
1 = Quand au moins une entrée du port B (de RB4 à RB7) a changé d’état.
0 = Aucune entrée de RB4 à RB7 n’a changé d’état.
4 –APPLICATIONS
EXERCICE D APPRENTISSAGE
U1
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
RB2
2 24
RA0/AN0 RB3/PGM
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
11
RC0/T1OSO/T1CKI
1 12
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
1° en se référant au schéma ci-dessus écrire un programme pour un compteur modulo 9.
5
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
program counter;
var i:integer;
begin
trisc:=$00;
portc:=0;
i:=0;
R1
10k
U1
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
RB2
2 24
RA0/AN0 RB3/PGM
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
11
RC0/T1OSO/T1CKI
1 12
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
6
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
program MyProject;
var j, i:integer;
procedure interrupt;
begin
for j:=…………………………………………..
begin
porta:= …………………………….…………
delay_ms(500);
…………………………………………..………………
end; end;
begin
j:=9;
trisa:=0;
intcon…………………………………………;
trisc:=0;
trisb:=$01;
portc:=0;
i:=0;
for i:=0 to 9 do
begin
portc:=i;
delay_ms(500);
end; end;
end.
On désire allumer une led branché sur RA0 chaque changement d’état sur RB0
7
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PROGRAMME COMMENTAIRE
program EXERCICE1;
PROCEDURE INTERRUPT;
BEGIN
IF PORTA.0=0 THEN PORTA.0:=1 ELSE
PORTA.0:=0;
…………………………………………………….
END; // remise à 0 du bit d'état (flag)
BEGIN
TRISB:=$FF;
TRISA:=$00;
INTCON…………………………………………………….
[Link]……………………………………
[Link]……………………………………
// activation de l'interruption RB0/INT
[Link]………………………………………
WHILE TRUE DO // activation générale des interruptions
BEGIN END; end.
EXERCICE 2
Clignotement d’une diode led D1 branché sur RC0 et signalisation d’une interruption externe par D2
delay_ms(1000); // attente de 1s
end;
end.
C1 C2
15p 15p
X1
U1 P1
4MHz
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
2
RB2
24 R3
RA0/AN0 RB3/PGM 4k7
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
7
RA4/T0CKI/C1OUT RB7/PGD D1
RA5/AN4/SS/C2OUT R1
11
RC0/T1OSO/T1CKI
1 12 220R
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL D2
RC4/SDI/SDA
15 R2
16
RC5/SDO
17 220R
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
9
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 3
program ex3;
var x0,x1,x2,x3,T1 : integer;
procedure interrupt;
begin
………………………………………………..……………..………………………
…………………………………………………………………..……………………
……………………………………………………………….………………………
end;
begin
adcon1:=7; trisa:=$00; trisb:=$ff;
porta:=0; portb:=0;
x0:=1; x1:=0; x2:=0; x3:=0;T1:=0;
INTCON := 0x90;
while true do begin
if x0 and portb.1 then begin
x1:=1; x0:=0;end;
if x1 and portb.2 then begin x2:=1; x1:=0;
end; if x2 and T1=1 then begin x3:=1;
x2:=0; end; if x3 and portb.3 then begin
x0:=1; x3:=0; end;
if x1 then porta.0:=1 else porta.0:=0;
if x2=0 then T1:=0 else
a-Compléter le Grafcet relatif a ce
begin T1:=0;
vdelay_ms(2000); T1:=1;
programme
end ; b- L’action sur RB0 entraine l’initialisation du
if x3 then porta.1:=1 else porta.1:=0; Grafcet
end; compléter les instructions nécessaires pour la
end. procédure interrupt sur le Port b
10
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 4
Soit à réaliser une interruption sur RB0 , sur RB4, RB5 , RB6, RB7. Avec pic 16f876A(voir schémas ci-
dessous)
Une action sur a (branché sur RB0 ) allume la led D1 (branché sur RA5 ) pendant 1s,
Une action sur b (branché sur RB4 ) allume la led D5 (branché sur RA0) pendant 1s
Une action sur c (branché sur RB5 ) allume la led D4 (branché sur RA1) pendant 1s
Une action sur d (branché sur RB6 ) allume la led D3 (branché sur RA2) pendant 1s
Une action sur d (branché sur RB7 ) allume la led D2 (branché sur RA3) pendant 1s
b
U1
9 21
10
OSC1/CLKIN RB0/INT
22 c
OSC2/CLKOUT RB1
1 23
MCLR/Vpp/THV RB2
24
2
RB3/PGM
25 d
RA0/AN0 RB4
3 26
RA1/AN1 RB5
4 27
5
RA2/AN2/VREF- RB6/PGC
28 e
RA3/AN3/VREF+ RB7/PGD
6
RA4/T0CKI
7 11
RA5/AN4/SS RC0/T1OSO/T1CKI
D5 D4 D3 D2 D1 RC1/T1OSI/CCP2
12
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
Bit 7 Bit 0
11
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PROGRAMME COMMENTAIRE
program EX4; …………………………………………………………………………………………………
procedure interrupt;
…………………………………………………………………………………………………
begin
//if testBit(intcon,intf)=1 then …………………………………………………………………………………………………
if [Link] =1 then
…………………………………………………………………………………………………
begin
porta.5:=1; …………………………………………………………………………………………………
//setbit(porta,5);
…………………………………………………………………………………………………
delay_ms(1000);
//clearBit(intcon,intf) …………………………………………………………………………………………………
[Link]:=0;
…………………………………………………………………………………………………
end;
if testBit(intcon,rbif)=1 then …………………………………………………………………………………………………
//if [Link]=1 then
…………………………………………………………………………………………………
begin
if (portb.4=1)then …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
porta:=$01;
delay_ms(1000);end else …………………………………………………………………………………………………
if(portb.5=1) then
…………………………………………………………………………………………………
begin
porta:=$02; …………………………………………………………………………………………………
delay_ms(1000);end else
…………………………………………………………………………………………………
if (portb.6=1) then
begin …………………………………………………………………………………………………
porta:=$04;
…………………………………………………………………………………………………
delay_ms(1000);end else
if(portb.7=1) then …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
porta:=$08;
delay_ms(1000); …………………………………………………………………………………………………
end;
…………………………………………………………………………………………………
clearBit(intcon,rbif);
end;end; …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
trisa:=$00;
porta:=$00; …………………………………………………………………………………………………
trisb:=$ff;
…………………………………………………………………………………………………
intcon:=$98;
while(1=1)do …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
porta:=$00;
end;
end.
begin
end.
12
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 5
soit à réaliser une interruption sur RB0 , sur RB5 ou RB6 avec pic 16f628A
pendant 1s LED-RED
D2 LED-RED
R4
1-Compléter le U1 registre intcon
220
16 17
15
RA7/OSC1/CLKIN RA0/AN0
18 D3
RA6/OSC2/CLKOUT RA1/AN1
1
R5
RA2/AN2/VREF LED-RED
4 2
RA5/MCLR RA3/AN3/CMP1
3
220 a
RA4/T0CKI/CMP2
6
RB0/INT
7
RB1/RX/DT
8 b 10k
RB2/TX/CK
9
RB3/CCP1
10
RB4
11 10k
RB5
12 c
RB6/T1OSO/T1CKI
13
RB7/T1OSI
PIC16F628A
R6 R1 R2
10k
13
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PROGRAMME COMMENTAIRE
program EX5; …………………………………………………………………………………………………
PROCEDURE INTERRUPT; …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
IF [Link] =1 then
begin …………………………………………………………………………………………………
porta:=$01; …………………………………………………………………………………………………
delay_ms(1000);
…………………………………………………………………………………………………
[Link] :=0;
end; …………………………………………………………………………………………………
IF [Link]=1 then …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
if (portb.5=1) then
begin …………………………………………………………………………………………………
porta:=$02; …………………………………………………………………………………………………
delay_ms(1000);
…………………………………………………………………………………………………
end else
…………………………………………………………………………………………………
if (portb.6=1) then
begin …………………………………………………………………………………………………
porta:=$04; …………………………………………………………………………………………………
delay_ms(1000);
…………………………………………………………………………………………………
end;
[Link]:=0; …………………………………………………………………………………………………
end; …………………………………………………………………………………………………
end;
…………………………………………………………………………………………………
begin
trisa:=$00; …………………………………………………………………………………………………
porta:=$00; …………………………………………………………………………………………………
trisb:=$ff;
…………………………………………………………………………………………………
intcon:=$98;
while (1=1)do …………………………………………………………………………………………………
begin …………………………………………………………………………………………………
porta:=$00; …………………………………………………………………………………………………
end;
…………………………………………………………………………………………………
end.
…………………………………………………………………………………………………
14
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
…………………………………………………………………………………………………
PROGRAMME COMMENTAIRE
program ex5bis; …………………………………………………………………………………………………
PROCEDURE INTERRUPT; …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
IF [Link] =1 then …………………………………………………………………………………………………
begin
…………………………………………………………………………………………………
porta:=$01;
delay_ms(1000); …………………………………………………………………………………………………
……………………………………….…………………………. …………………………………………………………………………………………………
end;
…………………………………………………………………………………………………
…………………………………………………………………………………………………
end;
…………………………………………………………………………………………………
begin
trisa:=$00; …………………………………………………………………………………………………
porta:=$00; …………………………………………………………………………………………………
trisb:=$ff;
…………………………………………………………………………………………………
intcon………………………………………...
while (1=1)do …………………………………………………………………………………………………
begin …………………………………………………………………………………………………
porta:=$00;
15
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
end; …………………………………………………………………………………………………
end.
EXERCICE 6
Une action sur SW1 D3 S’allume Une autre action sur SW1 D1 s’éteint.
SW2 allume D2 .
U1
16 17
RA7/OSC1/CLKIN RA0/AN0
15 18
RA6/OSC2/CLKOUT RA1/AN1
1
RA2/AN2/VREF
4 2
RA5/MCLR RA3/AN3/CMP1
3
RA4/T0CKI/CMP2
RB0/INT
6 D1 D2 D3
7
RB1/RX/DT
8
RB2/TX/CK
9
RB3/CCP1
10
RB4
RB5
11 R7
12
RB6/T1OSO/T1CKI
13 SW1 220
RB7/T1OSI
PIC16F628A
SW2
SW3
SW4
10k
16
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PROGRAMME COMMENTAIRE
program EX6;
VAR
bIntRB0: boolean ;
bIntRB4: boolean ;
iTmp: byte;
procedure interrupt;
begin
if [Link] ………………. then begin
[Link]: …………………….
bIntRB0:=true; interruption par RB0
end;
remise a 0 du flag
if [Link]=…………… then begin
[Link]: ………………… intrruption par RB4 à RB7
iTmp:=portb;
remise a 0 du flag
bintRB4:=true;
end; end; Lecture du portb
begin
trisa:=%00000000;
trisb:=%01111111;
intcon:=$00;
[Link]: …………………………………..
[Link]: …………………………………..
[Link]: ……………………………………..
[Link]: ………………………………………. Activation de l’interruption sur rb0
[Link]: ………………………………………… Remise à 0 du flag
porta:=$00;
bIntRB0:=false; Activation de l’interruption sur rb4 à rb7
bintRB4:=false; Mise à 0 du flag
while true do begin
if bIntRB0=true then begin Activation générale des interruptions
porta.0:=porta.0 xor 1;
bIntRB0:= false;
end;
if bIntRB4=true then
begin
porta.1:=( PORTB.4 or PORTB.6);
porta.2 :=(PORTB.5 or PORTB.6);
bintRB4:= false;
end;end;
begin
end.
17
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 7
program EX7;
var
D1 : sbit at portc.0;
D2 : ………………………………………………………………….
D3 : ………………………………………………………………….
D4 : ………………………………………………………………….
D5 : ………………………………………………………………….
etat : byte;
Procedure Interrupt;
begin
etat:=portb; // lecture du port B pour déverrouiller l'accès au bit RBIF
D2:=etat.4; //
D3:=etat.5;
D4:=etat.6;
D5:=etat.7;
delay_ms(500); // attente de 500ms
INTCON……………………………………………………………….. // Remise à zéro de l'indicateur RBIF
INTCON……………………………………………………………….. // Réactivation Globale des interruptions
end;
begin
TrisC:……………………………………………………….. // RC0, RC1, RC2, RC3, RC4 : sorties. Le reste: entrées
D1:=0; // état initial de la diode LED D1
D2:=0; // état initial de la diode LED D2
D3:=0; // état initial de la diode LED D3
D4:=0; // état initial de la diode LED D4
D5:=0; // état initial de la diode LED D5
INTCON:………………………………………………………………. // Validation Globale des interruptions + validation de RB0/INT
while true do
begin
D1:=1; // allumer la diode LED D1
delay_ms(1000); // attente de 1s
D1:=0; // eteindre la diode LED D1
delay_ms(1000); // attente de 1s
end;
end.
18
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
CD
R3
4k7
CG
R4
C1 C2 4k7
15p
CAV
15p R5
X1 4k7
CAR
R6
4MHz
U1 4k7
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
RB2
2 24
RA0/AN0 RB3/PGM
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
7
RA4/T0CKI/C1OUT RB7/PGD D1
RA5/AN4/SS/C2OUT R1
11
RC0/T1OSO/T1CKI
1 12 220R
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL D2
RC4/SDI/SDA
15 R2
16
RC5/SDO
17 220R
RC6/TX/CK
18
RC7/RX/DT D3
R7
PIC16F876A
220R
D4
R8
220R
D5
R9
220R
5-LE TMR 0
1- Le mode timer
2- Le mode compteur
19
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
REGISTRE OPTION_REG
20
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
000 /2 /1
001 /4 /2
010 /8 /4
011 /16 /8
110 /128 64
1- Le mode timer
Pour configurer le module TMR0 en mode timer, il faut au préalable que :T0CS = 0 (bit 5 du
registre OPTION_REG) Le contenu du registre TMR0 est alors incrémenté à chaque cycle de l'horloge
interne.
1 cycle correspond à une durée de 1 µs pour un oscillateur à quartz de 4 MHz (1 cycle = 4 / F OSC).
cela est vrai si le taux de prédivision (prescaler) est réglé à 1 (bit 3 du registre OPTION_REG PSA = 1).
Avec un taux de prédivision de 128 (PSA = 0, PS2 = 1, PS1 = 1, PS0 = 0), le contenu du registre
TMR0 est incrémenté tous les 128 cycles (ou 128 µs).
Notez que si le contenu du registre TMR0 est 255 (0xFF), il passera à 0 (0x00) à la prochaine
incrémentation. La fréquence des interruption est donnée par la formule suivante
Fosc 1 1
Fint X X
4 PREDIV 256
4 X PREDIV X 256
Tint
Fosc
21
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
2- Le mode compteur
Pour configurer le module TMR0 en mode compteur, il faut au préalable que :
Le contenu du registre TMR0 est alors incrémenté à chaque front du signal présent sur la broche
RA4/T0CKI
cela est vrai si le taux de prédivision (prescaler) est réglé à 1 (bit 3 du registre OPTION_REG =
PSA = 1).
Avec un taux de prédivision de 32 (PSA = 0, PS2 = 1, PS1 = 0, PS0 = 0), le contenu du registre TMR0
est incrémenté tous les 32 cycles du signal présent sur la broche RA4/T0CKI.
EXERCICE A
Soit les configurations ci-dessous
1°) Compléter la configuration des registres option_reg puis calculer la période pour chaque
configuration
a)
22
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
T=512µs
b)
T=256 µs
c)
T=65536 µs
UTILISATION EN MODE TIMER
EXERCICE 1
Soit à clignoter une led toute les 20ms
23
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Dou
4 X 20.10 3 X 106
PREDIV 78.125s
4 X 256
Donc on choisit du tableau soit 64 soit 128
12ms x
program EX1;
EXERCICE 2
On désire réaliser un compteur modulo 256 en utilisant un microcontrôleur de type PIC 16F628A
1
1
1
1
1
1
1
1
U1
16 17
RA7/OSC1/CLKIN RA0/AN0
15 18
RA6/OSC2/CLKOUT RA1/AN1
1
RA2/AN2/VREF
4 2
RA5/MCLR RA3/AN3/CMP1
3
RA4/T0CKI/CMP2
6
RB0/INT
7
RB1/RX/DT
8
RB2/TX/CK
9
RB3/CCP1
10
RB4
11
RB5
12
RB6/T1OSO/T1CKI
13
RB7/T1OSI
PIC16F628A
25
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EX2 COMMENTAIRE
program EX2;
PROCEDURE INTERRUPT;
begin
portB:=tmr0; ………………………………………………………………………………………
end;
begin
trisB:=0x00;
portb:=$ff;
option_reg:=0x00;
……………………………………………………………………………………
[Link]:=1;// gie_bit:=1;
intcon.t0ie:=1; ……………………………………………………………………………………
intcon.t0if:=0; ……………………………………………………………………………………
option_reg.psA:=0;
option_reg.ps0:=1;//regler le prédiviseur à
64 //ps0_bit:=1;
option_reg.ps1:=1;//
option_reg.ps2:=1; //
tmr0:=$ff;
while true do
begin
end;
end.
EXERCICE 3
Reprendre l’exercice précédent mais compteur binaire modulo 25
0
0
0
1
0
1
1
1
U1
16 17
RA7/OSC1/CLKIN RA0/AN0
15 18
RA6/OSC2/CLKOUT RA1/AN1
1
RA2/AN2/VREF
4 2
RA5/MCLR RA3/AN3/CMP1
3
RA4/T0CKI/CMP2
6
RB0/INT
7
RB1/RX/DT
8
RB2/TX/CK
9
RB3/CCP1
10
RB4
11
RB5
12
RB6/T1OSO/T1CKI
13
RB7/T1OSI
PIC16F628A
26
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
COMPTEUR
EXERCICE 4
En utilisant l’horloge externe(en mode compteur) (via RA4) sur front montant.
27
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
program EX4;
procedure interrupt;
begin
porta:=tmr0;
if porta =8 then begin tmr0: =$………………); ;//LE DEBORDEMENT S'EFFECTUE à 8 (mod9
end; end;
begin
PORTA:=0;
trisa:=$00;
[Link]:=1; ……………………………………………………………………….…………………………………………………
………………………………………………………………………………………………………………………..
intcon.t0ie:=1;
…………………………………………………………….………………………………………………………………
intcon.t0if:=0; ………………………………………………………………………………………………………………………….
option_reg.T0CS :=1; ………………………………………………………………………….……………………..
option_reg.PSA :=1; …………………………………………………………………………………………………
option_reg.T0SE :=0; ……………………………………………………………………………………………….…
tmr0 :=$ff; ………………………………………………………………………………………………….
intcon.t0if:=0;
while true do
begin
end;
end.
EXERCICE5
Soit à réaliser un générateur d impulsion à la fréquence f=1KHZ
U1
16 17
RA7/OSC1/CLKIN RA0/AN0 A
15 18
RA6/OSC2/CLKOUT RA1/AN1
1
RA2/AN2/VREF B
4 2
RA5/MCLR RA3/AN3/CMP1
3
RA4/T0CKI/CMP2 C
6
RB0/INT D
7
RB1/RX/DT
8
RB2/TX/CK
9
RB3/CCP1
10
RB4
11
RB5
12
RB6/T1OSO/T1CKI
13
RB7/T1OSI
PIC16F628A
28
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
porta.0:=porta.0 xor 1;
end;
begin
trisa:=$00;
option_reg :=$00;
[Link]:=1;
intcon.t0ie:=1;
intcon.t0if:=0;
tmr0 :=$00;
while true do
begin
end;
end.
I- Introduction :
Ce module est constitué d'un convertisseur Analogique Numérique 10 bits dont l'entrée analogique peut
être connectée sur l'une des 5 entrées analogiques externes. On dit qu'on a un CAN à 5 canaux. Les
entrées analogiques doivent être configurées en entrée à l'aide du registre TRISA.
Le contrôle du module se fait par les deux registres ADCON0 et ADCON1
29
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Bits « 5-4-3 » :CHS2 CHS1 CHS0 : bits de sélection du canal. Ce type de PIC possède 5 entrées
analogiques possibles
30
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Bit « 2 » : GO/DONE : une conversion démarre quand on place ce bit à 1. A la fin de la conversion,
il est remis automatiquement à zéro. Ce bit peut aussi être positionné automatiquement par le module
CCP2.
Bit « 1 » : non utilisé lire « 0 ».
conversion.
2- ADCON1 :
ADFM = 1 : justification à droite, c'est-à-dire que les 6 bits du poids fort de ADRESH sont à
000000XX XXXXXXXX
ADFM = 0 : justification à gauche, c'est-à-dire que les 6 bits du poids faible de ADRESL sont à
Bits « 3 à 0 » : PCFG3 PCFG2 PCFG1 PCFG0 : Configuration des E/S et des tensions de références.
Les 5 broches de PORTA peuvent être configurés soit en E/S digitales, soit en entrées analogiques.
31
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
32
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 1
?
?
?
?
?
?
?
?
?
?
potentiomètre branché entre
U1
RA2 et la masse, et affiche le
13 33
OSC1/CLKIN RB0/INT
14 34
RV1 OSC2/CLKOUT RB1
35
RB2
2 36
résultat sous forme binaire avec
RA0/AN0 RB3/PGM
3 37
RA1/AN1 RB4
4 38
RA2/AN2/VREF-/CVREF RB5
5 39
RA3/AN3/VREF+ RB6/PGC
6
7
RA4/T0CKI/C1OUT
RA5/AN4/SS/C2OUT
RB7/PGD
40
des LEDs.
1k 15
RC0/T1OSO/T1CKI
8 16
RE0/AN5/RD RC1/T1OSI/CCP2
9 17
RE1/AN6/WR RC2/CCP1
10 18
RE2/AN7/CS RC3/SCK/SCL
23
RC4/SDI/SDA
1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
PIC16F877A
Justification à droite
(ADCON0)
33
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE2
Reprendre l’exercice précédent avec affichage de (0.5v) ,1 v ,1.5 v ,2v ,……..5v
D1
R2
220R
C1
D2
R3
22p
X1
R1 C2 CRYSTAL D3
10k R4
U1
22p 9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
1 23
MCLR/Vpp/THV RB2
24 D4
2
RB3/PGM
25
R5
RA0/AN0 RB4
3 26
RA1/AN1 RB5
4 27
RV1 RA2/AN2/VREF- RB6/PGC
5 28
6
RA3/AN3/VREF+ RB7/PGD D5
7
RA4/T0CKI
11
R6
RA5/AN4/SS RC0/T1OSO/T1CKI
12
RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL
15 D6
RC4/SDI/SDA
16
R7
5k
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876
D7
R8
D8
R9
D9
R10
D10
R11
34
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
TRISA:=$ff; end;
TRISB:=%00000000; end.
35
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
DOSSIER TECHNIQUE
Les broches 15 et 16 ne sont présentes que sur les afficheurs LCD avec retroéclairage.
1 VSS - Masse
7 D0 TTL
8 D1 TTL
9 D2 TTL
10 D3 TTL
Bus de données bidirectionnel 3 états (haute impédance lorsque E=0)
11 D4 TTL
12 D5 TTL
13 D6 TTL
14 D7 TTL
15 A - Anode rétroéclairage (+5V)
EXERCICE3
Exemple de brochage d’un afficheur LCD
36
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
VAR lcd_init;
//rc0_bit
//portc.1
lcd_D5_direction :………………………………………………
lcd_D6_direction :……………………………………………..
lcd_D7_direction :……………………………………………….
begin
trisc:=$00;
trisa:=$00;
LCD1
L M 0 32L
VDD
VSS
VEE
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
U1
9 21
O S C 1 /C L K IN R B 0 /IN T
10 22
O S C 2 /C L K O UT R B1
23
R B2
2 24
R A 0 /AN0 R B 3 /PGM
3 25
R A 1 /AN1 R B4
4 26
R A 2 /A N 2 /V R E F -/CVREF R B5
5 27
R A 3 /A N 3 /VREF+ R B 6 /PGC
6 28
R A 4 /T0 C K I/C 1 O U T R B 7 /PGD
7
R A 5 /A N 4 /S S /C 2OUT
11
R C 0 /T1 O S O /T1 C K I
1 12
M C L R /V p p /THV R C 1 /T1 O S I/C C P2
13
R C 2 /C CP1
14
R C 3 /S C K /SCL
15
R C 4 /S D I/SDA
16
R C 5 /S DO
17
R C 6 /TX /CK
18
R C 7 /R X /DT
P IC 1 6 F 8 76A
37
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE4
Soit à convertir une tension variable de 0 à 5v branchée entre RA0 et la masse avec avec
program EX4;
var
valeur_conversion : word ;//2 octets car le resultat de conversion est sur 10 bits
// Connections de l'LCD
LCD1
LM032L
C1 C2
15p 15p
VDD
VSS
VEE
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
E
X1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4MHz
U1
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
50%
P1 RB2 RV1
2 24
RA0/AN0 RB3/PGM
3 25 10k
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
50%
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
11
10k RC0/T1OSO/T1CKI
1 12
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
38
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
begin
TRISA:=$FF;
TRISB:=$00;
TRISC:=$00;
adcon1:=%............................
adcon0:=%............................
while true do
begin
valeur_conversion := ……………………………………..
variable_calcul := …………………………………………………………………………………………………………..
tension:= word(variable_calcul );
// en texte
// l'affichage
end; end.
39
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 5 (ctn)(pic16f88)
La résistance et la tension d’une CTN varie suivant le tableau suivant
simuler.
([Link]
1k
1%
RV3
U1
16 17
RA7/OSC1/CLKIN RA0/AN0
15 18
RA6/OSC2/CLKOUT RA1/AN1
1
RA2/AN2/CVREF/VREF-
4 2
RA5/MCLR RA3/AN3/C1OUT/VREF+
3
RA4/AN4/T0CKI/C2OUT
RB0/INT/CCP1
6 D1 D2 D3 D4 D5 D6 D7 D8
7
RB1/SDI/SDA
8
RB2/SDO/RX/DT
9
RB3/CCP1
10
RB4/SCK/SCL
11
RB5/SS/TX/CK
12
RB6/AN5/T1OSO/T1CKI
RB7/AN6/T1OSI
13 R1 R8
330 330
PIC16F88
+88.8
Volts
40
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
PROGRAM CTN ;
Var
iADC0 : word ;
Out_led_1 :sbit at portb.0 ;
Out_led_2 :sbit at portb.1 ;
Out_led_3 :sbit at portb.2 ;
Out_led_4 :sbit at portb.3 ;
Out_led_5 :sbit at portb.4 ;
Out_led_6 :sbit at portb.5 ;
Out_led_7 :sbit at portb.6 ;
Out_led_8 :sbit at portb.7 ;
Begin
CMCON :=7 ;
TRISA := $ff ;
TRISB :=$00 ;
ANSEL :=%00001101 ;
ADCON1 := 00 ;
ADC_init;
PORTB :=$00 ;
While true do
Begin
iADC0 := ADC_read(0);
Out_led_1 :=(iADC0< 80);
Out_led_2 := (iADC0 >=80) and (iADC0<132);//-10°Cà 0°C
Out_led_3 := (iADC0 >=132) and (iADC0<203);//0°C à 10°C
Out_led_4 := (iADC0 >=203) and (iADC0<294);//10°C à
20°C;
Out_led_5 := (iADC0 >=294) and (iADC0<504);//20°C à
40°C;;
Out_led_6 :=(iADC0 >=504) and (iADC0<694);//400°C à
50°C;;
Out_led_7 :=(iADC0 >=694) and (iADC0<826);//60°C à
80°C;;
Out_led_8 :=(iADC0 >=826);//>80°C
END;
END.
41
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Les broches permettant de produire un signal PWM sont les bits 1 et 2 du PORTC
(RC1/CCP2 et RC2/CCP1).
Il faut dans un premier temps configurer ces broches comme étant des sorties grâce au
registre de configuration du PORTC : TRISC (voir document ressource sur la configuration des
ports d’entrées/sorties).
Les broches utilisées (RC1/CCP2 et RC2/CCP1) pour la production du signal PWM peuvent
être utilisées dans plusieurs types de fonctionnement :
-entrées/sorties du PORTC.
-mode de capture (capture mode) : c’est un mode qui permet de mesurer le temps
qui sépare deux fronts.
-mode de comparaison (compare mode) : c’est un mode qui permet de produire un
état logique (haut ou bas) en fonction du temps écoulé.
b7 b6 b5 b4 b3 b2 b1 b0
- - CCPxX CCPxY CCPxM3 CCPxM2 CCPxM1 CCPxM0
[CCPxM3…CCPxM0] : ce sont ces bits qui permettent de sélectionner le mode de fonctionnement des
broches.
[0000] : le mode de capture, le mode de comparaison et le mode de production du signal PWM ne
sont pas valides.
[0100] : mode de capture tous les fronts descendants.
[0101] : mode de capture tous les fronts montants.
[0110] : mode de capture tous les 4 fronts montants.
[0111] : mode de capture tous les 16 fronts montants.
[1000] : mode comparaison, mise à 1 de la sortie lorsque le bit CCPxIF passe à un. [1001]
: mode comparaison, mise à 0 de la sortie lorsque le bit CCPxIF passe à un. [1010] : mode
42
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
La sortie du TIMER2 est le registre 8 bits TMR2 qui correspond aux nombres d’impulsions sur
l’entrée du compteur CK.
La fréquence du signal FCK dépend de la fréquence d’horloge FOSC/4 du pic 16F876
Pour gérer des durées plus élevées, on a la possibilité de diviser la fréquence FOSC/4 comme suit
1, 4 ou 16. Cette division est gérée par les bits T2CKPS1 et T2CKPS0.
43
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
b7 b6 b5 b4 b3 b2 b1 b0
- TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
44
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
TOSC : c’est la période (en secondes) de l’horloge du PIC16F876. TMR2 prescale value : c’est
la pré-division du TIMER2
EXERCICE 1
1°Calculer la valeur de PR2 pour obtenir une période T=1ms
T 1.10 3
PR 2 1 249
4 XToscX Pr édiviseur 1
4X X4
4.10 6
2°Vérifier avec la calculette
T2CON
0 TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
……………… ……………… ……………… ……………… ……………… ……………… ……………… ………………
CCP1CON
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
0 0 CCP1X CCP1Y CCPM3 CCP1M2 CCP1M1 CCP1M0
…………… …………… …………… …………… …………… …………… …………… ……………
program EX1;
begin
portc:=0;
trisc:=0;
pr2:=……………………….
PWM1_start()
end.
OU
45
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EX1BIS
program EX1BIS;
begin
PWM1_Start();
end.
EXERCICE 2
On veut obtenir un signal de période T=1ms un rapport cyclique de 50%
TH=0.5ms
1° Calculer CCPRxL:CCPxCON<5:4>)=CCP1L
0.5.10 3
CCPRxL : CCPRxCON 5 : 4 CCP1L
TH
500 (0111110100)en..binaire
Tosc. Pr ediviseur 1
x4
4.10 6
3°réaliser le programme
46
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
OU
EX2BIS
program EX2BIS;
begin
PWM1_Init(……………); //fréquence volue
PWM1_Set_Duty(……………….);//rapport cyclique 50%
PWM1_Start;()
DELAY_MS(………………) ;
PWM1_Stop;()
end.
47
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 3
Reprendre L EX2 mais en variant la largeur d’inpultion par pas de 20
EXERCICE 5
Soit à commander la vitesse d’un moteur à courant continue par variation d’une tension (de 0à
5v) brancher à l’entrée RA0 et la masse, et faire la variation de vitesse en fonction ‘Va’.
On donne
Vref=VDD=5v et Vref=Vss=0V
Quartz 4MHZ
(justification à droite)
TPWM=1ms
C1 C2 B
15p 15p
C
X1 VCC
D
4MHz
U1
9 21
OSC1/CLKIN RB0/INT
10 22 M1
P1 OSC2/CLKOUT RB1
23
RB2 D1 5V
2 24
RA0/AN0 RB3/PGM
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
11
RC0/T1OSO/T1CKI
1
MCLR/Vpp/THV RC1/T1OSI/CCP2
12 R1 Q1
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
GND
4- Configurer T2CON :
5- Configurer CCP1CON :
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
0 0 CCP1X CCP1Y CCPM3 CCP1M2 CCP1M1 CCP1M0
……………. ……………. ……………. ……………. ……………. ……………. ……………. …………….
49
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
Non utilisés Complément bits Mode PWM : 11xx
T 1.10 3
PR 2 1 249
4 XToscX Pr édiviseur 1
4X X4
4.10 6
0.5.10 3
CCPRxL : CCPRxCON 5 : 4 CCP1L
TH
500 (0111110100)en..binaire
Tosc. Pr ediviseur 1
x 4
4.10 6
7- Configurer CCPR1L :
0 1 1 1 1 1 0 1 0 0
CCP1X CCP1Y
CCPR1L
Reg : CCP1CON
program EX5;
Var t : word; N : char ; Calc : real ;
begin
adc_init();
ADCON1: =%.................................... //justification à droite +choix de VDD,VSS
ADCON0:=% …………………… //Fosc/8
TRISA:=$FF;
TRISB:=%00000000;
TRISC:=0;
ccp1con: = ……………….;/ /MODE PWM et broche RC2 en sortie
t2con:=$................................; //activation du tmr2(tmr2on=1)et prédiviseur =4(t2ckps1 t2ckps0=01)
pr2:=…………………………….
while true do
begin
t:= ………………………………………. // t:= adc_read(2) Lecture du convertisseur
Calc := (………………………………………………; // calcul
N := byte(Calc) ; // transformation du résultat de calcul en octet
ccpr1l:=n;
pWM1_start();
delay_ms(10); end;
50
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
OU
program EX5BIS; t:= adc_get_sample(2); // t:= adc_read(2)
Var t : word; N : char ; Calc : real ; Lecture du convertisseur
begin Calc := (t*255)/1023; // calcul
pwm1_init(1000); N := byte(Calc) ; // transformation du
adc_init(); résultat de calcul en octet
ADCON1:=%10001110; //justification à pwm1_set_duty(N); //ccpr1l:=n;
droite +choix de VDD,VSS pWM1_start();
ADCON0:=%01010001;//Fosc/8 delay_ms(100);
while true do end;
begin END.
GESTION DU CLAVIER
Le poids faible de ce port sera réservé pour les colonnes et initialisé en entrée avec la valeur "1111"
tandis que son poids fort est affecté aux lignes et initialisé en sortie avec la valeur "0000". A noter
51
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
que les résistances PULLUP externes seront inutiles dans le cas de l'utilisation d'un micro possédant
ses propres PULLUP internes
P7 P6 P5 P4 P3 P2 P1 P0
touches LIGNES COLONNE
(Valeur ASCII => Valeur Hexa) D C B A 4 3 2 1 Code hex
'1'=>$31 1 1 1 0 1 1 1 0 $EE
'2'=>$32 1 1 1 0 1 1 0 1 $ED
'3'=>$33 1 1 1 0 1 0 1 1 $EB
'F'=>$46 1 1 1 0 0 1 1 1 $E7
'4'=>$34 1 1 0 1 1 1 1 0 $DE
'5'=>$35 1 1 0 1 1 1 0 1 $DD
'6'=>$36 1 1 0 1 1 0 1 1 $DB
'E'=>$45 1 1 0 1 0 1 1 1 $D7
'7'=>$37 1 0 1 1 1 1 1 0 $BE
'8'=>$38 1 0 1 1 1 1 0 1 $BD
'9'=>$39 1 0 1 1 1 0 1 1 $BB
'D'=>$44 1 0 1 1 0 1 1 1 $B7
'A'=>$41 0 1 1 1 1 1 1 0 $7E
'0'=>$30 0 1 1 1 1 1 0 1 $7D
'B'=>$42 0 1 1 1 1 0 1 1 $7B
'1'=>$31 0 1 1 1 0 1 1 1 $77
52
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
EXERCICE 1
Réaliser un programme pour commander l’afficheur du schémas ci-dessous par l’intermédiaire d’un
clavier matricielle 4x4
U1
16 17
OSC1/CLKIN RA0
15 18
OSC2/CLKOUT RA1
1
RA2
4 2
MCLR RA3
3
RA4/T0CKI
6
RB0/INT
RB1
7
A 7 8 9
8
RB2
9
RB3
10
RB4
RB5
11
B 4 5 6
12
RB6
13
RB7
PIC16F84A
C 1 2 3
ON
D
C 0 = +
1
53
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
TP1
1. Ecrire U n programme qui permet d’afficher ‘formation continue’ ,le code de la touche
appuyée du clavier matriciel sur LCD.
2. Faire la simulation.
3. Programmer le microcontrôleur .
4. Faire le câblage du schéma suivant sur la maquette et tester le fonctionnement.
LCD1
LM032L
C1 C2
15p 15p
X1 VDD
VSS
VEE
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
CRYSTAL E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
U1
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
1 23
MCLR/Vpp/THV RB2
24
RB3/PGM
2 25
RA0/AN0 RB4
3 26
RA1/AN1 RB5
4 27
RA2/AN2/VREF- RB6/PGC
5 28
RA3/AN3/VREF+ RB7/PGD
6
RA4/T0CKI
7 11
RA5/AN4/SS RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
12
A 7 8 9
13
RC2/CCP1
14
RC3/SCK/SCL
15
RC4/SDI/SDA
RC5/SDO
16
B 4 5 6
17
RC6/TX/CK
18
RC7/RX/DT
PIC16F876
C 1 2 3
ON
D
C 0 = +
1
4
9
8
7
6
5
4
3
2
RP1
RESPACK-8
1° METHODE
program EX2lcd;
var
kp : byte;
54
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
begin
lcd_init();
Keypad_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(2, 1, 'TOUCHE:');
while true do
begin
kp := 0;
while kp = 0 do
begin
kp := ……………………………………………….
end;
// structure du clavier;
end;
end.
2°METHODE
program CLAVLCD;
var
kp : byte;
begin
Keypad_Init(PORTC);
Lcd_Init(PORTB);
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
begin
kp := 0;
56
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
while kp = 0 do
begin
kp := Keypad_Released();
end;
// structure du clavier;
LCD_OUT(1,1,' bonjour');
end;
//until FALSE;
end.
57
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
TP3
LCD1
LM016L
VDD
VSS
VEE
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
U1
9 21
OSC1/CLKIN RB0/INT
10 22
OSC2/CLKOUT RB1
23
RB2
2 24
RA0/AN0 RB3/PGM
3 25
RA1/AN1 RB4
4 26
RA2/AN2/VREF-/CVREF RB5
5 27
RA3/AN3/VREF+ RB6/PGC
6 28
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
11
RC0/T1OSO/T1CKI
1 12
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14 H1 1 2 3 F
RC3/SCK/SCL
15
RC4/SDI/SDA
16
RC5/SDO
17
RC6/TX/CK
18 H2 4 5 6 E
RC7/RX/DT
PIC16F876A
H3 7 8 9 D
H4 A 0 B C
L1 L2 L3 L4
program TP3;
var
kp : byte;
var LCD_RS : sbit at RB2_bit;
var LCD_EN : sbit at RB3_bit;
var LCD_D4 : sbit at RB4_bit;
var LCD_D5 : sbit at RB5_bit;
var LCD_D6 : sbit at RB6_bit;
var LCD_D7 : sbit at RB7_bit;
var LCD_RS_Direction : sbit at TRISB2_bit;
var LCD_EN_Direction : sbit at TRISB3_bit;
var LCD_D4_Direction : sbit at TRISB4_bit;
var LCD_D5_Direction : sbit at TRISB5_bit;
var LCD_D6_Direction : sbit at TRISB6_bit;
var LCD_D7_Direction : sbit at TRISB7_bit;
58
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
var keypadPort : ………………………………………..
begin
lcd_init();
Keypad_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 2, 'formation continue');
Lcd_Out(2, 1, 'TOUCHE:');
while true do
begin
kp := 0;
while kp = 0 do
begin
kp := …………………………………….
end;
// structure du clavier;
if (kp = ……………….. ) then kp := '1';
if (kp ……………………. ) then kp := '4';
if (kp = …………………. ) then kp := '7';
if (kp = …………………...) then kp := 'A';
if (kp ……………………… ) then kp := '2';
if (kp ………………….. ) then kp := '5';
if (kp = 7 ) then kp := '8';
if (kp = ………………….. ) then kp := '0';
if (kp = …………………….) then kp := '3';
if (kp = 10) then kp := ………………………
if (kp = 11) then kp := ………………………
if (kp = 12) then kp := ……………………….
if (kp = 13) then kp := …………………………
if (kp = 14) then kp := ……………………………….
if (kp = 15) then kp := 'D';
if (kp = 16) then kp := 'C';
Lcd_Chr(2, 10, kp);
end;
end.
59
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
TP4
Compteur modulo 10000 :
begin portb:=………………………………………
trisb:=$F0; delay_ms(5);
trisc:=0; portc:=………………………………….
portb:=0; delay_ms(5);
begin end;
begin end.
uni := ……………………………………………………
60
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015
Formation continue : pour les prof de genie electriques
LES MICROCONTRÖLEURS
U1 U2
9 21 7 13
OSC1/CLKIN RB0/INT A QA
10 22 1 12
OSC2/CLKOUT RB1 B QB
23 2 11
RB2 C QC
2 24 6 10
RA0/AN0 RB3/PGM D QD
3 25 4 9
RA1/AN1 RB4 BI/RBO QE
4 26 5 15
RA2/AN2/VREF-/CVREF RB5 RBI QF
5 27 3 14
RA3/AN3/VREF+ RB6/PGC LT QG
6 28
RA4/T0CKI/C1OUT RB7/PGD
7 7447
RA5/AN4/SS/C2OUT
11
RC0/T1OSO/T1CKI
1 12
MCLR/Vpp/THV RC1/T1OSI/CCP2
13
RC2/CCP1
14
RC3/SCK/SCL
15 R4 R1 R2 R3
RC4/SDI/SDA
16
RC5/SDO
17 10k 10k 10k 10k
RC6/TX/CK
18
RC7/RX/DT
PIC16F876A
61
ANIME PAR YOUSSEF CHEDLY CREFOC SOUSSE 2014/2015