User manual

450
mikoC PRO for dsPIC
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 RF0_bit;
sbit SPExpanderCS at RF1_bit;
sbit SPExpanderRST_Direction at TRISF0_bit;
sbit SPExpanderCS_Direction at TRISF1_bit;
// End Port Expander module connections
unsigned int i = 0;
void main() {
ADPCFG = 0xFFFF; // initialize AN pins as digital
TRISB = 0x00;
LATB = 0xFF;
// If Port Expander Library uses SPI1 module
SPI1_Init(); // Initialize SPI module used with
PortExpander
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(100);
}
}