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.
Previous article:Problems implementing CAN communication using STM32F1xx_HAL library
Next article:STM32F4 uses FreeRTOS interrupts
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Comment and download the information, participate in the lucky draw, and let us take the first step in technical exchange and sharing! !
- "Power Amplifier Experiment Case" Application of Power Amplifier in Capacitive Coupling Wireless Power Transfer System
- PCB design principles of high frequency wiring
- Dry cell boost 3V, dry cell boost 3.3V chip output current test, maximum 500MA
- Its predecessor is Fairchild's motor product line. Come to the forum solo. Do you want to catch it for testing?
- [CH563L Review] Part 2 Based on uCos, using FIFO algorithm to implement UART queue
- [National Technology N32 MCU Development Package] --N32G032 Series
- [STM32WB55 Review] + Registers can be avoided, but RTOS cannot be avoided
- EEWORLD University ---- Linux Embedded Development
- [Today at 10:00 live broadcast] Deep digging into NXP LPC55S69: a new generation of secure and low-power MCU based on ARM Cortex-M33 core