Read and translate AHT20 sensor readings
This commit is contained in:
176
src/main.c
176
src/main.c
@@ -6,137 +6,14 @@
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
short TWI_ReadAHT20(void)
|
||||
{
|
||||
unsigned char data, crc;
|
||||
// TODO: Get readings from ADC ADS1115 over TWI.
|
||||
// TODO: Implement optional sensor value check with CRC8.
|
||||
// TODO: Either implement software PWM for the FAN03 timer
|
||||
// (which will be quite complicated) or pick a chip with
|
||||
// more than two 16-bit PWM outputs like the ATmega328PB.
|
||||
// https://www.mikrocontroller.net/articles/Soft-PWM
|
||||
|
||||
// After the transmission is initiated, the first
|
||||
// byte of the subsequent I2C transmission includes
|
||||
// the 7-bit I2C device address 0x38 and a SDA
|
||||
// direction bit.
|
||||
|
||||
// Soft reset device.
|
||||
|
||||
TWI_Start(0x38, 0);
|
||||
TWI_Write(0xBA);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Stop();
|
||||
|
||||
// Wait 40ms after power-on.
|
||||
|
||||
Sleep(40);
|
||||
|
||||
// Before reading the temperature and humidity
|
||||
// values, first check whether the calibration
|
||||
// enable bit Bit [3] of the status word is 1 (you
|
||||
// can get a byte of status word by sending 0x71).
|
||||
|
||||
TWI_Start(0x38, 0);
|
||||
TWI_Write(0x71);
|
||||
TWI_Wait_ACK();
|
||||
|
||||
data = TWI_Read_NACK();
|
||||
TWI_Stop();
|
||||
|
||||
Info("Received calibration status %02X.", data);
|
||||
|
||||
// If not 1, need to send 0xBE command (for
|
||||
// initialization), this command parameter has two
|
||||
// bytes, the first byte is 0x08, the second byte
|
||||
// is 0x00, and then wait for 10ms.
|
||||
|
||||
if (data & ~BIT(3)) {
|
||||
Info("Requesting calibration...");
|
||||
|
||||
TWI_Start(0x38, 0);
|
||||
TWI_Write(0xBE);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Write(0x08);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Write(0x00);
|
||||
TWI_Wait_ACK();
|
||||
|
||||
Sleep(10);
|
||||
data = TWI_Read_NACK();
|
||||
TWI_Stop();
|
||||
|
||||
if (data & ~BIT(3)) {
|
||||
Info("Error: Calibration failed.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Send the 0xAC command directly (trigger
|
||||
// measurement). The parameter of this command has
|
||||
// two bytes, the first byte is 0x33 and the second
|
||||
// byte is 0x00.
|
||||
|
||||
Info("Triggering measurement...");
|
||||
|
||||
TWI_Start(0x38, 0);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Write(0xAC);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Write(0x33);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Write(0x00);
|
||||
TWI_Wait_ACK();
|
||||
TWI_Stop();
|
||||
|
||||
// Wait for 80ms to wait for the measurement to be
|
||||
// completed. If the read status word Bit [7] is 0,
|
||||
// it indicates that the measurement is completed,
|
||||
// and six bytes can be read in a row. Otherwise
|
||||
// continue to wait.
|
||||
|
||||
Info("Reading measurement...");
|
||||
|
||||
TWI_Start(0x38, 1); // Read
|
||||
TWI_Wait_ACK();
|
||||
|
||||
do {
|
||||
Sleep(80);
|
||||
data = TWI_Read_ACK();
|
||||
} while (data & BIT(7));
|
||||
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
data = TWI_Read_ACK();
|
||||
Info("Received data byte %02X.", data);
|
||||
|
||||
// After receiving six bytes, the next byte is the
|
||||
// CRC check data, the user can read it as needed,
|
||||
// if the receiving end needs CRC check, then send
|
||||
// it after receiving the sixth byte ACK response,
|
||||
// otherwise NACK is sent out, CRC initial value is
|
||||
// 0xFF. The CRC8 check polynomial is:
|
||||
// CRC[7:0]= 1 + (x^4) + (x^5) + (x^8)
|
||||
|
||||
crc = TWI_Read_NACK();
|
||||
Info("Received CRC8 byte %02X.", crc);
|
||||
|
||||
// Calculate the temperature and humidity values.
|
||||
// Note: The calibration status check in the first
|
||||
// step only needs to be checked at power-on. No
|
||||
// operation is required during the normal
|
||||
// acquisition process.
|
||||
|
||||
// TODO: Calculate values
|
||||
|
||||
TWI_Stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int Init(void)
|
||||
{
|
||||
USART_Init();
|
||||
sei();
|
||||
@@ -154,22 +31,41 @@ int main(void)
|
||||
PWM_SetValue(FAN02, 50);
|
||||
PWM_SetValue(FAN03, 20);
|
||||
|
||||
// TODO: Set FAN03 timer frequency
|
||||
// TODO: Implement ADS1115 and AHT20
|
||||
TWI_SetChannel(AHT01);
|
||||
TWI_AHT20_Init();
|
||||
|
||||
// TEM01, TEM02, TEM03
|
||||
// HUM01, HUM02, HUM03
|
||||
TWI_SetChannel(AHT02);
|
||||
TWI_AHT20_Init();
|
||||
|
||||
// TWI_GetValue(TEM03)
|
||||
// TWI_GetValue(HUM03)
|
||||
TWI_SetChannel(AHT03);
|
||||
TWI_AHT20_Init();
|
||||
|
||||
TWI_SetChannel(AHT01); // I2C Mux
|
||||
TWI_ReadAHT20(); // TEMP and RH
|
||||
return 0;
|
||||
}
|
||||
|
||||
Info("Running idle loop...");
|
||||
void Update(void)
|
||||
{
|
||||
float temp, rhum;
|
||||
|
||||
TWI_SetChannel(AHT01);
|
||||
TWI_AHT20_Read(&temp, &rhum);
|
||||
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
|
||||
|
||||
TWI_SetChannel(AHT02);
|
||||
TWI_AHT20_Read(&temp, &rhum);
|
||||
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
|
||||
|
||||
TWI_SetChannel(AHT03);
|
||||
TWI_AHT20_Read(&temp, &rhum);
|
||||
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Init();
|
||||
|
||||
for (;;) {
|
||||
// Info("PING");
|
||||
Update();
|
||||
Sleep(1000);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user