Picaxe 40X2 to MCP2515 Interfacing "Library"

TonyKramer

New Member
Hello!

For a project I've been working on, I've been using ten PicAxe 40X2s connected to each other over a CAN bus by the use of the MCP2515 and MCP2551 ICs. That being said, although I could find some references to the MCP2515 here and there, I never really found any concrete "Library" functions for it. So over the last few days, I've been working closely with the PicAxe Manual 2 and the MCP2515 datasheet to bring forth this long function. To be honest, I have not tested it yet, because I am looking at refining the code a large amount. The code as of now uses over 1000 bytes. Another thing that I wanted to clear up is the deference between Subroutines and the Macro command. Is there any memory/speed advantage of using one over the other?

I just wanted to put this code out there for further suggestions and also in case someone needs a base upon which to build there own code. I am going to heavily refine it and add other functions, so I will include that later.

Although I've been part of the forum for a fairly long time now, I've never posted anything, so I am sorry if I happen to mess up a bunch of the rules.

Thank you,
-Tony

View attachment CAN Bus Implementation Mk2.bas I just realized that the Mk1 file has the same memory spaces used multiple times over and overwriting each other.
 

Attachments

Last edited:

mikeyBoo

Senior Member
hi Tony, What would be the purpose or application for hooking up all those 40X2s on a CAN bus? Is this an application where I2C won't work?
 

AllyCat

Senior Member
Hi Tony,

Welcome to the forum (as a contributor).

Another thing that I wanted to clear up is the deference between Subroutines and the Macro command. Is there any memory/speed advantage of using one over the other?
Yes, a Subroutine occurs only once in the code that is downloaded to the PICaxe, but a Macro is a "convenient" way to write the same code to the chip, each time that it is used in the program listing. Thus the Macro may potentially use up much more program code space than a subroutine. However, a subroutine may take a significant time to Call (Gosub) and Return (just over 3 ms with 4 MHz clock) and of course each Call and Return instruction consumes a few bytes of program space. So a Macro may be preferable if speed is critical, or if program size is not important. But Macros are not supported in PE5, which some of us still prefer to use sometimes,

Note that there is a "Finished Projects" / "Code Snippetts" section of the forum for "library" type contributions, but you are correct to have posted first in the Active Forum.

Cheers, Alan.
 

TonyKramer

New Member
So a Macro may be preferable if speed is critical, or if program size is not important.
OK, thank you, that is good to know. Considering I will be using most if not all of the available memory on a PicAxe 40X2, I will stick to Subroutines for now.

What would be the purpose or application for hooking up all those 40X2s on a CAN bus? Is this an application where I2C won't work?
The robotics project I am currently working on is going to require several different controllers to do different things (such as motor control, managing sensors, etc.) because there is going to be between 100-150 different inputs/outputs. I would have been fine with using I2C, except that I wanted to have a multi master bus that would be relatively easy to add more devices to. Another large issue with using I2C is that I am using a Nvidia Jetson TX1 to control all ten PicAxe 40X2s (over dual MCP2515 chips) so I really wanted to be able to make my own command structure for less confusion on my part. I2C did not look like the best option in my particular case, and I don't mind doing some extra work to get CAN working.
 

BESQUEUT

Senior Member
Another thing that I wanted to clear up is the deference between Subroutines and the Macro command. Is there any memory/speed advantage of using one over the other?
Note that Macro can also be used to improve readability, without changing anything to size or code speed.
IMHO, no need for useless comments if code is well writen...
For example this code is exactly the same as yours (1322 bytes) :
Code:
[color=Navy]#macro [/color][color=Black]SPI[/color][color=Blue]([/color][color=Black]Command, Value[/color][color=Blue])
      Low CS                              [/color][color=Green]'Start SPI
      [/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],Command,Value[/color][color=Blue])           [/color][color=Green]'Write BFPCTRL Byte 
      [/color][color=Blue]High CS                             [/color][color=Green]'End SPI
      [/color][color=Blue]Pause [/color][color=Navy]10                            [/color][color=Green]'Wait for SPI to write[/color]
[color=Navy]#endmacro[/color]

