![]() |
Forum Index : Microcontroller and PC projects : CAN-bus with picomite/webmite and MCP2515, proof of concept
Author | Message | ||||
Arne Newbie ![]() Joined: 05/01/2025 Location: GermanyPosts: 33 |
As I had no idea about CAN-bus I decided to make an absolute minimum test-program for the MCP2515 module to learn something about it. 3 picos (2xRP2350 and 1xRP2350W) with MCP2515 modules have been connected together to one CAN-bus line with a length of about 2 meters. For the test all picos used the same program. Each pico sent one data frame every second with increasing ID. Receiving of frames were initiated by the MCP2515 controller via interrupt line to the relevant pico. Additional features of the MCP2515 as e.g. - extended data and remote frames - data filtering - etc… have not been taken into account. The following test-program is based on my evaluations of the MCP2515 data sheet. Due no failure handling is implemented it can be possible that the data transfer via the bus suddenly stops e.g. when the same IDs were sent by different nodes at the same time. The controller then initiates a failure and waits for instructions how to go on… CAN.zip After my tests here the conclusion: Implementing a CAN-bus driver as e.g. for the MCP2515 into the picomite (firmware) can be a big benefit because interfacing to several industrial systems/sensors/actuators can be done. Also distributed systems with several picomites for home automation, robotics etc. can easily be designed. In addition: - It is a robust industrial standard field bus - Can have a maximum length of about 5 Km with minimum cabling (2 or 3 wires) - Depend on bus length/drivers it can handle up to 100 nodes. - In standard operation 2048 frames with 8 byte data can be transferred. - Extended and remote frames are possible Maybe the a.m. infos are interesting … @Peter: What are you thinking about implementing a CAN-bus driver as e.g. for the MCP2515 into the picomite firmware? For hobby programmers (as me) this can make life much easier (and the code more compact). Arne |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
For hobby (non-commercial) purposes, I would be tempted to couple the PicoMite with the ANNEX32 as it supports lots of stuff such as CAN, ModBus, Ethernet, Bluetooth, WiFi, etc. Could be a great companion as it has a similar, civilized programming syntax. Edited 2025-08-05 00:01 by PhenixRising |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7997 |
Personally, I'd argue against building it in. It would take up space that's better used for stuff that more people would probably use. However, I see no reason why suitable MMBasic routines shouldn't be on FotS, ready for using in a Library. MMBasic already has CRC error checking implemented, which can be used over any wired or wireless system. . Edited 2025-08-05 02:42 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5138 |
Hi Arne, Thank you for creating this example code. It is obvious not finished, but shows how to do it. Very useful. Thanks. Volhout PicomiteVGA PETSCII ROBOTS |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1010 |
This thread on FotS has an MMBasic driver for the MCP2515. It uses polling instead of interrupts. It still needs a tidy/clean up as it was initially a control for a power supply but does support filters. It originally was used on Micromite and Armmite F4 so needs changes to SPI OPEN/CLOSE for Picomites. MMBasic Driver For MCP2515 SPI CAN Adaptor Edited 2025-08-05 09:20 by disco4now F4 H7FotSF4xGT |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5138 |
Hi Gerry, Looks like you recently made some changes to the driver (5-8-2025). Any changes for compatibility to PicoMite ? Or is it just an update to the FOTS page ? What is the original application you made this for ? A service tool ? (there is a display, a rotary encoder). Volhout PicomiteVGA PETSCII ROBOTS |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1010 |
Hi Harm, Update yesterday was just a fix for the single to double precision function. The code was initially the start of an application to control one of these CAN based 48V power supplies, to make a battery charger. These are quiet a few different types, probably from de-commission telephone exchanges. EMERSON 48V Power supply I started on a Micromite, then to ARMMITE F4. I then got side tracked adding CAN support to ARMMites (they have inbuilt CAN controller),the plan was to document the driver for the MCP2515 by cleaning the up application but not quite finished yet. This site has info of these type of deviices Many use them to charge electric bikes. Gerry F4 H7FotSF4xGT |
||||
Arne Newbie ![]() Joined: 05/01/2025 Location: GermanyPosts: 33 |
Thanks for all the feedback! I only wanted to learn what is CAN-bus and what can I do with it. So I took one of the mostly and cheapest CAN-modules used to make some practical experiences. As a result I saw the amazing possibilities you could have if an easy to use driver would be available. Background: I use the picomite with monitor/keyboard as a cheap stand alone programmable and easy to use system. MMBasic is a great piece of software. Mainly everything you need (for easy programming) is part of it. For me it’s like a swiss army knife for easy programming and fast prototyping. I understand that the hardware is limited and therefore new functionalities/drivers have to be selected very carefully. It is always a decision for what the hardware/firmware/software shall be used and what benefit does a new functionality brings. But here maybe provokant questions: How many times the 3D engine of picomite is/was used? How many resources does it need? I myself see it more as a demo to show what is possible… In my opinion to implement an easy to use CAN-bus driver into the firmware can bring MMBasic and picomite a big step forward. It will open the door to a worldwide standard and raise the acceptance to use it. Anyway, imagine the possibilities you could have as e.g.: - distributed systems with hundreds of sensors/actuators - Interfacing to CAN-conform systems/sensors/actuators - Data exchange between several picomites (arduinos,ESP‘s,Raspis,PCs…) - Different separate systems on one CAN-bus - Easy expandability and long range data transfer And all that with a 3-wire cable and a cheap adapter for less than 2 Euros! Now I know why the Industrie is using it for automation,production,cars etc. Arne |
||||
circuit Senior Member ![]() Joined: 10/01/2016 Location: United KingdomPosts: 282 |
Geoff did some work on this in the early days; The "Special Version of V4.5 with support for CAN (Controller Area Network)" is still available on his website. (See 'Original Colour Maximite' - bottom of the page. I must agree that Arne's argument for bringing this concept into the current 2040/2350 firmware is very compelling. I would certainly be very enthusiastic about this for embedded controller applications. Perhaps it might be considered in the next version of the 2350 firmware, currently under Peter's authorship. Edited 2025-08-06 17:20 by circuit |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1010 |
The original Maximite and the current ARMmite F4 and ARMmite H7 have a CAN controller built into the chip.The latest ARMMite F4 and next (soon) ARMmite H7 now have CAN support build into MMBasic. The Micromite and Picomite chips don't have a CAN controller build into the chip so you would need to access one externally using SPI. F4 H7FotSF4xGT |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
|
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7997 |
Remember that CAN isn't a lightweight system. If you are trying to feed it over SPI can the Pico even do it? 5MHz, yes, but how much is your program slowed down if you go higher? Do you need more buffers than the Pico's RAM can handle? Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4061 |
An Armmite looks a better idea. John |
||||
mozzie Senior Member ![]() Joined: 15/06/2020 Location: AustraliaPosts: 171 |
G'day Arne, As you suggest, CAN is a great way of networking multiple controllers if data reliability is important, many years building heavy vehicle control systems has proven to me just how robust it is, even when abused ![]() Thanks for the demo code, I agree that having it built in to the firmware would be great but a few subroutines in the library can also work well depending on what the application would be. Also keep in mind it is not great for battery powered applications, the idle current of the drivers and MCP2515 is fairly high, shutdown modes do help. @Mixtel, Thankfully the MCP2515 isolates most of the "heavy lifting" from the micro-controller so the 5mhz SPI speed is normally overkill. Considering a CAN speed of 250kbps and the overheads, the maximum message speed is approx 2200 messages/sec and these can be filtered / buffered in the MCP2515 to only those required. I have tested MicroMites on 250kbps systems in the past with no problems. Regards, Lyle. Edited 2025-08-08 02:21 by mozzie |
||||
Zonker![]() Guru ![]() Joined: 18/08/2012 Location: United StatesPosts: 771 |
Good evening Gents.. Two boards for the Pico that have CAN interface using the MCP2515 IC.. https://www.waveshare.com/catalogsearch/result/?q=Pico+CAN+B https://www.longan-labs.cc/1030018.html I have both of these but just haven't had the time to get started yet... ![]() Thanks for this thread and help with this subject... ![]() |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
MCP2517 not the MCP2515 Just sayin ![]() |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
For giggles, I asked ChatGPT for a translation of the Python sample code for the Waveshare product: ' Configuration Registers CANSTAT = &H0E CANCTRL = &H0F BFPCTRL = &H0C TEC = &H1C REC = &H1D CNF3 = &H28 CNF2 = &H29 CNF1 = &H2A CANINTE = &H2B CANINTF = &H2C EFLG = &H2D TXRTSCTRL = &H0D ' Transmit Buffer 0 TXB0CTRL = &H30 TXB0SIDH = &H31 TXB0SIDL = &H32 TXB0EID8 = &H33 TXB0EID0 = &H34 TXB0DLC = &H35 TXB0D0 = &H36 TXB0D1 = &H37 TXB0D2 = &H38 TXB0D3 = &H39 TXB0D4 = &H3A TXB0D5 = &H3B TXB0D6 = &H3C TXB0D7 = &H3D ' SPI Commands CAN_RESET = &HC0 CAN_READ = &H03 CAN_WRITE = &H02 CAN_RTS = &H80 CAN_RTS_TXB0 = &H81 ' Initialize SPI SCK_PIN = 6 ' Set your SCK pin MOSI_PIN = 7 ' Set your MOSI pin MISO_PIN = 4 ' Set your MISO pin CS_PIN = 1 ' Set your CS pin SPI_Init(SCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN) ' Function to read a byte from the CAN controller FUNC ReadByte(addr) CS_PIN = 0 SPI_Write CAN_READ SPI_Write addr res = SPI_Read() CS_PIN = 1 RETURN res END FUNC ' Function to write a byte to the CAN controller FUNC WriteByte(addr, data) CS_PIN = 0 SPI_Write addr SPI_Write data CS_PIN = 1 END FUNC ' Function to reset the CAN controller FUNC Reset() CS_PIN = 0 SPI_Write CAN_RESET CS_PIN = 1 END FUNC ' Function to initialize the CAN controller FUNC Init(speed) PRINT "Reset" Reset() PAUSE 100 ' Set baud rate WriteByte(CNF1, 0x03) ' Example for 125Kbps WriteByte(CNF2, 0xB8) WriteByte(CNF3, 0x03) ' Set TXB0 WriteByte(TXB0SIDH, 0xFF) WriteByte(TXB0SIDL, 0xE0) WriteByte(TXB0DLC, 0x40 OR 0x08) ' DLC = 8 ' Set RXB0 WriteByte(RXB0SIDH, 0x00) WriteByte(RXB0SIDL, 0x60) WriteByte(RXB0CTRL, 0x60) WriteByte(RXB0DLC, 0x08) ' Enable interrupts WriteByte(CANINTE, 0x01) END FUNC ' Function to send data FUNC Send(CAN_ID, CAN_TX_Buf, length) WriteByte(TXB0SIDH, (CAN_ID >> 3) AND &HFF) WriteByte(TXB0SIDL, (CAN_ID AND &H07) << 5) WriteByte(TXB0DLC, length) FOR j = 0 TO length - 1 WriteByte(TXB0D0 + j, CAN_TX_Buf(j)) NEXT j WriteByte(CAN_RTS_TXB0) END FUNC ' Function to receive data FUNC Receive(CAN_ID) WriteByte(RXB0SIDH, (CAN_ID >> 3) AND &HFF) WriteByte(RXB0SIDL, (CAN_ID AND &H07) << 5) CAN_RX_Buf = [] WHILE TRUE IF (ReadByte(CANINTF) AND &H01) THEN len = ReadByte(RXB0DLC) FOR i = 0 TO len - 1 CAN_RX_Buf[i] = ReadByte(RXB0D0 + i) NEXT i ' Clear the interrupt flag WriteByte(CANINTF, 0) RETURN CAN_RX_Buf ENDIF WEND END FUNC ' Main program PRINT "--------------------------------------------------------" PRINT "Initializing CAN..." Init("125KBPS") PRINT "Sending data..." id = &H123 ' Example CAN ID data = [1, 2, 3, 4, 5, 6, 7, 8] ' Example data dlc = 8 Send(id, data, dlc) ' Loop to receive data WHILE TRUE readbuf = Receive(id) PRINT "Received Data: "; readbuf PAUSE 500 WEND PRINT "--------------------------------------------------------" Was supposed to be MMBasic but it isn't quite right. |
||||
Zonker![]() Guru ![]() Joined: 18/08/2012 Location: United StatesPosts: 771 |
Interesting... I have never played with ChatGPT, How did you converse with it..?? Did you give it a copy of the MMbasic manual and MCP2515 PDF files..? Just.. Awesome..!! ![]() |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
I tried copy/pasting the python code but it was too much so I described the product and stated that there was python code that I'd like translated to Micro mite Basic. After this, I tried Phind.com because it is claimed to be better for coding. Here I could paste and the generated code was closer to Micromite. Still had "FUNC" instead of "FUNCTION" but other than that, pretty close. Phind gives me errors like it's not working but it eventually does work. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |