Canbus
Dinoden
Posts: 19
Hi. I am looking for some help on a canbus project I am working on. All I would like to do is display my vehicle RPM on a lcd display. I am using the MCP2515 and the MPC2550 transceiver.
Nothing fancy. The code I have written just writes a $C9 to my display. Does not change as rpm's are increased. I believe I am reading the right buffer (RXB0). I am also using PID $0C (engine RPM).
Here is my sample program. Any help would be greatly appreciated. I've tried my best.
' {$STAMP BS2}
'All of these Mode changes occur by writing to Regsister $0F
'Same as a Reset Command, but written to CANCNTL register
'[ REQOP MODES ]
CONFIG CON $80 '%1000_0000
LISTEN CON $60 '%0110_0000
LOOP_BACK CON $40 '%0100_0000
SLEEPING CON $20 '%0010_0000
NORMAL CON $00 '%0000_0000
MASK CON $E0 '%1110_0000
'[ SPI Commands ]
SPI_RESET CON $C0 '%1100_0000
SPI_READ CON $03 '%0000_0011
SPI_WRITE CON $02 '%0000_0010
SPI_RD_RXBUF CON $90 '%1001_0000
SPI_LD_TXBUF CON $40 '%0100_0000
SPI_RTS CON $80 '%1000_0000
SPI_RTS_TXB0 CON $81 '%1000_0001
SPI_RTS_TXB1 CON $82 '%1000_0010
SPI_RTS_TXB2 CON $84 '%1000_0100
SPI_RD_STATUS CON $A0 '%1010_0000
SPI_RX_STATUS CON $B0 '%1011_0000
SPI_BIT_MOD CON $05 '%0000_0101
'************************ VARIABLES ***************************
CAN_DATA VAR Byte
'****************** RESET_CAN / INITIALIZE ********************
CAN_START:
LOW 11 'CS SHIFTOUT = PIN,CLK,MSBFIRST
SHIFTOUT 9, 10, 1, [SPI_RESET]
HIGH 11
PAUSE 10
'LOW 11 'CS
'SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,CONFIG] 'RESET DOES SAME
'HIGH 11
'PAUSE 10
' V1
LOW 11 'CS ' 250kB 500kB 250kB
SHIFTOUT 9, 10, 1, [SPI_WRITE,$28,$04] 'CNF3_REG $04 $04 [$02]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$29,$E4] 'CNF2_REG $E4 $E4 [$90]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2A,$01] 'CNF1_REG $01 $00 [$03]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$30,$03] 'TXB0CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$40,$03] 'TXB1CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$50,$03] 'TXB2CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0D,$00] 'TXRTSCTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2B,$81] 'CANINTE_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$00] 'CANINTF_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$FF] 'BFPCTRL_REG
HIGH 11
PAUSE 10
'********************* RXB0 MASK/FILTERS **********************
LOW 11 'V1 V2
SHIFTOUT 9, 10, 1, [SPI_WRITE,$20,$FF] 'RXM0SIDH_REG $00 $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$21,$FF] 'RXM0SIDL_REG $00 $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$00,$0C] 'RXF0SIDH_REG $00 $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$01,$0C] 'RXF0SIDL_REG $00 $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$04,$0C] 'RXF1SIDH_REG $FF $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$05,$0C] 'RXF1SIDL_REG $FF $0C
HIGH 11
PAUSE 10
'********************** RXB1 MASK/FILTERS *********************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$24,$FF] 'RXM1SIDH_REG $FF $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$25,$FF] 'RXM1SIDL_REG $FF $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$08,$00] 'RXF2SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$09,$00] 'RXF2SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$10,$00] 'RXF3SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$11,$00] 'RXF3SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$14,$00] 'RXF4SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$15,$00] 'RXF4SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$18,$00] 'RXF5SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$19,$00] 'RXF5SIDL_REG $FF $00
HIGH 11
PAUSE 10
'****************** MODE SELECT / EXIT CONFIG ****************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,$00] 'LISTEN $60 / NORMAL $00
HIGH 11
PAUSE 10
'************************** READ BUS **************************
READ_BUS:
LOW 11
'SHIFTOUT 9, 10, 1, [SPI_READ] '$03
SHIFTOUT 9, 10, 1, [SPI_RD_RXBUF] 'RXBUF $90
HIGH 11
PAUSE 10
LOW 11
SHIFTIN 8, 10, 0, [CAN_DATA]
HIGH 11
PAUSE 100
SEROUT 7,16468,[254,128]
SEROUT 7,16468,[" CAN DATA "]
SEROUT 7,16468,[254,192]
SEROUT 7,16468, [" ",DEC2 CAN_DATA," "]
'**************** ENTER CONFIG TO CLR CANINTF.RXnIF ***********
LOW 11 'CS
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,CONFIG]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$00] 'CANINTF_REG CLR RX0IF / RX1IF
HIGH 11
PAUSE 10
'****************** MODE SELECT / EXIT CONFIG ****************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,$00] 'LISTEN $60 / NORMAL $00
HIGH 11
PAUSE 10
GOTO READ_BUS
Thanks again.
Nothing fancy. The code I have written just writes a $C9 to my display. Does not change as rpm's are increased. I believe I am reading the right buffer (RXB0). I am also using PID $0C (engine RPM).
Here is my sample program. Any help would be greatly appreciated. I've tried my best.
' {$STAMP BS2}
'All of these Mode changes occur by writing to Regsister $0F
'Same as a Reset Command, but written to CANCNTL register
'[ REQOP MODES ]
CONFIG CON $80 '%1000_0000
LISTEN CON $60 '%0110_0000
LOOP_BACK CON $40 '%0100_0000
SLEEPING CON $20 '%0010_0000
NORMAL CON $00 '%0000_0000
MASK CON $E0 '%1110_0000
'[ SPI Commands ]
SPI_RESET CON $C0 '%1100_0000
SPI_READ CON $03 '%0000_0011
SPI_WRITE CON $02 '%0000_0010
SPI_RD_RXBUF CON $90 '%1001_0000
SPI_LD_TXBUF CON $40 '%0100_0000
SPI_RTS CON $80 '%1000_0000
SPI_RTS_TXB0 CON $81 '%1000_0001
SPI_RTS_TXB1 CON $82 '%1000_0010
SPI_RTS_TXB2 CON $84 '%1000_0100
SPI_RD_STATUS CON $A0 '%1010_0000
SPI_RX_STATUS CON $B0 '%1011_0000
SPI_BIT_MOD CON $05 '%0000_0101
'************************ VARIABLES ***************************
CAN_DATA VAR Byte
'****************** RESET_CAN / INITIALIZE ********************
CAN_START:
LOW 11 'CS SHIFTOUT = PIN,CLK,MSBFIRST
SHIFTOUT 9, 10, 1, [SPI_RESET]
HIGH 11
PAUSE 10
'LOW 11 'CS
'SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,CONFIG] 'RESET DOES SAME
'HIGH 11
'PAUSE 10
' V1
LOW 11 'CS ' 250kB 500kB 250kB
SHIFTOUT 9, 10, 1, [SPI_WRITE,$28,$04] 'CNF3_REG $04 $04 [$02]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$29,$E4] 'CNF2_REG $E4 $E4 [$90]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2A,$01] 'CNF1_REG $01 $00 [$03]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$30,$03] 'TXB0CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$40,$03] 'TXB1CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$50,$03] 'TXB2CTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0D,$00] 'TXRTSCTRL_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2B,$81] 'CANINTE_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$00] 'CANINTF_REG
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$FF] 'BFPCTRL_REG
HIGH 11
PAUSE 10
'********************* RXB0 MASK/FILTERS **********************
LOW 11 'V1 V2
SHIFTOUT 9, 10, 1, [SPI_WRITE,$20,$FF] 'RXM0SIDH_REG $00 $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$21,$FF] 'RXM0SIDL_REG $00 $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$00,$0C] 'RXF0SIDH_REG $00 $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$01,$0C] 'RXF0SIDL_REG $00 $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$04,$0C] 'RXF1SIDH_REG $FF $0C
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$05,$0C] 'RXF1SIDL_REG $FF $0C
HIGH 11
PAUSE 10
'********************** RXB1 MASK/FILTERS *********************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$24,$FF] 'RXM1SIDH_REG $FF $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$25,$FF] 'RXM1SIDL_REG $FF $FF
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$08,$00] 'RXF2SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$09,$00] 'RXF2SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$10,$00] 'RXF3SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$11,$00] 'RXF3SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$14,$00] 'RXF4SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$15,$00] 'RXF4SIDL_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$18,$00] 'RXF5SIDH_REG $FF $00
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$19,$00] 'RXF5SIDL_REG $FF $00
HIGH 11
PAUSE 10
'****************** MODE SELECT / EXIT CONFIG ****************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,$00] 'LISTEN $60 / NORMAL $00
HIGH 11
PAUSE 10
'************************** READ BUS **************************
READ_BUS:
LOW 11
'SHIFTOUT 9, 10, 1, [SPI_READ] '$03
SHIFTOUT 9, 10, 1, [SPI_RD_RXBUF] 'RXBUF $90
HIGH 11
PAUSE 10
LOW 11
SHIFTIN 8, 10, 0, [CAN_DATA]
HIGH 11
PAUSE 100
SEROUT 7,16468,[254,128]
SEROUT 7,16468,[" CAN DATA "]
SEROUT 7,16468,[254,192]
SEROUT 7,16468, [" ",DEC2 CAN_DATA," "]
'**************** ENTER CONFIG TO CLR CANINTF.RXnIF ***********
LOW 11 'CS
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,CONFIG]
HIGH 11
PAUSE 10
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$2C,$00] 'CANINTF_REG CLR RX0IF / RX1IF
HIGH 11
PAUSE 10
'****************** MODE SELECT / EXIT CONFIG ****************
LOW 11
SHIFTOUT 9, 10, 1, [SPI_WRITE,$0F,$00] 'LISTEN $60 / NORMAL $00
HIGH 11
PAUSE 10
GOTO READ_BUS
Thanks again.