[color=Blue]symbol BFPCTRL[/color][color=DarkCyan]=[/color][color=Navy]$0C[/color]
[color=Blue]symbol TXRTSCTRL[/color][color=DarkCyan]=[/color][color=Navy]$0D[/color]
[color=Blue]symbol CNF1[/color][color=DarkCyan]=[/color][color=Navy]$2A[/color]
[color=Blue]symbol CNF2[/color][color=DarkCyan]=[/color][color=Navy]$29[/color]
[color=Blue]symbol CNF3[/color][color=DarkCyan]=[/color][color=Navy]$28[/color]
[color=Blue]symbol CANINTE[/color][color=DarkCyan]=[/color][color=Navy]$2B[/color]

[color=Green]'CAN Init[/color]
[color=Blue]Hspisetup Spimode11[/color][color=Black], [/color][color=Blue]Spifast  [/color][color=Green]'SPI mode 1,1 running at 4MHz[/color]
[color=Blue]Symbol CS   [/color][color=DarkCyan]= [/color][color=Blue]D.1             [/color][color=Green]'Chip Select is pinD.1[/color]
[color=Blue]High CS                       [/color][color=Green]'Activly hold ChipSelect high[/color]
[color=Blue]Symbol INT  [/color][color=DarkCyan]= [/color][color=Blue]D.0             [/color][color=Green]'Interupt Is pinD.0[/color]
[color=Blue]Input INT                     [/color][color=Green]'Configure Interupt pin as an input[/color]
[color=Blue]Setint [/color][color=Navy]%00000000[/color][color=Black],[/color][color=Navy]%00000001[/color][color=Black],D  [/color][color=Green]'Set active low interupt on pinD.0
                              'Read is    %00000011 = 3
                              'Write is   %00000010 = 2
                              'Modify ia  %00000101 = 5[/color]
[color=Blue]Low CS                              [/color][color=Green]'Start SPI[/color]
[color=Blue]Hspiout ([/color][color=Navy]%11000000[/color][color=Blue])                 [/color][color=Green]'Send Reset Command[/color]
[color=Blue]High CS                             [/color][color=Green]'Emd SPI[/color]
[color=Blue]Pause [/color][color=Navy]10                      [/color][color=Green]'Wait for reset to finish[/color]

[color=Black]spi[/color][color=Blue](BFPCTRL[/color][color=Black],[/color][color=Navy]%00000000[/color][color=Blue])[/color]
[color=Black]spi[/color][color=Blue](TXRTSCTRL[/color][color=Black],[/color][color=Navy]%00000000[/color][color=Blue])[/color]
[color=Black]spi[/color][color=Blue](CNF3[/color][color=Black],[/color][color=Navy]%00000000[/color][color=Blue])[/color]
[color=Black]spi[/color][color=Blue](CNF2[/color][color=Black],[/color][color=Navy]%00000000[/color][color=Blue])[/color]
[color=Black]spi[/color][color=Blue](CNF1[/color][color=Black],[/color][color=Navy]%00000000[/color][color=Blue])[/color]
[color=Black]spi[/color][color=Blue](CANINTE[/color][color=Black],[/color][color=Navy]%00011111[/color][color=Blue])[/color]
...
also, LoadTX0, LoadTX1 and LoadTX2 can be rewriten like this (this time, code size is reducced, speed is same) :
Code:
[color=Black]LoadTX0:
      [/color][color=Blue]Low CS                  [/color][color=Green]'Start SPI
      [/color][color=Black]Hspiout [/color][color=Blue]([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$35[/color][color=Black],[/color][color=Purple]TXBC[/color][color=Blue])        [/color][color=Green]'Write TXB0DLC
      [/color][color=Blue]High CS                 [/color][color=Green]'End SPI
      [/color][color=Blue]Low CS                  [/color][color=Green]'Start SPI
            
      [/color][color=Blue]Select [/color][color=Purple]TXBC             [/color][color=Green]'Figure out how many bytes need be sent
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]1[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Blue])               [/color][color=Green]'Write TXB0D0 with data
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]2[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Blue])                  [/color][color=Green]'Write TXB0D0-1
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]3[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Blue])               [/color][color=Green]'Write TXB0D0-2
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]4[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Blue])            [/color][color=Green]'Write TXB0D0-3
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]5[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Blue])         [/color][color=Green]'Write TXB0D0-4
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]6[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Black],[/color][color=Purple]b5[/color][color=Blue])      [/color][color=Green]'Write TXB0D0-5
      [/color][color=Blue]Case [/color][color=DarkCyan]= [/color][color=Navy]7[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Black],[/color][color=Purple]b5[/color][color=Black],[/color][color=Purple]b6[/color][color=Blue])   [/color][color=Green]'Write TXB0D0-6
      [/color][color=Blue]Case [/color][color=DarkCyan]> [/color][color=Navy]7[/color][color=Black]:[/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$36[/color][color=Black],[/color][color=Purple]b0[/color][color=Black],[/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Black],[/color][color=Purple]b5[/color][color=Black],[/color][color=Purple]b6[/color][color=Black],[/color][color=Purple]b7[/color][color=Blue])[/color][color=Green]'Write TXB0D0-7
      [/color][color=Blue]Endselect
      High CS                       [/color][color=Green]'End SPI
      [/color][color=Blue]Low CS                        [/color][color=Green]'Start SPI
      [/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$32[/color][color=Black],[/color][color=Navy]%00[/color][color=Black],[/color][color=Purple]TXBI[/color][color=Black],[/color][color=Navy]%0000[/color][color=Blue])[/color][color=Green]'Write TXB0SIDH-L
      [/color][color=Blue]High CS                       [/color][color=Green]'End SPI
      [/color][color=Blue]Low CS                        [/color][color=Green]'Start SPI
      [/color][color=Blue]Hspiout ([/color][color=Navy]2[/color][color=Black],[/color][color=Navy]$30[/color][color=Black],[/color][color=Navy]%000000[/color][color=Black],[/color][color=Purple]PR0[/color][color=Black],[/color][color=Purple]PR1[/color][color=Blue])[/color][color=Green]'Write TXB0CTRL byte
      [/color][color=Blue]High CS                       [/color][color=Green]'End SPI
      [/color][color=Blue]Let [/color][color=Purple]TXF0 [/color][color=DarkCyan]= [/color][color=Navy]1            [/color][color=Green]'Acknowledge that TB0 is full[/color]
