K型熱電対

Dependencies:   mbed SDFileSystem

Revision:
0:95d7535981a1
Child:
1:c5e10503e6bf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 21 12:14:42 2016 +0000
@@ -0,0 +1,121 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+
+Serial pc(USBTX, USBRX);
+#define MOSI A6
+#define MISO A5
+#define SCLK A4
+#define CS   A3
+#define CS2  A2
+
+SPI typeK(MOSI, MISO, SCLK);
+DigitalOut cs(CS);
+DigitalOut cs2(CS2);
+
+int main(){
+
+    typeK.format(8, 3);
+    typeK.frequency(1000000);//1MHz
+    pc.baud(115200);
+    cs = 1;
+    cs2 = 1;
+
+      int thermocouple = 0; // 14-Bit Thermocouple Temperature Data + 2-Bit
+      int internal = 0; // 12-Bit Internal Temperature Data + 4-Bit
+      int thermocouple2 = 0;
+      int internal2 = 0;
+  
+    float thermocouple_T1 =0 , thermocouple_T2 = 0, base_T1 = 0, base_T2 = 0; // display value 
+    while(1){
+      //wait(0.2);
+      cs = 0;//digitalWrite(SLAVE, LOW);                             //  Enable the chip
+      thermocouple = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8;   //  Read high byte thermocouple
+      thermocouple |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00);       //  Read low byte thermocouple 
+      internal = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8;       //  Read high byte internal
+      internal |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00);           //  Read low byte internal 
+      cs = 1;//digitalWrite(SLAVE, HIGH);                            //  Disable the chip
+    
+      
+      if((thermocouple & 0x0001) != 0) {
+            pc.printf("ERROR: ");
+            if ((internal & 0x0004) !=0) {
+              pc.printf("Short to Vcc, ");
+            }
+            if ((internal & 0x0002) !=0) {
+              pc.printf("Short to GND, ");
+            }
+            if ((internal & 0x0001) !=0) {
+              pc.printf("Open Circuit, ");
+            }    
+            pc.printf("\r\n");
+      } 
+      else {
+            if((thermocouple & 0x8000) == 0){ // 0℃以上   above 0 Degrees Celsius 
+              thermocouple_T1 = (thermocouple >> 2) * 0.25;
+            } 
+            else {                          // 0℃未満   below zero
+              thermocouple_T1 = (0x3fff - (thermocouple >> 2) + 1)  * -0.25;
+            }
+            pc.printf("T1: ");
+            pc.printf("%f", thermocouple_T1);
+            pc.printf("\t");
+            //pc.printf(" // ");
+            
+            if((internal & 0x8000) == 0){ // 0℃以上   above 0 Degrees Celsius
+              base_T1 = (internal >> 4) * 0.0625;
+            }
+            else {                          // 0℃未満   below zero
+              base_T1 = (((0xffff - internal) >> 4) + 1)  * -0.0625;
+            }
+            
+            pc.printf("B1: ");
+            pc.printf("%f", base_T1);
+            pc.printf("\t");    
+      }
+      
+      cs2 = 0;//digitalWrite(SLAVE, LOW);                             //  Enable the chip
+      thermocouple2 = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8;   //  Read high byte thermocouple
+      thermocouple2 |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00);       //  Read low byte thermocouple 
+      internal2 = (unsigned short)typeK.write(0x00) << 8;//(unsigned int)SPI.transfer(0x00) << 8;       //  Read high byte internal
+      internal2 |= (unsigned short)typeK.write(0x00);//(unsigned int)SPI.transfer(0x00);           //  Read low byte internal 
+      cs2 = 1;//digitalWrite(SLAVE, HIGH);                            //  Disable the chip
+      
+     if((thermocouple2 & 0x0001) != 0) {
+            pc.printf("ERROR: ");
+            if ((internal2 & 0x0004) !=0) {
+              pc.printf("Short to Vcc, ");
+            }
+            if ((internal2 & 0x0002) !=0) {
+              pc.printf("Short to GND, ");
+            }
+            if ((internal2 & 0x0001) !=0) {
+              pc.printf("Open Circuit, ");
+            }    
+            pc.printf("\r\n");
+      } 
+      else {
+            if((thermocouple2 & 0x8000) == 0){ // 0℃以上   above 0 Degrees Celsius 
+              thermocouple_T2 = (thermocouple2 >> 2) * 0.25;
+            } 
+            else {                          // 0℃未満   below zero
+              thermocouple_T2 = (0x3fff - (thermocouple2 >> 2) + 1)  * -0.25;
+            }
+            pc.printf("T2: ");
+            pc.printf("%f", thermocouple_T2);
+            pc.printf("\t");
+            //pc.printf(" // ");
+            
+            if((internal2 & 0x8000) == 0){ // 0℃以上   above 0 Degrees Celsius
+              base_T2 = (internal2 >> 4) * 0.0625;
+            }
+            else {                          // 0℃未満   below zero
+              base_T2 = (((0xffff - internal2) >> 4) + 1)  * -0.0625;
+            }
+            
+            pc.printf("B2: ");
+            pc.printf("%f", base_T2);
+            pc.printf("\r\n");    
+      }
+  }
+}
\ No newline at end of file