ok

Dependencies:   mbed

Committer:
stersky
Date:
Tue Feb 12 14:46:02 2019 +0000
Revision:
0:6c60dedc1d7a
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stersky 0:6c60dedc1d7a 1 #include "mbed.h"
stersky 0:6c60dedc1d7a 2
stersky 0:6c60dedc1d7a 3 Serial centrale(p9,p10);
stersky 0:6c60dedc1d7a 4 Serial PC(USBTX,USBRX);
stersky 0:6c60dedc1d7a 5
stersky 0:6c60dedc1d7a 6 void lecture_centrale(int *ptr1, int *ptr2);
stersky 0:6c60dedc1d7a 7
stersky 0:6c60dedc1d7a 8 int main() {
stersky 0:6c60dedc1d7a 9 int vitesse,gouvernail;
stersky 0:6c60dedc1d7a 10 centrale.baud(9600);
stersky 0:6c60dedc1d7a 11 PC.baud(460800);
stersky 0:6c60dedc1d7a 12
stersky 0:6c60dedc1d7a 13 while(1) {
stersky 0:6c60dedc1d7a 14 lecture_centrale(&vitesse,&gouvernail);
stersky 0:6c60dedc1d7a 15 PC.printf("Vmoteur : %d\n\rDirMoteur : %d\n\r",vitesse,gouvernail);
stersky 0:6c60dedc1d7a 16 }
stersky 0:6c60dedc1d7a 17 }
stersky 0:6c60dedc1d7a 18
stersky 0:6c60dedc1d7a 19 void lecture_centrale(int* ptr1, int* ptr2)
stersky 0:6c60dedc1d7a 20 {
stersky 0:6c60dedc1d7a 21 char fin=0;
stersky 0:6c60dedc1d7a 22 char etat = 0;
stersky 0:6c60dedc1d7a 23 char a,i=0;
stersky 0:6c60dedc1d7a 24 char buffer[30];
stersky 0:6c60dedc1d7a 25
stersky 0:6c60dedc1d7a 26 while(fin==0)
stersky 0:6c60dedc1d7a 27 {
stersky 0:6c60dedc1d7a 28 if (centrale.readable())
stersky 0:6c60dedc1d7a 29 { // attention PC.readable reste à 1 tant qu'il n'y a pas eu de getc qui vide le buffer
stersky 0:6c60dedc1d7a 30 a=centrale.getc();
stersky 0:6c60dedc1d7a 31
stersky 0:6c60dedc1d7a 32 switch(etat)
stersky 0:6c60dedc1d7a 33 {
stersky 0:6c60dedc1d7a 34 case 0 :
stersky 0:6c60dedc1d7a 35 if(a=='$')//On attend le début d'une trame
stersky 0:6c60dedc1d7a 36 {
stersky 0:6c60dedc1d7a 37 buffer[0] = '$';
stersky 0:6c60dedc1d7a 38 i=1;
stersky 0:6c60dedc1d7a 39 etat = 1;
stersky 0:6c60dedc1d7a 40 }
stersky 0:6c60dedc1d7a 41 break;
stersky 0:6c60dedc1d7a 42
stersky 0:6c60dedc1d7a 43 case 1 ://Après le début d'une trame, on récupère la trame en entier et on l'analyse
stersky 0:6c60dedc1d7a 44 buffer[i] = a;
stersky 0:6c60dedc1d7a 45 i++;
stersky 0:6c60dedc1d7a 46 if(a=='*')
stersky 0:6c60dedc1d7a 47 {
stersky 0:6c60dedc1d7a 48 sscanf(buffer,"$%d|%d*",ptr1,ptr2);
stersky 0:6c60dedc1d7a 49 fin = 1;
stersky 0:6c60dedc1d7a 50 i=0;
stersky 0:6c60dedc1d7a 51 etat=0;
stersky 0:6c60dedc1d7a 52 }
stersky 0:6c60dedc1d7a 53 break;
stersky 0:6c60dedc1d7a 54
stersky 0:6c60dedc1d7a 55 default :
stersky 0:6c60dedc1d7a 56 etat = 0;
stersky 0:6c60dedc1d7a 57 break;
stersky 0:6c60dedc1d7a 58
stersky 0:6c60dedc1d7a 59
stersky 0:6c60dedc1d7a 60 }
stersky 0:6c60dedc1d7a 61 }
stersky 0:6c60dedc1d7a 62 }
stersky 0:6c60dedc1d7a 63 }