Datasheet
Library Example
The example demonstrates how to communicate with Port Expander MCP23S17.
Note that Port Expander pins A2 A1 A0 are connected to GND so Port Expander
Hardware Address is 0.
// Port Expander module connections
sbit SPExpanderRST at PORTB.B0;
sbit SPExpanderCS at PORTB.B1;
sbit SPExpanderRST_Direction at DDRB.B0;
sbit SPExpanderCS_Direction at DDRB.B1;
// End Port Expander module connections
// Pointer to appropriate SPI Read function
char (*SPI_Rd_Ptr)(char);
unsigned char i = 0;
void main() {
DDRC = 0xFF; // Set PORTC as output
// If Port Expander Library uses SPI1 module
SPI1_Init(); // Initialize SPI module used with PortExpander
SPI_Rd_Ptr = &SPI1_Read; // Pass pointer to SPI Read function
of used SPI module
// // If Port Expander Library uses SPI2 module
// SPI2_Init(); // Initialize SPI module used with PortExpander
// SPI_Rd_Ptr = &SPI2_Read; // Pass pointer to SPI Read function
of used SPI module
Expander_Init(0); // Initialize Port Expander
Expander_Set_DirectionPortA(0, 0x00); // Set Expander's PORTA to
be output
Expander_Set_DirectionPortB(0,0xFF); // Set Expander's PORTB to
be input
Expander_Set_PullUpsPortB(0,0xFF); // Set pull-ups to all of
the Expander's PORTB pins
while(1) { // Endless loop
Expander_Write_PortA(0, i++); // Write i to expander's PORTA
PORTC = Expander_Read_PortB(0); // Read expander's PORTB
and write it to LEDs
Delay_ms(100);
}
}
364
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6