Page 1 of 2

Arduino CAN bus SparkFun CAN

Posted: Fri Jul 24, 2020 3:56 pm
by fredrikah
Hi :-)

I am using the following HW
*Arduino UNO R3
*Sparfun CAN shield see: https://github.com/sparkfun/SparkFun_CA ... no_Library

I made a very basic program that should send a CAN message, and also listen to the CAN-BUS after message 5 and then lit a LED.

but i cant get it to send any messages.
and on the can bus analyzer i recive a full can bus, operating at 94%.
but there is no valid messages at all..
nothing else is connected. so i guess that it just fills the CAN bus with errors.

on the CAN Shield from sparfun, there is a 16MHz cristal, and in flowcode 8 i can only select 8 or 20 MHz.
I would guess that this is the fault?

is there any option to fix this?

when use Arduinos own tool to program, "Arduino IDE" with the sample code that i find on:
https://github.com/sparkfun/CAN-Bus_Shield
(green button, download zip)
then it works. but not within Flowcode 8.


Greets Fredrik

Re: Arduino CAN bus SparkFun CAN

Posted: Fri Jul 24, 2020 3:57 pm
by fredrikah
and when i use "Arduino IDE" and have a delay of 100 ms, i only have a canbus load of less than 1%

Re: Arduino CAN bus SparkFun CAN

Posted: Fri Jul 24, 2020 4:56 pm
by LeighM
Could you try selecting the 8Mhz Controller Osc setting, and a bus rate of 250KHz (i.e. both half what you really want)

Re: Arduino CAN bus SparkFun CAN

Posted: Sun Jul 26, 2020 10:03 pm
by fredrikah
awesome! that works with 8MHz and 250 kBit/sec
and then the CAN is operating on 500kBit/sec.

a workaround, but it makes it work.

since i anyway write here, is there a way to have 2 CAN bus shields?

I would like to take in a CAN message, process it, and transfer it to the other CAN bus.

Greets Fredrik

Re: Arduino CAN bus SparkFun CAN

Posted: Sun Jul 26, 2020 10:05 pm
by fredrikah
do I then just connect the MOSI and MISO pin to a different digital pin on the Arduino. and use the same pin for CLK. and what is the SS pin?

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Jul 27, 2020 4:24 pm
by LeighM
You should be able to connect the MOSI, MISO and SCK in parallel.
Then wire the two CAN shield CS inputs to different pins on the Arduino.
Use two CAN components in Flowcode, set the MOSI, MISO and SCK pins to the same values,
but set the SS pin to match the two CS pin values you have chosen.

Re: Arduino CAN bus SparkFun CAN

Posted: Thu Jul 30, 2020 9:53 pm
by fredrikah
big big thanks! LeighM

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 2:21 pm
by fredrikah
Dear Matrix team.

I have done some testing, and the CAN component is working really odd.

in the attached code, you will see that i send out every 500ms a CAN message.

then, if I recive a message, then it reads in byte 0 (should not the first byte be 1?)
and this Byte0 is saved as the lable "B1"

then in the send command, in the last Byte, i send out the B1 value.

so what happends is that it goes in a loop, that i understand since the filter is "accept All"

I check it on a CAN analyser.
the values that is sent out is: 8
so i try to picup the first byte that should be 4, but the chip recalculate it to the double.
Sent: 4
Chip recives, and send out as the last byte the variable B1 as the number 8

so, if you look in the beginning of this tread, the speed of the crystal on the CAN controller is 16, and we said, we should use 8, and half the speed (250)

this is a double factor, and that is what is beeing done with the chip, it doubles the values.

Can you please help me?
maybe you can add the future that you can have a CAN chip that runs on 16MHz so its made like it should?

Greets Fredrik

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:06 pm
by fredrikah
I also try to react on incomming CAN messages.
with my CAN analyzer i set it to transmitt on ID 5, and i try to set the second buffer to only accept that, but it does not work.

som something must be wrong.

I have made this with a PIC circuit, and if i set it the same, it does not work.

so the CAN component does not seam to work like it should.

Greets Fredrik

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:13 pm
by QMESAR
Hi
so the CAN component does not seam to work like it should.
I can assure you that the SPI CAN /external MCP2515 Component works ,I use it on various CAN projects with PIC16 and PIC 18's
Do you do a hard reset of the CAN Controller before you send the Configuration/initialization

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:16 pm
by fredrikah
Correct, absolutly, it works.
I have run it on PIC also, and there it works.

but in the config that i have with Arduino and CAN Sparkfun Shield. there there is a 16MHz crystal,
and as you see above, i need to set it to 8MHz and 250 kb instead of 500. and then it runs at that speed.


Transmitting CAN messages works perfect, but I dont manage to recive with any luck.

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:18 pm
by LeighM
Could you separate out the problems by first just sending a message and see what the CAN analyzer makes of it?

For the messages transmitted from the PIC/Arduino, what bus rate does the CAN analyzer indicate?
Does it detect any errors?

