Handle I2C multiplexer channel selection
This commit is contained in:
65
src/main.c
65
src/main.c
@@ -3,26 +3,67 @@
|
||||
#include "bus/twi.h"
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
#define Sleep(ms) _delay_ms(ms)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(void)
|
||||
{
|
||||
USART_Init();
|
||||
TWI_Init();
|
||||
|
||||
sei();
|
||||
|
||||
Info("Initializing...");
|
||||
Sleep(500);
|
||||
char crb;
|
||||
|
||||
// ADS1115 (ADC)
|
||||
// PCA9546 (Multiplexer TWI)
|
||||
// ATH20 (3x)
|
||||
// BMP280 (3x)
|
||||
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
USART_Init();
|
||||
TWI_Init();
|
||||
|
||||
sei();
|
||||
|
||||
Info("Initializing...");
|
||||
|
||||
// PCA9546 I2C Multiplexer
|
||||
// =======================
|
||||
|
||||
TWI_Stop();
|
||||
|
||||
// Excerpts taken from the PCA9546A datasheet:
|
||||
// https://www.ti.com/lit/ds/symlink/pca9546a.pdf
|
||||
|
||||
// Following a start condition, the bus master
|
||||
// must output the address of the slave it is
|
||||
// accessing.
|
||||
|
||||
// 7 6 5 4 | 3 2 1 0
|
||||
// 1 1 1 0 | A2 A1 A0 RW
|
||||
// FIXED_____ | SELECTABLE____
|
||||
|
||||
TWI_Start(0x70, 0);
|
||||
TWI_Wait_ACK();
|
||||
|
||||
// Following the successful acknowledgment of
|
||||
// the slave address, the bus master sends a byte
|
||||
// which is stored in the control register.
|
||||
|
||||
// 7 6 5 4 | 3 2 1 0
|
||||
// X X X X | B3 B2 B1 B0
|
||||
// | CHANNEL_______
|
||||
|
||||
crb = 0x01; // Channel 0 | 00000001
|
||||
// = 0x02 // Channel 1 | 00000010
|
||||
// = 0x04 // Channel 2 | 00000100
|
||||
// = 0x08 // Channel 3 | 00001000
|
||||
|
||||
TWI_Write(crb);
|
||||
|
||||
// When a channel is selected, the channel
|
||||
// becomes active after a stop condition has been
|
||||
// placed on the I2C bus. A stop condition always
|
||||
// must occur right after the acknowledge cycle.
|
||||
|
||||
TWI_Wait_ACK();
|
||||
TWI_Stop();
|
||||
|
||||
Info("Switched to TWI channel 0.");
|
||||
Sleep(500);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user