[color=Blue]Return[/color]
IHMO : readability is better...
 
Last edited:

TonyKramer

New Member
Alright, thank you, but does that have any effect other than making the code less winded? Still a great suggestion though, I'll probably end up rewriting the code in that way. Its just that I wrote the code in that way to remember which hex address went where, but now that I think of it, I could have probably just written that in a comment.
 

BESQUEUT

Senior Member
Alright, thank you, but does that have any effect other than making the code less winded? Still a great suggestion though, I'll probably end up rewriting the code in that way. Its just that I wrote the code in that way to remember which hex address went where, but now that I think of it, I could have probably just written that in a comment.
Absolutely no effect for first suggestion : the code send to the Picaxe is exactly the same.
For second suggestion, speed is the same, but code size is reducced.

HEX address are now here :

symbol BFPCTRL=$0C
symbol TXRTSCTRL=$0D
symbol CNF1=$2A
symbol CNF2=$29
symbol CNF3=$28
symbol CANINTE=$2B
symbol TXB0DLC=$35


is'nt that enought ?
 

TonyKramer

New Member
is'nt that enought ?
I guess so, but there is so many different addresses and registers on the MCP2515 that it gets really confusing really quickly. Redoing the TXLoad Subroutines is a great idea, I don't know why I hadn't though of combining all of the conditional statements into one singular Select Case statement.
 

BESQUEUT

Senior Member
IMHO, TXRTSCTRL is (a little...) more readable than $0D
You even may write something more explicit if necessary (with no effect on code size or speed):
TX_RTS_control
 

TonyKramer

New Member
Ok, I'll definitely rewrite it that way when I get everything figured out in its entirety (some of the registers I have write commands for should actually be bit modify commands according to the datasheet.)
But in the mean time, here's an updated version of the code with your second suggestion implemented.
View attachment CAN Bus Implementation Mk3.bas
That cut it down to a more reasonable 1065 bytes.
 

BESQUEUT

Senior Member
Ok, I'll definitely rewrite it that way when I get everything figured out in its entirety (some of the registers I have write commands for should actually be bit modify commands according to the datasheet.)
But in the mean time, here's an updated version of the code with your second suggestion implemented.
View attachment 19910
That cut it down to a more reasonable 1065 bytes.
My proposal for 712 bytes :
Code:
...

Symbol TX0		=%10000001	' JYB
Symbol TX1		=%10000010
Symbol TX2		=%10000100
Symbol TXall	=%10000111

