Shop OBEX P1 Docs P2 Docs Learn Events
SPI write and then verify is not working for me. — Parallax Forums

SPI write and then verify is not working for me.

LoopyBytelooseLoopyByteloose Posts: 12,537
edited 2006-08-04 08:51 in BASIC Stamp
I am trying to Write to a CANbus interface via SPI·with a BS2p24.
The purpose is to intialize its registers for normal operation.

For some unknown ·reason, when I write a value into the device's registers and follow with a read of the same register, I get a different value.· The different value is always consistent unless I change what I am writing.· So I presume that there is a timing problem in the change from SHIFTOUT to SHIFTIN.

Can anyone point out my mistake?
I suspect that I am dropping a clock pulse in some instance.
Some values come back consistently correct and other values come back consistently [noparse][[/noparse]always the same wrong answer] wrong.

I suppose I could test the code on an SPI EEPROM to see if the problem occurs there to verify software, but I would have to reconfigure my breadboard.


' {$STAMP BS2p}
' {$PBASIC 2.5}
·'***************************************
'
'···· MCP2515 Second Test Program - single test
'
'······ 8/2/2006·· V 0.50
'?????????????????????????????????????????????????
' NOTE- CURRENTLY RETURNING WRONG VALUES.·· /
'· HAVE NO IDEA WHY THIS IS SO.· Maybe Wiring·· /
'?????????????????????????????????????????????????
'···· Author: George Herzog
'
'···· Purpose: To verify the SPI interface is functional by
'···· Writing and then verifying values to SPECIFIC registers
'···· in the MCP2515
'
''***************************************
'
' Pin Assignments
'
PINcs·· PIN 15······· 'Chip Select Pin
PINdata PIN 14······· 'SPI combined data I/O
PINclk· PIN 13········ 'SPI clock
'
'
' SPI command codes
'
RESET······· CON· $C0
READspi····· CON· $03
WRITEspi···· CON· $02
'
'
'· VARIABLES
'
result···· VAR···· Byte
'
'TESTREGISTERS
'
'REGISTER $00
'
'·· WRITE· Register $00
'
· LOW PINcs
··· SHIFTOUT PINdata, PINclk, 1, [noparse][[/noparse]WRITEspi]
··· SHIFTOUT PINdata, PINclk, 1, [noparse][[/noparse]$00]
··· SHIFTOUT PINdata, PINclk, 1, [noparse][[/noparse]$0a]··· 'Write $0a
· HIGH PINcs
'
'·· READ· Register $00
'
· LOW PINcs
··· SHIFTOUT PINdata, PINclk, 1, [noparse][[/noparse]READspi]
··· SHIFTOUT PINdata, PINclk, 1, [noparse][[/noparse]$00]
'
'·· CHANGE from SPI output to input while PINcs remains LOW
'
··· SHIFTIN PINdata, PINclk, 0, [noparse][[/noparse]result]
· HIGH PINcs
'
· DEBUG " Reg $00 WROTE $0a >>> TEST VALUE = ",· IHEX2 result, CR······ 'Verify ·$0A


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If you want more fiber, eat the package.· Not enough?· Eat the manual."········
···················· Tropical regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan

