Gabriel Silva
/
mlx90615_lpc1768
program for temperature reading with mlx90615
Revision 2:c4552b8c47c0, committed 2020-07-16
- Comitter:
- glsfacom
- Date:
- Thu Jul 16 10:58:18 2020 -0400
- Parent:
- 1:c0d057b08394
- Commit message:
- One MLX setup
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Jul 16 10:58:18 2020 -0400 @@ -0,0 +1,4 @@ +^BUILD$ +^.mbed$ +^mbed-os$ +^crc8$
--- a/Mlx90615.cpp Wed Jul 15 18:55:10 2020 +0000 +++ b/Mlx90615.cpp Thu Jul 16 10:58:18 2020 -0400 @@ -1,114 +1,102 @@ #include "Mlx90615.h" -#include "smbus.h" +#include "crc8.h" +extern Serial pc; I2C i2c(p28, p27);//I2C_SDA, I2C_SCL -void Mlx90615::wake() + +inline void +Mlx90615::wake() { /*SCL = 0; wait_ms(50); SCL = 1;*/ MLX_VCC = 1; - wait_us(301*1000);//Waiting for valid data as datasheet says + ThisThread::sleep_for(301);//Waiting for valid data as datasheet says } -unsigned int Mlx90615::sleep(){ +inline void +Mlx90615::sleep(){ MLX_VCC = 0; - return 1; } -unsigned int Mlx90615::read(unsigned char memory, unsigned char address){ +float +Mlx90615::read(unsigned char memory, unsigned char address){ unsigned char crc, addr, status = 0; - unsigned int read = 0x0000; + char read[2]; + int ACK = 0; addr = memory + address; + + read[0] = 0x01; + read[1] = 0x00; - i2c.start(); - - smbus_send_address(0x5B, WRITE); - if(!i2c.ACK) return 0; - - smbus_send_byte(addr); - if(!i2c.ACK) return 0; - + char cmd = 0x27; i2c.start(); - - smbus_send_address(0x5B, READ); - if(!i2c.ACK) return 0; - - read = smbus_read_uint(&status, LITTLE_ENDIAN); - - crc = smbus_read_byte(&status); - - smbus_stop(); - - return read; + ACK = i2c.write(0x5b<<1); + if(!ACK) return -1; + ACK = i2c.write(addr); + if(!ACK) return -1; + i2c.start(); + ACK = i2c.write((0x5b<<1)|1); + if(!ACK) return -1; + read[0] = i2c.read(ACK); + if(!ACK) return -1; + read[1] = i2c.read(ACK); + i2c.read(ACK); + float temp = (float((read[1] << 8) | read[0])); + return temp*0.02-273.15; } -unsigned int Mlx90615::read_temperature(){ - unsigned int temp = read(RAM, 0x07); +float +Mlx90615::read_temperature(){ + float temp = read(RAM, 0x07); + int cont = 0; while(temp == 0) { + if(cont == 100) + { + pc.printf("got 100 times 0\n"); + cont = 0; + } wait_us(5000); temp = read(RAM, 0x07); + cont++; } return temp; } -/*float convert_to_celsius(unsigned int temperature){ - return temperature * 0.02 - 273.15; -}*/ -typedef unsigned char uint8_t; - -uint8_t gencrc(uint8_t *data, size_t len) -{ - uint8_t crc = 0xff; - size_t i, j; - for (i = 0; i < len; i++) { - crc ^= data[i]; - for (j = 0; j < 8; j++) { - if ((crc & 0x80) != 0) - crc = (uint8_t)((crc << 1) ^ 0x31); - else - crc <<= 1; - } - } - return crc; -} - -void Mlx90615::write(unsigned char address, unsigned int value){ +void +Mlx90615::write(unsigned char address, unsigned int value){ unsigned char addr = EEPROM + address; - unsigned char bytes[4], crc, vh, vl; + char *bytes; + unsigned char crc, vh, vl; vh = value >> 8; vl = value; bytes[0] = 0xB6; bytes[1] = addr; bytes[2] = vl; bytes[3] = vh; - crc = gencrc(bytes, 4); - - i2c.start(); - - smbus_send_address(0x5B, WRITE); - - smbus_send_byte(addr); + crc = crc8(bytes, 4); - smbus_send_byte(vl); - - smbus_send_byte(vh); - - smbus_send_byte(crc); - - smbus_stop(); + i2c.start(); + i2c.write(0x5b<<1); + i2c.write(addr); + i2c.write(vl); + i2c.write(vh); + i2c.write(crc); + i2c.stop(); } -void Mlx90615::erase_eeprom_address(unsigned char address){ - write(address, 0x0000); +inline void +Mlx90615::erase_eeprom_address(unsigned char address){ + Mlx90615::write(address, 0x0000); } -void Mlx90615::set_emissivity(float e){ +void +Mlx90615::set_emissivity(float e){ unsigned int emissivity; emissivity = 16384 * e; erase_eeprom_address(0x03); - wait_us(5*1000); - write(0x03, emissivity); + ThisThread::sleep_for(5); + Mlx90615::write(0x03, emissivity); }
--- a/Mlx90615.h Wed Jul 15 18:55:10 2020 +0000 +++ b/Mlx90615.h Thu Jul 16 10:58:18 2020 -0400 @@ -11,12 +11,12 @@ { public: void wake(); - unsigned int sleep(); - unsigned int read_temperature(); + void sleep(); + float read_temperature(); void erase_eeprom_address(unsigned char address); void set_emissivity(float e); private: - unsigned int read(unsigned char memory, unsigned char address); + float read(unsigned char memory, unsigned char address); void write(unsigned char address, unsigned int value); };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc8.lib Thu Jul 16 10:58:18 2020 -0400 @@ -0,0 +1,1 @@ +http://os.mbed.com/users/evwijk/code/crc8/#cc65a6cec1a181f708182f0e98e124c68c8d9a87 \ No newline at end of file
--- a/main.cpp Wed Jul 15 18:55:10 2020 +0000 +++ b/main.cpp Thu Jul 16 10:58:18 2020 -0400 @@ -8,22 +8,9 @@ float temp; -void setup() -{ - //Force SMBUS comm - c = 0; // Setup override pin to pull clock low - c.input(); // Make it input to start with... - c.mode(PullUp); // ...with pull up - c.output(); // Override clock pin low - ThisThread::sleep_for(0.00005); // Pause for treq 39ms - c.input(); // Remove override... - c.mode(PullUp); // ...with pull up - ThisThread::sleep_for(0.00005); // Pause again -} - int main(){ Mlx90615 mlx90615; - setup(); + pc.printf("Hello World!\n"); while(true){ temp=mlx90615.read_temperature();