revised work
Embed:
(wiki syntax)
Show/hide line numbers
Temperature.cpp
00001 #include "mbed.h" 00002 #include "Temperature.h" 00003 00004 //DigitalOut sensor_en ( PC8 ); // GPIO pin PC8 connected to the sensor 00005 00006 //I2C i2c(PD6, PD7); //SDA pin connected to PD6, SCL pin cpnnected to PD7 00007 Labb2::Labb2(char Address): sensor_en (PC8), i2c( PD6, PD7){ 00008 cAddress=Address;} 00009 float Labb2::get_temp() 00010 { 00011 00012 //const int address = 0x80; // address too the thermometer 00013 00014 char command[1]; // store commands 00015 command[0] = 0xF3; // measure temperature 00016 00017 char data[2]; // store data 00018 00019 float temp; // store our temperature 00020 00021 if(true) { 00022 00023 i2c.write(cAddress, command, 1); //Initialize SI7021 00024 00025 wait(0.5); 00026 00027 i2c.read(cAddress, data, 2); //Read data 00028 00029 temp = (uint16_t)((data[0] << 8)|data[1]); //convert two chars to int 00030 00031 temp = ((175.72*temp)/65536)-46.85; //16-bit word return to celsius 00032 00033 wait(2); 00034 return temp; 00035 } 00036 } 00037 00038 void Labb2::power_sensor(int i) //Starts sensor with 1 and stops with 0 00039 { 00040 if(i == 1) 00041 { 00042 sensor_en = 1; // enable power supply 00043 printf("Sensor is on\n"); 00044 wait (0.1) ; // give the sensor some time to start up 00045 } 00046 else 00047 { 00048 sensor_en = 0; // disable power supply 00049 printf("Sensor is off\n"); 00050 } 00051 } 00052 00053 00054 float Labb2::c_to_f(float celsius) 00055 { 00056 float fahrenheit; 00057 fahrenheit = (1.8 * celsius + 32); //converts temperature to Fahrenheit 00058 return fahrenheit; 00059 }
Generated on Tue Jul 12 2022 21:32:40 by 1.7.2