Skip to content

Commit

Permalink
Finished FW 103
Browse files Browse the repository at this point in the history
  • Loading branch information
d3kanesa committed Dec 20, 2024
1 parent 9db63c7 commit 9e65230
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
9 changes: 6 additions & 3 deletions projects/fw_103/src/ads1115.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAd

// Write Config register
/* TODO: fill out this value */
cmd = 0x0000;
cmd = 0b1000010010000011;
i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2);

/* TODO (optional) */
Expand All @@ -42,17 +42,20 @@ StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channe
uint16_t cmd;

// Write Config register
cmd = 0x0000;
cmd = 0b1000010010000011;
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, uint16_t *reading) {
/* TODO: complete function */
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 */
uint16_t raw_reading;
ads1115_read_raw(config, channel, &raw_reading);
*reading = ((float) raw_reading) / 65535 * 4.096 - 2.048;
return STATUS_CODE_OK;
}
40 changes: 37 additions & 3 deletions projects/fw_103/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,49 @@

#include "log.h"
#include "tasks.h"
#include "gpio.h"
#include <ads1115.h>
#include "delay.h"

// GpioAddress struct
GpioAddress led_addr = {
.port = GPIO_PORT_B,
.pin = 3,
};

TASK(toggle, TASK_STACK_512) {

GpioAddress ready_pin = {
.port = GPIO_PORT_B,
.pin = GPIO_Pin_0,
};

ADS1115_Config config = {
.handler_task = toggle,
.i2c_addr = ADS1115_ADDR_GND,
.i2c_port = ADS1115_I2C_PORT,
.ready_pin = &ready_pin,
};

float res;

while (true) {
gpio_toggle_state(&led_addr);
ads1115_read_converted(&config, ADS1115_CHANNEL_0, &res);
LOG_DEBUG("Voltage: %f\n", res);
delay_ms(1000);
}

}

int main() {
tasks_init();
log_init();
gpio_init();
LOG_DEBUG("Welcome to FW 103!\n");
gpio_init_pin(&led_addr, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);

tasks_init_task(toggle, TASK_PRIORITY(1), NULL);
tasks_start();

LOG_DEBUG("exiting main?\n");
return 0;
}
}

0 comments on commit 9e65230

Please sign in to comment.