CAN Autobaud Example
Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC18F26K83 Device Product Page for more information.
About this Code Example
This example shows a basic usage of the listen-only mode of the CAN module on the PIC18F26K83 to perform a simple auto-baud function. It is able to connect to an existing CAN bus and determine what baud rate the CAN is running at from a predetermined list (500k, 250k, 125k, and 100k), then flash an LED on further messages with the proper ID (0x123) to indicate it is now properly receiving messages at the correct baud rate.
To set up and demonstrate a CAN node as in this example, the following components will be required: • Curiosity HPC board • PIC18F2XK83 device in PDIP package (using PIC18F26K83 in this example) • CAN Transceiver click board (using MCP2542 in this example) • CAN bus analysis hardware (using Microchip CAN bus analyzer in this example)
Please see the "Getting Started with CAN on the PIC18F26K83 using the Curiosity HPC" Example for further details on the hardware setup
This example runs through a basic algorithm for performing this kind of auto-baud routine, which is as follows:
- Select a starting baud rate. This can be determined in a number of ways, which are application dependent. Some reccomendations for starting baud rate include: the most commonly used baud rate for CAN buses that the application is connecting to, the highest possible baud rate among CAN buses that the application is connecting to, or the baud rate of the CAN bus that the application was most recently connected to.
- In addition, have the remaining possible baud rate selections stored in non-volatile memory, either in code or EEPROM.
- Either set up the masks and filters to accept a known message on the CAN bus, or to accept all messages.
- Switch the CAN module into Listen-Only mode (CANCON<7:5>=0b011)
- Either trigger a message on the bus or wait long enough for at least one message to occur under normal bus operation
- Either using interrupts or polling, check the CAN Bus Message Error Interrupt Flag IRXIF (PIR5<7>)
- If the IRXIF flag is set, clear it, switch the CAN module into configuration mode(CANCON7:5=0b100) and change the baud rate to the next possible baud rate selection, then return to step 4
- If the IRXIF flag is not set, check the receive buffer interrupt flags. If they are set, then clear them and continue on to the application code. Optionally, store the current baud rate in non-volatile memory for use as the starting baud rate on further power-ups to streamline the autobaud sequence.
Running the Demo
The easiest way to demonstrate this is to use a system such as K2L's. With this hardware you can simulate two other nodes on the CAN bus (as this method of autobaud cannot work with only one other device) and send messages at a known baud rate between them in order to cause the autobaud code to find the correct rate and flash the LED to indicate that it is properly receiving messages.
- Dec 4, 2018