From: Nathael Pajani Date: Tue, 13 Sep 2016 16:27:19 +0000 (+0200) Subject: TSL256x I2C luminosity and IR sensor example X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=d2bc6155e424916ad64efa68bafad05ca52d1af5;p=soft%2Flpc82x%2Fexamples TSL256x I2C luminosity and IR sensor example --- diff --git a/i2c_light/Makefile b/i2c_light/Makefile new file mode 100644 index 0000000..097ff94 --- /dev/null +++ b/i2c_light/Makefile @@ -0,0 +1,12 @@ +# Makefile for example apps + +MODULE = $(shell basename $(shell cd .. && pwd && cd -)) +NAME = $(shell basename $(CURDIR)) + +.PHONY: $(NAME).bin +$(NAME).bin: + @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@ + +clean mrproper: + @make -C ../../.. --no-print-directory $@ + diff --git a/i2c_light/README b/i2c_light/README new file mode 100644 index 0000000..f94cb5e --- /dev/null +++ b/i2c_light/README @@ -0,0 +1,28 @@ +TSL256x I2C luminosity and IR sensor example + +Copyright 2016 Nathael Pajani + + +/* **************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +This example shows the support of the TSL256x I2C luminosity and IR sensor. + +Periodic conversions (decimal lux value and raw conversion values) are +sent on UART0 using the following frame format : +Lux: 421 (Comb: 0x00ee, IR: 0x0049) + + diff --git a/i2c_light/main.c b/i2c_light/main.c new file mode 100644 index 0000000..be3beea --- /dev/null +++ b/i2c_light/main.c @@ -0,0 +1,147 @@ +/**************************************************************************** + * i2c_light/main.c + * + * TSL256x I2C luminosity and IR sensor example + * + * Copyright 2016 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + + +#include "lib/stdint.h" +#include "core/system.h" +#include "core/systick.h" +#include "core/watchdog.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "lib/errno.h" +#include "drivers/i2c.h" +#include "drivers/serial.h" +#include "drivers/gpio.h" + +#include "extdrv/tsl256x_light_sensor.h" + + +#define MODULE_VERSION 0x01 +#define MODULE_NAME "E-Xanh Gardener" + + +#define SELECTED_FREQ FREQ_SEL_36MHz + + + +/***************************************************************************** */ +/* Pins configuration */ +/* Pins blocks are passed to set_pins() for pins configuration. + * Unused pin blocks can be removed safely with the corresponding set_pins() call + * All pins blocks may be safelly merged in a single block for single set_pins() call.. + */ +const struct pio_config common_pins[] = { + /* UART 0 */ + { LPC_GPIO_0_0, LPC_UART0_RX, 0 }, + { LPC_GPIO_0_4, LPC_UART0_TX, 0 }, + /* I2C 0 */ + { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 }, + { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 }, + ARRAY_LAST_PIO, +}; + + +/***************************************************************************** */ +/* Luminosity */ + +/* Note : These are 8bits address */ +#define TSL256x_ADDR 0x52 /* Pin Addr Sel (pin2 of tsl256x) connected to GND */ +struct tsl256x_sensor_config tsl256x_sensor = { + .bus_num = I2C0, + .addr = TSL256x_ADDR, + .gain = TSL256x_LOW_GAIN, + .integration_time = TSL256x_INTEGRATION_100ms, + .package = TSL256x_PACKAGE_T, +}; + +void lux_config(int uart_num) +{ + int ret = 0; + ret = tsl256x_configure(&tsl256x_sensor); + if (ret != 0) { + uprintf(uart_num, "Lux config error: %d\n", ret); + } +} + +void lux_display(int uart_num) +{ + uint16_t comb = 0, ir = 0; + uint32_t lux = 0; + int ret = 0; + + ret = tsl256x_sensor_read(&tsl256x_sensor, &comb, &ir, &lux); + if (ret != 0) { + uprintf(uart_num, "Lux read error\n"); + } else { + uprintf(uart_num, "Lux: %d (Comb: 0x%04x, IR: 0x%04x)\n", lux, comb, ir); + } +} + + +/***************************************************************************** */ +void system_init() +{ + system_set_default_power_state(); + clock_config(SELECTED_FREQ); + set_pins(common_pins); + gpio_on(); + /* System tick timer MUST be configured and running in order to use the sleeping + * functions */ + systick_timer_on(1); /* 1ms */ + systick_start(); +} + +/* Define our fault handler. This one is not mandatory, the dummy fault handler + * will be used when it's not overridden here. + * Note : The default one does a simple infinite loop. If the watchdog is deactivated + * the system will hang. + */ +void fault_info(const char* name, uint32_t len) +{ + uprintf(UART0, name); + while (1); +} + + + +/***************************************************************************** */ +int main(void) +{ + system_init(); + uart_on(UART0, 115200, NULL); + + + i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER); + + /* Configure lux sensor */ + lux_config(UART0); + msleep(5); + + while (1) { + /* Display luminosity from TSL256x sensor */ + lux_display(UART0); + msleep(250); + } + return 0; +} +