Skip to content

Commit

Permalink
Completed Part 2 of 103
Browse files Browse the repository at this point in the history
  • Loading branch information
AnmeetS committed Sep 18, 2024
1 parent 64bb5fd commit 2d222f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
22 changes: 19 additions & 3 deletions projects/fw_103/src/ads1115.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand All @@ -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;
}

Expand All @@ -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;
}
18 changes: 18 additions & 0 deletions projects/fw_103/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "gpio.h"
#include "delay.h"

#include "ads1115.h"

// GpioAddress struct
GpioAddress led_addr = {
.port = GPIO_PORT_B,
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 2d222f5

Please sign in to comment.