Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months 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:
- 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,
Oscilloscope results Figure-2.
Please !
2 Answers
7 years, 8 months 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, 8 months ago.
https://developer.mbed.org/handbook/I2C#api
The 7-bit address should left shift it by 1.
char addr = (0x10 << 1);