Mikroelektronika MIKROE-350 Fiche De Données

Page de 526
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.
program PortExpander
' Port Expander module connections
dim SPExpanderRST as sbit at PORTB.B0
SPExpanderCS  
as sbit at PORTB.B1
SPExpanderRST_Direction 
as sbit at DDRB.B0
SPExpanderCS_Direction  
as sbit at DDRB.B1
' End Port Expander module connections
dim counter as byte' = 0
main:
counter = 0
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
sub 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
sub 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
TRUE                        ' Endless loop
Expander_Write_PortA(0, counter)   ' Write i to expander"s PORTA
Inc(counter)
PORTC = Expander_Read_PortB(0)     ' Read expander"s PORTB and
write it to LEDs
Delay_ms(100)
wend
end.
300
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6