Bonjour, j'ai un programme sur lequel je dois incorporer une commande de rétro éclairage pour mon bac. J'ai déjà fait plusieurs essaies mais ils sont non concluant, c'est pourquoi je viens vous demander de l'aide.
Voici le programme:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include <16F877A.h>         // Déclaration du type de PIC utilisé
 
#device adc=10               // CAN sur 10 bits
#device icd=true             // Autorisation debugger
#FUSES NOWDT                 // No Watch Dog Timer
#FUSES HS                    // High speed Osc (> 4mhz)
#FUSES NOPUT                 // No Power Up Timer
#FUSES NOPROTECT             // Code not protected from reading
#FUSES NODEBUG               // No Debug mode for ICD
#FUSES BROWNOUT              // Reset when brownout detected
#FUSES NOLVP                 // No low voltage prgming, B3(PIC16) used for I/O
#FUSES NOCPD                 // No EE protection
#FUSES NOWRT                 // Program memory not write protected
 
#use delay(clock=20000000)   // Fréquence du quartz 20 MHz
#include "distributeur.h"    // Fonctions pour gestion afficheur LCD
                             // ce fichier doit être dans le même répertoire
                             // que le programme DISTRIBUTEUR
 
 
// Définition des paramètres pour la liaison série
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
 
// Définition des E/S TOR sur carte DISTRIBUTEUR
#define rplein    PIN_B1
#define distrib   PIN_B2
#define aurg      PIN_B4
#define pistol    PIN_B5
#define mot       PIN_C0
#define ev1       PIN_C1
#define mforc     PIN_C3
#define wd        PIN_C4
#define comm      PIN_C5
#define eclairage PIN_D3
 
// Déclaration des fonctions utilisées (utile pour le compilateur)
void initialisation();
void acqui_analog();
void envoi_controle();
void recoi_controle();
void retro_eclairage();
void affiche();
void calcul();
void lit_entrees();
void traite_auto();
void ecrit_sorties();
void convert(float nombre);
 
// Déclarations du type des variables globales
// int1 : 1 bit
   int1 pistolet, inter_dist, r_plein, arret_urg, marche_forcee;
   int1 moteur, vanne_dist;
   int1 autorise, dist, swd, scomm;
   int1 tempo_en_cours, fin_tempo;
 
// flottant
   float Input_high;
 
// int : 8 bits (-128 à 127)
   int nboctet_rec, nboctet_emi, trame_tx1[15], trame_rx1[6];
   int tempo, nb_it, ligne_lcd, trans, etape, b, i, indexana;
 
// char : 8 bits (0 à 255)
   char dimil, mil, cent, diz, unit;
 
// int16 = 16 bits (0 à 65535)
   int16 eana0, eana1, eana2;
   int16 teana0[5], teana1[5], teana2[5];
 
// int32 : 32 bits
   int32 prixlitre, volume, prixtotal, impulsion, seana0, seana1, seana2;
 
 
// =============   SOUS-PROGRAMMES D'INTERRUPTIONS   ==================
 
// Interruption lors de réception d'un caractère sur la liaison série
#int_RDA
rs232_isr()
{
   b = getch();                        // Lecture du caractère reçu
   if (nboctet_rec == 0)               // Si 1er caractère
   {
      if (b == 'C')                    // Si code début trame
      {
      trame_rx1[nboctet_rec] = b;      // rangement dans tableau de valeurs
      nboctet_rec++;
      }
   }
   else
   {
      trame_rx1[nboctet_rec] = b;      // rangement caractères suivants
      nboctet_rec = (nboctet_rec + 1) % sizeof(trame_rx1);
   }
}
 
// Interruption entrée B0 (débitmètre : 1800 impulsions / litre)
#int_EXT
impuls_isr()
{
   // Comptage volume si distribution en cours
   if (moteur == 1)
   {
      impulsion++;
      volume = impulsion / 2;       // Volume en 1/100 L (si division par 18)
   // limitation pour Web serveur (nb maxi = 32767
      if ((volume > 32700) || (prixtotal > 32700))
      {
         fin_tempo = 0;             // fin  distribution
         tempo_en_cours = 0;
         etape = 5;
      }
   }
}
 
