Arduino CAN bus SparkFun CAN

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Arduino CAN bus SparkFun CAN

Post 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
Attachments
Arduino 0.1.fcfx
(12.46 KiB) Downloaded 386 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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%

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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)

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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.

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post by fredrikah »

big big thanks! LeighM

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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
Attachments
Arduino 0.1_1.fcfx
(12.87 KiB) Downloaded 317 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Arduino CAN bus SparkFun CAN

Post 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

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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.

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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?

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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.
Attachments
CanComponent.PNG
CanComponent.PNG (28.35 KiB) Viewed 10130 times
test1.PNG
test1.PNG (34.47 KiB) Viewed 10130 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post by fredrikah »

here the flowcode
Attachments
FlowCode.PNG
FlowCode.PNG (52.67 KiB) Viewed 10130 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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.
Attachments
CanComponentBuffer0.PNG
CanComponentBuffer0.PNG (13.36 KiB) Viewed 10125 times
CanAnalyserBuffer1.PNG
CanAnalyserBuffer1.PNG (23.49 KiB) Viewed 10125 times
CanAnalyserBuffer0.PNG
CanAnalyserBuffer0.PNG (24 KiB) Viewed 10125 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post by fredrikah »

flowcode to that
Attachments
FlowCodeBuffer0.PNG
FlowCodeBuffer0.PNG (51.41 KiB) Viewed 10125 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post by fredrikah »

With your suggestion i get this result.
CheckRx (0)

below the screenshots
Attachments
Flowcode10.PNG
Flowcode10.PNG (55.9 KiB) Viewed 10125 times
CanComponent10.PNG
CanComponent10.PNG (12.84 KiB) Viewed 10125 times
CanAnalyser10.PNG
CanAnalyser10.PNG (23.43 KiB) Viewed 10125 times

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post by fredrikah »

as you see above, it does not even recive anything then.

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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.
Attachments
CanAnalyserTransmitt.PNG
CanAnalyserTransmitt.PNG (10.7 KiB) Viewed 10103 times
CprogramCheckCanTransmitt.PNG
CprogramCheckCanTransmitt.PNG (8.23 KiB) Viewed 10103 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Arduino CAN bus SparkFun CAN

Post 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?

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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.

fredrikah
Posts: 31
Joined: Fri Dec 17, 2010 7:57 am
Has thanked: 1 time

Re: Arduino CAN bus SparkFun CAN

Post 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)
Attachments
SwitchLED_Flowcode.PNG
SwitchLED_Flowcode.PNG (122.23 KiB) Viewed 10098 times

Post Reply