User manual

426
mikoBasic PRO for dsPIC30/33 and PIC24
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
program PortExpander
‘ Port Expander module connections
dim SPExpanderRST as sbit at LATF0_bit
SPExpanderCS as sbit at LATF1_bit
SPExpanderRST_Direction as sbit at TRISF0_bit
SPExpanderCS_Direction as sbit at TRISF1_bit
‘ End Port Expander module connections
dim counter as word
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 ( TRUE ) ‘ Endless loop
Expander_Write_PortA(0, counter) ‘ Write i to expander’s PORTA
Inc(counter)
PORTB = Expander_Read_PortB(0) ‘ Read expander’s PORTB and write it to LEDs
Delay_ms(100)
wend
end.