// Interruption débordement Timer 1 (103 mS)
#int_TIMER1
timer1_isr()
{
   // Elaboration tempo 1 seconde
   nb_it++;
   if (nb_it > 9)        // toutes les secondes (10 * 103ms)
   {
      nb_it = 0;
     if (bit_test(*24,1))  // Si erreur liaison série
      {
      bit_clear(*24,4);    // Raz liaison série dans registre PIC
      bit_set(*24,4);      // Redémarre liaison série
      nboctet_rec = 0;
      }
 
      if ((tempo_en_cours == 1) && (fin_tempo == 0))
      {
         tempo = tempo - 1;      // Gestion temporisation automatisme
         if (tempo == 0)
         {
            fin_tempo = 1;
            tempo_en_cours = 0;
         }
      }
 
      if (swd == 0)        // Clignotement LED activité (watch dog)
      {
         output_high(wd);
         swd = 1;
      }
      else
      {
         output_low(wd);
         swd = 0;
      }
   }
}
 
// ==================   PROGRAMME PRINCIPAL   =======================
void main()
{
       initialisation();
       // Autorisation des interruptions
       enable_interrupts (INT_TIMER1);    // Débordement timer 1 (103ms)
       enable_interrupts (INT_EXT);       // Impulsion sur port B0
       enable_interrupts (INT_RDA);       // Réception sur liaison série
       enable_interrupts (GLOBAL);        // Autorisation globale des IT
 
   do
      {
         acqui_analog();         // Lecture des entrées analogiques
         lit_entrees();          // Lecture des entrées TOR
         traite_auto();          // Traitement du cycle d'automatisme
         ecrit_sorties();        // Ecriture des sorties TOR
         calcul();               // Calcul prix total
         recoi_controle();       // Réception trame de la carte controle
	   retro_eclairage();      // Controle led retro-éclairage lcd
         affiche();              // Affichage sur LCD
      }
   while(1);                     // Le programme reboucle indéfiniment.
}
//   ===================   FIN DU PROGRAMME PRINCIPAL  ================
 
 
//   =======   FONCTIONS APPELEES PAR LE PROGRAMME PRINCIPAL  =========
 
 
// Initialisation du microcontroleur
void initialisation()
{
   // Configuration des timers
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
 
   // Configuration du convertisseur A/N
   setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
 
   // Initialisation des variables programme
   etape = 0;
   nb_it = 0;
   prixlitre = 1111;
   volume = 0;
   impulsion = 0;
   prixtotal = 0;
   autorise = 0;
   dist = 0;
   nboctet_emi = 0;
   nboctet_rec = 0;
   ligne_lcd = 0;
   tempo_en_cours = 0;
   fin_tempo = 0;
   indexana = 0;
   for (i = 0 ; i < 5 ; i++)
   {
      teana0[i] = 0;
      teana1[i] = 0;
   }
 
   // Initialisation de l'afficheur LCD
   delay_ms(500);                         //Attente démarrage LCD
   lcd_init();                            // Commande init LCD
   lcd_gotoxy(1,1);                       // Affichage titre système
   lcd_putc("    Station GPL     ");
   lcd_gotoxy(1,2);
   lcd_putc("BAC STI Electronique");
   lcd_gotoxy(1,3);
   lcd_putc("     Theme 2009     ");
   lcd_gotoxy(1,4);
   lcd_putc("Lycee JBDLS ** Lyon");
   for (b = 0 ; b < 4 ; b++)              // Clignotement pour test LED
   {
      delay_ms(600);
      output_high(eclairage);
      output_high(wd);
      output_high(comm);
      delay_ms(600);
      output_low(eclairage);
      output_low(wd);
      output_low(comm);
   }
}
 
 
// Lecture des entrées TOR
void lit_entrees()
{
      r_plein = input(rplein);
      inter_dist = input (distrib);
      arret_urg = input (aurg);
      pistolet = input (pistol);
      marche_forcee = !input(mforc);
}
 
