Program to read Temperature Sensor

Dependencies:   max32630fthr USBDevice

Committer:
tlyp
Date:
Fri Jun 21 00:00:47 2019 +0000
Revision:
0:161e2d4d4ef9
Simple program to read and control Temperature Sensor.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tlyp 0:161e2d4d4ef9 1 #include "mbed.h"
tlyp 0:161e2d4d4ef9 2 #include "max32630fthr.h"
tlyp 0:161e2d4d4ef9 3 #include "USBSerial.h"
tlyp 0:161e2d4d4ef9 4 #include "math.h"
tlyp 0:161e2d4d4ef9 5
tlyp 0:161e2d4d4ef9 6 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
tlyp 0:161e2d4d4ef9 7
tlyp 0:161e2d4d4ef9 8 // Hardware serial port over DAPLink
tlyp 0:161e2d4d4ef9 9 Serial daplink(P2_1, P2_0);
tlyp 0:161e2d4d4ef9 10
tlyp 0:161e2d4d4ef9 11 // Virtual serial port over USB
tlyp 0:161e2d4d4ef9 12 USBSerial microUSB;
tlyp 0:161e2d4d4ef9 13 I2C i2c(P5_7, P6_0); //sda,scl
tlyp 0:161e2d4d4ef9 14
tlyp 0:161e2d4d4ef9 15 const int Write_Addr = 0x90; //Write Addres for Ev Kit per data sheet for temperature sensor
tlyp 0:161e2d4d4ef9 16 const int Read_Addr = 0x91; //Last bit set to 1 in order to read (not write)
tlyp 0:161e2d4d4ef9 17 const int Temperature_Register_Addr = 0x00; //Address of Register holding Temperature data
tlyp 0:161e2d4d4ef9 18 char Configuration_Register_Addr[1]; //Address of Register holding Configuration Settings
tlyp 0:161e2d4d4ef9 19 char Configuration_Sleep_Data[1]; //Initialize Sensor into Sleep mode
tlyp 0:161e2d4d4ef9 20 char Configuration_OneShot_Data[1]; //Send signal for temperature reading, and then device goes back to sleep
tlyp 0:161e2d4d4ef9 21
tlyp 0:161e2d4d4ef9 22 int main()
tlyp 0:161e2d4d4ef9 23 {
tlyp 0:161e2d4d4ef9 24 microUSB.printf("=================STARTING=================\r\n");
tlyp 0:161e2d4d4ef9 25
tlyp 0:161e2d4d4ef9 26
tlyp 0:161e2d4d4ef9 27 char data[1]; //Hold Hexidecimal Value
tlyp 0:161e2d4d4ef9 28 char bdata [15]; //Hold Binary Value
tlyp 0:161e2d4d4ef9 29 microUSB.printf("Program beginingin now\r\n");
tlyp 0:161e2d4d4ef9 30
tlyp 0:161e2d4d4ef9 31 Configuration_Register_Addr[0] = 0x01;
tlyp 0:161e2d4d4ef9 32 Configuration_Register_Addr[1] = 0x00;
tlyp 0:161e2d4d4ef9 33
tlyp 0:161e2d4d4ef9 34 Configuration_Sleep_Data[0] = 0x01;
tlyp 0:161e2d4d4ef9 35 Configuration_Sleep_Data[1] = 0x01;
tlyp 0:161e2d4d4ef9 36
tlyp 0:161e2d4d4ef9 37 Configuration_OneShot_Data[0] = 0x01;
tlyp 0:161e2d4d4ef9 38 Configuration_OneShot_Data[1] = 0x81;
tlyp 0:161e2d4d4ef9 39
tlyp 0:161e2d4d4ef9 40 i2c.write(Write_Addr, Configuration_Sleep_Data , 2, false); //Set into Sleep Mode
tlyp 0:161e2d4d4ef9 41
tlyp 0:161e2d4d4ef9 42
tlyp 0:161e2d4d4ef9 43 while(1){
tlyp 0:161e2d4d4ef9 44 microUSB.printf("Reading initialzing\r\n");
tlyp 0:161e2d4d4ef9 45
tlyp 0:161e2d4d4ef9 46 i2c.write(Write_Addr, Configuration_OneShot_Data , 2, false); //Set into Sleep Mode
tlyp 0:161e2d4d4ef9 47 wait_ms(1);
tlyp 0:161e2d4d4ef9 48
tlyp 0:161e2d4d4ef9 49 i2c.write(Write_Addr, Temperature_Register_Addr , 1, true); //Send Address to begin communication, but leave true so we can
tlyp 0:161e2d4d4ef9 50 i2c.read(Read_Addr, data, 2, false); //Read the 2 bytes of temperature info collected by the sensor
tlyp 0:161e2d4d4ef9 51 //for (int i = 0; i<2;i++){
tlyp 0:161e2d4d4ef9 52 // microUSB.printf("%d ",data[i]); //Confirm values are being read from sensor via I2C
tlyp 0:161e2d4d4ef9 53 //}
tlyp 0:161e2d4d4ef9 54 //microUSB.printf("\r\n");
tlyp 0:161e2d4d4ef9 55 int count =15; //Loading bits from most significant to least
tlyp 0:161e2d4d4ef9 56 microUSB.printf("Starting Temperature Caluclation\r\n");
tlyp 0:161e2d4d4ef9 57 for (int k = 0; k<2;k++){ //Go through both bytes
tlyp 0:161e2d4d4ef9 58 //microUSB.printf("Converting %d from decimal to binary \r\n",data[k]);
tlyp 0:161e2d4d4ef9 59 for (int j = 7;j>=0;j--){
tlyp 0:161e2d4d4ef9 60 int r = data[k]>>j; //Check all 8 bits
tlyp 0:161e2d4d4ef9 61 if(r&1) //Binary 1
tlyp 0:161e2d4d4ef9 62 bdata[count] = 1; //Load 1 into array
tlyp 0:161e2d4d4ef9 63 else //Binary 0
tlyp 0:161e2d4d4ef9 64 bdata[count] = 0; //Load 0 into array
tlyp 0:161e2d4d4ef9 65 count --; //Move to next array position
tlyp 0:161e2d4d4ef9 66 }
tlyp 0:161e2d4d4ef9 67 }
tlyp 0:161e2d4d4ef9 68 double temp = 0;
tlyp 0:161e2d4d4ef9 69 int index = 14;
tlyp 0:161e2d4d4ef9 70 for (float z=6;z>=-8;z--){ //Calculate temperature from array holding binary value
tlyp 0:161e2d4d4ef9 71 float factor = pow (2,z); //Calculate Added Temperature for each bit (Per data sheet)
tlyp 0:161e2d4d4ef9 72 temp += bdata[index] * factor; //Combine all bit temperature to aquire total sum
tlyp 0:161e2d4d4ef9 73 index--;
tlyp 0:161e2d4d4ef9 74 }
tlyp 0:161e2d4d4ef9 75 microUSB.printf("Temperature is %f degrees Celcius\r\n", temp);
tlyp 0:161e2d4d4ef9 76 float fahrenheit = (((temp*9)/5) + 32); //Calculate temperature in F
tlyp 0:161e2d4d4ef9 77 microUSB.printf("Temperature is %f degrees fahrenheit\r\n", fahrenheit);
tlyp 0:161e2d4d4ef9 78 microUSB.printf("Reading Complete\r\n");
tlyp 0:161e2d4d4ef9 79 microUSB.printf("\r\n\n\n\n\n\n");
tlyp 0:161e2d4d4ef9 80 wait(5); //Sensor measures every 10 seconds
tlyp 0:161e2d4d4ef9 81 }
tlyp 0:161e2d4d4ef9 82 }