Symbol Temp1	= b5		' temporary

#Macro Send(N)			'Macro for sending  N
	Low CS			'Start SPI
	Hspiout (N)			'RTS Command
	High CS			'End SPI
#EndMacro

#Macro LoadTx(N)
	Temp1=N*$10+$35
	gosub _LoadTx
#EndMacro

'CAN Init
Hspisetup Spimode11, Spifast	'SPI mode 1,1 running at 4MHz
Symbol CS	= D.1			'Chip Select is pinD.1
High CS				'Activly hold ChipSelect high
Symbol INT	= D.0			'Interupt Is pinD.0
Input INT				'Configure Interupt pin as an input
Setint %00000000,%00000001,D	'Set active low interupt on pinD.0
					'Read is 	%00000011 = 3
					'Write is	%00000010 = 2
					'Modify is 	%00000101 = 5
Low CS					'Start SPI
Hspiout (%11000000)			'Send Reset Command
High CS					'End SPI
Pause 10				'Wait for reset to finish

Low CS					'Start SPI
Hspiout (2,$0C,%00000000)		'Write BFPCTRL Byte 
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$0D,%00000000)		'Write TXRTSCTRL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$28,%00000000)		'Write CNF3 Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$29,%00000000)		'Write CNF2 Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$2A,%00000000)		'Write CNF1 Byte
High CS					'End SPI
Pause 10				'Wait for SPI to Write

Low CS					'Start SPI
Hspiout (2,$2B,%00011111)		'Write CANINTE Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$2C,%00000000)		'Write CANINTF Byte, Clear INT
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$2D,%00000000)		'Write EFLG Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

Low CS					'Start SPI
Hspiout (2,$60,%00100100)		'Write RXB0CTRL Byte
High CS					'End SPI
Pause 10				'Wair for SPI to write

Low CS					'Start SPI
Hspiout (2,$70,%00100000)		'Write RXB1CTRL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write

'I choose a filter of 3 for this device, so this is just setting every
'filter to accept 3 just to make sure there are not any stray messages.
Low CS					'Start SPI
Hspiout (2,$00,%00000000)		'Write RXF0SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$04,%00000000)		'Write RXF1SIDH Byte
High CS					'End SPI
Pause	10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$08,%00000000)		'Write RXF2SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$10,%00000000)		'Write RXF3SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$14,%00000000)		'Write RXF4SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$18,%00000000)		'Write RXF5SIDH Byte
High CS					'End SPI
Pause 10 				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$01,%10100000)		'Write RXF0SIDL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$05,%10100000)		'Write RXF1SIDL Byte
High CS					'End SPI
Pause	10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$09,%10100000)		'Write RXF2SIDL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$11,%10100000)		'Write RXF3SIDL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$15,%10100000)		'Write RXF4SIDL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$19,%10100000)		'Write RXF5SIDL Byte
High CS					'End SPI
Pause 10 				'Wait for SPI to write
'And now to set the masks to only check some bits.
Low CS					'Start SPI
Hspiout (2,$20,%00011111)		'Write RXM0SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$24,%00011111)		'Write RXM1SIDH Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$21,%11100000)		'Write RXM0SIDL Byte
High CS					'End SPI
Pause 10				'Wait for SPI to write
Low CS					'Start SPI
Hspiout (2,$25,%11100000)		'Write RXM1SIDL Byte
High CS					'End SPI
Pause 10				'Wait for Spi to write

Low  CS					'Start SPI
Hspiout (2,$0F,%00000000)		'Write CANCTRL Byte, must be last
High CS					'End SPI
Pause 10				'Wait for SPI to write

Return


' SendTX0:	replaced by Send(TX0)	' JYB
' SendTX1:	replaced by Send(TX1)
' SendTX2:	replaced by Send(TX2)
' Sendall:	replaced by Send(TXall)

' LoadTX0:   replaced by LoadTx(0)
' LoadTX1:   replaced by LoadTx(1)
' LoadTX2:   replaced by LoadTx(2)