// Traitement du cycle d'automatisme
void traite_auto()
{
   if (arret_urg == 0)     // Arrêt d'urgence
      {
         etape = 0;
         dist = 0;
      }
 
   switch (etape)
   {
      case 0:              // Etape "ARRET"
         moteur = 0;
         dist = 0;
         vanne_dist = 0;
         tempo_en_cours = 0;
         fin_tempo = 0;
 
         trans = (autorise || marche_forcee) && arret_urg;
         if (trans == 1)
            etape = 1;
      break;
 
 
      case 1:              // Etape "PRET"
         moteur = 0;
         dist = 0;
         vanne_dist = 0;
         autorise = 0;
 
         trans = pistolet;
         if (trans == 1)
            etape = 2;
      break;
 
 
      case 2:              // Etape "ATTENTE"
         moteur = 1;
         dist = 1;
         vanne_dist = 0;
         volume = 0;
         impulsion = 0;
 
         trans = pistolet && inter_dist;
         if(trans == 1)
            etape = 3;
 
         trans = !pistolet;
         if(trans == 1)
            etape = 0;
      break;
 
 
      case 3:              // Etape "EN COURS"
         moteur = 1;
         dist = 1;
         vanne_dist = 1;
 
         trans = !pistolet || !inter_dist;
         if(trans == 1)
            etape = 4;
 
         trans = r_plein;
         if(trans == 1)
            etape = 5;
      break;
 
 
      case 4:              // Etape "PAUSE"
         moteur = 1;
         dist = 1;
         vanne_dist = 0;
 
         if ((tempo_en_cours == 0) && (fin_tempo == 0))
         {
            tempo = 15;
            tempo_en_cours = 1;
         }
 
         trans = !r_plein && pistolet && inter_dist;
         if (trans == 1)
         {
            fin_tempo = 0;
            tempo_en_cours = 0;
            etape = 3;
         }
 
         trans = !pistolet || fin_tempo;
         if (trans == 1)
         {
            fin_tempo = 0;
            tempo_en_cours = 0;
            etape = 5;
         }
      break;
 
 
      case 5:              // Etape "FIN"
         moteur = 0;
         dist = 0;
         vanne_dist = 0;
         autorise = 0;
 
         if ((tempo_en_cours == 0) && (fin_tempo ==0))
         {
            tempo = 10;
            tempo_en_cours = 1;
         }
 
         trans = fin_tempo && !r_plein && !pistolet && !inter_dist;
         if (trans == 1)
         {
            fin_tempo = 0;
            tempo_en_cours = 0;
            etape = 0;
         }
      break;
   }
}
 
 
// Ecriture des sorties TOR
void ecrit_sorties()
{
      if (moteur == 0)
         output_low(mot);
      else
         output_high(mot);
 
      if (vanne_dist == 0)
         output_low(ev1);
      else
         output_high(ev1);
 
}
 
// Acquisition et mémorisation des entrées analogiques
void acqui_analog()
{
   set_adc_channel(0);                 // Sélection CAN voie 0 (pression)
   delay_us(10);
   teana0[indexana] = read_adc();      // Acquisition voie 0
 
   set_adc_channel(1);                 // Sélection CAN voie 1 (temperature)
   delay_us(10);
   teana1[indexana] = read_adc();      // Acquisition voie 1
 
   set_adc_channel(2);                 // Sélection CAN voie 2 (luminosite)
   delay_us(10);
   teana2[indexana] = read_adc();      // Acquisition voie 2
 
   indexana++;
   if (indexana == 5)
      indexana = 0;
 
   seana0 = 0;                         // Filtrage numérique par calcul de la
   seana1 = 0;                         // moyenne sur 5 mesures
   seana2 = 0;
 
   for (i = 0 ; i < 5 ; i++)
   {
      seana0 = seana0 + teana0[i];
      seana1 = seana1 + teana1[i];
	seana2 = seana2 + teana2[i];
   }
   eana0 = seana0 / 5;
   eana1 = seana1 / 5;
   eana2 = seana2 / 5;
}
 
// Calcul prix total
void calcul()
{
   prixtotal = (prixlitre * volume) / 1000;  // prixtotal en 1/100 Euro
                                             // prixlitre en 1/1000 Euro
                                             // volume en 1/100 L
   if (prixtotal > 99999)
      prixtotal = 99999;
}
 
 
 
// Réception trame de la carte CONTROLE
void recoi_controle()
{
   if (nboctet_rec > 3)                // Si au moins 4 octets reçus
   {
      disable_interrupts (INT_RDA);    // Blocage des interruptions
      nboctet_rec = 0;                 // Réinitialisation compteur octets
      if  (trame_rx1[0] == 'C')        // Si le message reçu est OK
      {                                // Traitement du message reçu
         prixlitre = trame_rx1[1] + trame_rx1[2] * 256;
         if (prixlitre > 9999)
            prixlitre = 9999;
 
         if ((trame_rx1[3] && 0x01) == 1)
            autorise = 1;
 
         envoi_controle();             // Réponse à carte contrôle
 
         if (scomm == 0)              // LED communication
         {
            output_high(comm);
            scomm = 1;
         }
         else
         {
            output_low(comm);
            scomm = 0;
         }
      }
      enable_interrupts (INT_RDA);     // Relance interruptions
   }
}
 
 
// Controle rétro-éclairage
void retro_eclairage()
 
