Im having trouble getting this to work. my circuit is the same as Gadelhas has in his schematic, except im not grabbing any inputs with my circuit and i'm using a different PIC . The way I have it coded is so that I can use it with a 16F648A w/ 4Mhz internal OSC and I think it should light each led on the MCP23S17s PORTA one at a time before reseting at the end of the string and repeating, but its not working in that the leds never light. I have an LED on PORTB.4 as kind of a watch dog blinky and its behaving as I would expect. Any ideas on whats wrong with my version of Gadelhas' code or something else I'm missing?
Code:
' CONFIGS
' ================================================================
@ DEVICE PIC16F648A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF, PROTECT_OFF, BOD_OFF, LVP_OFF
' DEFINES & INCLUDES
' ================================================================
DEFINE OSC 4
include "modedefs.bas"
' REGISTERS AND PINOUT ( 1 = IN; 0 = OUT )
' ================================================================
'76543210
CMCON = 7 ' Turn off un-needed PIC hardware
VRCON = 0 ' '' ''
CCP1CON = 0 ' '' ''
INTCON = %00000000 ' '' ''
' ALIAS & MODIFIERS
' ================================================================
nCS VAR PORTA.0
nRST VAR PORTB.5
SCLK VAR PORTB.0
SDO VAR PORTB.3
' VARIABLES & COSTANTS
' ================================================================
MCPWRT CON $40 'Command o write in MCP23S17 Address $00
MCPRED CON $41 'Command o read from MCP23S17 Address $00
IOCON CON $0A 'Register address to MCP23S17 Config
IODIRA CON $00 'Register address to MCP23S17 PortA(1)
IODIRB CON $01 'Register address to MCP23S17 PortB(2)
GPIOA CON $12 'Register address to MCP23S17 GPIOA(1)
GPIOB CON $13 'Register address to MCP23S17 GPIOB(2)
OLATA CON $14 'Register address to MCP23S17 LATA(1)
OLATB CON $15 'Register address to MCP23S17 LATB(2)
DataOut VAR BYTE 'Variable for Data Byte to Send to the MCP23S17
DataIn VAR BYTE 'Variable for Data Byte to Receive from the MCP23S17
MCPReg VAR BYTE 'Variable for the Register to Send to the MCP23S17
' PROGRAM INIT
' ================================================================
clear
pause 100
nrst = 1
ncs = 1
sclk = 0
sdo = 0
pause 100
GOSUB INIT_MCP23S17 'Init the MCP23S17
' MAIN LOOP
' ================================================================
MAIN:
MCPReg=OLATA 'This light up the leds, connected in
DataOut=DataOut+1 'PortA(1) of the MCP23S17
if dataout >=8 then
dataout=0
GOSUB SEND_MCP23S17
gosub INIT_MCP23S17:
endif
GOSUB SEND_MCP23S17
' MCPReg = GPIOB 'This read the PortB(1) of the MCP23S17
' GOSUB RECEIVE_MCP23S17 'and show the result on the PORTB of the
' PORTB = DataIn 'PIC
PAUSE 500
if dataout = 0 then
high PORTB.4
pause 250
low PORTB.4
pause 250
endif
high PORTB.4
pause 500
low PORTB.4
GOTO MAIN
' SUB - ROTINES
' ================================================================
INIT_MCP23S17:
nRST = 0 'Reset the port expander
PAUSE 1
nRST = 1
MCPReg = IOCON 'Configures the MCP23S17
DataOut = $20
GOSUB SEND_MCP23S17
MCPReg = IODIRA 'Configures PortA(1) All Output
DataOut = $00
GOSUB SEND_MCP23S17
MCPReg = IODIRB 'Configures PortB(2) All Input
DataOut = $FF
GOSUB SEND_MCP23S17
RETURN
SEND_MCP23S17:
nCS = 0 'Enable the MCP23S17
SHIFTOUT SDO, SCLK, MSBFIRST, [MCPWRT,MCPReg,DataOut]
nCS = 1 'Disable the MCP23S17
RETURN
'RECEIVE_MCP23S17:
' nCS = 0 'Enable the MCP23S17
' SHIFTOUT SDO, SCLK, MSBFIRST, [MCPRED,MCPReg]
' SHIFTIN SDI, SCLK, MSBPRE, [DataIn]
' nCS = 1 'Disable the MCP23S17
'RETURN
END
Bookmarks