STM32 CAN bus usage

Publisher:WhisperingWindLatest update time:2019-04-12 Keywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This debugging uses the STM32 core board of Zhuhai D-Chip Technology. This core board contains a CAN bus driver. It can easily connect and communicate with multiple industrial devices at the same time.


The CAN bus of the STM32 processor has multiple working modes. This test is in normal mode. To use it in normal mode, you first need to initialize the registers and enable related functional components.


The first step is to initialize the GPIO and clock. The CAN bus GPIO of STM32F103C is GPIOA_PIN_11 and GPIOA_PIN_12.



RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //Enable PORTA clock                    

 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //Enable CAN1 clock

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//Multiplex push-pull

GPIO_Init(GPIOA, &GPIO_InitStructure);// Initialize IO

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//Pull up input

GPIO_Init(GPIOA, &GPIO_InitStructure);// Initialize IO


After configuring GPIO, you need to configure the CAN bus


//CAN unit settings

CAN_InitStructure.CAN_TTCM=DISABLE;//Non-time triggered communication mode  

CAN_InitStructure.CAN_ABOM=ENABLE;//Automatic offline management of software 

CAN_InitStructure.CAN_AWUM=DISABLE;//Sleep mode wakeup by software (clear the SLEEP bit of CAN->MCR)

CAN_InitStructure.CAN_NART=ENABLE;// Disable automatic message transmission 

CAN_InitStructure.CAN_RFLM=DISABLE; //The message is not locked, the new one overwrites the old one  

CAN_InitStructure.CAN_TXFP=DISABLE;//The priority is determined by the message identifier 

CAN_InitStructure.CAN_Mode= mode;        //Mode setting: mode:0, normal mode; 1, loopback mode; 

//Set the baud rate

CAN_InitStructure.CAN_SJW=tsjw;//Resynchronization jump width (Tsjw) is tsjw+1 time unit  

CAN_InitStructure.CAN_BS1=tbs1;//Tbs1=tbs1+1 time unit

CAN_InitStructure.CAN_BS2=tbs2;//Tbs2=tbs2+1 time unit

CAN_InitStructure.CAN_Prescaler=brp; //Frequency division factor (Fdiv) is brp+1

CAN_Init(CAN1, &CAN_InitStructure);        //Initialize CAN1 


The CAN bus also needs to configure the receiving ID and filter


CAN_FilterInitStructure.CAN_FilterNumber=0;//Filter 0

CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;// Mask bit pattern

CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//32 bits wide 

CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//32-bit ID

CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;

CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32-bit MASK

CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;

CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0; //Filter 0 is associated with FIFO0

CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //Activate filter 0

 

CAN_FilterInit(&CAN_FilterInitStructure);// Initialize the filter


Finally, set up the CAN interrupt service



CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0 message registration interrupt is enabled.    

 

NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // The main priority is 1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // The sub-priority is 0

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);



I tested that the 1Mhz speed communication is fine. And the CAN bus will not get stuck even if the automatic offline management is turned on and the two pins are short-circuited.


Keywords:STM32 Reference address:STM32 CAN bus usage

Previous article:Problems implementing CAN communication using STM32F1xx_HAL library
Next article:STM32F4 uses FreeRTOS interrupts

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号