diff --git a/projects/fw_103/src/ads1115.c b/projects/fw_103/src/ads1115.c index 558228149..160bb023d 100644 --- a/projects/fw_103/src/ads1115.c +++ b/projects/fw_103/src/ads1115.c @@ -14,18 +14,21 @@ StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAd uint16_t cmd; // Write Config register - /* TODO: fill out this value */ - cmd = 0x0000; + /* TODO: fill out + 0b0000010010000011 in hex + (everything default except cts mode) + */ + cmd = 0x0483; i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2); /* TODO (optional) */ // Set low thresh to zero - cmd = 0x0000; + cmd = 0x8000; i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2); /* TODO (optional) */ // Set high thresh to 1V - cmd = 0x0000; + cmd = 0x7FFF; i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2); // Register the ALRT pin @@ -40,19 +43,26 @@ StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channe } uint16_t cmd; - // Write Config register - cmd = 0x0000; + cmd = 0x0000; i2c_write_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2); return STATUS_CODE_OK; } +//retrieves ADC readings StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, uint16_t *reading) { /* TODO: complete function */ + i2c_read_reg(config->i2c_port, confi->i2c_addr, ADS1115_REG_CONVERSION, (uint8_t *)reading, 2); return STATUS_CODE_OK; } +//converts ADC readings to voltage StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading) { /* TODO: complete function */ + uint16_t raw_reading; //value between 0 and 65535 + ads1115_read_raw(config, channel, &raw_reading); + + *reading = ((raw_reading / 65535.0) * 4.096) - 2.048; //raw_reading/max *4.096V -2.048V + return STATUS_CODE_OK; } diff --git a/projects/fw_103/src/main.c b/projects/fw_103/src/main.c index 907eb64cf..74ee131a7 100644 --- a/projects/fw_103/src/main.c +++ b/projects/fw_103/src/main.c @@ -12,14 +12,56 @@ #include #include "log.h" +#include "gpio.h" #include "tasks.h" +#include "delay.h" + +GpioAddress led_addr = { + .port = GPIO_PORT_B, + .pin = 3, +}; + +GpioAddress ready_pin = { + .port = GPIO_PORT_B, + .pin = GPIO_Pin_0; +}; + +ADS1115_Config config = { + .handler_task = task2, + .i2c_addr = ADS1115_ADDR_GND, + .i2c_port = ADS1115_I2C_PORT, + .ready_pin = &ready_pin, +}; + +TASK(task1, TASK_STACK_512) { + gpio_init_pin(&led_addr, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW); + while (true) { + gpio_toggle_state(&led_addr); + LOG_DEBUG("LED Toggled\n"); + delay_ms(1000); + } +} + +TASK(task2, TASK_STACK_512){ + float voltage; + while(true){ + //stores converted ADC readings (in V) in voltage variable + ads1115_read_converted(&config, ADS1115_CHANNEL_0, &voltage); + LOG_DEBUG("Voltage Reading: %.3f", voltage); + delay_ms(100); + } +} int main() { + gpio_init(); tasks_init(); log_init(); gpio_init(); LOG_DEBUG("Welcome to FW 103!\n"); + tasks_init_task(task1, TASK_PRIORITY(1), NULL); + tasks_init_task(task2, TASK_PRIORITY(1), NULL); + tasks_start(); LOG_DEBUG("exiting main?\n");