Comments

  • JimAvantiJimAvanti Posts: 15
    edited 2006-08-03 15:26
    I tried your code on my test board I built and it is working.· Of course I had to change the PIN assignments to match my board.

    Results:

    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A
    ·Reg $00 WROTE $0a >>> TEST VALUE = $0A





    CANTX········ PIN 13······················· 'CAN Transmit
    CANRX········ PIN 12······················· 'CAN Receive
    CANCS········ PIN 11······················· 'CAN Chip Select
    CANCLK······· PIN 10······················· 'CAN Clock

    CANData····· VAR Byte····

    TestCAN:
    ·· LOW CANcs
    ··· SHIFTOUT CANTX, CANCLK, 1, [noparse][[/noparse]WRITEspi]···· 'WRITE· Register $00
    ··· SHIFTOUT CANTX, CANCLK, 1, [noparse][[/noparse]$00]
    ··· SHIFTOUT CANTX, CANCLK, 1, [noparse][[/noparse]$0a]············ 'Write $0a
    ·· HIGH CANCS
    ·· LOW CANCS
    ··· SHIFTOUT CANTX, CANCLK, 1, [noparse][[/noparse]READspi]
    ··· SHIFTOUT CANTX, CANCLK, 1, [noparse][[/noparse]$00]
    ··· SHIFTIN CANRX, CANCLK, 0, [noparse][[/noparse]CANData]······· 'CHANGE from SPI output to input while CANCS remains LOW
    ·· HIGH CANCS
    ·· DEBUG " Reg $00 WROTE $0a >>> TEST VALUE = ",· IHEX2 CANData, CR······ 'Verify· $0A
    GOTO TestCAN
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-08-03 18:13
    Jim,
    Try the code with some other values. Like $C0, $AA, and $BD.

    If you get consistent results, I may have a bad chip.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If you want more fiber, eat the package.· Not enough?· Eat the manual."········
    ···················· Tropical regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-08-03 18:26
    Jim,
    I see you went with the separate Input and Output Pins.

    The board I am presently using has that darn'd combined I/O with a 4.7K resistor.
    It may be inserting a timing delay.

    I have my own board coming from the fabricators that is like your set up.
    I may have to wait for that in order to have something that functions right.

    Can anyone who has seen a similar problem lend advice?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If you want more fiber, eat the package.· Not enough?· Eat the manual."········
    ···················· Tropical regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • JimAvantiJimAvanti Posts: 15
    edited 2006-08-03 20:08
    I just tried $C0, $AA, and $BD and in all cases I got back the correct result.
  • JimAvantiJimAvanti Posts: 15
    edited 2006-08-03 20:18
    I tried·writing random numbers and the results where good.· Eventually I will try combining the Input and output together with the 4.7k resistor like you did and see if the results are still good.

    Debug Results:

    Reg $00 WROTE $21 >>> TEST VALUE = $21
    ·Reg $00 WROTE $32 >>> TEST VALUE = $32
    ·Reg $00 WROTE $D8 >>> TEST VALUE = $D8
    ·Reg $00 WROTE $06 >>> TEST VALUE = $06
    ·Reg $00 WROTE $5D >>> TEST VALUE = $5D
    ·Reg $00 WROTE $F5 >>> TEST VALUE = $F5
    ·Reg $00 WROTE $8F >>> TEST VALUE = $8F
    ·Reg $00 WROTE $15 >>> TEST VALUE = $15
    ·Reg $00 WROTE $B7 >>> TEST VALUE = $B7
    ·Reg $00 WROTE $2B >>> TEST VALUE = $2B
    ·Reg $00 WROTE $D4 >>> TEST VALUE = $D4
    ·Reg $00 WROTE $BD >>> TEST VALUE = $BD
    ·Reg $00 WROTE $CD >>> TEST VALUE = $CD
    ·Reg $00 WROTE $B1 >>> TEST VALUE = $B1
    ·Reg $00 WROTE $76 >>> TEST VALUE = $76
    ·Reg $00 WROTE $21 >>> TEST VALUE = $21
    ·Reg $00 WROTE $32 >>> TEST VALUE = $32
    ·Reg $00 WROTE $D8 >>> TEST VALUE = $D8
    ·Reg $00 WROTE $06 >>> TEST VALUE = $06
    ·Reg $00 WROTE $5D >>> TEST VALUE = $5D
    ·Reg $00 WROTE $F5 >>> TEST VALUE = $F5
    ·Reg $00 WROTE $8F >>> TEST VALUE = $8F
    ·Reg $00 WROTE $15 >>> TEST VALUE = $15
    ·Reg $00 WROTE $B7 >>> TEST VALUE = $B7



    · char = 100

    TestCAN:
    ·· RANDOM char
    ·· LOW CANcs
    ··· SHIFTOUT CANTX, CANCLK, MSBFIRST, [noparse][[/noparse]WRITEspi]·· 'WRITE· Register $00
    ··· SHIFTOUT CANTX, CANCLK, MSBFIRST, [noparse][[/noparse]$00]
    ··· SHIFTOUT CANTX, CANCLK, MSBFIRST, [noparse][[/noparse]char]
    ·· HIGH CANCS

    ·· LOW CANCS
    ··· SHIFTOUT CANTX, CANCLK, MSBFIRST, [noparse][[/noparse]READspi]
    ··· SHIFTOUT CANTX, CANCLK, MSBFIRST, [noparse][[/noparse]$00]
    ··· SHIFTIN CANRX, CANCLK, MSBPRE, [noparse][[/noparse]CANData]···· 'CHANGE SPI Out to In while CANCS remains LOW
    ·· HIGH CANCS
    ·· DEBUG " Reg $00 WROTE ", IHEX2 char, " >>> TEST VALUE = ",· IHEX2 CANData, CR······ 'Verify Result
    GOTO TestCAN
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-08-04 08:51
    Jim,
    I will send you my program in a PM.
    I am definitely getting strange results for anything above $7F.

    I will have to swap out my MCP2515 chip as I suspect it is defective.
    I still have not ruled out that the combined I/O scheme is to blame.

    None the less, I am pleased to see you appear completely functional.

    You might try writing $80 to the $0e and $0f registers. These are the CANSTAT and CANCNTRL.

    In $0e, I get a reply of $01 AND in $0f, I get a reply of $00.

    The Microchip PDF says that ALL the registers may be written to after a reset because you are in Configure mode until you change CANCNTRL. CANCNTRL must be %1000 0??? for this mode, but I seem to not be getting there.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If you want more fiber, eat the package.· Not enough?· Eat the manual."········
    ···················· Tropical regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan

    Post Edited (Kramer) : 8/4/2006 3:26:26 PM GMT
Sign In or Register to comment.