K型熱電対
Dependencies: mbed SDFileSystem
main.cpp@1:c5e10503e6bf, 2016-10-22 (annotated)
- Committer:
- Gaku0606
- Date:
- Sat Oct 22 12:49:02 2016 +0000
- Revision:
- 1:c5e10503e6bf
- Parent:
- 0:95d7535981a1
K???????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gaku0606 | 0:95d7535981a1 | 1 | #include "mbed.h" |
Gaku0606 | 1:c5e10503e6bf | 2 | #include "SDFileSystem.h" |
Gaku0606 | 1:c5e10503e6bf | 3 | #include <math.h> |
Gaku0606 | 0:95d7535981a1 | 4 | DigitalOut myled(LED1); |
Gaku0606 | 0:95d7535981a1 | 5 | |
Gaku0606 | 0:95d7535981a1 | 6 | Serial pc(USBTX, USBRX); |
Gaku0606 | 0:95d7535981a1 | 7 | #define MOSI A6 |
Gaku0606 | 0:95d7535981a1 | 8 | #define MISO A5 |
Gaku0606 | 0:95d7535981a1 | 9 | #define SCLK A4 |
Gaku0606 | 0:95d7535981a1 | 10 | #define CS A3 |
Gaku0606 | 1:c5e10503e6bf | 11 | #define CS2 D3 |
Gaku0606 | 1:c5e10503e6bf | 12 | #define SD_CS A1 |
Gaku0606 | 0:95d7535981a1 | 13 | |
Gaku0606 | 1:c5e10503e6bf | 14 | SDFileSystem sd(MOSI, MISO, SCLK, SD_CS, "sd", NC, SDFileSystem::SWITCH_NONE, 1000000); |
Gaku0606 | 1:c5e10503e6bf | 15 | //SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, SwitchType cdtype = SWITCH_NONE, int hz = 1000000); |
Gaku0606 | 1:c5e10503e6bf | 16 | |
Gaku0606 | 1:c5e10503e6bf | 17 | SPI spi(MOSI, MISO, SCLK); |
Gaku0606 | 0:95d7535981a1 | 18 | DigitalOut cs(CS); |
Gaku0606 | 0:95d7535981a1 | 19 | DigitalOut cs2(CS2); |
Gaku0606 | 0:95d7535981a1 | 20 | |
Gaku0606 | 1:c5e10503e6bf | 21 | void printBinary(int16_t data){ |
Gaku0606 | 1:c5e10503e6bf | 22 | pc.printf("0b"); |
Gaku0606 | 1:c5e10503e6bf | 23 | for(int i = 0; i< 16; i++){ |
Gaku0606 | 1:c5e10503e6bf | 24 | if(((data << i) & 0b1000000000000000) != 0) pc.printf("1"); |
Gaku0606 | 1:c5e10503e6bf | 25 | else pc.printf("0"); |
Gaku0606 | 1:c5e10503e6bf | 26 | } |
Gaku0606 | 1:c5e10503e6bf | 27 | pc.printf(" %d\r\n", data); |
Gaku0606 | 1:c5e10503e6bf | 28 | } |
Gaku0606 | 0:95d7535981a1 | 29 | |
Gaku0606 | 1:c5e10503e6bf | 30 | int main(){ |
Gaku0606 | 1:c5e10503e6bf | 31 | sd.mount(); |
Gaku0606 | 1:c5e10503e6bf | 32 | //float offset = 0; |
Gaku0606 | 1:c5e10503e6bf | 33 | float T1_offset = 0; |
Gaku0606 | 1:c5e10503e6bf | 34 | float T2_offset = 0;//4128.6875; |
Gaku0606 | 1:c5e10503e6bf | 35 | |
Gaku0606 | 1:c5e10503e6bf | 36 | spi.format(8, 3); |
Gaku0606 | 1:c5e10503e6bf | 37 | spi.frequency(1000000);//1MHz |
Gaku0606 | 0:95d7535981a1 | 38 | pc.baud(115200); |
Gaku0606 | 0:95d7535981a1 | 39 | cs = 1; |
Gaku0606 | 0:95d7535981a1 | 40 | cs2 = 1; |
Gaku0606 | 0:95d7535981a1 | 41 | |
Gaku0606 | 1:c5e10503e6bf | 42 | int16_t thermocouple = 0; // 14-Bit Thermocouple Temperature Data + 2-Bit |
Gaku0606 | 1:c5e10503e6bf | 43 | int16_t internal = 0; // 12-Bit Internal Temperature Data + 4-Bit |
Gaku0606 | 1:c5e10503e6bf | 44 | int16_t thermocouple2 = 0; |
Gaku0606 | 1:c5e10503e6bf | 45 | int16_t internal2 = 0; |
Gaku0606 | 0:95d7535981a1 | 46 | |
Gaku0606 | 1:c5e10503e6bf | 47 | float thermocouple_T1 = 0, thermocouple_T2 = 0, base_T1 = 0, base_T2 = 0; // display value |
Gaku0606 | 1:c5e10503e6bf | 48 | FILE *fp = fopen("/sd/temp_data.txt", "w"); |
Gaku0606 | 1:c5e10503e6bf | 49 | if(fp != NULL){ |
Gaku0606 | 1:c5e10503e6bf | 50 | pc.printf("SD OK!!\r\n"); |
Gaku0606 | 1:c5e10503e6bf | 51 | fclose(fp); |
Gaku0606 | 1:c5e10503e6bf | 52 | } |
Gaku0606 | 1:c5e10503e6bf | 53 | else{ |
Gaku0606 | 1:c5e10503e6bf | 54 | while(1){ |
Gaku0606 | 1:c5e10503e6bf | 55 | pc.printf("SD error\r\n"); |
Gaku0606 | 1:c5e10503e6bf | 56 | wait(1.0); |
Gaku0606 | 1:c5e10503e6bf | 57 | } |
Gaku0606 | 1:c5e10503e6bf | 58 | } |
Gaku0606 | 1:c5e10503e6bf | 59 | |
Gaku0606 | 0:95d7535981a1 | 60 | while(1){ |
Gaku0606 | 1:c5e10503e6bf | 61 | wait(0.2); |
Gaku0606 | 1:c5e10503e6bf | 62 | char data[4]; |
Gaku0606 | 1:c5e10503e6bf | 63 | uint16_t short_temp = 0; |
Gaku0606 | 1:c5e10503e6bf | 64 | spi.format(8, 3); |
Gaku0606 | 1:c5e10503e6bf | 65 | spi.frequency(1000000);//1MHz |
Gaku0606 | 1:c5e10503e6bf | 66 | |
Gaku0606 | 1:c5e10503e6bf | 67 | cs = 0;//digitalWrite(SLAVE, LOW); // Enable the chip |
Gaku0606 | 1:c5e10503e6bf | 68 | for(int i = 0; i< 4; i++){ |
Gaku0606 | 1:c5e10503e6bf | 69 | data[i] = spi.write(0x00);//dummy |
Gaku0606 | 1:c5e10503e6bf | 70 | } |
Gaku0606 | 1:c5e10503e6bf | 71 | cs = 1; |
Gaku0606 | 1:c5e10503e6bf | 72 | |
Gaku0606 | 1:c5e10503e6bf | 73 | short_temp = ((uint16_t)data[0] << 8) | ((uint16_t)data[1]); |
Gaku0606 | 1:c5e10503e6bf | 74 | thermocouple = (int16_t)short_temp; |
Gaku0606 | 1:c5e10503e6bf | 75 | short_temp = ((uint16_t)data[2] << 8) | ((uint16_t)data[3]); |
Gaku0606 | 1:c5e10503e6bf | 76 | internal = (int16_t)short_temp; |
Gaku0606 | 0:95d7535981a1 | 77 | |
Gaku0606 | 0:95d7535981a1 | 78 | if((thermocouple & 0x0001) != 0) { |
Gaku0606 | 0:95d7535981a1 | 79 | pc.printf("ERROR: "); |
Gaku0606 | 0:95d7535981a1 | 80 | if ((internal & 0x0004) !=0) { |
Gaku0606 | 0:95d7535981a1 | 81 | pc.printf("Short to Vcc, "); |
Gaku0606 | 0:95d7535981a1 | 82 | } |
Gaku0606 | 0:95d7535981a1 | 83 | if ((internal & 0x0002) !=0) { |
Gaku0606 | 0:95d7535981a1 | 84 | pc.printf("Short to GND, "); |
Gaku0606 | 0:95d7535981a1 | 85 | } |
Gaku0606 | 0:95d7535981a1 | 86 | if ((internal & 0x0001) !=0) { |
Gaku0606 | 0:95d7535981a1 | 87 | pc.printf("Open Circuit, "); |
Gaku0606 | 0:95d7535981a1 | 88 | } |
Gaku0606 | 0:95d7535981a1 | 89 | pc.printf("\r\n"); |
Gaku0606 | 0:95d7535981a1 | 90 | } |
Gaku0606 | 0:95d7535981a1 | 91 | else { |
Gaku0606 | 1:c5e10503e6bf | 92 | |
Gaku0606 | 1:c5e10503e6bf | 93 | thermocouple = thermocouple & 0xFFFC; |
Gaku0606 | 1:c5e10503e6bf | 94 | thermocouple_T1 = (thermocouple / 4.0) * 0.25 + T1_offset; |
Gaku0606 | 1:c5e10503e6bf | 95 | //printBinary(thermocouple); |
Gaku0606 | 0:95d7535981a1 | 96 | pc.printf("T1: "); |
Gaku0606 | 0:95d7535981a1 | 97 | pc.printf("%f", thermocouple_T1); |
Gaku0606 | 0:95d7535981a1 | 98 | pc.printf("\t"); |
Gaku0606 | 0:95d7535981a1 | 99 | //pc.printf(" // "); |
Gaku0606 | 0:95d7535981a1 | 100 | |
Gaku0606 | 1:c5e10503e6bf | 101 | internal = internal & 0xFFF0; |
Gaku0606 | 1:c5e10503e6bf | 102 | base_T1 = (internal / 16.0) * 0.0625; |
Gaku0606 | 0:95d7535981a1 | 103 | pc.printf("B1: "); |
Gaku0606 | 0:95d7535981a1 | 104 | pc.printf("%f", base_T1); |
Gaku0606 | 0:95d7535981a1 | 105 | pc.printf("\t"); |
Gaku0606 | 1:c5e10503e6bf | 106 | |
Gaku0606 | 1:c5e10503e6bf | 107 | fp = fopen("/sd/temp_data.txt", "a"); |
Gaku0606 | 1:c5e10503e6bf | 108 | if(fp != NULL){ |
Gaku0606 | 1:c5e10503e6bf | 109 | fprintf(fp,"%f\t%f\r\n", thermocouple_T1, base_T1); |
Gaku0606 | 1:c5e10503e6bf | 110 | fclose(fp); |
Gaku0606 | 1:c5e10503e6bf | 111 | } |
Gaku0606 | 1:c5e10503e6bf | 112 | |
Gaku0606 | 0:95d7535981a1 | 113 | } |
Gaku0606 | 1:c5e10503e6bf | 114 | spi.format(8, 3); |
Gaku0606 | 1:c5e10503e6bf | 115 | spi.frequency(1000000);//1MHz |
Gaku0606 | 0:95d7535981a1 | 116 | cs2 = 0;//digitalWrite(SLAVE, LOW); // Enable the chip |
Gaku0606 | 1:c5e10503e6bf | 117 | for(int i = 0; i< 4; i++){ |
Gaku0606 | 1:c5e10503e6bf | 118 | data[i] = spi.write(0x00);//dummy |
Gaku0606 | 1:c5e10503e6bf | 119 | } |
Gaku0606 | 1:c5e10503e6bf | 120 | cs2 = 1; |
Gaku0606 | 1:c5e10503e6bf | 121 | |
Gaku0606 | 1:c5e10503e6bf | 122 | short_temp = ((uint16_t)data[0] << 8) | ((uint16_t)data[1]); |
Gaku0606 | 1:c5e10503e6bf | 123 | thermocouple2 = (int16_t)short_temp; |
Gaku0606 | 1:c5e10503e6bf | 124 | short_temp = ((uint16_t)data[2] << 8) | ((uint16_t)data[3]); |
Gaku0606 | 1:c5e10503e6bf | 125 | internal2 = (int16_t)short_temp; |
Gaku0606 | 0:95d7535981a1 | 126 | |
Gaku0606 | 0:95d7535981a1 | 127 | if((thermocouple2 & 0x0001) != 0) { |
Gaku0606 | 0:95d7535981a1 | 128 | pc.printf("ERROR: "); |
Gaku0606 | 0:95d7535981a1 | 129 | if ((internal2 & 0x0004) !=0) { |
Gaku0606 | 0:95d7535981a1 | 130 | pc.printf("Short to Vcc, "); |
Gaku0606 | 0:95d7535981a1 | 131 | } |
Gaku0606 | 0:95d7535981a1 | 132 | if ((internal2 & 0x0002) !=0) { |
Gaku0606 | 0:95d7535981a1 | 133 | pc.printf("Short to GND, "); |
Gaku0606 | 0:95d7535981a1 | 134 | } |
Gaku0606 | 0:95d7535981a1 | 135 | if ((internal2 & 0x0001) !=0) { |
Gaku0606 | 0:95d7535981a1 | 136 | pc.printf("Open Circuit, "); |
Gaku0606 | 0:95d7535981a1 | 137 | } |
Gaku0606 | 0:95d7535981a1 | 138 | pc.printf("\r\n"); |
Gaku0606 | 0:95d7535981a1 | 139 | } |
Gaku0606 | 0:95d7535981a1 | 140 | else { |
Gaku0606 | 1:c5e10503e6bf | 141 | |
Gaku0606 | 1:c5e10503e6bf | 142 | thermocouple2 = thermocouple2 & 0xFFFC; |
Gaku0606 | 1:c5e10503e6bf | 143 | thermocouple_T2 = (thermocouple2 / 4.0) * 0.25 + T2_offset; |
Gaku0606 | 1:c5e10503e6bf | 144 | |
Gaku0606 | 1:c5e10503e6bf | 145 | |
Gaku0606 | 0:95d7535981a1 | 146 | pc.printf("T2: "); |
Gaku0606 | 1:c5e10503e6bf | 147 | pc.printf("%f", thermocouple_T2+T2_offset); |
Gaku0606 | 0:95d7535981a1 | 148 | pc.printf("\t"); |
Gaku0606 | 1:c5e10503e6bf | 149 | |
Gaku0606 | 1:c5e10503e6bf | 150 | internal2 = internal2 & 0xFFF0; |
Gaku0606 | 1:c5e10503e6bf | 151 | base_T2 = (internal2 / 16.0) * 0.0625; |
Gaku0606 | 0:95d7535981a1 | 152 | pc.printf("B2: "); |
Gaku0606 | 0:95d7535981a1 | 153 | pc.printf("%f", base_T2); |
Gaku0606 | 0:95d7535981a1 | 154 | pc.printf("\r\n"); |
Gaku0606 | 1:c5e10503e6bf | 155 | |
Gaku0606 | 1:c5e10503e6bf | 156 | |
Gaku0606 | 0:95d7535981a1 | 157 | } |
Gaku0606 | 0:95d7535981a1 | 158 | } |
Gaku0606 | 0:95d7535981a1 | 159 | } |