FormazioneSitael / Mbed 2 deprecated TesysRail_Rev_FINALE

Dependencies:   mbed

Revision:
3:307e8b3d4968
Parent:
2:0db92723d44f
Child:
4:3a3ba27a08ee
--- a/main.cpp	Mon Feb 10 11:56:18 2020 +0000
+++ b/main.cpp	Mon Feb 10 15:47:04 2020 +0000
@@ -7,6 +7,25 @@
 #define DEBUG_PRINTF(...)
 #endif
 
+// tempo in secondi dl Ticker
+#define SECPERTICK 5
+
+// numero di volte che il programma entra nella funzione Ticker
+int nCountTick;
+int nOldCountTick;
+
+// variabili contenenti le tensioni lette con gli ADC
+float fOut;
+float fPiezo;
+float fBatt;
+
+// indice per i cicli
+int nIndex;
+
+
+
+Ticker TimeTick;
+
 namespace {
     Serial pc(USBTX, USBRX);
     
@@ -61,20 +80,21 @@
     
         /* flash control registerへのアクセス許可 */
         HAL_FLASH_Unlock();
-        DEBUG_PRINTF("unlocked.\n");
+        DEBUG_PRINTF("Unlocked\n\r");
     
         /* 消去(電圧 [2.7V to 3.6V]) */
         FLASH_Erase_Sector(FLASH_SECTOR[sector].sector, FLASH_VOLTAGE_RANGE_3);
-        DEBUG_PRINTF("erased.\n");
+        DEBUG_PRINTF("Erased\n\r");
 
         /* 書込み(4byte単位) */
         uint32_t addr = FLASH_SECTOR[sector].addr;
+        DEBUG_PRINTF("Writing\n\r");
         for (int lp = 0 ; lp < Len; lp++) {
-            DEBUG_PRINTF("addr:0x%08x  data=%02x\n", addr, *pData);
+            //DEBUG_PRINTF("addr:0x%08x  data=%02x\n", addr, *pData);
             ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
             if (ret != HAL_OK) {
                 //trouble!!
-                DEBUG_PRINTF("fail\n");
+                DEBUG_PRINTF("Fail\n\r");
                 while (true) {}
             }
             addr++;
@@ -89,11 +109,67 @@
          return (const uint8_t *)FLASH_SECTOR[sector].addr; }
 }
 
-
+void TimeTickFunction()
+{
+    // indice di tick
+    nCountTick++;
+    
+    // leggo i valori dagli ADC
+    fOut++;
+    fPiezo++;
+    fBatt++;
+    
+    // visualizza i valori
+    pc.printf("\r\n-- Nel ticker --\r\n");
+    pc.printf("Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f\n\r", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
+}
 
 int main()
 {
+    nCountTick=0;
+    nOldCountTick =-1;
+    char caLineToWrite[]="Seconds= 1800; fOut = 4.531; fPiezo = 19.872; fBatt = 3.523"; 
+    //uint8_t str[]="Seconds= 0000; fOut = 00000; fPiezo = 000000; fBatt = 00000000";
     
+    uint8_t str[8000];
+    long nIndexFlash=0;
+    fOut = 4.531;
+    fPiezo = 9.872;
+    fBatt = 3.523;
+    
+    // resetta array
+    for(nIndex=0; nIndex < 8000; nIndex++)
+    {
+        str[nIndex] =0;
+    }
+    TimeTick.attach(&TimeTickFunction,SECPERTICK);
+    while(true)
+    {
+        if(nCountTick != nOldCountTick)
+        {
+            nOldCountTick = nCountTick;
+            sprintf(caLineToWrite, "Seconds= %d; fOut = %.3f; fPiezo = %.3f; fBatt = %.3f", (nCountTick*SECPERTICK), fOut, fPiezo, fBatt); // scrive linea completa
+            for(nIndex =0; nIndex < strlen(caLineToWrite); nIndex++) // trasferisce linea completa in formato leggibile da programByte
+            {
+                str[nIndex] = caLineToWrite[nIndex];
+            }
+            //str[nIndex++] = NULL;
+                
+            //pc.printf("%s\n\r", caLineToWrite);    
+            //DEBUG_PRINTF("%s\n\r", caLineToWrite);
+            
+            programByte(4, str, sizeof(str));
+            wait(1);
+            DEBUG_PRINTF("%s\n\r", getFlash(4));
+        }    
+    }
+    /*
+    programByte(4, str, sizeof(str));
+    DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
+    */
+    
+    
+    /*
     uint8_t str[] = "t:hh:mm:Ss;P=19.2;B=3.2;O=3.1";
     
     
@@ -106,5 +182,6 @@
         DEBUG_PRINTF("success[%s]\n\r", getFlash(7));
         wait(4);
     }
+    */
     
 }