当前位置:网站首页>[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 !
边栏推荐
- Routing policy interferes with routing
- Classic network learning RESNET code implementation
- Domain driven model (DDD)
- Go common standard library -time
- BGP introduction
- hello csdn
- Conceptual distinction between Po, Bo, VO, dto and POJO
- Operator explanation - C language
- JS foundation -- data
- JS written test question -- prototype, new, this comprehensive question
猜你喜欢

Mark down learning

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port

R language one page and many pictures

Use pytest + allure to show the chart results (3)

IO (1) -io layering

JS written test questions -- random numbers, array de duplication

Ctfshow misc introduction

If there is a segment in the encryption field, are you "bronze" or "King"?

Beginners must see the markdown User Guide

Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem
随机推荐
Beginners must see the markdown User Guide
HAC cluster is modified to stand-alone
Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
It7259q-13, it7259ex-24 feature wearable devices
Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
Download the jar package of jsqlparser and PageHelper
The latest interview questions and analysis of software testing in 2022
Mp4 package analysis
Matlab for circular pit
"Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
Study notes of filebeat
English grammar_ Reflexive pronoun
"Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
List.stream common operations
Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
Explorer TSSD 2019 software installation package download and installation tutorial
Domain driven model (DDD)
SQL Server 2022 installation
Learning record XIII