![](/media/cache/group/logo-cachanx.jpg.50x50_q85.jpg)
Exemple d'automate en C
Dependencies: mbed
automate.cpp@1:032d4021bf3a, 2016-11-30 (annotated)
- Committer:
- gr66
- Date:
- Wed Nov 30 12:46:03 2016 +0000
- Revision:
- 1:032d4021bf3a
- Child:
- 2:63147e335ea8
Exemple d'automate en C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gr66 | 1:032d4021bf3a | 1 | //Projet a télécharger sue : https://developer.mbed.org/teams/NBoard/code/Automate/ |
gr66 | 1:032d4021bf3a | 2 | #include "mbed.h" |
gr66 | 1:032d4021bf3a | 3 | DigitalIn BP0 (PA_9,PullUp) ; // bouton BP0 |
gr66 | 1:032d4021bf3a | 4 | DigitalOut L0 (PB_3) ; // led L0 |
gr66 | 1:032d4021bf3a | 5 | DigitalOut L1 (PA_7) ; // led L1 |
gr66 | 1:032d4021bf3a | 6 | DigitalOut L2 (PA_6) ; // led L2 |
gr66 | 1:032d4021bf3a | 7 | // prototype et variables globales et constantes |
gr66 | 1:032d4021bf3a | 8 | // ------------------------------------- |
gr66 | 1:032d4021bf3a | 9 | void automate(void); |
gr66 | 1:032d4021bf3a | 10 | // |
gr66 | 1:032d4021bf3a | 11 | void main(void) |
gr66 | 1:032d4021bf3a | 12 | { |
gr66 | 1:032d4021bf3a | 13 | // début de l'application |
gr66 | 1:032d4021bf3a | 14 | // ---------------------- |
gr66 | 1:032d4021bf3a | 15 | while(1) { |
gr66 | 1:032d4021bf3a | 16 | wait(0.2); //on poiraute 200 miliseconde |
gr66 | 1:032d4021bf3a | 17 | automate(); |
gr66 | 1:032d4021bf3a | 18 | } |
gr66 | 1:032d4021bf3a | 19 | } |
gr66 | 1:032d4021bf3a | 20 | void automate(void) |
gr66 | 1:032d4021bf3a | 21 | { |
gr66 | 1:032d4021bf3a | 22 | // définition d'un type énuméré permettant de manipuler l'état de l'automate |
gr66 | 1:032d4021bf3a | 23 | // par son nom |
gr66 | 1:032d4021bf3a | 24 | // ------------------------------------------------------------------------- |
gr66 | 1:032d4021bf3a | 25 | typedef enum {etat0=0, etat1=1, etat2=2} type_etat; |
gr66 | 1:032d4021bf3a | 26 | type_etat etat_actuel; |
gr66 | 1:032d4021bf3a | 27 | static type_etat etat_futur=etat0; |
gr66 | 1:032d4021bf3a | 28 | unsigned char entree; |
gr66 | 1:032d4021bf3a | 29 | // gestion des entrees |
gr66 | 1:032d4021bf3a | 30 | // ------------------- |
gr66 | 1:032d4021bf3a | 31 | entree = !BP0; |
gr66 | 1:032d4021bf3a | 32 | // gestion du diagramme de transition |
gr66 | 1:032d4021bf3a | 33 | // ---------------------------------- |
gr66 | 1:032d4021bf3a | 34 | etat_actuel = etat_futur; |
gr66 | 1:032d4021bf3a | 35 | switch(etat_actuel) { |
gr66 | 1:032d4021bf3a | 36 | case etat0 : |
gr66 | 1:032d4021bf3a | 37 | if (entree==1) etat_futur = etat1; |
gr66 | 1:032d4021bf3a | 38 | break; |
gr66 | 1:032d4021bf3a | 39 | case etat1 : |
gr66 | 1:032d4021bf3a | 40 | etat_futur = etat2; |
gr66 | 1:032d4021bf3a | 41 | break; |
gr66 | 1:032d4021bf3a | 42 | case etat2 : |
gr66 | 1:032d4021bf3a | 43 | etat_futur = etat0; |
gr66 | 1:032d4021bf3a | 44 | break; |
gr66 | 1:032d4021bf3a | 45 | } |
gr66 | 1:032d4021bf3a | 46 | // gestion des sorties |
gr66 | 1:032d4021bf3a | 47 | // ------------------- |
gr66 | 1:032d4021bf3a | 48 | switch(etat_actuel) { |
gr66 | 1:032d4021bf3a | 49 | case etat0 : |
gr66 | 1:032d4021bf3a | 50 | L0=1; |
gr66 | 1:032d4021bf3a | 51 | L1=0; |
gr66 | 1:032d4021bf3a | 52 | L2=0; |
gr66 | 1:032d4021bf3a | 53 | break; |
gr66 | 1:032d4021bf3a | 54 | case etat1 : |
gr66 | 1:032d4021bf3a | 55 | L0=1; |
gr66 | 1:032d4021bf3a | 56 | L1=1; |
gr66 | 1:032d4021bf3a | 57 | L2=0; |
gr66 | 1:032d4021bf3a | 58 | break; |
gr66 | 1:032d4021bf3a | 59 | case etat2 : |
gr66 | 1:032d4021bf3a | 60 | L0=1; |
gr66 | 1:032d4021bf3a | 61 | L1=1; |
gr66 | 1:032d4021bf3a | 62 | L2=1; |
gr66 | 1:032d4021bf3a | 63 | break; |
gr66 | 1:032d4021bf3a | 64 | } |
gr66 | 1:032d4021bf3a | 65 | } |