A program to measure the temperature using the Max6675 board. The temperature is shown on 8x8 Max7219 matrix display and it is saved on SD as well.
Dependencies: MAX7219 SDFileSystem mbed
Fork of MAXREFDES99_demo by
The program measures the temperature using the Max6675 board designed for Arduino. The temperature is shown on Max7219 8x8 matrix display (designed for Arduino as well) as text marque from right to left. The fonf size is 5x7 so the last row is used to show some info:
- All leds off: Normal temperature measuring - Leds shifting: Saving on SD card - Leds blinking: SD card missing or damaged
The Blue "user button" is used to save measured temperature on SD card. First click start saving, the second stop the process. Together the temperature is saved also the time. The time is reset when the process start. Connecting the USB to PC it's possible to see instant temperature and all saved data on SD.
main.cpp@12:1bd4a9f09a8d, 2018-10-04 (annotated)
- Committer:
- lore71
- Date:
- Thu Oct 04 11:37:43 2018 +0000
- Revision:
- 12:1bd4a9f09a8d
- Parent:
- 8:a6a0c9e280ae
A program to measure the temperature using the Max6675 board.; The temperature is shown on 8x8 Max7219 matrix display and it is saved on SD as well.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lore71 | 12:1bd4a9f09a8d | 1 | // By Lorenzo Cervelli - Sept.2018 |
lore71 | 12:1bd4a9f09a8d | 2 | // OK su STM32-F401RE |
lore71 | 12:1bd4a9f09a8d | 3 | |
lore71 | 12:1bd4a9f09a8d | 4 | #include "mbed.h" |
lore71 | 12:1bd4a9f09a8d | 5 | |
lore71 | 12:1bd4a9f09a8d | 6 | // Display 8x8 con Max7219 |
pradipv | 8:a6a0c9e280ae | 7 | #include "maxrefdes99.h" |
j3 | 0:6b0161c3e440 | 8 | |
lore71 | 12:1bd4a9f09a8d | 9 | // Sensore di temperatura con Max6675 |
lore71 | 12:1bd4a9f09a8d | 10 | #include "max6675.h" |
lore71 | 12:1bd4a9f09a8d | 11 | |
lore71 | 12:1bd4a9f09a8d | 12 | // Per gestire la SD |
lore71 | 12:1bd4a9f09a8d | 13 | #include "SDFileSystem.h" |
lore71 | 12:1bd4a9f09a8d | 14 | |
lore71 | 12:1bd4a9f09a8d | 15 | // Per il pulsante 'on board' |
lore71 | 12:1bd4a9f09a8d | 16 | InterruptIn my_button(USER_BUTTON); |
lore71 | 12:1bd4a9f09a8d | 17 | bool Btn = 0; |
lore71 | 12:1bd4a9f09a8d | 18 | |
lore71 | 12:1bd4a9f09a8d | 19 | // Per la SD |
lore71 | 12:1bd4a9f09a8d | 20 | FILE *fp; |
lore71 | 12:1bd4a9f09a8d | 21 | Serial pc(USBTX, USBRX); |
lore71 | 12:1bd4a9f09a8d | 22 | |
lore71 | 12:1bd4a9f09a8d | 23 | // Per il tempo di funzionamento (da quando e' alimentato) |
lore71 | 12:1bd4a9f09a8d | 24 | Timer t; |
lore71 | 12:1bd4a9f09a8d | 25 | |
lore71 | 12:1bd4a9f09a8d | 26 | void pressed() // Per il pulsante 'on board' |
lore71 | 12:1bd4a9f09a8d | 27 | { |
lore71 | 12:1bd4a9f09a8d | 28 | // Resetto |
lore71 | 12:1bd4a9f09a8d | 29 | if (Btn==0) |
lore71 | 12:1bd4a9f09a8d | 30 | { |
lore71 | 12:1bd4a9f09a8d | 31 | Btn=1; |
lore71 | 12:1bd4a9f09a8d | 32 | |
lore71 | 12:1bd4a9f09a8d | 33 | // Rimuovo il file precedente |
lore71 | 12:1bd4a9f09a8d | 34 | remove("/sd/sdtest.txt"); |
lore71 | 12:1bd4a9f09a8d | 35 | wait(1); // Lascio il tempo al file-system |
lore71 | 12:1bd4a9f09a8d | 36 | |
lore71 | 12:1bd4a9f09a8d | 37 | // Apro il file sulla SD x la scrittura |
lore71 | 12:1bd4a9f09a8d | 38 | printf(" OK, writing to SD card... \r\n"); |
lore71 | 12:1bd4a9f09a8d | 39 | fp = fopen("/sd/sdtest.txt", "w"); |
lore71 | 12:1bd4a9f09a8d | 40 | wait(2); // Lascio il tempo al file-system |
lore71 | 12:1bd4a9f09a8d | 41 | |
lore71 | 12:1bd4a9f09a8d | 42 | // Faccio partire il tempo |
lore71 | 12:1bd4a9f09a8d | 43 | t.start(); |
lore71 | 12:1bd4a9f09a8d | 44 | } |
lore71 | 12:1bd4a9f09a8d | 45 | else |
lore71 | 12:1bd4a9f09a8d | 46 | { |
lore71 | 12:1bd4a9f09a8d | 47 | Btn=0; |
lore71 | 12:1bd4a9f09a8d | 48 | |
lore71 | 12:1bd4a9f09a8d | 49 | // Chiudo il file |
lore71 | 12:1bd4a9f09a8d | 50 | fclose(fp); |
lore71 | 12:1bd4a9f09a8d | 51 | printf(" OK successfully saved! \r\n"); |
lore71 | 12:1bd4a9f09a8d | 52 | |
lore71 | 12:1bd4a9f09a8d | 53 | // Lascio il tempo al file-system |
lore71 | 12:1bd4a9f09a8d | 54 | wait(1); |
lore71 | 12:1bd4a9f09a8d | 55 | |
lore71 | 12:1bd4a9f09a8d | 56 | // Fermo il tempo |
lore71 | 12:1bd4a9f09a8d | 57 | t.stop(); |
lore71 | 12:1bd4a9f09a8d | 58 | |
lore71 | 12:1bd4a9f09a8d | 59 | // Leggo tutto il file delle temperature e mostro sulla seriale |
lore71 | 12:1bd4a9f09a8d | 60 | int c; |
lore71 | 12:1bd4a9f09a8d | 61 | pc.printf("\n Reading from SD card... \r\n"); |
lore71 | 12:1bd4a9f09a8d | 62 | pc.printf(" ----- BEGIN ----- \r\n"); |
lore71 | 12:1bd4a9f09a8d | 63 | fp = fopen("/sd/sdtest.txt", "r"); |
lore71 | 12:1bd4a9f09a8d | 64 | if (fp) |
lore71 | 12:1bd4a9f09a8d | 65 | { |
lore71 | 12:1bd4a9f09a8d | 66 | while ((c = getc(fp)) != EOF) |
lore71 | 12:1bd4a9f09a8d | 67 | pc.putc(c); |
lore71 | 12:1bd4a9f09a8d | 68 | fclose(fp); |
lore71 | 12:1bd4a9f09a8d | 69 | } |
lore71 | 12:1bd4a9f09a8d | 70 | pc.printf(" ----- END ----- \r\n"); |
lore71 | 12:1bd4a9f09a8d | 71 | } |
lore71 | 12:1bd4a9f09a8d | 72 | } |
j3 | 0:6b0161c3e440 | 73 | |
j3 | 0:6b0161c3e440 | 74 | int main(void) |
lore71 | 12:1bd4a9f09a8d | 75 | { |
lore71 | 12:1bd4a9f09a8d | 76 | // Sensore di temperatura con Max6675 (Morpho Connectors su F401RE) |
lore71 | 12:1bd4a9f09a8d | 77 | SPI spi(PC_12, PC_11, PC_10); // MOSI, MISO, SCLK |
lore71 | 12:1bd4a9f09a8d | 78 | max6675 max(spi, PA_14); // CS |
lore71 | 12:1bd4a9f09a8d | 79 | |
lore71 | 12:1bd4a9f09a8d | 80 | // Scheda di memoria SD (Arduino Connectors su F401RE) |
lore71 | 12:1bd4a9f09a8d | 81 | SDFileSystem sd(D11, D12, D13, D8, "sd");// MOSI, MISO, SCK, CS |
lore71 | 12:1bd4a9f09a8d | 82 | |
lore71 | 12:1bd4a9f09a8d | 83 | // Settaggi SPI per 8x8 Led matrix Max7219 (Morpho Connectors su F401RE) |
lore71 | 12:1bd4a9f09a8d | 84 | // PB_15 --> DIN (MOSI) |
lore71 | 12:1bd4a9f09a8d | 85 | // PB_13 --> CLK (SCK) |
lore71 | 12:1bd4a9f09a8d | 86 | // PB_1 --> LOAD (CS) |
lore71 | 12:1bd4a9f09a8d | 87 | // PB_14 --> MISO |
lore71 | 12:1bd4a9f09a8d | 88 | Max7219 display(PB_15, PB_14, PB_13, PB_1); |
j3 | 0:6b0161c3e440 | 89 | |
j3 | 0:6b0161c3e440 | 90 | //struct for holding MAX7219 configuration data |
j3 | 0:6b0161c3e440 | 91 | max7219_configuration_t display_config; |
pradipv | 8:a6a0c9e280ae | 92 | |
lore71 | 12:1bd4a9f09a8d | 93 | // MAX7219 configuration data |
lore71 | 12:1bd4a9f09a8d | 94 | display_config.decode_mode = 0; // no BCD decode |
lore71 | 12:1bd4a9f09a8d | 95 | display_config.intensity = Max7219::MAX7219_INTENSITY_F; // max intensity |
lore71 | 12:1bd4a9f09a8d | 96 | display_config.scan_limit = Max7219::MAX7219_SCAN_8; // scan all digits |
j3 | 0:6b0161c3e440 | 97 | |
lore71 | 12:1bd4a9f09a8d | 98 | // MAX7219 set number of MAX7219 devices being used |
lore71 | 12:1bd4a9f09a8d | 99 | display.set_num_devices(1); |
j3 | 0:6b0161c3e440 | 100 | |
lore71 | 12:1bd4a9f09a8d | 101 | // MAX7219 config display |
j3 | 0:6b0161c3e440 | 102 | display.init_display(display_config); |
j3 | 0:6b0161c3e440 | 103 | |
lore71 | 12:1bd4a9f09a8d | 104 | // MAX7219 ensure all data registers are 0 |
j3 | 0:6b0161c3e440 | 105 | display.display_all_off(); |
j3 | 0:6b0161c3e440 | 106 | display.enable_display(); |
j3 | 0:6b0161c3e440 | 107 | |
lore71 | 12:1bd4a9f09a8d | 108 | // MAX7219 variables |
lore71 | 12:1bd4a9f09a8d | 109 | uint32_t user_input = 0; |
j3 | 0:6b0161c3e440 | 110 | char user_char; |
lore71 | 12:1bd4a9f09a8d | 111 | char *p_str; |
lore71 | 12:1bd4a9f09a8d | 112 | |
lore71 | 12:1bd4a9f09a8d | 113 | // MAX7219 Luminosita' (value from 0 to 15) |
lore71 | 12:1bd4a9f09a8d | 114 | display_config.intensity = 5; |
lore71 | 12:1bd4a9f09a8d | 115 | display.init_display(display_config); |
lore71 | 12:1bd4a9f09a8d | 116 | |
lore71 | 12:1bd4a9f09a8d | 117 | // MAX7219 Posizione di partenza (value from 1 to 32) |
lore71 | 12:1bd4a9f09a8d | 118 | user_char = 1; |
lore71 | 12:1bd4a9f09a8d | 119 | print_char(&display, user_input, user_char); |
lore71 | 12:1bd4a9f09a8d | 120 | |
lore71 | 12:1bd4a9f09a8d | 121 | // MAX7219 Contatore per utilizzare l'ultima riga della matrice 8x8 |
lore71 | 12:1bd4a9f09a8d | 122 | int Cur=1; |
j3 | 0:6b0161c3e440 | 123 | |
lore71 | 12:1bd4a9f09a8d | 124 | // Set button 'on board' |
lore71 | 12:1bd4a9f09a8d | 125 | my_button.fall(&pressed); |
lore71 | 12:1bd4a9f09a8d | 126 | |
lore71 | 12:1bd4a9f09a8d | 127 | // Ciclo continuo |
lore71 | 12:1bd4a9f09a8d | 128 | while(1) |
lore71 | 12:1bd4a9f09a8d | 129 | { |
lore71 | 12:1bd4a9f09a8d | 130 | int cnt; |
lore71 | 12:1bd4a9f09a8d | 131 | int idx; |
lore71 | 12:1bd4a9f09a8d | 132 | |
lore71 | 12:1bd4a9f09a8d | 133 | // Leggo la temperatura del Max6675 |
lore71 | 12:1bd4a9f09a8d | 134 | float TC = max.read_temp(); |
lore71 | 12:1bd4a9f09a8d | 135 | // Lascio il tempo al sensore di temperatura |
lore71 | 12:1bd4a9f09a8d | 136 | wait(0.5); |
lore71 | 12:1bd4a9f09a8d | 137 | |
lore71 | 12:1bd4a9f09a8d | 138 | // Stampo sulla seriale (Temperatura con 2 digits dopo la virgola e Time con 3 digits dopo la virgola) |
lore71 | 12:1bd4a9f09a8d | 139 | printf(" Temperature(C): %.2f Time(Sec): %.3f \r\n", TC, t.read()); |
lore71 | 12:1bd4a9f09a8d | 140 | |
lore71 | 12:1bd4a9f09a8d | 141 | // Scrivo nel file sulla SD |
lore71 | 12:1bd4a9f09a8d | 142 | if (Btn==1) |
j3 | 0:6b0161c3e440 | 143 | { |
lore71 | 12:1bd4a9f09a8d | 144 | // Scrivo nel file sulla SD |
lore71 | 12:1bd4a9f09a8d | 145 | if (fp != NULL) |
lore71 | 12:1bd4a9f09a8d | 146 | { |
lore71 | 12:1bd4a9f09a8d | 147 | fprintf(fp, "Temperature(C): %.2f Time(Sec): %.3f \r\n", TC, t.read()); |
lore71 | 12:1bd4a9f09a8d | 148 | |
lore71 | 12:1bd4a9f09a8d | 149 | // Lascio il tempo al file-system |
lore71 | 12:1bd4a9f09a8d | 150 | wait(0.1); |
lore71 | 12:1bd4a9f09a8d | 151 | |
lore71 | 12:1bd4a9f09a8d | 152 | // Accendo i digit dell'ultima riga della matrice in sequenza per far vedere che sto' registrando |
lore71 | 12:1bd4a9f09a8d | 153 | if (Cur==1){ display.write_digit(1, 8, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 154 | if (Cur==2){ display.write_digit(1, 7, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 155 | if (Cur==3){ display.write_digit(1, 6, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 156 | if (Cur==4){ display.write_digit(1, 5, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 157 | if (Cur==5){ display.write_digit(1, 4, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 158 | if (Cur==6){ display.write_digit(1, 3, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 159 | if (Cur==7){ display.write_digit(1, 2, 0x01); } // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 160 | if (Cur==8) |
lore71 | 12:1bd4a9f09a8d | 161 | { |
lore71 | 12:1bd4a9f09a8d | 162 | display.write_digit(1, 1, 0x01); // n of matrix, column, byte |
lore71 | 12:1bd4a9f09a8d | 163 | Cur=0; |
lore71 | 12:1bd4a9f09a8d | 164 | } |
lore71 | 12:1bd4a9f09a8d | 165 | // Incremento il contatore per utilizzare l'ultima riga della matrice 8x8 |
lore71 | 12:1bd4a9f09a8d | 166 | Cur++; |
lore71 | 12:1bd4a9f09a8d | 167 | } |
lore71 | 12:1bd4a9f09a8d | 168 | else |
lore71 | 12:1bd4a9f09a8d | 169 | { |
lore71 | 12:1bd4a9f09a8d | 170 | int f; |
lore71 | 12:1bd4a9f09a8d | 171 | for(f = 0; f <= 9; f++) |
lore71 | 12:1bd4a9f09a8d | 172 | { |
lore71 | 12:1bd4a9f09a8d | 173 | display.write_digit(1, f, 0x01); |
lore71 | 12:1bd4a9f09a8d | 174 | } |
lore71 | 12:1bd4a9f09a8d | 175 | |
lore71 | 12:1bd4a9f09a8d | 176 | printf(" Write failed! \r\n"); |
lore71 | 12:1bd4a9f09a8d | 177 | Btn=0; |
lore71 | 12:1bd4a9f09a8d | 178 | } |
lore71 | 12:1bd4a9f09a8d | 179 | |
lore71 | 12:1bd4a9f09a8d | 180 | // Tempo accensione led |
lore71 | 12:1bd4a9f09a8d | 181 | wait(0.5); |
lore71 | 12:1bd4a9f09a8d | 182 | |
lore71 | 12:1bd4a9f09a8d | 183 | printf(" Press button to stop saving \r\n"); |
j3 | 0:6b0161c3e440 | 184 | } |
lore71 | 12:1bd4a9f09a8d | 185 | else |
lore71 | 12:1bd4a9f09a8d | 186 | { |
lore71 | 12:1bd4a9f09a8d | 187 | printf(" Press button to save \r\n"); |
lore71 | 12:1bd4a9f09a8d | 188 | } |
lore71 | 12:1bd4a9f09a8d | 189 | |
lore71 | 12:1bd4a9f09a8d | 190 | // Floating to Char |
lore71 | 12:1bd4a9f09a8d | 191 | char array[6]; // Numero dei digits (virgola compresa) |
lore71 | 12:1bd4a9f09a8d | 192 | snprintf(array, sizeof(array), "%f", TC); |
lore71 | 12:1bd4a9f09a8d | 193 | |
lore71 | 12:1bd4a9f09a8d | 194 | // Concatenzaione tra Float e String in un nuovo Char |
lore71 | 12:1bd4a9f09a8d | 195 | char* Aggiunta="^C "; // NB: Deve finire con 2 spazi alla fine |
lore71 | 12:1bd4a9f09a8d | 196 | char dest[sizeof(array)+sizeof(Aggiunta)]; // Size dell'array precedente + size di quello dellaggiunta |
lore71 | 12:1bd4a9f09a8d | 197 | strcpy( dest, array ); |
lore71 | 12:1bd4a9f09a8d | 198 | strcat( dest, Aggiunta ); |
lore71 | 12:1bd4a9f09a8d | 199 | |
lore71 | 12:1bd4a9f09a8d | 200 | // Faccio il reverse del testo |
lore71 | 12:1bd4a9f09a8d | 201 | size_t l = strlen(dest); |
lore71 | 12:1bd4a9f09a8d | 202 | char* r = (char*)malloc((l + 1) * sizeof(char)); |
lore71 | 12:1bd4a9f09a8d | 203 | r[l] = '\0'; |
lore71 | 12:1bd4a9f09a8d | 204 | int i; |
lore71 | 12:1bd4a9f09a8d | 205 | for(i = 0; i < l; i++) |
lore71 | 12:1bd4a9f09a8d | 206 | { |
lore71 | 12:1bd4a9f09a8d | 207 | r[i] = dest[l - 1 - i]; |
lore71 | 12:1bd4a9f09a8d | 208 | } |
lore71 | 12:1bd4a9f09a8d | 209 | // Riassegno |
lore71 | 12:1bd4a9f09a8d | 210 | p_str = r; |
lore71 | 12:1bd4a9f09a8d | 211 | |
lore71 | 12:1bd4a9f09a8d | 212 | // Testo nello shift register |
lore71 | 12:1bd4a9f09a8d | 213 | print_string(&display, user_input, p_str); |
lore71 | 12:1bd4a9f09a8d | 214 | |
lore71 | 12:1bd4a9f09a8d | 215 | // Valore per scorrere l'intero testo |
lore71 | 12:1bd4a9f09a8d | 216 | cnt=strlen(r)*6; // num char *6 (5 char + 1 space) |
lore71 | 12:1bd4a9f09a8d | 217 | for(idx = 0; idx < cnt; idx++) |
lore71 | 12:1bd4a9f09a8d | 218 | { |
lore71 | 12:1bd4a9f09a8d | 219 | // Scorro la matrice |
lore71 | 12:1bd4a9f09a8d | 220 | shift_display_right(&display, 1, 50); |
lore71 | 12:1bd4a9f09a8d | 221 | } |
j3 | 0:6b0161c3e440 | 222 | } |
lore71 | 12:1bd4a9f09a8d | 223 | |
j3 | 0:6b0161c3e440 | 224 | } |