_LoadTX:
      Low CS                  'Start SPI
      Hspiout (2,Temp1,TXBC)  'Write TXB n DLC
      High CS                 'End SPI
      Low CS                  'Start SPI
	
	inc Temp1
      Select TXBC             'Figure out how many bytes need be sent
      Case = 1:Hspiout (2,Temp1,b0)               'Write TXB2D0 with data
      Case = 2:Hspiout (2,Temp1,b0,b1)                  'Write TXB2D0-1
      Case = 3:Hspiout (2,Temp1,b0,b1,b2)               'Write TXB2D0-2
      Case = 4:Hspiout (2,Temp1,b0,b1,b2,b3)            'Write TXB2D0-3
      Case = 5:Hspiout (2,Temp1,b0,b1,b2,b3,b4)         'Write TXB2D0-4
      Case = 6:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5)      'Write TXB2D0-5
      Case = 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6)   'Write TXB2D0-6
      Case > 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6,b7)'Write TXB2D0-7
      Endselect
      High CS                      	 	'End SPI
      Low CS                        	'Start SPI
	
	Temp1=Temp1-4
      Hspiout (2,Temp1,%00,TXBI,%0000)	'Write TXB n SIDH-L
      High CS                       	'End SPI
      Low CS                        	'Start SPI
	
	Temp1=Temp1-2
      Hspiout (2,Temp1,%000000,PR0,PR1)	'Write TXB n CTRL byte
      High CS                       	'End SPI
      Let TXF2 = 1            		'Acknowledge that TB2 is full
Return

...
 

BESQUEUT

Senior Member
New proposal, using Table to reduce code size to 575 bytes :
Code:
Symbol TX0		=%10000001	' JYB
Symbol TX1		=%10000010
Symbol TX2		=%10000100
Symbol TXall	=%10000111

Symbol Temp1	= b5		' temporary  Note : to be relocated somewhere !
Symbol Temp2	= b6		' temporary
Symbol Temp3	= b7		' temporary

#Macro Send(N)			'Macro for sending  N
	Low CS			'Start SPI
	Hspiout (N)			'RTS Command
	High CS			'End SPI
#EndMacro

#Macro LoadTx(N)
	Temp1=N*$10+$35
	gosub _LoadTx
#EndMacro

'CAN Init
Hspisetup Spimode11, Spifast	'SPI mode 1,1 running at 4MHz
Symbol CS	= D.1			'Chip Select is pinD.1
High CS				'Activly hold ChipSelect high
Symbol INT	= D.0			'Interupt Is pinD.0
Input INT				'Configure Interupt pin as an input
Setint %00000000,%00000001,D	'Set active low interupt on pinD.0
					'Read is 	%00000011 = 3
					'Write is	%00000010 = 2
					'Modify is 	%00000101 = 5

send(%11000000)		'Send Reset Command
Pause 10				'Wait for reset to finish


'         BFPCTRL       TXRTSCTRL     CNF3          CNF2          CNF1
Table 0,($0C,%00000000,$0D,%00000000,$28,%00000000,$29,%00000000,$2A,%00000000)

'         CANINTE       CANINTF       EFLG          RXB0CTRL      RXB1CTRL
Table 10,($2B,%00011111,$2C,%00000000,$2D,%00000000,$60,%00100100,$70,%00100000)

for temp1=0 to 18 step 2
	readtable Temp1,Temp2,Temp3
	Low CS				'Start SPI
	Hspiout (2,Temp2,Temp3)		'Write  Byte 
	High CS				'End SPI
	Pause 10				'Wait for SPI to write
next Temp1


'I choose a filter of 3 for this device, so this is just setting every
'filter to accept 3 just to make sure there are not any stray messages.

' RXF0SIDH RXF2SIDH RXF3SIDH RXF4SIDH RXF5SIDH RXF5SIDH
Table 20,($00,$04,$08,$10,$14,$18)

for Temp1 =20 to 25
	readtable Temp1,Temp2
	Low CS					'Start SPI
	Hspiout (2,Temp2,%00000000)		'Write H Byte
	High CS					'End SPI
	Pause 10					'Wait for SPI to write
	
	inc Temp2
	Low CS					'Start SPI
	Hspiout (2,Temp2,%10100000)		'Write L Byte
	High CS					'End SPI
	Pause 10					'Wait for SPI to write
next Temp1


'And now to set the masks to only check some bits.
'         RXM0SIDH       RXM1SIDH      RXM0SIDL      RXM1SIDL      CANCTRL
Table 27,($20,%00011111,$24,%00011111,$21,%11100000,$25,%11100000,$0F,%00000000)

