K型熱電対

Dependencies:   mbed SDFileSystem

Committer:
Gaku0606
Date:
Fri Oct 21 12:14:42 2016 +0000
Revision:
0:95d7535981a1
Child:
1:c5e10503e6bf
??????????SPI?K????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gaku0606 0:95d7535981a1 1 #include "mbed.h"
Gaku0606 0:95d7535981a1 2
Gaku0606 0:95d7535981a1 3 DigitalOut myled(LED1);
Gaku0606 0:95d7535981a1 4
Gaku0606 0:95d7535981a1 5 Serial pc(USBTX, USBRX);
Gaku0606 0:95d7535981a1 6 #define MOSI A6
Gaku0606 0:95d7535981a1 7 #define MISO A5
Gaku0606 0:95d7535981a1 8 #define SCLK A4
Gaku0606 0:95d7535981a1 9 #define CS A3
Gaku0606 0:95d7535981a1 10 #define CS2 A2
Gaku0606 0:95d7535981a1 11
Gaku0606 0:95d7535981a1 12 SPI typeK(MOSI, MISO, SCLK);
Gaku0606 0:95d7535981a1 13 DigitalOut cs(CS);
Gaku0606 0:95d7535981a1 14 DigitalOut cs2(CS2);
Gaku0606 0:95d7535981a1 15
Gaku0606 0:95d7535981a1 16 int main(){
Gaku0606 0:95d7535981a1 17
Gaku0606 0:95d7535981a1 18 typeK.format(8, 3);
Gaku0606 0:95d7535981a1 19 typeK.frequency(1000000);//1MHz
Gaku0606 0:95d7535981a1 20 pc.baud(115200);
Gaku0606 0:95d7535981a1 21 cs = 1;
Gaku0606 0:95d7535981a1 22 cs2 = 1;
Gaku0606 0:95d7535981a1 23
Gaku0606 0:95d7535981a1 24 int thermocouple = 0; // 14-Bit Thermocouple Temperature Data + 2-Bit
Gaku0606 0:95d7535981a1 25 int internal = 0; // 12-Bit Internal Temperature Data + 4-Bit
Gaku0606 0:95d7535981a1 26 int thermocouple2 = 0;
Gaku0606 0:95d7535981a1 27 int internal2 = 0;
Gaku0606 0:95d7535981a1 28
Gaku0606 0:95d7535981a1 29 float thermocouple_T1 =0 , thermocouple_T2 = 0, base_T1 = 0, base_T2 = 0; // display value
Gaku0606 0:95d7535981a1 30 while(1){
Gaku0606 0:95d7535981a1 31 //wait(0.2);
Gaku0606 0:95d7535981a1 32 cs = 0;//digitalWrite(SLAVE, LOW); // Enable the chip
Gaku0606 0:95d7535981a1 33 thermocouple = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8; // Read high byte thermocouple
Gaku0606 0:95d7535981a1 34 thermocouple |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00); // Read low byte thermocouple
Gaku0606 0:95d7535981a1 35 internal = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8; // Read high byte internal
Gaku0606 0:95d7535981a1 36 internal |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00); // Read low byte internal
Gaku0606 0:95d7535981a1 37 cs = 1;//digitalWrite(SLAVE, HIGH); // Disable the chip
Gaku0606 0:95d7535981a1 38
Gaku0606 0:95d7535981a1 39
Gaku0606 0:95d7535981a1 40 if((thermocouple & 0x0001) != 0) {
Gaku0606 0:95d7535981a1 41 pc.printf("ERROR: ");
Gaku0606 0:95d7535981a1 42 if ((internal & 0x0004) !=0) {
Gaku0606 0:95d7535981a1 43 pc.printf("Short to Vcc, ");
Gaku0606 0:95d7535981a1 44 }
Gaku0606 0:95d7535981a1 45 if ((internal & 0x0002) !=0) {
Gaku0606 0:95d7535981a1 46 pc.printf("Short to GND, ");
Gaku0606 0:95d7535981a1 47 }
Gaku0606 0:95d7535981a1 48 if ((internal & 0x0001) !=0) {
Gaku0606 0:95d7535981a1 49 pc.printf("Open Circuit, ");
Gaku0606 0:95d7535981a1 50 }
Gaku0606 0:95d7535981a1 51 pc.printf("\r\n");
Gaku0606 0:95d7535981a1 52 }
Gaku0606 0:95d7535981a1 53 else {
Gaku0606 0:95d7535981a1 54 if((thermocouple & 0x8000) == 0){ // 0℃以上 above 0 Degrees Celsius
Gaku0606 0:95d7535981a1 55 thermocouple_T1 = (thermocouple >> 2) * 0.25;
Gaku0606 0:95d7535981a1 56 }
Gaku0606 0:95d7535981a1 57 else { // 0℃未満 below zero
Gaku0606 0:95d7535981a1 58 thermocouple_T1 = (0x3fff - (thermocouple >> 2) + 1) * -0.25;
Gaku0606 0:95d7535981a1 59 }
Gaku0606 0:95d7535981a1 60 pc.printf("T1: ");
Gaku0606 0:95d7535981a1 61 pc.printf("%f", thermocouple_T1);
Gaku0606 0:95d7535981a1 62 pc.printf("\t");
Gaku0606 0:95d7535981a1 63 //pc.printf(" // ");
Gaku0606 0:95d7535981a1 64
Gaku0606 0:95d7535981a1 65 if((internal & 0x8000) == 0){ // 0℃以上 above 0 Degrees Celsius
Gaku0606 0:95d7535981a1 66 base_T1 = (internal >> 4) * 0.0625;
Gaku0606 0:95d7535981a1 67 }
Gaku0606 0:95d7535981a1 68 else { // 0℃未満 below zero
Gaku0606 0:95d7535981a1 69 base_T1 = (((0xffff - internal) >> 4) + 1) * -0.0625;
Gaku0606 0:95d7535981a1 70 }
Gaku0606 0:95d7535981a1 71
Gaku0606 0:95d7535981a1 72 pc.printf("B1: ");
Gaku0606 0:95d7535981a1 73 pc.printf("%f", base_T1);
Gaku0606 0:95d7535981a1 74 pc.printf("\t");
Gaku0606 0:95d7535981a1 75 }
Gaku0606 0:95d7535981a1 76
Gaku0606 0:95d7535981a1 77 cs2 = 0;//digitalWrite(SLAVE, LOW); // Enable the chip
Gaku0606 0:95d7535981a1 78 thermocouple2 = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8; // Read high byte thermocouple
Gaku0606 0:95d7535981a1 79 thermocouple2 |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00); // Read low byte thermocouple
Gaku0606 0:95d7535981a1 80 internal2 = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8; // Read high byte internal
Gaku0606 0:95d7535981a1 81 internal2 |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00); // Read low byte internal
Gaku0606 0:95d7535981a1 82 cs2 = 1;//digitalWrite(SLAVE, HIGH); // Disable the chip
Gaku0606 0:95d7535981a1 83
Gaku0606 0:95d7535981a1 84 if((thermocouple2 & 0x0001) != 0) {
Gaku0606 0:95d7535981a1 85 pc.printf("ERROR: ");
Gaku0606 0:95d7535981a1 86 if ((internal2 & 0x0004) !=0) {
Gaku0606 0:95d7535981a1 87 pc.printf("Short to Vcc, ");
Gaku0606 0:95d7535981a1 88 }
Gaku0606 0:95d7535981a1 89 if ((internal2 & 0x0002) !=0) {
Gaku0606 0:95d7535981a1 90 pc.printf("Short to GND, ");
Gaku0606 0:95d7535981a1 91 }
Gaku0606 0:95d7535981a1 92 if ((internal2 & 0x0001) !=0) {
Gaku0606 0:95d7535981a1 93 pc.printf("Open Circuit, ");
Gaku0606 0:95d7535981a1 94 }
Gaku0606 0:95d7535981a1 95 pc.printf("\r\n");
Gaku0606 0:95d7535981a1 96 }
Gaku0606 0:95d7535981a1 97 else {
Gaku0606 0:95d7535981a1 98 if((thermocouple2 & 0x8000) == 0){ // 0℃以上 above 0 Degrees Celsius
Gaku0606 0:95d7535981a1 99 thermocouple_T2 = (thermocouple2 >> 2) * 0.25;
Gaku0606 0:95d7535981a1 100 }
Gaku0606 0:95d7535981a1 101 else { // 0℃未満 below zero
Gaku0606 0:95d7535981a1 102 thermocouple_T2 = (0x3fff - (thermocouple2 >> 2) + 1) * -0.25;
Gaku0606 0:95d7535981a1 103 }
Gaku0606 0:95d7535981a1 104 pc.printf("T2: ");
Gaku0606 0:95d7535981a1 105 pc.printf("%f", thermocouple_T2);
Gaku0606 0:95d7535981a1 106 pc.printf("\t");
Gaku0606 0:95d7535981a1 107 //pc.printf(" // ");
Gaku0606 0:95d7535981a1 108
Gaku0606 0:95d7535981a1 109 if((internal2 & 0x8000) == 0){ // 0℃以上 above 0 Degrees Celsius
Gaku0606 0:95d7535981a1 110 base_T2 = (internal2 >> 4) * 0.0625;
Gaku0606 0:95d7535981a1 111 }
Gaku0606 0:95d7535981a1 112 else { // 0℃未満 below zero
Gaku0606 0:95d7535981a1 113 base_T2 = (((0xffff - internal2) >> 4) + 1) * -0.0625;
Gaku0606 0:95d7535981a1 114 }
Gaku0606 0:95d7535981a1 115
Gaku0606 0:95d7535981a1 116 pc.printf("B2: ");
Gaku0606 0:95d7535981a1 117 pc.printf("%f", base_T2);
Gaku0606 0:95d7535981a1 118 pc.printf("\r\n");
Gaku0606 0:95d7535981a1 119 }
Gaku0606 0:95d7535981a1 120 }
Gaku0606 0:95d7535981a1 121 }