I2C&ThermalSensor

Dependents:   COG4050_blink COG4050_adxl35x_adxrs290 COG4050_adxrs290_adxrs453 COG4050_adxl355_adxl357 ... more

Committer:
APS_Lab
Date:
Tue Jun 05 13:03:07 2018 +0000
Revision:
0:74a0756399ff
Child:
1:0901bc032f82
I2C&??????ADT7420

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:74a0756399ff 1 #include "mbed.h"
APS_Lab 0:74a0756399ff 2
APS_Lab 0:74a0756399ff 3 #define ADT7420 0x48
APS_Lab 0:74a0756399ff 4
APS_Lab 0:74a0756399ff 5 #define RESET_REG 0x2F
APS_Lab 0:74a0756399ff 6 #define TEMP_REG 0x00
APS_Lab 0:74a0756399ff 7 #define ID_REG 0x0B
APS_Lab 0:74a0756399ff 8
APS_Lab 0:74a0756399ff 9
APS_Lab 0:74a0756399ff 10 DigitalOut led1(LED1);
APS_Lab 0:74a0756399ff 11 DigitalOut sensor(D8);
APS_Lab 0:74a0756399ff 12
APS_Lab 0:74a0756399ff 13 I2C Myi2c(I2C_SDA, I2C_SCL);
APS_Lab 0:74a0756399ff 14
APS_Lab 0:74a0756399ff 15 Serial pc(USBTX, USBRX);
APS_Lab 0:74a0756399ff 16
APS_Lab 0:74a0756399ff 17 void ADT7420_reset(void);
APS_Lab 0:74a0756399ff 18 int ADT7420_GetID(void);
APS_Lab 0:74a0756399ff 19 int ADT7420_GetTemp(void);
APS_Lab 0:74a0756399ff 20
APS_Lab 0:74a0756399ff 21 // main() runs in its own thread in the OS
APS_Lab 0:74a0756399ff 22 int main() {
APS_Lab 0:74a0756399ff 23 float status;
APS_Lab 0:74a0756399ff 24 int res;
APS_Lab 0:74a0756399ff 25 sensor=1;
APS_Lab 0:74a0756399ff 26 //configure for UART
APS_Lab 0:74a0756399ff 27 pc.baud(115200);
APS_Lab 0:74a0756399ff 28 pc.printf("I2C ADT7420 Demo\n");
APS_Lab 0:74a0756399ff 29
APS_Lab 0:74a0756399ff 30 //configure for I2C @400KHz
APS_Lab 0:74a0756399ff 31 Myi2c.frequency(100000);
APS_Lab 0:74a0756399ff 32
APS_Lab 0:74a0756399ff 33 //configure for ADT7420
APS_Lab 0:74a0756399ff 34
APS_Lab 0:74a0756399ff 35 pc.printf("[RESET ] ADT7420\n");
APS_Lab 0:74a0756399ff 36 ADT7420_reset();
APS_Lab 0:74a0756399ff 37 wait(0.5);
APS_Lab 0:74a0756399ff 38
APS_Lab 0:74a0756399ff 39 pc.printf("[CHECK ID] ADT7420\n");
APS_Lab 0:74a0756399ff 40 ADT7420_GetID();
APS_Lab 0:74a0756399ff 41
APS_Lab 0:74a0756399ff 42
APS_Lab 0:74a0756399ff 43 while (true) {
APS_Lab 0:74a0756399ff 44 led1 = 0;
APS_Lab 0:74a0756399ff 45
APS_Lab 0:74a0756399ff 46 pc.printf("[Get Temp] ADT7420\n");
APS_Lab 0:74a0756399ff 47 res = ADT7420_GetTemp();
APS_Lab 0:74a0756399ff 48
APS_Lab 0:74a0756399ff 49 if(res & 0x1000)
APS_Lab 0:74a0756399ff 50 {
APS_Lab 0:74a0756399ff 51 status = (float)((res - 8192) / 16);
APS_Lab 0:74a0756399ff 52 }
APS_Lab 0:74a0756399ff 53 else
APS_Lab 0:74a0756399ff 54 {
APS_Lab 0:74a0756399ff 55 status = (float)(res / 16);
APS_Lab 0:74a0756399ff 56 }
APS_Lab 0:74a0756399ff 57
APS_Lab 0:74a0756399ff 58 pc.printf("Current Temp %.0f \n", status);
APS_Lab 0:74a0756399ff 59
APS_Lab 0:74a0756399ff 60 wait(0.5);
APS_Lab 0:74a0756399ff 61 led1 = 1;
APS_Lab 0:74a0756399ff 62 wait(0.5);
APS_Lab 0:74a0756399ff 63 }
APS_Lab 0:74a0756399ff 64
APS_Lab 0:74a0756399ff 65 }
APS_Lab 0:74a0756399ff 66
APS_Lab 0:74a0756399ff 67 void ADT7420_reset(void)
APS_Lab 0:74a0756399ff 68 {
APS_Lab 0:74a0756399ff 69 char cmd_reset[1];
APS_Lab 0:74a0756399ff 70 cmd_reset[0] = RESET_REG;
APS_Lab 0:74a0756399ff 71 Myi2c.write(ADT7420, cmd_reset, 1);
APS_Lab 0:74a0756399ff 72 }
APS_Lab 0:74a0756399ff 73
APS_Lab 0:74a0756399ff 74 int ADT7420_GetID(void)
APS_Lab 0:74a0756399ff 75 {
APS_Lab 0:74a0756399ff 76 char cmd_id[1], read_val;
APS_Lab 0:74a0756399ff 77 cmd_id[0]=ID_REG;
APS_Lab 0:74a0756399ff 78 Myi2c.write(ADT7420, cmd_id, 1, 1);
APS_Lab 0:74a0756399ff 79 Myi2c.read(ADT7420, &read_val, 1);
APS_Lab 0:74a0756399ff 80 pc.printf("ID %02x\n", read_val);
APS_Lab 0:74a0756399ff 81 return read_val;
APS_Lab 0:74a0756399ff 82 }
APS_Lab 0:74a0756399ff 83
APS_Lab 0:74a0756399ff 84 int ADT7420_GetTemp(void)
APS_Lab 0:74a0756399ff 85 {
APS_Lab 0:74a0756399ff 86 char cmd_temp[1], read_val[2];
APS_Lab 0:74a0756399ff 87 int temp=0;
APS_Lab 0:74a0756399ff 88 cmd_temp[0]=TEMP_REG;
APS_Lab 0:74a0756399ff 89 Myi2c.write(ADT7420, cmd_temp, 1, 1);
APS_Lab 0:74a0756399ff 90 Myi2c.read(ADT7420, read_val, 2);
APS_Lab 0:74a0756399ff 91 pc.printf("Temp Upper %02x\n", read_val[0]);
APS_Lab 0:74a0756399ff 92 pc.printf("Temp Lower %02x\n", read_val[1]);
APS_Lab 0:74a0756399ff 93
APS_Lab 0:74a0756399ff 94 temp = (int)((read_val[0] << 8) | read_val[1]);
APS_Lab 0:74a0756399ff 95 temp >>= 3;
APS_Lab 0:74a0756399ff 96 return temp;
APS_Lab 0:74a0756399ff 97 }