7 years ago.

use IAR Embedded Workbench ARM to develop lpc824 sampling veml6040

My question: I use IAR Embedded Workbench ARM to develop lpc824 sampling veml6040 [ambient light sensor data, but the data is always incorrect for.

code:

  1. include "mbed.h"

I2C i2c(P0_10,P0_11);SDA,SCL

int main(){

int rgbt;

char cmd[2];

char addr=0x10;

i2c.frequency(100000);

cmd[0] = 0x00;

cmd[1] = 0x00;

i2c.write(addr, cmd, 2);

wait(0.1);

addr=0x10;

cmd[0]=0x08;

i2c.write(addr, cmd,1);

while(true){

wait(0.05);

i2c.read(addr, cmd, 2);

wait(0.05);

uint16_t rgb=cmd[1]<<8 |cmd[0];

printf("red=%d",rgb);

float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);

printf("red1=%f \r\n",tmp);

wait(0.1);

}

}

After Compiler and download to lpc824, Results after running:

Virtual serial data Figure-1, /media/uploads/zhjb62/i-s_from_pc_spi_datas.jpg

Oscilloscope results Figure-2. /media/uploads/zhjb62/wave_was_i2c_sclock_-_sdata.jpg

Please !

2 Answers

7 years ago.

Please use the code tags to post readable code.

The I2C message shows that you try to read from slave address 0x10 but that the slave does not acknowledge (bit 9 is high level). So the address is wrong or there is a wiring problem. The missing ack results in an aborted read operation and means that the following printed results just show the already present values in your byte array and are not coming from the sensor.

7 years ago.

https://developer.mbed.org/handbook/I2C#api

The 7-bit address should left shift it by 1.

char addr = (0x10 << 1);