i broke everything whaaat
This commit is contained in:
@@ -400,7 +400,9 @@ send stop : 1011000 TW_MR_DATA_NACK -> Data recei
|
||||
// TW_ST_SLA_ACK 0xA8 = 0b10101000 = 168 -> SLA+R received, ACK returned
|
||||
// TW_NO_INFO 0xF8 = 0b11111000 = 248 -> no state information available
|
||||
|
||||
#define SLAVE_ADDRESS 0x38 // doc AHT20, 7.3 : address of thermistor : 0b111000, 56
|
||||
#define AHT20_ADDRESS 0x38 // doc AHT20, 7.3 : address of thermistor : 0b111000, 56
|
||||
|
||||
// volatile uint8_t i2c_data = 0;
|
||||
|
||||
void print_hex_value(char c) {
|
||||
char buffer[3] = {0};
|
||||
@@ -429,8 +431,7 @@ void print_status_meaning(uint16_t status) {
|
||||
}
|
||||
}
|
||||
|
||||
void print_status(char *str) {
|
||||
return;
|
||||
void _print_status(char *str) {
|
||||
uint8_t status;
|
||||
status = TWSR & 0b11111000; // Table 22-2. Status codes for Master Transmitter Mode
|
||||
uint16_t size = 53;
|
||||
@@ -445,16 +446,73 @@ void print_status(char *str) {
|
||||
print_status_meaning(status);
|
||||
uart_printstr_endl("");
|
||||
}
|
||||
void print_status_anyway(char *str) {
|
||||
_print_status(str);
|
||||
}
|
||||
void print_status(char *str) {
|
||||
_print_status(str);
|
||||
return;
|
||||
}
|
||||
|
||||
void print_data() {
|
||||
uint8_t data;
|
||||
for (uint8_t i = 0; i < 10; i++) {
|
||||
data = i2c_read_nack();
|
||||
print_hex_value(data);
|
||||
if (i == 0) {
|
||||
uart_tx('(');
|
||||
uart_printstr_itoa_base(data, 2);
|
||||
uart_tx(')');
|
||||
}
|
||||
if (i < 9) {
|
||||
uart_tx(' ');
|
||||
}
|
||||
}
|
||||
uart_printstr_endl("");
|
||||
}
|
||||
|
||||
Boolean aht20_is_status_ready(void) {
|
||||
uint8_t status;
|
||||
|
||||
i2c_start();
|
||||
i2c_send_addr_w(AHT20_ADDRESS);
|
||||
// i2c_write(0xAC);
|
||||
// i2c_write(0x33);
|
||||
// i2c_write(0x00);
|
||||
i2c_start_repeat();
|
||||
i2c_send_addr_r(AHT20_ADDRESS);
|
||||
status = i2c_read_nack();
|
||||
i2c_stop();
|
||||
|
||||
uart_printstr("- status : ");
|
||||
uart_printstr_itoa_base_endl(status, 2);
|
||||
if (status & 0b10000000) {
|
||||
return TRUE;
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void aht20_wait_for_status() {
|
||||
for (uint8_t i = 0; i < 30; i++) {
|
||||
uart_tx('|');
|
||||
if (aht20_is_status_ready()) {
|
||||
break;
|
||||
}
|
||||
_delay_ms(10);
|
||||
}
|
||||
}
|
||||
|
||||
// description
|
||||
int main() {
|
||||
uart_init();
|
||||
|
||||
// 0x18 : 11000 : 24
|
||||
// 0x33 : 110011 : 51 -> DATA0
|
||||
// 0x38 : 111000 : 56
|
||||
// 0x70 : 1110000 : 112 -> SLA+W
|
||||
// 0x71 : 1110001 : 113 -> SLA+R
|
||||
// 0x18 : 11000 : 24
|
||||
// 0x33 : 110011 : 51 -> DATA0
|
||||
// 0x38 : 111000 : 56
|
||||
// 0x70 : 1110000 : 112 -> SLA+W
|
||||
// 0x71 : 1110001 : 113 -> SLA+R
|
||||
// 0x80 : 10000000 : 128
|
||||
//
|
||||
// doc AHT20 7.4 : trigger measurement data and read temperature and humidity
|
||||
// 0. init i2c
|
||||
@@ -477,17 +535,19 @@ int main() {
|
||||
i2c_start();
|
||||
print_status(" after start");
|
||||
|
||||
// print_status("\r\nwaiting 200ms...");
|
||||
// _delay_ms(200);
|
||||
// print_status(" done waiting");
|
||||
// aht20_wait_for_status();
|
||||
|
||||
print_status("\r\nwaiting 200ms...");
|
||||
_delay_ms(200);
|
||||
print_status(" done waiting");
|
||||
|
||||
print_status("\r\nsend SLA+W");
|
||||
i2c_send_addr_w(SLAVE_ADDRESS);
|
||||
i2c_send_addr_w(AHT20_ADDRESS);
|
||||
print_status(" after SLA+W");
|
||||
|
||||
// print_status("\r\nwaiting 10ms...");
|
||||
// _delay_ms(10);
|
||||
// print_status(" done waiting");
|
||||
print_status("\r\nwaiting 20ms...");
|
||||
_delay_ms(20);
|
||||
print_status(" done waiting");
|
||||
|
||||
print_status("\r\nsend 0xAC (trigger measurement)");
|
||||
i2c_write(0xAC);
|
||||
@@ -499,29 +559,27 @@ int main() {
|
||||
i2c_write(0x00);
|
||||
print_status(" after 0x00");
|
||||
|
||||
// print_status("\r\nwaiting 10ms...");
|
||||
// _delay_ms(10);
|
||||
// print_status(" done waiting");
|
||||
|
||||
print_status("\r\nsend repeat start");
|
||||
i2c_start_repeat();
|
||||
print_status(" after repeat start");
|
||||
|
||||
print_status("\r\nsend SLA+R");
|
||||
i2c_send_addr_r(SLAVE_ADDRESS);
|
||||
print_status(" after SLA+R");
|
||||
;
|
||||
print_status("\r\nbefore read data");
|
||||
uint8_t data;
|
||||
for (uint8_t i = 0; i < 10; i++) {
|
||||
data = i2c_read();
|
||||
print_hex_value(data);
|
||||
if (i < 9) {
|
||||
uart_tx(' ');
|
||||
}
|
||||
// print_status("\r\nwaiting 100ms...");
|
||||
// _delay_ms(100);
|
||||
// print_status(" done waiting");
|
||||
|
||||
// aht20_wait_for_status();
|
||||
|
||||
print_status("\r\nsend repeat start");
|
||||
i2c_start_repeat();
|
||||
print_status(" after repeat start");
|
||||
|
||||
print_status("\r\nsend SLA+R");
|
||||
i2c_send_addr_r(AHT20_ADDRESS);
|
||||
print_status_anyway(" after SLA+R");
|
||||
|
||||
print_status("\r\nbefore read data");
|
||||
print_data();
|
||||
print_status(" after read data");
|
||||
|
||||
_delay_ms(300);
|
||||
}
|
||||
uart_printstr_endl("");
|
||||
print_status(" after read data");
|
||||
|
||||
print_status("\r\nsend stop");
|
||||
i2c_stop();
|
||||
|
||||
Reference in New Issue
Block a user