From 2d222f556b809c86b8eb3c091c0f9e613aa9fd32 Mon Sep 17 00:00:00 2001 From: Anmeet Sekhon Date: Wed, 18 Sep 2024 05:29:42 +0000 Subject: [PATCH] Completed Part 2 of 103 --- projects/fw_103/src/ads1115.c | 22 +++++++++++++++++++--- projects/fw_103/src/main.c | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/projects/fw_103/src/ads1115.c b/projects/fw_103/src/ads1115.c index d05cfa0d7..155f49d23 100644 --- a/projects/fw_103/src/ads1115.c +++ b/projects/fw_103/src/ads1115.c @@ -13,9 +13,13 @@ StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAd uint16_t cmd; + // Bit #: | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + // Field: | OS | MUX | PGA |MODE| DR | COMP_MODE | COMP_POL | COMP_LAT | COMP_QUE | + // Value: | 1 | 000 | 010 | 0 | 100 | 0 | 0 | 0 | 11 | + // Write Config register /* TODO: fill out this value */ - cmd = 0x0000; + cmd = 0x8483; // This should be the hex code for that binary value i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2); /* TODO (optional) */ @@ -30,7 +34,6 @@ StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAd // Register the ALRT pin /* TODO (optional) */ - return STATUS_CODE_OK; } @@ -42,17 +45,30 @@ StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channe uint16_t cmd; // Write Config register - cmd = 0x0000; + cmd = 0x8483; //I'm assuming the same configruation as earlier should be written here. I feel like it might be wrong i2c_write_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2); return STATUS_CODE_OK; } StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, int16_t *reading) { /* TODO: complete function */ + StatusCode status = ads1115_select_channel(config, channel); + if (status != STATUS_CODE_OK) { + return status; + } + + i2c_read_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONVERSION, (uint8_t *)reading, 2); // return STATUS_CODE_OK; } StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading) { /* TODO: complete function */ + int16_t raw_reading; + StatusCode status = ads1115_read_raw(config, channel, &raw_reading); + if (status != STATUS_CODE_OK) { + return status; + } + + *reading = raw_reading * (2.048 / 65535); return STATUS_CODE_OK; } diff --git a/projects/fw_103/src/main.c b/projects/fw_103/src/main.c index 59a7e5fa6..507543a1d 100644 --- a/projects/fw_103/src/main.c +++ b/projects/fw_103/src/main.c @@ -17,6 +17,8 @@ #include "gpio.h" #include "delay.h" +#include "ads1115.h" + // GpioAddress struct GpioAddress led_addr = { .port = GPIO_PORT_B, @@ -25,9 +27,25 @@ GpioAddress led_addr = { TASK(led_gpio_task, TASK_STACK_512) { gpio_init_pin(&led_addr, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW); + float reading; + + //Ngl, i'm not fully sure why this is being done in the same task as the led thing from before but this seems to be what the instructions are saying. + GpioAddress ready_pin = { + .port = GPIO_PORT_B, + .pin = GPIO_Pin_0, + }; + ADS1115_Config config = { + .handler_task = led_gpio_task, + .i2c_addr = ADS1115_ADDR_GND, + .i2c_port = ADS1115_I2C_PORT, + .ready_pin = &ready_pin, + }; + while(true) { gpio_toggle_state(&led_addr); LOG_DEBUG("Toggling LED\n"); // Added this line to see if something is even happening in the scons build + ads1115_read_converted(&config, ADS1115_CHANNEL_0, &reading); + LOG_DEBUG("Reading: %f\n", reading); // Added to see if a value actually comes delay_ms(1000); } }