Skip to content

Commit

Permalink
Megan Lee Completed FW103
Browse files Browse the repository at this point in the history
  • Loading branch information
mzqan committed Nov 6, 2024
1 parent 9db63c7 commit 8e4dc15
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
22 changes: 16 additions & 6 deletions projects/fw_103/src/ads1115.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
42 changes: 42 additions & 0 deletions projects/fw_103/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,56 @@
#include <stdio.h>

#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");
Expand Down

0 comments on commit 8e4dc15

Please sign in to comment.