/* * I2C_Infrared_sensor.c * * Created on: Oct 9, 2012 * Author: Aspire 4736G */ #include "stdio.h" #include "math.h" #include "lpc17xx_pinsel.h" #include "lpc17xx_i2c.h" #include "lpc17xx_timer.h" //#include "lpc17xx_gpio.h" #define TRUE 1 #define FALSE 0 #define I2CDEV LPC_I2C2 #define vcnl4000_i2c_addr 0x13 #define vcnl4000_1r0 0x80 #define vcnl4000_1r1 0x81 #define vcnl4000_1r3 0x83 #define vcnl4000_1r4 0x84 #define vcnl4000_1r5 0x85 #define vcnl4000_1r6 0x86 #define vcnl4000_1r7 0x87 #define vcnl4000_1r8 0x88 #define vcnl4000_1r9 0x89 #define vcnl4000_1r10 0x8A uint8_t vcnl400_7and8[2] = {vcnl4000_1r7,vcnl4000_1r8}; static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type txsetup; txsetup.sl_addr7bit = addr; txsetup.tx_data = buf; txsetup.tx_length = len; txsetup.rx_data = NULL; txsetup.rx_length = 0; txsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type rxsetup; rxsetup.sl_addr7bit = addr; rxsetup.tx_data = NULL;// Get address to read at writing address rxsetup.tx_length = 0; rxsetup.rx_data = buf; rxsetup.rx_length = len; rxsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } static void init_i2c(void) { PINSEL_CFG_Type PinCfg; /* Initialize I2C2 pin connect */ PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 11; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(LPC_I2C2, 100000); /* Enable I2C1 operation */ I2C_Cmd(LPC_I2C2, ENABLE); } void vcnl_read(int32_t *value) { uint8_t buf_L[1],buf_H[1]; uint32_t low,high; //int32_t infrared; buf_L[0] = vcnl4000_1r8; I2CWrite(vcnl4000_i2c_addr,buf_L,1); I2CRead(vcnl4000_i2c_addr,buf_L,1); low = (int8_t)buf_L[0]; buf_H[0] = vcnl4000_1r7; I2CWrite(vcnl4000_i2c_addr,buf_H,1); I2CRead(vcnl4000_i2c_addr,buf_H,1); high = (int8_t)buf_H[0]; *value = (high << 8) | low; // printf("%d\n",*value); } int main(void) { init_i2c(); int32_t infrared; while(1) { vcnl_read(&infrared); printf("infrared = %ld lux\n", infrared); } return 0; } |