SHENG-HEN HSIEH
/
MLX90614_STM32compatible
works fine on STM
main.cpp@3:5811c736c8ef, 2017-02-15 (annotated)
- Committer:
- open4416
- Date:
- Wed Feb 15 11:35:37 2017 +0000
- Revision:
- 3:5811c736c8ef
- Parent:
- 2:09b8571eb8ce
bug in Buff high byte
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
open4416 | 0:ec2a1057416b | 1 | #include "mbed.h" |
open4416 | 0:ec2a1057416b | 2 | |
open4416 | 0:ec2a1057416b | 3 | #define MLX90614_ADDR (0x5A) // Slave address |
open4416 | 0:ec2a1057416b | 4 | #define MLX90614_TOBJ (0x07) // Object temperature register |
open4416 | 0:ec2a1057416b | 5 | |
open4416 | 0:ec2a1057416b | 6 | DigitalOut led(D13); //link leg |
open4416 | 0:ec2a1057416b | 7 | I2C i2c(D14, D15); //I2C reg(SDA, SCL) |
open4416 | 0:ec2a1057416b | 8 | Serial pc(D1, D0); //Serial reg |
open4416 | 0:ec2a1057416b | 9 | Ticker TT; //call a timer |
open4416 | 0:ec2a1057416b | 10 | |
open4416 | 0:ec2a1057416b | 11 | char data_write[1]; //buff for write |
open4416 | 0:ec2a1057416b | 12 | char data_read [3]; //buff for read |
open4416 | 0:ec2a1057416b | 13 | short Buff = 0x00; |
open4416 | 0:ec2a1057416b | 14 | float TOBJ = 0.0; |
open4416 | 0:ec2a1057416b | 15 | void read_MLX(); |
open4416 | 0:ec2a1057416b | 16 | |
open4416 | 0:ec2a1057416b | 17 | int main() |
open4416 | 0:ec2a1057416b | 18 | { |
open4416 | 0:ec2a1057416b | 19 | led = 0; |
open4416 | 0:ec2a1057416b | 20 | pc.baud(9600); //set baud rate |
open4416 | 1:f23e11d251ff | 21 | i2c.frequency(100000); //100kHz |
open4416 | 0:ec2a1057416b | 22 | read_MLX(); //update MLX data |
open4416 | 0:ec2a1057416b | 23 | TOBJ = ((float)Buff * 0.02f) - 273.0f; |
open4416 | 0:ec2a1057416b | 24 | printf("%.2f\n",TOBJ); |
open4416 | 0:ec2a1057416b | 25 | } |
open4416 | 0:ec2a1057416b | 26 | |
open4416 | 0:ec2a1057416b | 27 | |
open4416 | 0:ec2a1057416b | 28 | void read_MLX(void) //read MLX data give raw data |
open4416 | 0:ec2a1057416b | 29 | { |
open4416 | 0:ec2a1057416b | 30 | led = 1; |
open4416 | 0:ec2a1057416b | 31 | //TOBJ |
open4416 | 0:ec2a1057416b | 32 | data_write[0] = MLX90614_TOBJ; //TOBJ address to buff |
open4416 | 0:ec2a1057416b | 33 | i2c.write( MLX90614_ADDR<<1 | 0x00, data_write, 1, 1); //command to RAM access SR activated |
open4416 | 0:ec2a1057416b | 34 | i2c.read ( MLX90614_ADDR<<1 | 0x01, data_read , 3, 0); //read callback |
open4416 | 3:5811c736c8ef | 35 | Buff = data_read[1] << 8; // merging |
open4416 | 0:ec2a1057416b | 36 | Buff |= data_read[0]; |
open4416 | 0:ec2a1057416b | 37 | led = 0; |
open4416 | 0:ec2a1057416b | 38 | } |