Adesso funziona...
Dependencies: mbed
Fork of TempSensorBATTISTA by
TempSensor.cpp@2:dbaf3140560a, 2018-05-12 (annotated)
- Committer:
- pinofal
- Date:
- Sat May 12 12:44:47 2018 +0000
- Revision:
- 2:dbaf3140560a
- Child:
- 3:dca542c609c9
Amaldi Exercise 7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pinofal | 2:dbaf3140560a | 1 | // Tested: NUCLEO-F207ZG |
pinofal | 2:dbaf3140560a | 2 | #include "mbed.h" |
pinofal | 2:dbaf3140560a | 3 | |
pinofal | 2:dbaf3140560a | 4 | // numero di acqusizioni su cui effettuare la media della temperatura |
pinofal | 2:dbaf3140560a | 5 | #define NUMSAMPLE 300 |
pinofal | 2:dbaf3140560a | 6 | |
pinofal | 2:dbaf3140560a | 7 | AnalogIn InWave(PC_0); |
pinofal | 2:dbaf3140560a | 8 | Serial pc(USBTX, USBRX); |
pinofal | 2:dbaf3140560a | 9 | |
pinofal | 2:dbaf3140560a | 10 | |
pinofal | 2:dbaf3140560a | 11 | // Output LED di diagnostica |
pinofal | 2:dbaf3140560a | 12 | DigitalOut led1(LED1); // verde |
pinofal | 2:dbaf3140560a | 13 | DigitalOut led2(LED2); // blu |
pinofal | 2:dbaf3140560a | 14 | DigitalOut led3(LED3); // rosso |
pinofal | 2:dbaf3140560a | 15 | |
pinofal | 2:dbaf3140560a | 16 | |
pinofal | 2:dbaf3140560a | 17 | // ticker per l'acquisizione dell'onda con ADC |
pinofal | 2:dbaf3140560a | 18 | Ticker SamplingTicker; |
pinofal | 2:dbaf3140560a | 19 | |
pinofal | 2:dbaf3140560a | 20 | // carattere in arrivo dal PC ed equivalente numerico |
pinofal | 2:dbaf3140560a | 21 | volatile char cReadChar; |
pinofal | 2:dbaf3140560a | 22 | volatile int nReadChar; |
pinofal | 2:dbaf3140560a | 23 | |
pinofal | 2:dbaf3140560a | 24 | // flag che diventa true quando si vuole fermare l'acquisizione |
pinofal | 2:dbaf3140560a | 25 | volatile bool bStop; |
pinofal | 2:dbaf3140560a | 26 | |
pinofal | 2:dbaf3140560a | 27 | // valore letto dall'ADC e corrispondente in tensione |
pinofal | 2:dbaf3140560a | 28 | volatile unsigned short usReadADC; |
pinofal | 2:dbaf3140560a | 29 | volatile float fReadVoltage; |
pinofal | 2:dbaf3140560a | 30 | |
pinofal | 2:dbaf3140560a | 31 | // valore di temperatura letto dall'ADC |
pinofal | 2:dbaf3140560a | 32 | volatile float fTemp; |
pinofal | 2:dbaf3140560a | 33 | |
pinofal | 2:dbaf3140560a | 34 | //*************************** |
pinofal | 2:dbaf3140560a | 35 | // Acquisizione da ADC |
pinofal | 2:dbaf3140560a | 36 | //*************************** |
pinofal | 2:dbaf3140560a | 37 | void Sampling() |
pinofal | 2:dbaf3140560a | 38 | { |
pinofal | 2:dbaf3140560a | 39 | // indice per i cicli |
pinofal | 2:dbaf3140560a | 40 | int nIndex; |
pinofal | 2:dbaf3140560a | 41 | // valore medio della temperatura su NUMACQUISISIONI acquisizioni |
pinofal | 2:dbaf3140560a | 42 | float fAvgTemp; |
pinofal | 2:dbaf3140560a | 43 | |
pinofal | 2:dbaf3140560a | 44 | // se è stato inviato il comando Stop, non fare niente fino a nuovo comando |
pinofal | 2:dbaf3140560a | 45 | if(bStop) |
pinofal | 2:dbaf3140560a | 46 | { |
pinofal | 2:dbaf3140560a | 47 | } |
pinofal | 2:dbaf3140560a | 48 | else // se non è stato inviato il comando di bStop continua |
pinofal | 2:dbaf3140560a | 49 | { |
pinofal | 2:dbaf3140560a | 50 | // inizializza il valore medio della temperatura |
pinofal | 2:dbaf3140560a | 51 | fAvgTemp=0.0; |
pinofal | 2:dbaf3140560a | 52 | for(nIndex=0; nIndex < NUMSAMPLE; nIndex++) |
pinofal | 2:dbaf3140560a | 53 | { |
pinofal | 2:dbaf3140560a | 54 | // acquisisce dato da ADC |
pinofal | 2:dbaf3140560a | 55 | usReadADC = InWave.read_u16(); |
pinofal | 2:dbaf3140560a | 56 | fReadVoltage=(usReadADC*3.3)/65535.0; // converte in Volt il valore numerico letto dall'ADC |
pinofal | 2:dbaf3140560a | 57 | //fReadVoltage=InWave.read(); // acquisisce il valore dall'ADC come valore di tensione in volt |
pinofal | 2:dbaf3140560a | 58 | fTemp= ((fReadVoltage-0.25)*100.0)/(3.05-0.25); //applica la formula della retta tra i valori minimo e massimo del sensore |
pinofal | 2:dbaf3140560a | 59 | fAvgTemp+=fTemp; |
pinofal | 2:dbaf3140560a | 60 | } |
pinofal | 2:dbaf3140560a | 61 | // calcola valore medio su NUMSAMPLE acquisizioni |
pinofal | 2:dbaf3140560a | 62 | fAvgTemp/= NUMSAMPLE; |
pinofal | 2:dbaf3140560a | 63 | |
pinofal | 2:dbaf3140560a | 64 | // accendi LED in base a superamento soglie |
pinofal | 2:dbaf3140560a | 65 | if (fAvgTemp <= 30.0) |
pinofal | 2:dbaf3140560a | 66 | { |
pinofal | 2:dbaf3140560a | 67 | // accendi LED Blu sotto i 28° |
pinofal | 2:dbaf3140560a | 68 | led1=0; |
pinofal | 2:dbaf3140560a | 69 | led2=1; |
pinofal | 2:dbaf3140560a | 70 | led3=0; |
pinofal | 2:dbaf3140560a | 71 | } |
pinofal | 2:dbaf3140560a | 72 | else |
pinofal | 2:dbaf3140560a | 73 | { |
pinofal | 2:dbaf3140560a | 74 | if(fAvgTemp >= 32.0) |
pinofal | 2:dbaf3140560a | 75 | { |
pinofal | 2:dbaf3140560a | 76 | // accendi LED Rosso sopra i 32° |
pinofal | 2:dbaf3140560a | 77 | led1=0; |
pinofal | 2:dbaf3140560a | 78 | led2=0; |
pinofal | 2:dbaf3140560a | 79 | led3=1; |
pinofal | 2:dbaf3140560a | 80 | } |
pinofal | 2:dbaf3140560a | 81 | else |
pinofal | 2:dbaf3140560a | 82 | { |
pinofal | 2:dbaf3140560a | 83 | // accendi LED verde tra 28 e 30 |
pinofal | 2:dbaf3140560a | 84 | led1=1; |
pinofal | 2:dbaf3140560a | 85 | led2=0; |
pinofal | 2:dbaf3140560a | 86 | led3=0; |
pinofal | 2:dbaf3140560a | 87 | } |
pinofal | 2:dbaf3140560a | 88 | } |
pinofal | 2:dbaf3140560a | 89 | |
pinofal | 2:dbaf3140560a | 90 | |
pinofal | 2:dbaf3140560a | 91 | |
pinofal | 2:dbaf3140560a | 92 | |
pinofal | 2:dbaf3140560a | 93 | // invia il dato al PC |
pinofal | 2:dbaf3140560a | 94 | //pc.printf("\n\r--- Voltage= %.1f [Volt]; Temperature= %.1f [Celsius] ---\n\r", fReadVoltage, fTemp); |
pinofal | 2:dbaf3140560a | 95 | pc.printf("\n\r--- Digital= %d [Volt]; Temperature= %.2f [Celsius] ---\n\r", usReadADC, fTemp); |
pinofal | 2:dbaf3140560a | 96 | |
pinofal | 2:dbaf3140560a | 97 | |
pinofal | 2:dbaf3140560a | 98 | |
pinofal | 2:dbaf3140560a | 99 | /* |
pinofal | 2:dbaf3140560a | 100 | // prepara il pacchetto di dati acquisiti da restituire al PC |
pinofal | 2:dbaf3140560a | 101 | caTxPacket[nSampleInCount]= (char)(usReadADC&0xFF); |
pinofal | 2:dbaf3140560a | 102 | //+++caTxPacket[nSampleInCount]= 'a'; |
pinofal | 2:dbaf3140560a | 103 | nSampleInCount++; |
pinofal | 2:dbaf3140560a | 104 | caTxPacket[nSampleInCount] = (char)((usReadADC>>8)&0xFF); |
pinofal | 2:dbaf3140560a | 105 | //++++caTxPacket[nSampleInCount]= 'b'; |
pinofal | 2:dbaf3140560a | 106 | */ |
pinofal | 2:dbaf3140560a | 107 | |
pinofal | 2:dbaf3140560a | 108 | } |
pinofal | 2:dbaf3140560a | 109 | } |
pinofal | 2:dbaf3140560a | 110 | |
pinofal | 2:dbaf3140560a | 111 | |
pinofal | 2:dbaf3140560a | 112 | |
pinofal | 2:dbaf3140560a | 113 | |
pinofal | 2:dbaf3140560a | 114 | //******************* |
pinofal | 2:dbaf3140560a | 115 | // Loop Principale |
pinofal | 2:dbaf3140560a | 116 | //******************* |
pinofal | 2:dbaf3140560a | 117 | int main() |
pinofal | 2:dbaf3140560a | 118 | { |
pinofal | 2:dbaf3140560a | 119 | // periodo di campionamento |
pinofal | 2:dbaf3140560a | 120 | int nDeltaT; |
pinofal | 2:dbaf3140560a | 121 | |
pinofal | 2:dbaf3140560a | 122 | |
pinofal | 2:dbaf3140560a | 123 | // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto |
pinofal | 2:dbaf3140560a | 124 | pc.baud(921600); //921600 bps |
pinofal | 2:dbaf3140560a | 125 | // messaggio di benvenuto |
pinofal | 2:dbaf3140560a | 126 | pc.printf("\r\nHallo Amaldi Students - Exercise 7 \r\n"); |
pinofal | 2:dbaf3140560a | 127 | pc.printf("\r\n*** Bluetooth Temp Acquisition ***\r\n"); |
pinofal | 2:dbaf3140560a | 128 | |
pinofal | 2:dbaf3140560a | 129 | // inizializza variabili |
pinofal | 2:dbaf3140560a | 130 | bStop=true; |
pinofal | 2:dbaf3140560a | 131 | |
pinofal | 2:dbaf3140560a | 132 | // test dei LED |
pinofal | 2:dbaf3140560a | 133 | led1=1; //Verde |
pinofal | 2:dbaf3140560a | 134 | wait_ms(1000); |
pinofal | 2:dbaf3140560a | 135 | led1=0; |
pinofal | 2:dbaf3140560a | 136 | led2=1; // Blu |
pinofal | 2:dbaf3140560a | 137 | wait_ms(1000); |
pinofal | 2:dbaf3140560a | 138 | led2=0; |
pinofal | 2:dbaf3140560a | 139 | led3=1; //Rosso |
pinofal | 2:dbaf3140560a | 140 | wait_ms(1000); |
pinofal | 2:dbaf3140560a | 141 | led3=0; |
pinofal | 2:dbaf3140560a | 142 | |
pinofal | 2:dbaf3140560a | 143 | |
pinofal | 2:dbaf3140560a | 144 | |
pinofal | 2:dbaf3140560a | 145 | while(true) |
pinofal | 2:dbaf3140560a | 146 | { |
pinofal | 2:dbaf3140560a | 147 | // verifica se è arrivato un carattere dalla seriale del PC |
pinofal | 2:dbaf3140560a | 148 | if(pc.readable()) |
pinofal | 2:dbaf3140560a | 149 | { |
pinofal | 2:dbaf3140560a | 150 | cReadChar = pc.getc(); // Read hyperterminal |
pinofal | 2:dbaf3140560a | 151 | |
pinofal | 2:dbaf3140560a | 152 | if((cReadChar == 'S') || (cReadChar == 's')) // blocca acquisizione se riceve 'S' oppure 's' |
pinofal | 2:dbaf3140560a | 153 | { |
pinofal | 2:dbaf3140560a | 154 | bStop= true; |
pinofal | 2:dbaf3140560a | 155 | pc.printf("\n\r--- Acquisition Stopped ---\n\r"); |
pinofal | 2:dbaf3140560a | 156 | } |
pinofal | 2:dbaf3140560a | 157 | if((cReadChar >= '0') && (cReadChar <='9')) |
pinofal | 2:dbaf3140560a | 158 | { |
pinofal | 2:dbaf3140560a | 159 | bStop = false; |
pinofal | 2:dbaf3140560a | 160 | nReadChar = cReadChar -'0'; //converte il carattere acquisito dal PC nel corrispondente valore numerico |
pinofal | 2:dbaf3140560a | 161 | nDeltaT = nReadChar; // sampling Period in sec |
pinofal | 2:dbaf3140560a | 162 | pc.printf("\n\r--- Acquisition Started DeltaT = %c ---\n\r", cReadChar); |
pinofal | 2:dbaf3140560a | 163 | SamplingTicker.attach(&Sampling,nDeltaT); |
pinofal | 2:dbaf3140560a | 164 | } |
pinofal | 2:dbaf3140560a | 165 | |
pinofal | 2:dbaf3140560a | 166 | } //lettura da pc |
pinofal | 2:dbaf3140560a | 167 | } // while(true) |
pinofal | 2:dbaf3140560a | 168 | } |
pinofal | 2:dbaf3140560a | 169 | |
pinofal | 2:dbaf3140560a | 170 | |
pinofal | 2:dbaf3140560a | 171 | |
pinofal | 2:dbaf3140560a | 172 | |
pinofal | 2:dbaf3140560a | 173 | |
pinofal | 2:dbaf3140560a | 174 | |
pinofal | 2:dbaf3140560a | 175 | |
pinofal | 2:dbaf3140560a | 176 | |
pinofal | 2:dbaf3140560a | 177 | |
pinofal | 2:dbaf3140560a | 178 | |
pinofal | 2:dbaf3140560a | 179 | |
pinofal | 2:dbaf3140560a | 180 | |
pinofal | 2:dbaf3140560a | 181 | |
pinofal | 2:dbaf3140560a | 182 |