User manual

Table Of Contents
394
mikoC PRO for PIC32
MikroElektronika
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.
Copy Code To Clipboard
// Port Expander module connections
sbit SPExpanderRST at LATF0_bit;
sbit SPExpanderCS at LATF1_bit;
sbit SPExpanderRST_Direction at TRISF0_bit;
sbit SPExpanderCS_Direction at TRISF1_bit;
// End Port Expander module connections
unsigned int i = 0;
void main() {
CHECON = 0x32;
AD1PCFG = 0xFFFF; // Congure AN pins as digital
TRISB = 0x00;
LATB = 0xFF;
// If Port Expander Library uses SPI2 module
SPI2_Init_Advanced(_SPI_MASTER,_SPI_8_BIT, 4, _SPI_SS_DISABLE,_SPI_DATA_SAMPLE_
MIDDLE,_SPI_CLK_IDLE_LOW,_SPI_ACTIVE_2_IDLE);
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
PORTB = Expander_Read_PortB(0); // Read expander’s PORTB and write it to LEDs
Delay_ms(75);
}
}