Hi everyone, we are gonna talk about how to communicate with CAN BUS protocol with CAN INTERRUPTS on STM32F407 Discovery board.

Now lets get started. Firstly I want to talk about what CAN BUS is?

CAN is a communication protocol that connects all devices can communicate with each other. CAN stands for Controller Area Network. This technology developed by Bosch Automotive company to reduce cabling loads and make systems seperates from each other. Because if there is a problem in any sensor, this problem will harm all system. In can bus system all systems working individual and just communicate via CAN BUS. Debugging in CAN BUS is very easy, because every device has a unique id code and devices use this id number to communicate each other. CAN BUS can communicate with very high distances fast and secure.

Now SHOW TIME. Lets get started to code…

First we need to create some variables to Receive and send datas with CANBUS.

Now lets create CAN BUS data sender method. We will use the TxMessage Variable in here. We will set message id number, DLC and data packets. After setting these configurations we are going to transmit data to CAN bus.

Lets write this code:

We have created the sender method.

As I said begining of the article we will use the interrupts to receive CAN data. So now let me create method to get CAN bus data.and load it on the RxMessage struct. There is a __weak method named CAN1_RX0_IRQHandler in Std Periphal libraries. We will declare this function in our main.c file and set this as IRQ_Handler. First create method and load datas into struct. In main function we will create an NVIC and set this handler as interrupt function.

ATTENTION : NEVER CHANGE THE NAME OF FUNCTION. HEADER LIBRARY KNOW THIS INTERRUPT WITH THIS NAME.

Now just we need to prepare a NVIC to use CAN BUS interrupt. We will set the NVIC and CAN BUS in main function. Now lets get started to configure these configuration.

Here is the my main function. I will share the code first then explain it part by part.

Lets get started to explain part by part…

In here we configured the can bus communication frequency as 1MBaud. APB1 bus has 42MHz. We are using to configure frequency we are using the SJW, BS1, BS2 and prescaler values.

Here is the equasition :

BusFrequency / Prescaler * ( BS1 + BS2 + SJW ) .

You can set the filter consigurations as you want. So I will not explain again the filter settings. Now I will explain the NVIC settings.

First we activvated the FIFO for CAN BUS. FIFO stands for First In First Out. So the datas comes first will out first. We can do something the datas as serialized as received.

Now you are good to use the CAN BUS with STM32F4 in Standart Periphals. This is the end of the article.

Keep following my articles.

Have a nice coding.

Burak Hamdi TUFAN…

LEAVE A REPLY

Please enter your comment!
Please enter your name here