diff --git a/src/bus/i2c.c b/src/bus/i2c.c index 3de6a19..d777187 100644 --- a/src/bus/i2c.c +++ b/src/bus/i2c.c @@ -3,26 +3,27 @@ #include -#define TW_START 0x08 -#define TW_REP_START 0x10 -#define TW_MT_SLA_ACK 0x18 -#define TW_MT_SLA_NACK 0x20 -#define TW_MT_DATA_ACK 0x28 -#define TW_MR_SLA_ACK 0x40 -#define TW_MR_SLA_NACK 0x48 -#define TW_MR_DATA_ACK 0x50 +#define TW_START 0x08 // Start condition transmitted +#define TW_REP_START 0x10 // Repeated start condition transmitted +#define TW_MT_SLA_ACK 0x18 // SLA+W transmitted, ACK received +#define TW_MT_SLA_NACK 0x20 // SLA+W transmitted, NACK received +#define TW_MT_DATA_ACK 0x28 // Data transmitted, ACK received +#define TW_MR_SLA_ACK 0x40 // SLA+R transmitted, ACK received +#define TW_MR_SLA_NACK 0x48 // SLA+R transmitted, NACK received +#define TW_MR_DATA_ACK 0x50 // Data received, ACK returned -// TODO: Error handling and recovery without watchdog timer. -// TODO: Implement I2C_vect ISR? This may not actually be +// TODO: Error handling and recovery besides watchdog timer. +// 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. -static void I2C_AHT20_Reset(void); -static bool I2C_AHT20_IsCalibrated(void); -static void I2C_AHT20_Calibrate(void); -static void I2C_AHT20_Trigger(void); +static void I2C_AHT20_Reset(void); +static bool I2C_AHT20_IsCalibrated(void); +static void I2C_AHT20_Calibrate(void); +static void I2C_AHT20_Trigger(void); -static int I2C_ADS1115_WriteRegister(byte reg, word data); -static word I2C_ADS1115_ReadRegister(byte reg); +static int I2C_ADS1115_WriteRegister(byte reg, word data); +static word I2C_ADS1115_ReadRegister(byte reg); int I2C_Init(void) { @@ -516,13 +517,13 @@ word I2C_ADS1115_Read(int channel) // 111 | AINP = AIN3, AINN = GND mux = 0; - if (channel == 0) + if (channel == ADS01) mux = 0x4000; // | 01000000 00000000 - else if (channel == 1) + else if (channel == ADS02) mux = 0x5000; // | 01010000 00000000 - else if (channel == 2) + else if (channel == ADS03) mux = 0x6000; // | 01100000 00000000 - else if (channel == 3) + else if (channel == ADS04) mux = 0x7000; // | 01110000 00000000 // Single-shot conversion, active low, continous mode diff --git a/src/bus/i2c.h b/src/bus/i2c.h index ce004c1..10f0dff 100644 --- a/src/bus/i2c.h +++ b/src/bus/i2c.h @@ -8,6 +8,12 @@ #define AHT02 0x1 // Middle #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_Start(byte addr, byte mode); int I2C_SetChannel(int channel); diff --git a/src/main.c b/src/main.c index d764866..49fb84f 100644 --- a/src/main.c +++ b/src/main.c @@ -16,7 +16,7 @@ // TODO: Write an improved command parser (low priority). // TODO: Proper error handling and recovery (after testing). -int Init(void) +static int Init(void) { // MOSFETS control things like the heating element // so they are the highest priority to initialize @@ -94,7 +94,7 @@ int Init(void) return 0; } -void Update(void) +static void Update(void) { float temp[3], rh[3]; short adc[4]; @@ -110,10 +110,10 @@ void Update(void) I2C_SetChannel(AHT03); I2C_AHT20_Read(&temp[2], &rh[2]); - adc[0] = I2C_ADS1115_Read(0); - adc[1] = I2C_ADS1115_Read(1); - adc[2] = I2C_ADS1115_Read(2); - adc[3] = I2C_ADS1115_Read(3); + adc[0] = I2C_ADS1115_Read(ADS01); + adc[1] = I2C_ADS1115_Read(ADS02); + adc[2] = I2C_ADS1115_Read(ADS03); + adc[3] = I2C_ADS1115_Read(ADS04); Info("TEMP0=%.2fC, RH0=%.2f%%", temp[0], rh[0]); Info("TEMP1=%.2fC, RH1=%.2f%%", temp[1], rh[1]);