Microcontrôleurs Iset de Nabeul
Correction TRAVAUX DIRIGES N° : 1
AFFICHAGE DES DONNEES
Exercice 1.2 :
Question 1 :
#define B1 RA0_bit PORTB=0x00; Delay_ms(500);
void main() }
{ else
TRISA=0xFF; TRISB=0x00; PORTB=0x00; {
while(1) PORTB=0x55; Delay_ms(500);
{ PORTB=0xAA; Delay_ms(500);
if(B1==0) }
{ }
PORTB=0xFF; Delay_ms(500); }
Question 2 :
#define B1 RA0_bit PORTB=0xFF; Delay_ms(500);
int i; PORTB=0x00; Delay_ms(500);
void main() }
{ }
TRISA=0xFF; else
TRISB=0x00; {
PORTB=0x00; PORTB=0x03; Delay_ms(1000);
while(1) PORTB=0x0C; Delay_ms(2000);
{ PORTB=0x30; Delay_ms(3000);
if(B1==0) PORTB=0xC0; Delay_ms(4000);
{ PORTB=0x00; Delay_ms(1000);
for(i=0; i<2; i++) }
Mr Nizar TOUJENI 121
Microcontrôleurs Iset de Nabeul
{ }
Exercice 1.3 :
1.
void main()
int i, CHIFFRE[10] = {0x3F, 0x06, 0x51011, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
TRISB = 0x00; PORTB = 0;
while(1)
if(RA0_Bit==1)
for(i=0;i<10;i++) {PORTB = CHIFFRE[i]; delay_ms(300);}
if(RA1_Bit==1)
for(i=0;i<10;i++) {PORTB = CHIFFRE[9-i]; delay_ms(300);}
2.
void main()
int i, CHIFFRE[10] = {0x3F, 0x06, 0x51011, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
TRISB = 0x00; PORTB = 0;
while(1)
Mr Nizar TOUJENI 122
Microcontrôleurs Iset de Nabeul
if(RA0_Bit==1)
for(i=0;i<10;i+=2) {PORTB = CHIFFRE[i]; delay_ms(300);}
if(RA1_Bit==1)
for(i=1;i<10;i+=2) {PORTB = CHIFFRE[i]; delay_ms(300);}
Exercice 1.5 :
#define B1 RB2_bit D1 = 0; D2 = 0; D3 = 0;
#define B2 RB1_bit while(1)
#define B3 RB0_bit {
#define D1 RC0_bit if(B1==1&B2==0&B3==0)
#define D2 RC3_bit {
#define D3 RC6_bit Lcd_Out(1, 1, msg1);
sbit LCD_RS at RD2_bit; D1 = 1; delay_ms(500);
sbit LCD_EN at RD3_bit; D1 = 0; delay_ms(500);
sbit LCD_D4 at RD4_bit; }
sbit LCD_D5 at RD5_bit; else if(B1==0&B2==1&B3==0)
sbit LCD_D6 at RD6_bit; {
sbit LCD_D7 at RD7_bit; Lcd_Out(1, 1, msg2);
sbit LCD_RS_Direction at TRISD2_bit; D2=1;delay_ms(500);D2=0;delay_ms(500);
sbit LCD_EN_Direction at TRISD3_bit; }
sbit LCD_D4_Direction at TRISD4_bit; else if(B1==0&B2==0&B3==1)
sbit LCD_D5_Direction at TRISD5_bit; {
Mr Nizar TOUJENI 123
Microcontrôleurs Iset de Nabeul
sbit LCD_D6_Direction at TRISD6_bit; Lcd_Out(1, 1, msg3);
sbit LCD_D7_Direction at TRISD7_bit; D3=1;delay_ms(500);D3=0;delay_ms(500);
char msg1[]="BONJOUR"; }
char msg2[]="BONSOIR"; else
char msg3[]="SALAM"; {
void main() Lcd_Cmd(_LCD_CLEAR);
{ D1 = 0; D2 = 0; D3 = 0;
Lcd_Init(); }
Lcd_Cmd(_LCD_CLEAR); }
Lcd_Cmd(_LCD_CURSOR_OFF); }
TRISB = 0xFF; TRISC = 0x00;
Mr Nizar TOUJENI 124
Microcontrôleurs Iset de Nabeul
Correction TRAVAUX DIRIGES N° : 2
SYSTEMES D’ENTREE DES DONNEES (1)
Exercice 2.1 :
#define A RA0_bit if (A | B | !B & !C) S1=1;
#define B RA1_bit else S1=0;
#define C RA2_bit // Equation de S2
#define S1 RB0_bit if(A & !B | B & !C | C) S2=1;
#define S2 RB1_bit else S2=0;
#define S3 RB2_bit // Equation de S3
#define S4 RB3_bit if(!(A | B) | !B & C) S3=1;
#define S5 RB4_bit else S3=0;
void main( ) // Equation de S4
{ if(A | B & !C) S4=1;
TRISA=0xFF; else S4=0;
TRISB=0x00; // Equation de S5
PORTB = 0; if(A & B & !C) S5=1;
while(1) else S5=0;
{ }
// Equation de S1 }
Exercice 2.3
#define BP RA0_Bit Led = 1; delay_ms(60000); Led = 0;
#define Led RA2_Bit }
#define C1 RB5_Bit if(BP==1&C2==1)
#define C2 RB6_Bit {
#define C5 RB7_Bit Led = 1; delay_ms(120000); Led = 0;
void main() }
Mr Nizar TOUJENI 125
Microcontrôleurs Iset de Nabeul
{ if(BP==1&C5==1)
TRISA=0x01; TRISB=0x07; {
while(1) Led = 1; delay_ms(300000); Led = 0;
{ }
if(BP==1&C1==1) }
{ }
Mr Nizar TOUJENI 126
Microcontrôleurs Iset de Nabeul
Correction TRAVAUX DIRIGES N° : 3
SYSTEMES D’ENTREE DES DONNEES (2)
Exercice 3.1 :
char keypadPort at PORTD; do
sbit LCD_RS at RB2_bit; kpi = Keypad_Key_Click ( );
sbit LCD_EN at RB3_bit; while (!kpi);
sbit LCD_D7 at RB7_bit; switch (kpi) {
sbit LCD_D6 at RB6_bit; case 1: kpi = 55; break; // 7
sbit LCD_D5 at RB5_bit; case 2: kpi = 52; break; // 4
sbit LCD_D4 at RB4_bit; case 3: kpi = 49; break; // 1
sbit LCD_RS_Direction at TRISB2_bit; case 4: kpi = 32; break; // Espace
sbit LCD_EN_Direction at TRISB3_bit; case 5: kpi = 56; break; // 8
sbit LCD_D7_Direction at TRISB7_bit; case 6: kpi = 53; break; // 5
sbit LCD_D6_Direction at TRISB6_bit; case 7: kpi = 50; break; // 2
sbit LCD_D5_Direction at TRISB5_bit; case 8: kpi = 48; break; // 0
sbit LCD_D4_Direction at TRISB4_bit; case 9: kpi = 57; break; // 9
int kpi; case 10: kpi = 54; break; // 6
void main ( ) case 11: kpi = 51; break; // 3
{ case 12: kpi = 61; break; // =
Lcd_Init ( ); case 13: kpi = 47; break; // /
Lcd_Cmd (_LCD_CLEAR); case 14: kpi = 42; break; // x
Keypad_Init ( ); case 15: kpi = 45; break; // -
Lcd_Out (1,1,"Tapez une touche"); case 16: kpi = 43; break; // +
delay_ms (1000); }
Lcd_Cmd (_LCD_CLEAR); Lcd_Chr (1, 2, kpi);
Mr Nizar TOUJENI 127
Microcontrôleurs Iset de Nabeul
do }
{ while (1);
kpi = 0; }
Exercice 3.3
char keypadPort at PORTC; case 9: return '7';
sbit LCD_RS at RB4_bit; case 10: return '8';
sbit LCD_EN at RB5_bit; case 11: return '9';
sbit LCD_D7 at RB3_bit; case 12: return 'C';
sbit LCD_D6 at RB2_bit; case 13: return '*';
sbit LCD_D5 at RB1_bit; case 14: return '0';
sbit LCD_D4 at RB0_bit; case 15: return '#';
sbit LCD_RS_Direction at TRISB4_bit; case 16: return 'D';
sbit LCD_EN_Direction at TRISB5_bit; default: return 0;
sbit LCD_D7_Direction at TRISB3_bit; }
sbit LCD_D6_Direction at TRISB2_bit; }
sbit LCD_D5_Direction at TRISB1_bit; void main ( )
sbit LCD_D4_Direction at TRISB0_bit; {
char Lecture_Clavier (void) char clavier;
{ Lcd_Init ( );
switch (Keypad_Key_Press ( )) Lcd_Cmd (_LCD_CURSOR_OFF);
{ Lcd_Out (1, 1,"Valeur:");
case 1: return '1'; while(1)
case 2: return '2'; {
case 3: return '3'; clavier = Lecture_Clavier ( );
case 4: return 'A'; Lcd_Chr (2,1,clavier);
Mr Nizar TOUJENI 128
Microcontrôleurs Iset de Nabeul
case 5: return '4'; delay_ms (500);
case 6: return '5'; }
case 7: return '6'; }
case 8: return 'B';
Mr Nizar TOUJENI 129
Microcontrôleurs Iset de Nabeul
Correction TRAVAUX DIRIGES N° : 4
CONVERTISSEUR ANALOGIQUE NUMERIQUE
Exercice 4.1
int CHIFFRE[3] = {0B01101101, 0B01111001, 0B00000110};
unsigned int temp_res0, temp_res1;
float tension0, tension1;
void main()
{
TRISA=0xFF; TRISB=0x00;
while(1)
{
temp_res0=ADC_Read(0); temp_res1=ADC_Read(1);
tension0=(float)(temp_res0*5)/1024; tension1=(float)(temp_res1*5)/1024;
if(tension0 > tension1) PORTB = CHIFFRE[0];
if(tension0 == tension1) PORTB = CHIFFRE[1];
if(tension0 < tension1) PORTB = CHIFFRE[2];
}
}
Exercice 4.2
int i, j, chiffre [11] = {0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110,
0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111,0b01110110};
unsigned int temp_res0, temp_res1;
float tension0, tension1;
void main()
{
TRISA=0xFF; TRISB=0x00; TRISC=0x00;
while(1)
{
temp_res0=ADC_Read(0); temp_res1=ADC_Read(1);
tension0=(float)(temp_res0*5)/1024; tension1=(float)(temp_res1*5)/1024;
if(tension0 > tension1)
Mr Nizar TOUJENI 130
Microcontrôleurs Iset de Nabeul
{
for(i=0; i<2; i++)
{
PORTB = CHIFFRE[i];
for(j=0;j<10;j++)
{
PORTC = CHIFFRE[j]; delay_ms(500);
}
}
}
if(tension0 == tension1)
{
PORTB = CHIFFRE[10]; PORTC = CHIFFRE[10];
}
if(tension0 < tension1)
{
for(i=0; i<2; i++)
{
PORTB = CHIFFRE[1-i];
for(j=0; j<10; j++)
{
PORTC = CHIFFRE[9-j]; delay_ms(500);
}
}
}
}
}
Mr Nizar TOUJENI 131
Microcontrôleurs Iset de Nabeul
Correction TRAVAUX DIRIGES N° : 5
MODULATION A LARGEUR D’IMPULSION
Exercice 5.1
void main ( ) PWM1_Start ( );
{ while(1)
TRISB = 0xFF; {
TRISC = 0x00; PWM1_Set_Duty(PORTB);
PORTC = 0; }
delay_ms(2000); }
PWM1_Init(1000);
Exercice 5.2
unsigned short cd=155, od=0; while(1)
void initMain () {
{ if(Button(&PORTB, 0,1,0)) cd++;
TRISB = 0xFF; if(Button(&PORTB, 1,1,0)) cd--;
TRISC = 0; if(od != cd)
TRISD = 0; {
PORTC = 0; PWM1_Set_Duty (cd);
PORTD = 0; od = cd;
PWM1_Init (5000); PORTD = od;
} }
void main ( ) Delay_ms(150);
{ }
initMain (); }
PWM1_Start ();
Mr Nizar TOUJENI 132