It might help if you replaced the 500mS delays with a fixed count loop and a short delay and a call to CheckRx.
With the two transmits and long delays I suspect the Rx might miss some messages?

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:24 pm
by fredrikah
Shield has a 16 MHz crystal.

settings in Flowcode 8 MHz 250 kbs

Can analyser, 500 Mhz,

sending works, correct numbers.


then to recive:
Sending with Can anlyser ID 5
at this stage there is several problems.
first of all, can you help me to be sure i have the correct settings?

1.
What should i have for settings in RX buffer 0 to filter out that i only recive ID 5 ?
Mask ?
Filter ?

2.
call component macro
Check Rx 0 ? or Check Rx 1?

3.
call component Macro GetRxData(0 or 1? , First byte is 0 or 1 ?)

i had this sorted on the PIC programming, so normally it should work, but to be sure?

Thanks Fredrik

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 4:47 pm
by LeighM
1.
What should i have for settings in RX buffer 0 to filter out that i only recive ID 5 ?
Mask ?
Filter ?
Where the Mask bit is 1, the Filter bit will be checked to match with the receive ID bit
So if Mask is 0x7ff and Filter is 5, then only message ID 5 will be received
2.
call component macro
Check Rx 0 ? or Check Rx 1?
CheckRx(0) or CheckRx(1), you have the filter set on Buffer 1, so use CheckRx(1)

3.
call component Macro GetRxData(0 or 1? , First byte is 0 or 1 ?)
GetRxData(x, 0) for the first byte (in the array)
Note: For external CAN the x is ignored, the data will be from the last message read when you called CheckRx(1)

Hope that helps
Leigh

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:00 pm
by fredrikah
see attached picture,

as you see i enable the CAN analyser to send ID 5 every 1000 ms.

and the Aruduino does not react.

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:02 pm
by fredrikah
here the flowcode

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:46 pm
by LeighM
I've just had another look at your program,
you need to do a CheckRx(0)
Because Rx buffer 0 is set to accept all, it will receive the message with ID 5, not buffer 1

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:48 pm
by fredrikah
This is more intresting.

as we know, i have used CheckRx(1)

if i set buffer 0 to "acceptAll" and Buffer 1 to reject all, then i get Rx data that is not like it should. compleatly wrong values.

if i set buffer 0 to "Reject all" and Buffer 1 to "Accept all" then i get nothing.

here pitctures.

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:48 pm
by fredrikah
flowcode to that

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:53 pm
by fredrikah
With your suggestion i get this result.
CheckRx (0)

below the screenshots

Re: Arduino CAN bus SparkFun CAN

Posted: Mon Sep 14, 2020 5:54 pm
by fredrikah
as you see above, it does not even recive anything then.

Re: Arduino CAN bus SparkFun CAN

Posted: Tue Sep 15, 2020 9:46 am
by fredrikah
Here i now created a Cprogram and transfered it to the Arduino,
just to verify that my CanAnalyser is sending the correct data.

and this works, as you se with the attached pictures

the Com7 picture, is read out of the Arduino.

Re: Arduino CAN bus SparkFun CAN

Posted: Tue Sep 15, 2020 10:04 am
by LeighM
Could you remove the sending from the Flowcode project, and just do the receive?
Maybe flash a LED or output when the message is received?

I am having trouble absorbing all the information :?
Are you concluding that a message is being received, but that the data is being read incorrectly?

Re: Arduino CAN bus SparkFun CAN

Posted: Tue Sep 15, 2020 10:22 am
by fredrikah
yes, correct.

first of all, i dont get the rx buffers correct. witch one to be witch "CheckRx()"
if i try "CheckRx(0)"
then i dont recive any messages.

if i set it to "CheckRx(1)"
and set Rx buffer 0 to "accept all"
then i recive messages.

but, then next problem.

i send out ID 0x5 from the CAN analyser, and then i send out the low and hi ID as you see in the code, and it sends out everyting else but 0x5. see screenshots.

So, the facts:
it sends out the correct values,
but it does not recive correct values.

if i program in Arduino IDE directly C-code, it works.


and as you see on the "com7" picture, i tested to write an Arduino IDE c-code program, and it decodes it perfect to 0x5.
so my Can analyser sends out the correct info.

am currently doing your test with only recive.

Re: Arduino CAN bus SparkFun CAN

Posted: Tue Sep 15, 2020 10:32 am
by fredrikah
Now i made this simple code.



if i recive any CAN message, Set LED D7

if not Set LED D6


so in the beginning, it starts the LED D6.

as soon as i send any CAN message, D6 is off, an D7 is on.
but after that when i dont send out any messages, then still the D7 is on.
so its like the CheckRx(1) does not get empty..... but there is nothing on the CAN bus.
in my programs on a PIC that i made a few years ago, then as I have once read the CheckRs(1) then it goes back to 0 the next time i read it.

please see my attachment, should not the variable "RXCan1" be 0 next time i read the CheckRx(1)