当前位置:网站首页>[stm32f103rct6] can communication
[stm32f103rct6] can communication
2022-07-25 02:54:00 【Jingle cat that can use magic】
【 software design 】
CAN Pin initialization
/*
* Function name :CAN_GPIO_Config
* describe :CAN Of GPIO To configure
* Input : nothing
* Output : nothing
* call : Internal calls
*/
static void CAN_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(CAN_TX_GPIO_CLK | CAN_RX_GPIO_CLK, ENABLE);
// Remap pin
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
/* Configure CAN TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN_TX_GPIO_PORT, &GPIO_InitStructure);
/* Configure CAN RX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Pull up input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN_RX_GPIO_PORT, &GPIO_InitStructure);
}CAN Interrupt priority configuration
/*
* Function name :CAN_NVIC_Config
* describe :CAN Of NVIC To configure , The first 1 Priority groups ,0,0 priority
* Input : nothing
* Output : nothing
* call : Internal calls
*/
static void CAN_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Interrupt settings */
NVIC_InitStructure.NVIC_IRQChannel = CAN_RX_IRQ; //CAN1 RX0 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // preemption 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // The sub priority is 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}CAN Mode configuration
/*
* Function name :CAN_Mode_Config
* describe :CAN The pattern of To configure
* Input : nothing
* Output : nothing
* call : Internal calls
*/
static void CAN_Mode_Config(void)
{
CAN_InitTypeDef CAN_InitStructure;
/************************CAN Communication parameter settings **********************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
/*CAN Register initialization */
CAN_DeInit(CANx);
CAN_StructInit(&CAN_InitStructure);
/*CAN Unit initialization */
CAN_InitStructure.CAN_TTCM = DISABLE; //MCR-TTCM Turn off time trigger communication mode enable
CAN_InitStructure.CAN_ABOM = ENABLE; //MCR-ABOM Automatic offline Management
CAN_InitStructure.CAN_AWUM = ENABLE; //MCR-AWUM Use auto wake mode
CAN_InitStructure.CAN_NART = DISABLE; //MCR-NART Automatic retransmission of messages is prohibited DISABLE- Automatic retransmission
CAN_InitStructure.CAN_RFLM = DISABLE; //MCR-RFLM receive FIFO Lock mode DISABLE- In case of overflow, the new message will cover the original message
CAN_InitStructure.CAN_TXFP = DISABLE; //MCR-TXFP send out FIFO priority DISABLE- The priority depends on the message identifier
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; // Normal operation mode
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //BTR-SJW Resynchronize jump width 2 Time units
/* ss=1 bs1=9 bs2=8 Bit time width is (1+8+9) The baud rate is the clock period tq*(1+8+9) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; //BTR-TS1 Period of time 1 Occupied 9 Time units
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; //BTR-TS1 Period of time 9 Occupied 8 Time units
/* CAN Baudrate = 1 MBps (1MBps Already been stm32 Of CAN Maximum speed ) (CAN The clock frequency is APB1 = 36 MHz) */
CAN_InitStructure.CAN_Prescaler = 48; BTR-BRP Baud rate divider Defines the time length of the time unit 36/(1+8+9)/4= 0.5 Mbps
CAN_Init(CANx, &CAN_InitStructure);
}CAN Filter configuration
/*
* Function name :CAN_Filter_Config
* describe :CAN Filter To configure
* Input : nothing
* Output : nothing
* call : Internal calls
*/
static void CAN_Filter_Config(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/*CAN Filter initialization */
CAN_FilterInitStructure.CAN_FilterNumber = 0; // Filter group 0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // Working in mask mode
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // The filter bit width is single 32 position .
/* Enable filter , Compare and screen according to the content of the mark , Expand ID Don't just throw away the following , If yes , Will be deposited in the FIFO0. */
// Configuration of standard data frame
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)0x45<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)0x45<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
// Configuration of extended data frame
// CAN_FilterInitStructure.CAN_FilterIdHigh = ((((u32)0x45 << 3) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF0000) >> 16; // To be screened ID High position
// CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)0x45 << 3) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF; // To be screened ID Low position
// CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; // Filter high 16 Each bit must match
// CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; // Filter low 16 Each bit must match
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; // The filter is associated with FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; // Enable filter
CAN_FilterInit(&CAN_FilterInitStructure);
/*CAN Communication interrupt enable */
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}CAN Consolidation configuration
/*
* Function name :CAN_Config
* describe : Full configuration CAN The function of
* Input : nothing
* Output : nothing
* call : External call
*/
void CAN_Config(void)
{
CAN_GPIO_Config();
CAN_NVIC_Config();
CAN_Mode_Config();
CAN_Filter_Config();
}initialization RxMessage Data structures
/**
* @brief initialization RxMessage Data structures
* @param RxMessage: Point to the data structure to initialize
* @retval None
*/
void Init_RxMes(CanRxMsg *RxMessage)
{
uint8_t ubCounter = 0;
/* Clear the receiving structure */
RxMessage->StdId = 0x00;
RxMessage->ExtId = 0x00;
RxMessage->IDE = CAN_ID_STD;
RxMessage->DLC = 0;
RxMessage->FMI = 0;
for (ubCounter = 0; ubCounter < 8; ubCounter++)
{
RxMessage->Data[ubCounter] = 0x00;
}
}Set up CAN_SetMsg
/*
* Function name :CAN_SetMsg
* describe :CAN Content setting of communication message , Set a data content to 0-7 Data packets of
* Input : Send message structure
* Output : nothing
* call : External call
*/
void CAN_SetMsg(CanTxMsg *TxMessage)
{
// uint8_t ubCounter = 0;
TxMessage->StdId=0x45; // Standards used ID
//TxMessage->ExtId = 0x45;
TxMessage->IDE = CAN_ID_STD; // The standard model
TxMessage->RTR = CAN_RTR_DATA; // It's sending data
TxMessage->DLC = 8; // The data length is 8 byte
/* Set the data to send 0-7*/
// for (ubCounter = 0; ubCounter < 8; ubCounter++)
// {
// TxMessage->Data[ubCounter] = ubCounter;
// }
TxMessage->Data[0] = 0;
TxMessage->Data[1] = speedct*1000;
TxMessage->Data[2] = poct*1000;
TxMessage->Data[3] = 0;
TxMessage->Data[4] = 0;
TxMessage->Data[5] = 0;
TxMessage->Data[6] = 0;
TxMessage->Data[7] = 0;
}CAN Accept service interruption
/*
* Function name :USB_LP_CAN1_RX0_IRQHandler
* describe :USB Break and CAN Receive interrupt service program ,USB Follow CAN public I/O, Only... Is used here CAN The interrupt .
* Input : nothing
* Output : nothing
* call : nothing
*/
void CAN_RX_IRQHandler(void)
{
/* Read messages from the mailbox */
CAN_Receive(CANx, CAN_FIFO0, &RxMessage);
/* Compare ID Is it 0x1314 */
if((RxMessage.StdId==0x45) && (RxMessage.IDE==CAN_ID_STD) && (RxMessage.DLC==8) )
{
flag = 1; // Successful reception
}
else
{
flag = 0; // Reception failed
}
}The main function
int main(void)
{
MY_NVIC_PriorityGroupConfig(2); //===== Set interrupt grouping
delay_init(); //===== Delay function initialization
KEY_Init(); //===== Key initialization
usart1_init(115200); //===== A serial port 1 initialization
CAN_Config(); /* initialization can, In interrupt reception CAN Data packets */
MiniBalance_PWM_Init(7199, 0); //===== initialization PWM 10KHZ, For driving motor To initialize the electric regulating interface
TIM5_Cap_Init(0XFFFF,72-1); //===== Ultrasonic wave begins to melt Default comment Ultrasonic wiring Reference resources timer.h file
TIM3_Cap_Init(0XFFFF,72-1); //===== Ultrasonic wave begins to melt Default comment Ultrasonic wiring Reference resources timer.h file
while(1)
{
/* Set the message to send */
CAN_SetMsg(&TxMessage);
/* Store the message in the sending mailbox , send out */
CAN_Transmit(CANx, &TxMessage);
can_delay(10000);// Wait for the transmission to finish , You can use CAN_TransmitStatus Check the status
if(flag==1)
{
printf("\r\nCAN Data received :\r\n");
CAN_DEBUG_ARRAY(RxMessage.Data,8);
x = RxMessage.Data[0];
ang = RxMessage.Data[1];
flag=0;
}
}
}
CAN The header file
#ifndef __CAN_H
#define __CAN_H
#include "stm32f10x.h"
#include "usart.h"
#define CANx CAN1
#define CAN_CLK RCC_APB1Periph_CAN1
#define CAN_RX_IRQ USB_LP_CAN1_RX0_IRQn
#define CAN_RX_IRQHandler USB_LP_CAN1_RX0_IRQHandler
#define CAN_RX_PIN GPIO_Pin_8
#define CAN_TX_PIN GPIO_Pin_9
#define CAN_TX_GPIO_PORT GPIOB
#define CAN_RX_GPIO_PORT GPIOB
#define CAN_TX_GPIO_CLK (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB)
#define CAN_RX_GPIO_CLK RCC_APB2Periph_GPIOB
/*debug*/
#define CAN_DEBUG_ON 1
#define CAN_DEBUG_ARRAY_ON 1
#define CAN_DEBUG_FUNC_ON 1
// Log define
#define CAN_INFO(fmt, arg...) printf("<<-CAN-INFO->> " fmt "\n", ##arg)
#define CAN_ERROR(fmt, arg...) printf("<<-CAN-ERROR->> " fmt "\n", ##arg)
#define CAN_DEBUG(fmt, arg...) \
do \
{ \
if (CAN_DEBUG_ON) \
printf("<<-CAN-DEBUG->> [%d]" fmt "\n", __LINE__, ##arg); \
} while (0)
#define CAN_DEBUG_ARRAY(array, num) \
do \
{ \
int32_t i; \
float *a = array; \
if (CAN_DEBUG_ARRAY_ON) \
{ \
printf("<<-CAN-DEBUG-ARRAY->>\n"); \
for (i = 0; i < (num); i++) \
{ \
printf("%f ", (a)[i]); \
if ((i + 1) % 10 == 0) \
{ \
printf("\n"); \
} \
} \
printf("\n"); \
} \
} while (0)
#define CAN_DEBUG_FUNC() \
do \
{ \
if (CAN_DEBUG_FUNC_ON) \
printf("<<-CAN-FUNC->> Func:%[email protected]:%d\n", __func__, __LINE__); \
} while (0)
static void CAN_GPIO_Config(void);
static void CAN_NVIC_Config(void);
static void CAN_Mode_Config(void);
static void CAN_Filter_Config(void);
void CAN_Config(void);
void CAN_SetMsg(CanTxMsg *TxMessage);
void Init_RxMes(CanRxMsg *RxMessage);
#endif
I'm a rookie , Everyone's encouragement is the driving force for me to continue to create , If you think it's good , Welcome to your attention , give the thumbs-up , Collection , forward , thank you !
边栏推荐
- Digital business cloud: how to realize the application value of supplier SRM management system?
- Class notes (4) (2) -- 572. Compete
- Js a simple way to store several objects in an array
- Selenium framework operation steelth.min.js file hides browser fingerprint features
- Beginners must see the markdown User Guide
- After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
- "Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
- How to communicate with aliens
- Details of C language compilation preprocessing and comparison of macros and functions
- Case analysis of building exhibition service management system with low code development platform
猜你喜欢

Use unicloud cloud function to decode wechat motion steps in applet

Keil compile download error: no algorithm found for: 08000000h - 08001233h solution

Mgre.hdlc.ppp.chap.nat comprehensive experiment

English grammar_ Reflexive pronoun

Dynamic programming -- Digital DP

Mark down learning

Conceptual distinction between Po, Bo, VO, dto and POJO

Sword finger offer 11. rotate the minimum number of the array

Case analysis of building exhibition service management system with low code development platform

Vulntarget vulnerability shooting range -vulntarget-b
随机推荐
Matlab draws radar chart (four lines of code)
Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template
Sequence diagram of UML diagram series
Interview question -- event cycle
JS foundation -- math
JS foundation -- data
SQL recursive follow-up
Operator explanation - C language
JS written test question -- browser kernel
Visualization of correlation coefficient matrix
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Nuscenes data set summary
Explorer TSSD 2019 software installation package download and installation tutorial
JS foundation -- hijacking of this keyword
Automatic backup of Linux server PostgreSQL database
Cookies and sessions
Physical experiment simulation
Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming