I2C&ThermalSensor

Dependents:   COG4050_blink COG4050_adxl35x_adxrs290 COG4050_adxrs290_adxrs453 COG4050_adxl355_adxl357 ... more

Committer:
APS_Lab
Date:
Tue May 14 02:00:12 2019 +0000
Revision:
1:0901bc032f82
Parent:
0:74a0756399ff
Latest 2019.05.14

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 1:0901bc032f82 18 char ADT7420_GetID(void);
APS_Lab 0:74a0756399ff 19 int ADT7420_GetTemp(void);
APS_Lab 1:0901bc032f82 20 int ADT7420_RegDump(void);
APS_Lab 1:0901bc032f82 21
APS_Lab 1:0901bc032f82 22 char ALLREG[16];
APS_Lab 0:74a0756399ff 23
APS_Lab 0:74a0756399ff 24 // main() runs in its own thread in the OS
APS_Lab 0:74a0756399ff 25 int main() {
APS_Lab 0:74a0756399ff 26 float status;
APS_Lab 0:74a0756399ff 27 int res;
APS_Lab 0:74a0756399ff 28 sensor=1;
APS_Lab 0:74a0756399ff 29 //configure for UART
APS_Lab 0:74a0756399ff 30 pc.baud(115200);
APS_Lab 0:74a0756399ff 31 pc.printf("I2C ADT7420 Demo\n");
APS_Lab 0:74a0756399ff 32
APS_Lab 1:0901bc032f82 33 //configure for I2C @100KHz
APS_Lab 1:0901bc032f82 34 //Myi2c.init();
APS_Lab 1:0901bc032f82 35 Myi2c.frequency(10000);
APS_Lab 0:74a0756399ff 36
APS_Lab 0:74a0756399ff 37 //configure for ADT7420
APS_Lab 1:0901bc032f82 38 Myi2c.lock();
APS_Lab 0:74a0756399ff 39 pc.printf("[RESET ] ADT7420\n");
APS_Lab 0:74a0756399ff 40 ADT7420_reset();
APS_Lab 0:74a0756399ff 41 wait(0.5);
APS_Lab 0:74a0756399ff 42
APS_Lab 0:74a0756399ff 43 pc.printf("[CHECK ID] ADT7420\n");
APS_Lab 0:74a0756399ff 44 ADT7420_GetID();
APS_Lab 0:74a0756399ff 45
APS_Lab 0:74a0756399ff 46
APS_Lab 0:74a0756399ff 47 while (true) {
APS_Lab 0:74a0756399ff 48 led1 = 0;
APS_Lab 0:74a0756399ff 49
APS_Lab 0:74a0756399ff 50 pc.printf("[Get Temp] ADT7420\n");
APS_Lab 0:74a0756399ff 51 res = ADT7420_GetTemp();
APS_Lab 0:74a0756399ff 52
APS_Lab 0:74a0756399ff 53 if(res & 0x1000)
APS_Lab 0:74a0756399ff 54 {
APS_Lab 1:0901bc032f82 55 status = (float)(res * 0.0625 * -1.0);
APS_Lab 1:0901bc032f82 56
APS_Lab 0:74a0756399ff 57 }
APS_Lab 0:74a0756399ff 58 else
APS_Lab 0:74a0756399ff 59 {
APS_Lab 1:0901bc032f82 60 status = (float)(res * 0.0625);
APS_Lab 0:74a0756399ff 61 }
APS_Lab 0:74a0756399ff 62
APS_Lab 1:0901bc032f82 63 pc.printf("Current Temp %.4f C\n", status);
APS_Lab 0:74a0756399ff 64
APS_Lab 0:74a0756399ff 65 wait(0.5);
APS_Lab 0:74a0756399ff 66 led1 = 1;
APS_Lab 0:74a0756399ff 67 wait(0.5);
APS_Lab 1:0901bc032f82 68
APS_Lab 1:0901bc032f82 69 ADT7420_RegDump();
APS_Lab 0:74a0756399ff 70 }
APS_Lab 0:74a0756399ff 71
APS_Lab 0:74a0756399ff 72 }
APS_Lab 0:74a0756399ff 73
APS_Lab 0:74a0756399ff 74 void ADT7420_reset(void)
APS_Lab 0:74a0756399ff 75 {
APS_Lab 1:0901bc032f82 76 char cmd_reset;
APS_Lab 1:0901bc032f82 77 cmd_reset = RESET_REG;
APS_Lab 1:0901bc032f82 78 Myi2c.write(ADT7420);
APS_Lab 1:0901bc032f82 79 if(Myi2c.write(0xFF))
APS_Lab 1:0901bc032f82 80 {
APS_Lab 1:0901bc032f82 81 pc.printf("Ack \n");
APS_Lab 1:0901bc032f82 82 }
APS_Lab 1:0901bc032f82 83 else
APS_Lab 1:0901bc032f82 84 {
APS_Lab 1:0901bc032f82 85 pc.printf("Nack \n");
APS_Lab 1:0901bc032f82 86 }
APS_Lab 1:0901bc032f82 87
APS_Lab 1:0901bc032f82 88 Myi2c.write(ADT7420);
APS_Lab 1:0901bc032f82 89 if(Myi2c.write(cmd_reset))
APS_Lab 1:0901bc032f82 90 {
APS_Lab 1:0901bc032f82 91 pc.printf("Reset Done\n");
APS_Lab 1:0901bc032f82 92 }
APS_Lab 1:0901bc032f82 93 else
APS_Lab 1:0901bc032f82 94 {
APS_Lab 1:0901bc032f82 95 pc.printf("Reset Failure\n");
APS_Lab 1:0901bc032f82 96 }
APS_Lab 0:74a0756399ff 97 }
APS_Lab 0:74a0756399ff 98
APS_Lab 1:0901bc032f82 99 char ADT7420_GetID(void)
APS_Lab 0:74a0756399ff 100 {
APS_Lab 1:0901bc032f82 101 char cmd_id, read_val;
APS_Lab 1:0901bc032f82 102 int sts;
APS_Lab 1:0901bc032f82 103 cmd_id=ID_REG;
APS_Lab 1:0901bc032f82 104 //Myi2c.write(ADT7420, &cmd_id, 1, 0);
APS_Lab 1:0901bc032f82 105 sts = Myi2c.read(ADT7420, &cmd_id, 1, 0);
APS_Lab 1:0901bc032f82 106 if(sts==0)
APS_Lab 1:0901bc032f82 107 {
APS_Lab 1:0901bc032f82 108 pc.printf("ID ACK\n");
APS_Lab 1:0901bc032f82 109 }
APS_Lab 1:0901bc032f82 110 else
APS_Lab 1:0901bc032f82 111 {
APS_Lab 1:0901bc032f82 112 pc.printf("ID NACK\n");
APS_Lab 1:0901bc032f82 113 pc.printf("Return Value STS %d \n", sts);
APS_Lab 1:0901bc032f82 114 }
APS_Lab 1:0901bc032f82 115 Myi2c.stop();
APS_Lab 1:0901bc032f82 116 pc.printf("ID REG %02x\n", cmd_id);
APS_Lab 1:0901bc032f82 117 return cmd_id;
APS_Lab 0:74a0756399ff 118 }
APS_Lab 0:74a0756399ff 119
APS_Lab 0:74a0756399ff 120 int ADT7420_GetTemp(void)
APS_Lab 0:74a0756399ff 121 {
APS_Lab 0:74a0756399ff 122 char cmd_temp[1], read_val[2];
APS_Lab 0:74a0756399ff 123 int temp=0;
APS_Lab 0:74a0756399ff 124 cmd_temp[0]=TEMP_REG;
APS_Lab 1:0901bc032f82 125 Myi2c.start();
APS_Lab 1:0901bc032f82 126 Myi2c.write(ADT7420, cmd_temp, 2);
APS_Lab 1:0901bc032f82 127 wait(0.2);
APS_Lab 1:0901bc032f82 128 Myi2c.read(ADT7420, read_val, 3);
APS_Lab 0:74a0756399ff 129 pc.printf("Temp Upper %02x\n", read_val[0]);
APS_Lab 0:74a0756399ff 130 pc.printf("Temp Lower %02x\n", read_val[1]);
APS_Lab 0:74a0756399ff 131
APS_Lab 0:74a0756399ff 132 temp = (int)((read_val[0] << 8) | read_val[1]);
APS_Lab 0:74a0756399ff 133 temp >>= 3;
APS_Lab 0:74a0756399ff 134 return temp;
APS_Lab 0:74a0756399ff 135 }
APS_Lab 1:0901bc032f82 136
APS_Lab 1:0901bc032f82 137 int ADT7420_RegDump(void)
APS_Lab 1:0901bc032f82 138 {
APS_Lab 1:0901bc032f82 139 int idx, result, status;
APS_Lab 1:0901bc032f82 140 char reg[1], data[16];
APS_Lab 1:0901bc032f82 141 reg[0]=0x09;
APS_Lab 1:0901bc032f82 142 Myi2c.write(ADT7420,reg, 1, 0);
APS_Lab 1:0901bc032f82 143 wait(0.5);
APS_Lab 1:0901bc032f82 144 status = Myi2c.read(ADT7420, data, sizeof(data));
APS_Lab 1:0901bc032f82 145
APS_Lab 1:0901bc032f82 146 //if(status > 10) {
APS_Lab 1:0901bc032f82 147
APS_Lab 1:0901bc032f82 148 for(idx=0; idx < 16; idx++)
APS_Lab 1:0901bc032f82 149 {
APS_Lab 1:0901bc032f82 150 pc.printf("Reg ID %d : Reg Val %02x\n", idx+4, data[idx]);
APS_Lab 1:0901bc032f82 151 }
APS_Lab 1:0901bc032f82 152 //}
APS_Lab 1:0901bc032f82 153 //else
APS_Lab 1:0901bc032f82 154 //{
APS_Lab 1:0901bc032f82 155 // pc.printf("Return value %d\n", status);
APS_Lab 1:0901bc032f82 156 //}
APS_Lab 1:0901bc032f82 157 //return 0;
APS_Lab 1:0901bc032f82 158 }