Define constants for ADC multiplexer settings and describe I2C status flags

This commit is contained in:
2024-09-06 19:47:09 +02:00
parent f4a6c655bd
commit 4ded84f3f8
3 changed files with 33 additions and 26 deletions

View File

@@ -3,26 +3,27 @@
#include <avr/io.h> #include <avr/io.h>
#define TW_START 0x08 #define TW_START 0x08 // Start condition transmitted
#define TW_REP_START 0x10 #define TW_REP_START 0x10 // Repeated start condition transmitted
#define TW_MT_SLA_ACK 0x18 #define TW_MT_SLA_ACK 0x18 // SLA+W transmitted, ACK received
#define TW_MT_SLA_NACK 0x20 #define TW_MT_SLA_NACK 0x20 // SLA+W transmitted, NACK received
#define TW_MT_DATA_ACK 0x28 #define TW_MT_DATA_ACK 0x28 // Data transmitted, ACK received
#define TW_MR_SLA_ACK 0x40 #define TW_MR_SLA_ACK 0x40 // SLA+R transmitted, ACK received
#define TW_MR_SLA_NACK 0x48 #define TW_MR_SLA_NACK 0x48 // SLA+R transmitted, NACK received
#define TW_MR_DATA_ACK 0x50 #define TW_MR_DATA_ACK 0x50 // Data received, ACK returned
// TODO: Error handling and recovery without watchdog timer. // TODO: Error handling and recovery besides watchdog timer.
// TODO: Implement I2C_vect ISR? This may not actually be // TODO: Add more documentation from the atmel data sheet.
// TODO: Implement TWI_vect ISR? This may not actually be
// much better than the blocking approach. // much better than the blocking approach.
static void I2C_AHT20_Reset(void); static void I2C_AHT20_Reset(void);
static bool I2C_AHT20_IsCalibrated(void); static bool I2C_AHT20_IsCalibrated(void);
static void I2C_AHT20_Calibrate(void); static void I2C_AHT20_Calibrate(void);
static void I2C_AHT20_Trigger(void); static void I2C_AHT20_Trigger(void);
static int I2C_ADS1115_WriteRegister(byte reg, word data); static int I2C_ADS1115_WriteRegister(byte reg, word data);
static word I2C_ADS1115_ReadRegister(byte reg); static word I2C_ADS1115_ReadRegister(byte reg);
int I2C_Init(void) int I2C_Init(void)
{ {
@@ -516,13 +517,13 @@ word I2C_ADS1115_Read(int channel)
// 111 | AINP = AIN3, AINN = GND // 111 | AINP = AIN3, AINN = GND
mux = 0; mux = 0;
if (channel == 0) if (channel == ADS01)
mux = 0x4000; // | 01000000 00000000 mux = 0x4000; // | 01000000 00000000
else if (channel == 1) else if (channel == ADS02)
mux = 0x5000; // | 01010000 00000000 mux = 0x5000; // | 01010000 00000000
else if (channel == 2) else if (channel == ADS03)
mux = 0x6000; // | 01100000 00000000 mux = 0x6000; // | 01100000 00000000
else if (channel == 3) else if (channel == ADS04)
mux = 0x7000; // | 01110000 00000000 mux = 0x7000; // | 01110000 00000000
// Single-shot conversion, active low, continous mode // Single-shot conversion, active low, continous mode

View File

@@ -8,6 +8,12 @@
#define AHT02 0x1 // Middle #define AHT02 0x1 // Middle
#define AHT03 0x2 // Lower #define AHT03 0x2 // Lower
// ADC channels
#define ADS01 0x0 // AIN0 GND
#define ADS02 0x1 // AIN1 GND
#define ADS03 0x2 // AIN2 GND
#define ADS04 0x3 // AIN3 GND
int I2C_Init(void); int I2C_Init(void);
int I2C_Start(byte addr, byte mode); int I2C_Start(byte addr, byte mode);
int I2C_SetChannel(int channel); int I2C_SetChannel(int channel);

View File

@@ -16,7 +16,7 @@
// TODO: Write an improved command parser (low priority). // TODO: Write an improved command parser (low priority).
// TODO: Proper error handling and recovery (after testing). // TODO: Proper error handling and recovery (after testing).
int Init(void) static int Init(void)
{ {
// MOSFETS control things like the heating element // MOSFETS control things like the heating element
// so they are the highest priority to initialize // so they are the highest priority to initialize
@@ -94,7 +94,7 @@ int Init(void)
return 0; return 0;
} }
void Update(void) static void Update(void)
{ {
float temp[3], rh[3]; float temp[3], rh[3];
short adc[4]; short adc[4];
@@ -110,10 +110,10 @@ void Update(void)
I2C_SetChannel(AHT03); I2C_SetChannel(AHT03);
I2C_AHT20_Read(&temp[2], &rh[2]); I2C_AHT20_Read(&temp[2], &rh[2]);
adc[0] = I2C_ADS1115_Read(0); adc[0] = I2C_ADS1115_Read(ADS01);
adc[1] = I2C_ADS1115_Read(1); adc[1] = I2C_ADS1115_Read(ADS02);
adc[2] = I2C_ADS1115_Read(2); adc[2] = I2C_ADS1115_Read(ADS03);
adc[3] = I2C_ADS1115_Read(3); adc[3] = I2C_ADS1115_Read(ADS04);
Info("TEMP0=%.2fC, RH0=%.2f%%", temp[0], rh[0]); Info("TEMP0=%.2fC, RH0=%.2f%%", temp[0], rh[0]);
Info("TEMP1=%.2fC, RH1=%.2f%%", temp[1], rh[1]); Info("TEMP1=%.2fC, RH1=%.2f%%", temp[1], rh[1]);