for temp1=27 to 35 step 2
	readtable Temp1,Temp2,Temp3
	Low CS				'Start SPI
	Hspiout (2,Temp2,Temp3)		'Write  Byte 
	High CS				'End SPI
	Pause 10				'Wait for SPI to write
next Temp1

Return


' SendTX0:	replaced by Send(TX0)	' JYB
' SendTX1:	replaced by Send(TX1)
' SendTX2:	replaced by Send(TX2)
' Sendall:	replaced by Send(TXall)

' LoadTX0:   replaced by LoadTx(0)
' LoadTX1:   replaced by LoadTx(1)
' LoadTX2:   replaced by LoadTx(2)

_LoadTX:
      Low CS                  'Start SPI
      Hspiout (2,Temp1,TXBC)  'Write TXB n DLC
      High CS                 'End SPI
      Low CS                  'Start SPI
	
	inc Temp1
      Select TXBC             'Figure out how many bytes need be sent
      Case = 1:Hspiout (2,Temp1,b0)               'Write TXB2D0 with data
      Case = 2:Hspiout (2,Temp1,b0,b1)                  'Write TXB2D0-1
      Case = 3:Hspiout (2,Temp1,b0,b1,b2)               'Write TXB2D0-2
      Case = 4:Hspiout (2,Temp1,b0,b1,b2,b3)            'Write TXB2D0-3
      Case = 5:Hspiout (2,Temp1,b0,b1,b2,b3,b4)         'Write TXB2D0-4
      Case = 6:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5)      'Write TXB2D0-5
      Case = 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6)   'Write TXB2D0-6
      Case > 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6,b7)'Write TXB2D0-7
      Endselect
      High CS                      	 	'End SPI
      Low CS                        	'Start SPI
	
	Temp1=Temp1-4
      Hspiout (2,Temp1,%00,TXBI,%0000)	'Write TXB n SIDH-L
      High CS                       	'End SPI
      Low CS                        	'Start SPI
	
	Temp1=Temp1-2
      Hspiout (2,Temp1,%000000,PR0,PR1)	'Write TXB n CTRL byte
      High CS                       	'End SPI
      Let TXF2 = 1            		'Acknowledge that TB2 is full
Return
Note that b0,b1,b2,b3,... are already defined
so
Case = 1:Hspiout (2,Temp1,b0) 'Write TXB2D0 with data
Case = 2:Hspiout (2,Temp1,b0,b1) 'Write TXB2D0-1
Case = 3:Hspiout (2,Temp1,b0,b1,b2) 'Write TXB2D0-2
Case = 4:Hspiout (2,Temp1,b0,b1,b2,b3) 'Write TXB2D0-3
Case = 5:Hspiout (2,Temp1,b0,b1,b2,b3,b4) 'Write TXB2D0-4
Case = 6:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5) 'Write TXB2D0-5
Case = 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6) 'Write TXB2D0-6
Case > 7:Hspiout (2,Temp1,b0,b1,b2,b3,b4,b5,b6,b7)'Write TXB2D0-7


must be rewriten (I suggest using @ptrinc... )

Also note that I dont't known if
Hspiin (MERRF,WAKIF,ERRIF,TX2IF,TX1IF,TX0IF,RX1IF,RX0IF) 'Read CANINTF
will work as it may read 8 bytes
not 8 bits...
 
Last edited:

BESQUEUT

Senior Member
New suggestion :
Code:
'I choose a filter of 3 for this device, so this is just setting every
'filter to accept 3 just to make sure there are not any stray messages.

' RXF0SIDH RXF2SIDH RXF3SIDH RXF4SIDH RXF5SIDH RXF5SIDH
Table 20,($00,$04,$08,$10,$14,$18)

for Temp1 =20 to 25
	readtable Temp1,Temp2
	Low CS					'Start SPI
	Hspiout (2,Temp2,%00000000,%10100000)		'Write H,L Bytes
	High CS					'End SPI
	Pause 10					'Wait for SPI to write
next Temp1
Did not look at the datasheet, but it's very surprising that data are not 4 bytes spaced ???

==> if so, no need for Table in that case : semply calculate Temp2 from Temp1...
 

TonyKramer

New Member
Ok, to be completely honest, I haven't been able to get anything to work with the setup I have going on right now, I've been trying for the past few days now, has anyone worked with this chip before, and can anyone confirm that what I am doing will work at all?
 
Top