{
	#define eclairage PIN_D3
	(level_light == 2)
	if (eana2 < level_light)
	{
		Output_high;
	}
	else
	{
		Output_low;
	}
 
}
 
 
// Envoi trame à la carte CONTROLE
void envoi_controle()
{
   trame_tx1[0] = 'D';                    // Préparation message réponse
   trame_tx1[2] = (volume/256);           // Volume sur 2 octets
   trame_tx1[1] = (volume - trame_tx1[2] * 256);
 
   trame_tx1[4] = (prixtotal/256);        // Prix total sur 2 octets
   trame_tx1[3] = (prixtotal - trame_tx1[4] * 256);
 
   trame_tx1[5] = dist;                   // Distribution en cours (0 ou 1)
 
   trame_tx1[7] = (eana0/256);            // Valeur entrée analogique 0
   trame_tx1[6] = (eana0 - trame_tx1[7] * 256);
 
   trame_tx1[9] = (eana1/256);            // Valeur entrée analogique 1
   trame_tx1[8] = (eana1 - trame_tx1[9] * 256);
 
   trame_tx1[10] = etape;                 // N° étape en cours
 
   for (nboctet_emi = 0 ; nboctet_emi < 11 ; nboctet_emi++)
   {
         putc(trame_tx1[nboctet_emi]);   // Envoi sur port série de 11 octets
   }
}
 
 
// Gestion de l'afficheur LCD
void affiche()
{
    ligne_lcd++;
    if (ligne_lcd > 3)
      ligne_lcd = 0;
 
   switch (ligne_lcd)         // Affichage 1 ligne à la fois
   {
   case 0 :
      // affichage état du distributeur
      lcd_gotoxy(1,1);
      lcd_putc("Etat = ");
 
      if (etape == 0)
      lcd_putc("ARRET        ");
 
      if (etape == 1)
      lcd_putc("PRET         ");
 
      if (etape == 2)
      lcd_putc("ATTENTE      ");
 
      if (etape == 3)
      lcd_putc("EN COURS     ");
 
      if (etape == 4)
      {
      lcd_putc("PAUSE     ");
      convert(tempo);
      lcd_putc(diz+'0');
      lcd_putc(unit+'0');
      lcd_putc('s');
      }
 
      if (etape == 5)
      lcd_putc("FIN DISTRIB  ");
   break;
 
 
   case 1 :
      // affichage volume
      lcd_gotoxy(1,2);
      lcd_putc("Volume = ");
      convert(volume);
 
      if (dimil == 0)
         lcd_putc(" ");
      else
         lcd_putc(dimil+'0');
 
      if ((dimil == 0) && (mil == 0))
         lcd_putc(" ");
      else
         lcd_putc(mil+'0');
      lcd_putc(cent+'0');
      lcd_putc('.');
      lcd_putc(diz+'0');
      lcd_putc(unit+'0');
      lcd_putc(" L   ");
   break;
 
   case 2:
      // affichage prix litre
      lcd_gotoxy(1,3);
      lcd_putc("Prix U =   ");
      convert(prixlitre);
      lcd_putc(mil+'0');
      lcd_putc('.');
      lcd_putc(cent+'0');
      lcd_putc(diz+'0');
      lcd_putc(unit+'0');
      lcd_putc(" E/L");
   break;
 
 case 3:
      // affichage prix total
      lcd_gotoxy(1,4);
      lcd_putc("Prix T = ");
      convert(prixtotal);
 
      if (dimil == 0)
         lcd_putc(" ");
      else
         lcd_putc(dimil+'0');
 
      if ((dimil == 0) && (mil == 0))
         lcd_putc(" ");
      else
         lcd_putc(mil+'0');
      lcd_putc(cent+'0');
      lcd_putc('.');
      lcd_putc(diz+'0');
      lcd_putc(unit+'0');
      lcd_putc(" E   ");
   break;
   }
}
 
// Conversion nombre en 5 caractères ASCII
void convert(float nombre)
{
   if (nombre > 99999)
      nombre = 99999;
 
   dimil = nombre/10000;
   mil = (nombre/1000) - (10 * dimil);
   cent = (nombre/100) - (100 * dimil + 10 * mil);
   diz = (nombre / 10) - (1000 * dimil + 100 * mil + 10 * cent);
   unit = nombre - (10000 * dimil + 1000 * mil + 100 * cent + 10 * diz);
}
Toutes la commande du rétro éclairage est de ma conception, donc c'est là dedans qu'il y a des erreurs.
Quand je compile, il me dis commande non valide.
Merci de m'aider.