
Name of the projects
l152_max44009_hw_i2c
l152_max44009_sw_i2c
Description
The projects send data to a PC via a serial connection from a LUX light sensor that is read via a hardware or software I2C.
Code snippet (initial code, project structure and VSCode files generated by VPC – only one project presented here):
char t32_buff[25];
const uint8_t str1[] = "Lux = ";
//const uint8_t str2[] = " ";
const uint8_t str3[] = "\r\n";
const uint8_t str4[] = "Out of range!!!";
int main(void){
/* local variables */
uint8_t mant = 0, exponent = 0;
float lux = 0.0;
/* mandatory system initializations */
vpc_system_init();
//vpc_rtc_init(); /* enable if required */
vpc_gpio_init();
vpc_usart2_uart_init(); /* enable if required */
vpc_i2c1_init();
/* third-party initializations */
/* do your own initializations below */
max44009_setup_auto_noint(0); // auto, cycle at 800ms
while(1){
/* your forever repeating code */
uart_putstrF(USART2, str1);
_max44009_reg0 = 0;
_max44009_reg1 = 0;
max44009_read_2regs(MAX44009_REG_HIGH_BYTE);
exponent = (_max44009_reg0 & 0xf0) >> 4;
if (exponent < 15) {
mant = (_max44009_reg0 & 0x0f) << 4 | _max44009_reg1;
//lux = (float)(((0x00000001 << exponent) * (float)mant) * 0.045);
lux = (float) ((pow(2, (float) exponent) * (float) mant) * 0.045);
sprintf(t32_buff, "%10.3f", lux);
//double2dec((uint32_t)lux, (uint8_t *)t32_buff);
uart_putstr(USART2, (uint8_t *) t32_buff); // light the green led
} else {
uart_putstrF(USART2, str4); // out of range error! - light the red led
lux = 0.0;
}
uart_putstrF(USART2, str3);
my_delay_ms(1000);
GPIO_ToggleBits(LD2_GPIO_Port, LD2_Pin);
}
return 0;
}
Project published in repository.