Test

Committer:
APS_Lab
Date:
Tue May 14 03:20:59 2019 +0000
Revision:
1:b7f576b50a32
Parent:
0:00acc05bf822
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:00acc05bf822 1 /* mbed Microcontroller Library
APS_Lab 0:00acc05bf822 2 * Copyright (c) 2018 ARM Limited
APS_Lab 0:00acc05bf822 3 * SPDX-License-Identifier: Apache-2.0
APS_Lab 0:00acc05bf822 4 */
APS_Lab 0:00acc05bf822 5
APS_Lab 0:00acc05bf822 6 #include "mbed.h"
APS_Lab 0:00acc05bf822 7 #include "stats_report.h"
APS_Lab 0:00acc05bf822 8
APS_Lab 1:b7f576b50a32 9 #define ADT7420 0x48
APS_Lab 1:b7f576b50a32 10
APS_Lab 1:b7f576b50a32 11 #define RESET_REG 0x2F
APS_Lab 1:b7f576b50a32 12 #define TEMP_REG 0x00
APS_Lab 1:b7f576b50a32 13 #define ID_REG 0x0B
APS_Lab 1:b7f576b50a32 14
APS_Lab 0:00acc05bf822 15
APS_Lab 1:b7f576b50a32 16 DigitalOut led1(LED1);
APS_Lab 1:b7f576b50a32 17 DigitalOut sensor(D8);
APS_Lab 1:b7f576b50a32 18
APS_Lab 1:b7f576b50a32 19 I2C Myi2c(I2C_SDA, I2C_SCL);
APS_Lab 1:b7f576b50a32 20
APS_Lab 1:b7f576b50a32 21 Serial pc(USBTX, USBRX);
APS_Lab 1:b7f576b50a32 22
APS_Lab 1:b7f576b50a32 23 void ADT7420_reset(void);
APS_Lab 1:b7f576b50a32 24 char ADT7420_GetID(void);
APS_Lab 1:b7f576b50a32 25 int ADT7420_GetTemp(void);
APS_Lab 0:00acc05bf822 26
APS_Lab 1:b7f576b50a32 27 int main() {
APS_Lab 1:b7f576b50a32 28 float status;
APS_Lab 1:b7f576b50a32 29 int res;
APS_Lab 1:b7f576b50a32 30 sensor=1;
APS_Lab 1:b7f576b50a32 31 //configure for UART
APS_Lab 1:b7f576b50a32 32 pc.baud(115200);
APS_Lab 1:b7f576b50a32 33 pc.printf("I2C ADT7420 Demo\n");
APS_Lab 1:b7f576b50a32 34
APS_Lab 1:b7f576b50a32 35 //configure for I2C @100KHz
APS_Lab 1:b7f576b50a32 36 //Myi2c.init();
APS_Lab 1:b7f576b50a32 37 Myi2c.frequency(10000);
APS_Lab 1:b7f576b50a32 38
APS_Lab 1:b7f576b50a32 39 //configure for ADT7420
APS_Lab 1:b7f576b50a32 40 Myi2c.lock();
APS_Lab 1:b7f576b50a32 41 pc.printf("[RESET ] ADT7420\n");
APS_Lab 1:b7f576b50a32 42 ADT7420_reset();
APS_Lab 1:b7f576b50a32 43 wait(0.5);
APS_Lab 1:b7f576b50a32 44
APS_Lab 1:b7f576b50a32 45 pc.printf("[CHECK ID] ADT7420\n");
APS_Lab 1:b7f576b50a32 46 ADT7420_GetID();
APS_Lab 0:00acc05bf822 47
APS_Lab 1:b7f576b50a32 48
APS_Lab 0:00acc05bf822 49 while (true) {
APS_Lab 1:b7f576b50a32 50 led1 = 0;
APS_Lab 1:b7f576b50a32 51
APS_Lab 1:b7f576b50a32 52 pc.printf("[Get Temp] ADT7420\n");
APS_Lab 1:b7f576b50a32 53 res = ADT7420_GetTemp();
APS_Lab 1:b7f576b50a32 54
APS_Lab 1:b7f576b50a32 55 if(res & 0x1000)
APS_Lab 1:b7f576b50a32 56 {
APS_Lab 1:b7f576b50a32 57 status = (float)(res * 0.0625 * -1.0);
APS_Lab 1:b7f576b50a32 58
APS_Lab 1:b7f576b50a32 59 }
APS_Lab 1:b7f576b50a32 60 else
APS_Lab 1:b7f576b50a32 61 {
APS_Lab 1:b7f576b50a32 62 status = (float)(res * 0.0625);
APS_Lab 1:b7f576b50a32 63 }
APS_Lab 1:b7f576b50a32 64
APS_Lab 1:b7f576b50a32 65 pc.printf("Current Temp %.4f C\n", status);
APS_Lab 1:b7f576b50a32 66
APS_Lab 1:b7f576b50a32 67 wait(0.5);
APS_Lab 1:b7f576b50a32 68 led1 = 1;
APS_Lab 1:b7f576b50a32 69 wait(0.5);
APS_Lab 1:b7f576b50a32 70 }
APS_Lab 1:b7f576b50a32 71
APS_Lab 1:b7f576b50a32 72 }
APS_Lab 0:00acc05bf822 73
APS_Lab 1:b7f576b50a32 74 void ADT7420_reset(void)
APS_Lab 1:b7f576b50a32 75 {
APS_Lab 1:b7f576b50a32 76 char cmd_reset;
APS_Lab 1:b7f576b50a32 77 cmd_reset = RESET_REG;
APS_Lab 1:b7f576b50a32 78 Myi2c.write(ADT7420);
APS_Lab 1:b7f576b50a32 79 if(Myi2c.write(0xFF))
APS_Lab 1:b7f576b50a32 80 {
APS_Lab 1:b7f576b50a32 81 pc.printf("Ack \n");
APS_Lab 1:b7f576b50a32 82 }
APS_Lab 1:b7f576b50a32 83 else
APS_Lab 1:b7f576b50a32 84 {
APS_Lab 1:b7f576b50a32 85 pc.printf("Nack \n");
APS_Lab 1:b7f576b50a32 86 }
APS_Lab 1:b7f576b50a32 87
APS_Lab 1:b7f576b50a32 88 Myi2c.write(ADT7420);
APS_Lab 1:b7f576b50a32 89 if(Myi2c.write(cmd_reset))
APS_Lab 1:b7f576b50a32 90 {
APS_Lab 1:b7f576b50a32 91 pc.printf("Reset Done\n");
APS_Lab 1:b7f576b50a32 92 }
APS_Lab 1:b7f576b50a32 93 else
APS_Lab 1:b7f576b50a32 94 {
APS_Lab 1:b7f576b50a32 95 pc.printf("Reset Failure\n");
APS_Lab 0:00acc05bf822 96 }
APS_Lab 0:00acc05bf822 97 }
APS_Lab 1:b7f576b50a32 98
APS_Lab 1:b7f576b50a32 99 char ADT7420_GetID(void)
APS_Lab 1:b7f576b50a32 100 {
APS_Lab 1:b7f576b50a32 101 char cmd_id, read_val;
APS_Lab 1:b7f576b50a32 102 int sts;
APS_Lab 1:b7f576b50a32 103 cmd_id=ID_REG;
APS_Lab 1:b7f576b50a32 104 //Myi2c.write(ADT7420, &cmd_id, 1, 0);
APS_Lab 1:b7f576b50a32 105 sts = Myi2c.read(ADT7420, &cmd_id, 1, 0);
APS_Lab 1:b7f576b50a32 106 if(sts==0)
APS_Lab 1:b7f576b50a32 107 {
APS_Lab 1:b7f576b50a32 108 pc.printf("ID ACK\n");
APS_Lab 1:b7f576b50a32 109 }
APS_Lab 1:b7f576b50a32 110 else
APS_Lab 1:b7f576b50a32 111 {
APS_Lab 1:b7f576b50a32 112 pc.printf("ID NACK\n");
APS_Lab 1:b7f576b50a32 113 pc.printf("Return Value STS %d \n", sts);
APS_Lab 1:b7f576b50a32 114 }
APS_Lab 1:b7f576b50a32 115 Myi2c.stop();
APS_Lab 1:b7f576b50a32 116 pc.printf("ID REG %02x\n", cmd_id);
APS_Lab 1:b7f576b50a32 117 return cmd_id;
APS_Lab 1:b7f576b50a32 118 }
APS_Lab 1:b7f576b50a32 119
APS_Lab 1:b7f576b50a32 120 int ADT7420_GetTemp(void)
APS_Lab 1:b7f576b50a32 121 {
APS_Lab 1:b7f576b50a32 122 char cmd_temp[1], read_val[2];
APS_Lab 1:b7f576b50a32 123 int temp=0;
APS_Lab 1:b7f576b50a32 124 cmd_temp[0]=TEMP_REG;
APS_Lab 1:b7f576b50a32 125 Myi2c.start();
APS_Lab 1:b7f576b50a32 126 Myi2c.write(ADT7420, cmd_temp, 2);
APS_Lab 1:b7f576b50a32 127 wait(0.2);
APS_Lab 1:b7f576b50a32 128 Myi2c.read(ADT7420, read_val, 3);
APS_Lab 1:b7f576b50a32 129 pc.printf("Temp Upper %02x\n", read_val[0]);
APS_Lab 1:b7f576b50a32 130 pc.printf("Temp Lower %02x\n", read_val[1]);
APS_Lab 1:b7f576b50a32 131
APS_Lab 1:b7f576b50a32 132 temp = (int)((read_val[0] << 8) | read_val[1]);
APS_Lab 1:b7f576b50a32 133 temp >>= 3;
APS_Lab 1:b7f576b50a32 134 return temp;
APS_Lab 1:b7f576b50a32 135 }