Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 28:6f0009bce093
- Parent:
- 27:53f556d60967
- Child:
- 29:fbc5f53d8d0f
--- a/main.cpp Wed Jul 15 13:52:55 2020 +0000
+++ b/main.cpp Wed Jun 08 15:02:53 2022 +0000
@@ -1,12 +1,212 @@
#include "mbed.h"
+/**************************************************************/
+/* Déclaration des Sous fonctions */
+/**************************************************************/
+void ISR_read(); // lecture liaison serie
+uint8_t gencrc2(uint8_t *data); // calcul crc NMEA
+DigitalOut myled(PTB22);
+// initialisation de la liaison serie
+static UnbufferedSerial capt(PTC4,PTC3,4800);
+char c;
+char Rx_buffer[100];
+char trame[100];
+char trame_cpy[100];
+volatile bool flag_ISR_read=0;
+volatile int index=0;
+// initialisation des variables du GPS
+static char type[20];
+static char horaire[20];
+static char alerte[3];
+static char lattitude[20];
+static char hemisphere[2];
+static char longitude[20];
+static char dir[2];
+static char vitesse[20];
+static char cap[20];
+static char date[20];
+static char magn[20];
+static char crc[10];
+static char qualif[10];
+static char nb_satellites[10];
+static char DOP[10];
+static char altitude[10];
+static char altitude_cor[10];
+const char * separators = ",";
+ char i;
+ size_t len;
+ uint8_t val_crc;
-DigitalOut myled(PTA1);
-
-int main() {
+int main()
+{
+ capt.attach(&ISR_read,SerialBase::RxIrq);
while(1) {
- myled = 1; // led D3 allumée
- wait(1.5);
- myled = 0; // led D3 éteinte
- wait(3.5);
+ if (flag_ISR_read==1) {
+ //printf("%s",trame); // ligne de test gps
+ char* token = strtok(trame,"*"); //on met dans trame cpy la trame sans crc
+ strcpy(trame_cpy,token);
+ token = strtok(NULL,"*"); // on copie le crc
+ strcpy(crc,token);
+ val_crc=gencrc2((uint8_t *)trame_cpy); //calcul du crc sur la trame
+ uint8_t val_crc2;
+ sscanf(crc,"%x",&val_crc2);
+ if (val_crc!=val_crc2) {
+ printf ("crc error\n");
+ } else {
+ printf ("crc OK\n");
+ }
+ } // fin flag
+ }//fin while
+ } // fin programme
+ /* char* token = strtok(trame,"*"); //on met dans trame cpy la trame sans crc
+ strcpy(trame_cpy,token);
+ token = strtok(NULL,"*"); // on copie le crc
+ strcpy(crc,token);
+ val_crc=gencrc2((uint8_t *)trame_cpy); //calcul du crc sur la trame
+ uint8_t val_crc2;
+ sscanf(crc,"%x",&val_crc2);
+ if (val_crc!=val_crc2) {
+ printf ("crc error\n");
+ } else {
+ printf ("crc OK\n");
+ char* token = strtok((char*)trame_cpy,separators);
+ strcpy(type,token);
+ if (strcmp(type,"$GPRMC")==0) { // traitement d'une trame RMC
+ token = strtok(NULL,separators);
+ strcpy(horaire,token);
+ token = strtok(NULL,separators);
+ strcpy(alerte,token);
+ token = strtok(NULL,separators);
+ strcpy(lattitude,token);
+ token = strtok(NULL,separators);
+ strcpy(hemisphere,token);
+ token = strtok(NULL,separators);
+ strcpy(longitude,token);
+ token = strtok(NULL,separators);
+ strcpy(dir,token);
+ token = strtok(NULL,separators);
+ strcpy(vitesse,token);
+ token = strtok(NULL,separators);
+ strcpy(cap,token);
+ token = strtok(NULL,separators);
+ strcpy(date,token);
+ token = strtok(NULL,separators);
+ strcpy(magn,token);
+ //printf ("%s : %s : %s : %s \n",date,horaire,lattitude, longitude);
+ } // fin compare chaines RMC
+ if (strcmp(type,"$GPGGA")==0) { // traitement d'une trame GGA
+ token = strtok(NULL,separators);
+ strcpy(horaire,token);
+ token = strtok(NULL,separators);
+ strcpy(lattitude,token);
+ token = strtok(NULL,separators);
+ strcpy(hemisphere,token);
+ token = strtok(NULL,separators);
+ strcpy(longitude,token);
+ token = strtok(NULL,separators);
+ strcpy(dir,token);
+ token = strtok(NULL,separators);
+ strcpy(qualif,token);
+ token = strtok(NULL,separators);
+ strcpy(nb_satellites,token);
+ token = strtok(NULL,separators);
+ strcpy(DOP,token);
+ token = strtok(NULL,separators);
+ strcpy(altitude,token);
+ token = strtok(NULL,separators);
+ strcpy(altitude_cor,token);
+ token = strtok(NULL,separators);
+ token = strtok(NULL,separators);
+ int n=atoi(nb_satellites); // calcul du nombre de satellites
+ printf("n=%d date = %s \n",n,date);
+ if (n>3&&(strcmp(date,"")!=0)) { // si precision correcte on envoie la trame et on éteint le GPS
+ // enregistrement des donnees GPS dans la carte SD
+ // serial_port.attach(0,SerialBase::RxIrq);
+ Led_rouge=0;
+ // Montage de la carte SD (Led rouge temoin allumee 1s)
+ Led_rouge=0;
+ thread_sleep_for (1000);
+ // Montage du systeme de fichier
+ BlockDevice *bd = BlockDevice::get_default_instance();
+ FATFileSystem fs("fs");
+ printf("Montage de la carte SD ... : ");
+ int err = fs.mount(bd);
+ if (err!=0) printf("ERREUR !!! \n"); // Message d'erreur
+ else {
+ printf("OK \n");
+ Led_rouge=1;
+ }
+ //thread_sleep_for (1000);
+ printf("Opening \"/fs/test1.txt\"... : ");
+ FILE *fichier = fopen("/fs/test1.txt", "a");
+ if (fichier==NULL) printf("ERREUR !!! \n"); // Message d'erreur
+ else {
+ printf(" OK \n");
+ fprintf (fichier,"%s : %s : %s : %s : %s\n : %s \n ",date,horaire,lattitude, longitude,altitude,nb_satellites);
+ fclose(fichier); // Fermeture du fichier test1.txt
+ err = fs.unmount();
+ Cmd_GPS=0; // on eteintle GPS lors d'une lecture correcte
+ Led_rouge=1;
+ // on charge la nouvelle alarme
+ horloge.get_time(&time); // lecture heure courante sur htr
+ printf ("Programme Lance a %d : %d : %d \n",time.hours,time.minutes,time.seconds);
+ secondes=time.seconds;
+ minutes=time.minutes+1;
+ heures=time.hours;
+ if (secondes>60) {
+ secondes = secondes-60;
+ minutes=minutes+1; // calcul temps alarme prochain
+ if (minutes >59) {
+ minutes=minutes-60;
+ heures = heures+1;
+ if (heures>23) heures=0;
+ }//fin minutes
+ }//fin secondes
+ alarm.seconds=secondes;
+ alarm.minutes=minutes;
+ alarm.hours=heures;
+ alarm.am1=0; // on declenche ici une alarme à l'heure reglee
+ alarm.am2=0;
+ alarm.am3=0;
+ alarm.am4=1;
+ test = horloge.set_alarm(alarm, 1);
+ // on coupe l'alarme alim repart a l'alerme programmee
+ DS_3231_cntl_stat_t data = {0x05, 0x00}; // configuration registres etat et controle
+ horloge.set_cntl_stat_reg(data);
+ }// fin else
+ }// fin n>4
+ } // fin compare chaines GGA
+ } //fin traitement chaine sans erreur de crc
+ flag_ISR_read=0; // raz ISR
+ } //fin if (flag_ISR_read==1)
+ }// fin while true
+ }// fin programme principal
}
-}
+ }
+ */
+
+
+ /******* Sous fonctions ***************/
+ void ISR_read() { // lecture liaison serie
+ char carac;
+ capt.read(&carac, 1);
+ if (index==100 || carac=='$') index=0;
+ Rx_buffer[index]=carac;
+ index++;
+ if (carac==0x0a) {
+ Rx_buffer[index]=0;
+ for (char i=0; i<index+1; i++) trame[i] = Rx_buffer[i];
+ index=0;
+ flag_ISR_read=1;
+ }
+ }
+
+ uint8_t gencrc2(uint8_t *data) { // calcul crc NMEA
+ uint8_t crc;
+ crc=data[1];
+ char i=2;
+ while (data[i]!=0) {
+ crc = crc^data[i];
+ i++;
+ }
+ return crc;
+ }
\ No newline at end of file