Hi,
I read through examples where developers are talking about the “code for several things at the same time” and showing code examples of switching on two to four LED for a set duration, triggered by external analogue input. There are other topics suggesting using timers and interrupts instead of delays.
I can not get my head around how to implement suggested approach to my scenario
To send into the CAN Bus two to four CAN messages, each could have different preset period e.g. CanMsg1 - 10ms, CanMsg2 - 50ms.
There no need for input. The only trigger for sending messages into CAN Bus is powering the board. Messages to be sent as long as the board is powered for as long as 24 hours.
Schematics I am using (nano + mcp2515) is the same as here:
I am using example from that library: called “CAN_Write” with small modification in CAN Bus speed and mcp2515 frequency:
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
With that code what happens is:
CanMsg1 is being sent non-stop with period < 1ms - that is too frequent, need it with 10ms period
canMsg2 being sent with period < 1ms stops shortly after power up. has to continue with 50 ms period
Could you suggest modification to the code (perhaps using timers and interrupts) so that those two messages are sent non-stop with set up period, e.g. 10ms and 50ms?
You might try this. I don't have the mcp2515 library installed and so couldn't compile it.
Basically, it delays sending the 1st message by 1mS and the 2nd message by 6mS (giving a time displacement between the two messages of 5mS.)
Once the 1st message of each type goes, the period is then set to 10mS and 50mS. The offset of 5mS is intended to prevent both messages being sent "simultaneously" every 5th 1st message. Here, the messages are sent like:
Thank you Blackfin for the quick reply.
I tried it - same story. Second message stopped after being sent 18 times, First messages goes non-stop.
Period each message sent is 4 times per 1 ms (i.e. 0.25us), see monitor and trace screenshots:
Well, I've verified both with an oscilloscope and with a logic analyzer that the msg1 frame is being sent every 10mS and the msg2 frame is sent once every 50mS:
The spaces represent periods where SS goes high so for each transfer, there are actually 4 SS events.
Question:
You want msg1 sent every 10mS and msg2 every 50mS. Does that mean that you want 4 msg1 messages (10, 20, 30 40mS) and at 50mS send msg2 instead of msg1? Or what I'm doing which is sending msg1 every 10mS and slipping in a msg2 5mS after the 5th msg1?
valer1:
message 2 - doesn't show at all
message 1 - non-stop with 0,25uc period
The Nano & MCP2515 combination has a max message rate of about 600 per second.
Seeing a continuous stream of messages faster than that indicates a problem at the CAN level.
My guess is that the sending MCP2515 isn't receiving ACK bits and therefore keeps sending the same message.
Is the CAN analyzer configured as a "listen only" device?
If so you need to configure it to send ACK bits.
I followed hint from mikb55, big thanks for that, and "ticked off" "listen only" in my CAN Hacker.
Here is the Trace:
Time ID DLC Data
04.270 036 8 0E 00 00 08 01 00 00 A0
04.275 0F6 8 8E 87 32 FA 26 8E BE 86
04.285 0F6 8 8E 87 32 FA 26 8E BE 86
04.295 0F6 8 8E 87 32 FA 26 8E BE 86
04.304 0F6 8 8E 87 32 FA 26 8E BE 86
04.314 0F6 8 8E 87 32 FA 26 8E BE 86
04.319 036 8 0E 00 00 08 01 00 00 A0
04.324 0F6 8 8E 87 32 FA 26 8E BE 86
04.334 0F6 8 8E 87 32 FA 26 8E BE 86
04.344 0F6 8 8E 87 32 FA 26 8E BE 86
04.354 0F6 8 8E 87 32 FA 26 8E BE 86
04.364 0F6 8 8E 87 32 FA 26 8E BE 86
04.369 036 8 0E 00 00 08 01 00 00 A0
04.374 0F6 8 8E 87 32 FA 26 8E BE 86
04.384 0F6 8 8E 87 32 FA 26 8E BE 86
04.394 0F6 8 8E 87 32 FA 26 8E BE 86
04.404 0F6 8 8E 87 32 FA 26 8E BE 86
04.414 0F6 8 8E 87 32 FA 26 8E BE 86
04.419 036 8 0E 00 00 08 01 00 00 A0
Blackfin:
Question:
You want msg1 sent every 10mS and msg2 every 50mS.
You code worked perfectly. In this set up it was a proof of concept that two messages with different pre-set period on nano+mcp2515 could actually work. That means I am now next to the step two - get the block from the wrecked car I want to emulate, record it's messages which I expect to be no more than 4, make an emulator and do the field test.