K型熱電対

Dependencies:   mbed SDFileSystem

main.cpp

Committer:
Gaku0606
Date:
2016-10-22
Revision:
1:c5e10503e6bf
Parent:
0:95d7535981a1

File content as of revision 1:c5e10503e6bf:

#include "mbed.h"
#include "SDFileSystem.h"
#include <math.h>
DigitalOut myled(LED1);

Serial pc(USBTX, USBRX);
#define MOSI A6
#define MISO A5
#define SCLK A4
#define CS   A3
#define CS2  D3
#define SD_CS A1

SDFileSystem sd(MOSI, MISO, SCLK, SD_CS, "sd", NC, SDFileSystem::SWITCH_NONE, 1000000);
//SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, SwitchType cdtype = SWITCH_NONE, int hz = 1000000);

SPI spi(MOSI, MISO, SCLK);
DigitalOut cs(CS);
DigitalOut cs2(CS2);

void printBinary(int16_t data){
    pc.printf("0b");
    for(int i = 0; i< 16; i++){
         if(((data << i) & 0b1000000000000000) != 0) pc.printf("1");
         else pc.printf("0");
    }
    pc.printf(" %d\r\n", data);   
}

int main(){
    sd.mount();
    //float offset = 0;
    float T1_offset = 0;
    float T2_offset = 0;//4128.6875;

    spi.format(8, 3);
    spi.frequency(1000000);//1MHz
    pc.baud(115200);
    cs = 1;
    cs2 = 1;

      int16_t thermocouple = 0; // 14-Bit Thermocouple Temperature Data + 2-Bit
      int16_t internal = 0; // 12-Bit Internal Temperature Data + 4-Bit
      int16_t thermocouple2 = 0;
      int16_t internal2 = 0;
  
    float thermocouple_T1 = 0, thermocouple_T2 = 0, base_T1 = 0, base_T2 = 0; // display value 
    FILE *fp = fopen("/sd/temp_data.txt", "w");
    if(fp != NULL){
        pc.printf("SD OK!!\r\n");
        fclose(fp);   
    }
    else{
        while(1){
            pc.printf("SD error\r\n");
            wait(1.0);   
        }   
    }
  
    while(1){
      wait(0.2);
      char data[4];
      uint16_t short_temp = 0;
      spi.format(8, 3);
      spi.frequency(1000000);//1MHz
      
      cs = 0;//digitalWrite(SLAVE, LOW); //  Enable the chip
      for(int i = 0; i< 4; i++){
        data[i] = spi.write(0x00);//dummy    
      }
      cs = 1;
      
      short_temp = ((uint16_t)data[0] << 8) | ((uint16_t)data[1]);
      thermocouple = (int16_t)short_temp;
      short_temp = ((uint16_t)data[2] << 8) | ((uint16_t)data[3]);
      internal = (int16_t)short_temp;
      
      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 {
           
            thermocouple = thermocouple & 0xFFFC;
            thermocouple_T1 = (thermocouple / 4.0) * 0.25 + T1_offset; 
            //printBinary(thermocouple);
            pc.printf("T1: ");
            pc.printf("%f", thermocouple_T1);
            pc.printf("\t");
            //pc.printf(" // ");
            
            internal = internal & 0xFFF0;
            base_T1 = (internal / 16.0) * 0.0625;
            pc.printf("B1: ");
            pc.printf("%f", base_T1);
            pc.printf("\t");    
            
            fp = fopen("/sd/temp_data.txt", "a");
            if(fp != NULL){
                fprintf(fp,"%f\t%f\r\n", thermocouple_T1, base_T1);
                fclose(fp);   
            }
            
      }
      spi.format(8, 3);
      spi.frequency(1000000);//1MHz
      cs2 = 0;//digitalWrite(SLAVE, LOW);                             //  Enable the chip
      for(int i = 0; i< 4; i++){
        data[i] = spi.write(0x00);//dummy    
      }
      cs2 = 1;
      
      short_temp = ((uint16_t)data[0] << 8) | ((uint16_t)data[1]);
      thermocouple2 = (int16_t)short_temp;
      short_temp = ((uint16_t)data[2] << 8) | ((uint16_t)data[3]);
      internal2 = (int16_t)short_temp;
      
     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 {
            
            thermocouple2 = thermocouple2 & 0xFFFC;
            thermocouple_T2 = (thermocouple2 / 4.0) * 0.25 + T2_offset; 
            
              
            pc.printf("T2: ");
            pc.printf("%f", thermocouple_T2+T2_offset);
            pc.printf("\t");
           
            internal2 = internal2 & 0xFFF0;
            base_T2 = (internal2 / 16.0) * 0.0625;
            pc.printf("B2: ");
            pc.printf("%f", base_T2);
            pc.printf("\r\n");    
            
            
      }
  }
}