当前位置:网站首页>Can communication test based on STM32: turn the globe
Can communication test based on STM32: turn the globe
2022-07-25 06:32:00 【an520_】
Although it was right very early CAN I have heard of the agreement , But I have never had a chance to explore , So like many people , Yes CAN Our cognition only stays at “ Communication used in the automotive field ”. Because I have been looking at lithium batteries recently BMS:
The content involves CAN signal communication , And I will also hear some from customers at work J1939、NMEA2000 etc. , So I felt it was time to understand CAN 了 .
One 、CAN
Controller area network (Controller Area Network), It was originally developed specifically to solve the serial communication of passenger cars , It has high real-time processing ability 、 High reliability and good error detection ability under strong electromagnetic interference . Its main purpose is to simplify the control connection between electronic control units in the car , cost reduction .CAN Each communication node in the protocol is usually composed of a controller and a transceiver :

This communication structure is non master-slave , That is, any node can speak freely on the bus , The biggest problem with broadcast communication is that there is no object , Each node always selects the message that it cares about on the bus to receive , Then process , Filter out what you don't care about . That's the question , How do nodes distinguish messages ? This is actually where I was confused at the beginning , Until you understand the arbitration domain in the message frame .
Two 、 message
CAN The message frame of is divided into standard frame and extended frame , That is to say CAN2.0A and CAN2.0B,CAN2.0A It only supports standard frames , and CAN2.0B It supports extended frames . In common use CAN Take data frame as an example , The standard frame structure is as follows :

Total standard frames 127bits, The identifiers in the arbitration domain are 11bits, this 11 Bit identifiers are used not only as priorities but also as message differentiation , But this 11 Bit identifiers limit the maximum number of nodes in the same network , To expand the largest communication node , So there is the extended frame :

The expansion frame is mainly increased 18 Extended identifier of bit , Therefore, the identifier of the extended frame reaches 29bits, The total length of the frame is 150bits.
3、 ... and 、SAE J1939
SAE J1939 With CAN2.0B Based on , adopt CAN Bus for data communication , Its data link layer defines the data structure of the information frame 、 Encoding rules 、 Including communication priority 、 transport 、 Communication requirements 、 Bus arbitration, etc , Responsible for 29 Bit identifiers for regrouping definitions , Enable the message identifier to describe all the characteristics of the message , Including target address and source address . stay SAE J1939 The most important part of is the protocol data unit (PDU), It defines a framework to organize data frames ,PDU The composition of the is as follows :

P: priority , The highest level is 0
R: Keep a , Default 0
DP: Data pages
PF: Group numbered fields (PGN)
PS: Destination address
SA: source address
DataField: data
For source address and destination address , stay J1939 There is a definite definition in , This also means that this set of agreements is a proprietary agreement , In fact, this agreement is usually used for trucks . Now let's simply list some defined addresses :
0x00 For engine controller ;
0x0B For the brake system controller ;
0x13 For the steering controller ;
0x18 For dash cam ;
0x33 For tire pressure controller ;
0xFF For the whole ( That is, all nodes );
Four 、STM32 CAN
STM32 Integrated CAN controller , Controller support CAN2.0A And CAN2.0B, So in use STM32f103 Minimum system development board CAN Communication time , Just put it outside CAN Transceiver is ok , The CAN Transceiver is TJA1050, The line connections are as follows :

STM32 There are corresponding CAN library , Need to declare reference :

And then there was GPIO Port configuration :

And then there was NVIC To configure ,CAN The reception of is using USB Interrupt to achieve , So you need to configure NVIC Interrupt priority :

And then there was CAN Configuration of :

You can see CAN_Filter Both high and low positions of are set 0, Indicates no filtering , In this way, it will receive all messages . If a filter is used, the configuration is as follows :

The filter has two modes : Masks and lists . seeing the name of a thing one thinks of its function , Mask mode can filter single or one segment ID, The list mode can only filter the , Filter details are as follows :

The mask setting process is as follows :

CAN Sending function of Library :

CAN The receiving function of the Library :

Usually put the receiving function into the interrupt function :

No data has been received during the first test , The reason is that I forgot to write USB Interrupt function .
5、 ... and 、 Connect
because CAN Communication is the communication between nodes , Therefore, if you want to view the sent message, you need to transfer the protocol and interface , At present, what is more commonly used is CAN box , You can directly CAN Message through USB Output to PC, But this time STM32 Development board , You can directly put CAN Message through USART Output to PC, The control schematic diagram is as follows :
Connect the actual picture :

The stepper motor part is directly applied to what was done before PWM Control motor .
6、 ... and 、 test
CAN The data field is the largest 64bits, That's the most 8Bytes data , Therefore, the data structure defined this time is that the first four digits are the speed setting value of the stepping motor , The last four digits are the set value of the stepper motor position , for example 02003200 It means that the speed is 200, The position value is 3200, The test video is as follows :
be based on STM32 Of CAN Communication test : Let the globe turn
The stepper motor driver is set to 3200 pulse / turn , Therefore, the position is set to 3200 Then turn around , The position is set to 1600 Then turn half a turn , The test results are also as expected .
【 Learning exchange group 769843038】
【 Free information package of online disk collected by yourself , If you need it, you can collect it yourself 】:
【 Study the tutorial 】:
Build a smart home project from scratch
Take you to learn stm32 surf the internet
STM32 Single chip microcomputer GPIO Development
STM32- Press the key to shake off
边栏推荐
- MySQL index collation summary
- 容器内组播
- 【transformer】DeiT
- Analysis of the calling principle of Changan chain solid smart contract
- Detailed explanation of the difference, working principle and basic structure between NMOS and PMOS
- Some interview questions collected
- Keilc51 usage details (III)
- Easy gene chip SEQ analysis method: practical workflow and advanced applications
- mysql数据库备份和恢复
- Cout format output common functions and flags summary
猜你喜欢

【C】程序环境和预处理

Labelme labels different objects, displays different colors and batch conversion

Detailed explanation of the difference, working principle and basic structure between NMOS and PMOS

深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
![[datawhale202207] reinforcement learning: the foundation of reinforcement learning](/img/1e/6ba657da14508854207aac51992ad8.png)
[datawhale202207] reinforcement learning: the foundation of reinforcement learning

The code of Keil and Si compiler is not aligned??
![[C language] program environment and preprocessing](/img/d6/d59a0d8d286ea9408043d8ad1e1348.png)
[C language] program environment and preprocessing

Addition, deletion, modification and query of DOM elements

It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here

Do you know the same period last year in powerbi
随机推荐
Mlx90640 infrared thermal imager temperature measurement module development notes (I)
R strange grammar summary
四、MFC工具栏、运行时类信息机制、运行时创建机制
target_compile_features specified unknown feature “cxx_std_14“ for target
4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism
SAP FICO 第三节 BDC和LTMC导入S4财务科目
UML modeling tools Visio, rational rose, powerdesign
[reprint] pycharm packages.Py program as executable exe
Ida Pro novice tutorial
What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
MySQL queries the table name under the current database
百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!
U-boot-1.1.6 transplant notes (beginner)
MFC IniFile Unicode mode reading method
What projects can make money online? Is it reliable to be we media?
【datawhale202207】强化学习:策略梯度和近端策略优化
Function template learning record
【C】 Program environment and pretreatment
Standard C language 89
Seekbar attribute reference