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.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:3a3ba27a08ee
- Parent:
- 3:307e8b3d4968
diff -r 307e8b3d4968 -r 3a3ba27a08ee main.cpp
--- a/main.cpp Mon Feb 10 15:47:04 2020 +0000
+++ b/main.cpp Mon Feb 17 22:07:11 2020 +0000
@@ -1,23 +1,42 @@
#include "mbed.h"
-#include "stm32f4xx_hal_flash.h"
-
-#if 1
-#define DEBUG_PRINTF pc.printf
-#else
-#define DEBUG_PRINTF(...)
-#endif
// tempo in secondi dl Ticker
-#define SECPERTICK 5
+#define SECPERTICK 1
+
+DigitalOut MyLed(LED1);
+AnalogIn In20(PA_0); //20 Volt
+AnalogIn In5(PB_0); //5 Volt
+AnalogIn In42(PC_0); //4.2 Volt
+Serial pc(USBTX,USBRX);
+Serial MyBle(PA_9,PA_10);
// numero di volte che il programma entra nella funzione Ticker
int nCountTick;
int nOldCountTick;
+volatile bool bSwitch;
+volatile float fSommaMisureVPiezo = 0.0;
+volatile float fSommaMisureVOut = 0.0;
+volatile float fSommaMisureVBatt = 0.0;
+volatile int nContaMisure= 0;
+volatile float fAverageVBatt =0.0;
+volatile float fAverageVOut =0.0;
+volatile float fAverageVPiezo =0.0;
// variabili contenenti le tensioni lette con gli ADC
float fOut;
float fPiezo;
float fBatt;
+float temp;
+char stringa[128];
+char stringapc[128];
+const float R1 = 562000.0;
+const float R2 = 100000.0;
+const float R3 = 274000.0;
+const float R4 = 562000.0;
+const float R5 = 100000.0;
+const float R6 = 332000.0;
+int contStr = 0;
+int contStrPc = 0;
// indice per i cicli
int nIndex;
@@ -26,162 +45,163 @@
Ticker TimeTick;
-namespace {
- Serial pc(USBTX, USBRX);
-
- //STM32F411.C/E(DM00119316.pdf p.43 Table 4)
- const struct {
- uint8_t sector;
- uint32_t addr;
- } FLASH_SECTOR[] = {
- {
- FLASH_SECTOR_0,
- (uint32_t)0x08000000 /* Base address of Sector 0, 16 Kbytes */
- },
- {
- FLASH_SECTOR_1,
- (uint32_t)0x08004000 /* Base address of Sector 1, 16 Kbytes */
- },
- {
- FLASH_SECTOR_2,
- (uint32_t)0x08008000 /* Base address of Sector 2, 16 Kbytes */
- },
+void TimeTickFunction()
+{
+ if(bSwitch == true) //Variabile accensione o spegnimento trasmissione dati(cambia attraverso uno switch sull'app)
{
- FLASH_SECTOR_3,
- (uint32_t)0x0800c000 /* Base address of Sector 3, 16 Kbytes */
- },
- {
- FLASH_SECTOR_4,
- (uint32_t)0x08010000 /* Base address of Sector 4, 64 Kbytes */
- },
- {
- FLASH_SECTOR_5,
- (uint32_t)0x08020000 /* Base address of Sector 5, 128 Kbytes */
- },
- {
- FLASH_SECTOR_6,
- (uint32_t)0x08040000 /* Base address of Sector 6, 128 Kbytes */
- },
- {
- FLASH_SECTOR_7,
- (uint32_t)0x08060000 /* Base address of Sector 7, 128 Kbytes */
+ // Calcolo valore medio di: VPiezo,VOut,VBatt
+ fAverageVBatt = fSommaMisureVBatt/nContaMisure;
+ fAverageVPiezo = fSommaMisureVPiezo/nContaMisure;
+ fAverageVOut = fSommaMisureVOut/nContaMisure;
+ // Reinizzializzo le variabili necessarie alla somma
+ fSommaMisureVBatt =0.0;
+ fSommaMisureVOut = 0.0;
+ fSommaMisureVPiezo = 0.0;
+ /* Mando Valori medi all'applicazione tramite Bluetooth utilizzando il seguente protocollo:
+ *P Piezo
+ *O VOut
+ *B VBatt
+ */
+ MyBle.printf("*P%.2f",fAverageVPiezo);
+ pc.printf("PC>: P %.2f\n\r",fAverageVPiezo);
+ MyBle.printf("*O%.2f",fAverageVOut);
+ pc.printf("PC>: O %.2f\n\r",fAverageVOut);
+ MyBle.printf("*B%.2f",fAverageVBatt);
+ pc.printf("PC>: B %.2f\n\r",fAverageVBatt);
+
+ // indice di tick
+ nCountTick++;
+ // reinizializzo contatore di misure
+ nContaMisure =0;
+ //Mando valori medi in seriale al pc per questioni di diagnostica
+ pc.printf("Secondi= %d; fPiezo = %.3f; fOut = %.3f; fBatt = %.3f\n\r", (nCountTick*SECPERTICK), fAverageVPiezo, fAverageVOut, fAverageVBatt);
}
- };
+
+
- /** @brief 内蔵FLASHへのbyte書込み(セクタ消去有り)
- *
- * @param[in] addr 書込み先アドレス(ADDR_FLASH_SECTOR_x)
- * @param[in] pData 書き込みデータ
- * @param[in] Len 書き込みデータサイズ
- */
- void programByte(int sector, const uint8_t *pData, uint8_t Len)
- {
- HAL_StatusTypeDef ret;
-
- /* flash control registerへのアクセス許可 */
- HAL_FLASH_Unlock();
- 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\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);
- ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, *pData);
- if (ret != HAL_OK) {
- //trouble!!
- DEBUG_PRINTF("Fail\n\r");
- while (true) {}
- }
- addr++;
- pData++;
- }
-
- /* flash control registerへのアクセス禁止 */
- HAL_FLASH_Lock();
- }
-
- const uint8_t *getFlash(int sector) {
- 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;
- }
+ //Inizializzazione del contatore delle misure
+ nContaMisure =0;
TimeTick.attach(&TimeTickFunction,SECPERTICK);
+ char cRxChar;
+ char pcRxChar;
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));
- */
+ {
+ fPiezo=In20.read(); //Acquisice la tensione in dal piezo
+ fPiezo=fPiezo*3.3; //Converte la tensione del piezo in un range da 0 a 3.3 Volt
+ fPiezo= (fPiezo/(R2/(R1+R2))); //Riporta il valore di tensione a prima del cambiamento causato dai partitori di tensione
+ fSommaMisureVPiezo += fPiezo;
+ fOut=In5.read(); //Acquisice la tensione in uscita dall'alimentatore
+ fOut=fOut*3.3; //Converte la tensione del tensione in uscita in un range da 0 a 3.3 Volt
+ fOut= (fOut/(R4/(R3+R4))); //Riporta il valore di tensione a prima del cambiamento causato dai partitori di tensione
+ fSommaMisureVOut += fOut;
+ fBatt=In42.read(); //Acquisice la tensione della batteria
+ fBatt=fBatt*3.3; //Converte la tensione della batteria in un range da 0 a 3.3 Volt
+ fBatt= (fBatt/(R6/(R5+R6))); //Riporta il valore di tensione a prima del cambiamento causato dai partitori di tensione
+ fSommaMisureVBatt +=fBatt;
+ nContaMisure++;
+
+
+
+
- /*
- uint8_t str[] = "t:hh:mm:Ss;P=19.2;B=3.2;O=3.1";
-
-
- DEBUG_PRINTF("%s\n", str);
-
- programByte(4, str, sizeof(str));
- while(true)
- {
- DEBUG_PRINTF("success[%s]\n\r", getFlash(4));
- DEBUG_PRINTF("success[%s]\n\r", getFlash(7));
- wait(4);
- }
- */
-
+ //Non ci possono essere trasmissioni contemporanee da Bluetooth e da Usb
+
+ //Protocollo comunicazione, invio messaggi da PC a Bluetooth
+ while(MyBle.readable())
+ {
+ cRxChar = MyBle.getc();
+ //Protocollo di accensione e spegnimento della visualizzazione delle tensioni sull'APP
+ if(cRxChar == 'S')
+ { pc.printf("PC<: Communication switched on\n\r");
+ MyBle.printf("*#Communication switched on\n\r*");
+ bSwitch = true;
+
+ }
+
+ if(cRxChar == 'E')
+ {
+ pc.printf("PC<: Communication switched off\n\r");
+ MyBle.printf("*#Communication switched off\n\r*");
+ bSwitch = false;
+ }
+
+ //Protocollo comunicazione, invio messaggi da Bluetooh a PC
+ if(cRxChar == '#')
+ {
+ stringa[0] = '#';
+ for(int i = 1;i<=128;i++)
+ {
+ stringa[i] = MyBle.getc(); //Acquisisce caratteri dallo smartphone e li inserisco in un'array
+ contStr = i;
+ if(stringa[i] == '#') //Termina il caricamento dell'array quando riceve il '#'
+ {
+ break;
+ }
+
+ }
+ pc.printf("\rPC<: ");
+
+ for(int i = 1;i<contStr;i++)
+ //Stampa sulla seriale del PC l'array caricato precedentemente
+ { pc.printf("%c", stringa[i]);
+
+ }
+ pc.printf("\n\r");
+ }
+
+ }//if(MyBle.readable())
+ // SISTEMA DI COMUNICAZIONE FUNZIONANTE
+ while(pc.readable())
+ {
+ pcRxChar = pc.getc();
+ switch(pcRxChar)
+ {
+ case '#':
+ if(stringapc[0] == '#')
+ {
+ pc.printf("\rPC>: ");
+ MyBle.putc('*');
+
+ for(int i = 0;i<contStrPc;i++)
+ {
+ MyBle.printf("%c" , stringapc[i]);
+ pc.printf("%c", stringapc[i]);
+ }
+ pc.printf("\n\r");
+ MyBle.printf("\n\r*");
+ for(int i = 0;i<contStrPc;i++)
+ {
+ stringapc[i] = ' ';
+ }
+ contStrPc = 0;
+
+
+ }
+ else
+ {
+ stringapc[0] = '#';
+ contStrPc++;
+ }
+ break;
+ default:
+ if(stringapc[0] == '#')
+ {
+ stringapc[contStrPc] = pcRxChar;
+ contStrPc++;
+ }
+ break;
+
+ }//switch(pcRxChar)
+ }//while(MyBle.readable())
+
+ } //while (true)
+
}
+
+
+
\ No newline at end of file