ok

Dependencies:   mbed

Committer:
stersky
Date:
Sun Jan 27 20:48:32 2019 +0000
Revision:
0:33f12c4938ba
kb

Who changed what in which revision?

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