CAN 设备通讯实验代码+HAL 与原生库分别实现

实验准备:
两块带有CAN 收发器的STM32 MCU板子
在这里插入图片描述

1.HAL 库实现部分

pin 设置,依据datasheet 和原理图
在这里插入图片描述
在这里插入图片描述

时钟设置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

工程设置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1) main.c

/* USER CODE BEGIN Header */
/********************************************************************************* @file    can.c* @brief   This file provides code for the configuration*          of the CAN instances.******************************************************************************* @attention** Copyright (c) 2022 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "can.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 */CAN_HandleTypeDef hcan;void ledOnGetSignal() {HAL_GPIO_TogglePin(GPIOC, LED_Pin);HAL_Delay(1000);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	//HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_Delay(1000);
}
void ledOnGetSignal2() {HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	HAL_Delay(500);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	//HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_Delay(500);
}
/* USER CODE END Includes */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{  extern int g;CAN_RxHeaderTypeDef  RxMessage;uint8_t Data[2];//  extern uint16_t feedback;RxMessage.DLC = 2;RxMessage.StdId = 0x00;RxMessage.ExtId = 0x0000;RxMessage.IDE=CAN_ID_EXT;RxMessage.RTR=CAN_RTR_DATA;if (HAL_CAN_GetState(hcan)!= RESET){HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxMessage, Data);//	feedback=Data[2]<<8 |Data[3];    			if (Data[0] == 0x99) {ledOnGetSignal();}if (Data[0] == 0x99 ) {ledOnGetSignal2();}}
}/* CAN init function */
void MX_CAN_Init(void)
{/* USER CODE BEGIN CAN_Init 0 *//* USER CODE END CAN_Init 0 *//* USER CODE BEGIN CAN_Init 1 *//* USER CODE END CAN_Init 1 */hcan.Instance = CAN1;hcan.Init.Prescaler = 60;hcan.Init.Mode = CAN_MODE_NORMAL;hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;hcan.Init.TimeSeg1 = CAN_BS1_3TQ;hcan.Init.TimeSeg2 = CAN_BS2_2TQ;hcan.Init.TimeTriggeredMode = ENABLE;hcan.Init.AutoBusOff = DISABLE;hcan.Init.AutoWakeUp = DISABLE;hcan.Init.AutoRetransmission = DISABLE;hcan.Init.ReceiveFifoLocked = DISABLE;hcan.Init.TransmitFifoPriority = ENABLE;if (HAL_CAN_Init(&hcan) != HAL_OK){Error_Handler();}/* USER CODE BEGIN CAN_Init 2 */CAN_FilterTypeDef sFilterConfig;sFilterConfig.FilterBank = 0;   //chenal 0sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;   //????????sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;  //????????32?sFilterConfig.FilterIdHigh = 0x0000;  //??????   sFilterConfig.FilterIdLow = 0x0000;   //??????   //MASK bit 0 means don't care,bit 0 means match sFilterConfig.FilterMaskIdHigh = 0x0000;   //?????  //?????????????,???????????????? sFilterConfig.FilterMaskIdLow = 0x0000;    //?????                                 sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; //FIFO0????FIFO1????????,?????????????FIFO0?sFilterConfig.FilterActivation = CAN_FILTER_ENABLE;   //enable filtersFilterConfig.SlaveStartFilterBank = 0;    //???can????????,????CAN??,????????HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);/* USER CODE END CAN_Init 2 */}void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(canHandle->Instance==CAN1){/* USER CODE BEGIN CAN1_MspInit 0 *//* USER CODE END CAN1_MspInit 0 *//* CAN1 clock enable */__HAL_RCC_CAN1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**CAN GPIO ConfigurationPA11     ------> CAN_RXPA12     ------> CAN_TX*/GPIO_InitStruct.Pin = GPIO_PIN_11;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_12;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* CAN1 interrupt Init */HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);/* USER CODE BEGIN CAN1_MspInit 1 *//* USER CODE END CAN1_MspInit 1 */}
}void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{if(canHandle->Instance==CAN1){/* USER CODE BEGIN CAN1_MspDeInit 0 *//* USER CODE END CAN1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_CAN1_CLK_DISABLE();/**CAN GPIO ConfigurationPA11     ------> CAN_RXPA12     ------> CAN_TX*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);/* CAN1 interrupt Deinit */HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);/* USER CODE BEGIN CAN1_MspDeInit 1 *//* USER CODE END CAN1_MspDeInit 1 */}
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */
  1. gpio.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file    gpio.c* @brief   This file provides code for the configuration*          of all used GPIO pins.******************************************************************************* @attention*     * Copyright (c) 2022 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "gpio.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 *//* USER CODE END 1 *//** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTI
*/
void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);/*Configure GPIO pin : PtPin */GPIO_InitStruct.Pin = LED_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}/* USER CODE BEGIN 2 *//* USER CODE END 2 */
  1. can.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file    can.c* @brief   This file provides code for the configuration*          of the CAN instances.******************************************************************************* @attention** Copyright (c) 2022 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "can.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 */CAN_HandleTypeDef hcan;void ledOnGetSignal() {HAL_GPIO_TogglePin(GPIOC, LED_Pin);HAL_Delay(1000);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	//HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_Delay(1000);
}
void ledOnGetSignal2() {HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	HAL_Delay(500);
//	HAL_GPIO_TogglePin(GPIOC, LED_Pin);
//	//HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET);
//	HAL_Delay(500);
}
/* USER CODE END Includes */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{  extern int g;CAN_RxHeaderTypeDef  RxMessage;uint8_t Data[2];//  extern uint16_t feedback;RxMessage.DLC = 2;RxMessage.StdId = 0x00;RxMessage.ExtId = 0x0000;RxMessage.IDE=CAN_ID_EXT;RxMessage.RTR=CAN_RTR_DATA;if (HAL_CAN_GetState(hcan)!= RESET){HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxMessage, Data);//	feedback=Data[2]<<8 |Data[3];    			if (Data[0] == 0x99) {ledOnGetSignal();}if (Data[0] == 0x99 ) {ledOnGetSignal2();}}
}/* CAN init function */
void MX_CAN_Init(void)
{/* USER CODE BEGIN CAN_Init 0 *//* USER CODE END CAN_Init 0 *//* USER CODE BEGIN CAN_Init 1 *//* USER CODE END CAN_Init 1 */hcan.Instance = CAN1;hcan.Init.Prescaler = 60;hcan.Init.Mode = CAN_MODE_NORMAL;hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;hcan.Init.TimeSeg1 = CAN_BS1_3TQ;hcan.Init.TimeSeg2 = CAN_BS2_2TQ;hcan.Init.TimeTriggeredMode = ENABLE;hcan.Init.AutoBusOff = DISABLE;hcan.Init.AutoWakeUp = DISABLE;hcan.Init.AutoRetransmission = DISABLE;hcan.Init.ReceiveFifoLocked = DISABLE;hcan.Init.TransmitFifoPriority = ENABLE;if (HAL_CAN_Init(&hcan) != HAL_OK){Error_Handler();}/* USER CODE BEGIN CAN_Init 2 */CAN_FilterTypeDef sFilterConfig;sFilterConfig.FilterBank = 0;   //chenal 0sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;   //????????sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;  //????????32?sFilterConfig.FilterIdHigh = 0x0000;  //??????   sFilterConfig.FilterIdLow = 0x0000;   //??????   //MASK bit 0 means don't care,bit 0 means match sFilterConfig.FilterMaskIdHigh = 0x0000;   //?????  //?????????????,???????????????? sFilterConfig.FilterMaskIdLow = 0x0000;    //?????                                 sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; //FIFO0????FIFO1????????,?????????????FIFO0?sFilterConfig.FilterActivation = CAN_FILTER_ENABLE;   //enable filtersFilterConfig.SlaveStartFilterBank = 0;    //???can????????,????CAN??,????????HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);/* USER CODE END CAN_Init 2 */}void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(canHandle->Instance==CAN1){/* USER CODE BEGIN CAN1_MspInit 0 *//* USER CODE END CAN1_MspInit 0 *//* CAN1 clock enable */__HAL_RCC_CAN1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**CAN GPIO ConfigurationPA11     ------> CAN_RXPA12     ------> CAN_TX*/GPIO_InitStruct.Pin = GPIO_PIN_11;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_12;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* CAN1 interrupt Init */HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);/* USER CODE BEGIN CAN1_MspInit 1 *//* USER CODE END CAN1_MspInit 1 */}
}void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{if(canHandle->Instance==CAN1){/* USER CODE BEGIN CAN1_MspDeInit 0 *//* USER CODE END CAN1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_CAN1_CLK_DISABLE();/**CAN GPIO ConfigurationPA11     ------> CAN_RXPA12     ------> CAN_TX*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);/* CAN1 interrupt Deinit */HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);/* USER CODE BEGIN CAN1_MspDeInit 1 *//* USER CODE END CAN1_MspDeInit 1 */}
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */
  1. ST 老版本库
  1. main.c
/******************** ********************* ÎļþÃû  £ºmain.c* ÃèÊö    £º¸øÏàͬÁ½¸öMINI°åÏÂÔش˳ÌÐòºó£¬°´ÏÂÒ»¸ö°å×ÓÉϵİ´¼ü£¬¿ÉÒÔµãÁÁÁíÒ»¸ö°å×Ó*           É϶ÔÓ¦µÄLED¡£Á½¸ö°å×Ó²»·ÖÖ÷´Ó¡£     receiv site       * ʵÑéƽ̨£ºMINI STM32¿ª·¢°å »ùÓÚSTM32F103VET6* ¿â°æ±¾  £ºST3.0.0   																										  
*********************************************************/
#include "stm32f10x.h"
#include "can.h"
#include "led.h"void Delay(vu32 nCount);/*°´¼ü¹Ü½Å³õʼ»¯*/
void KeyInit(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ʹÄÜÍâÉèʱÖÓRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ʹÄÜÍâÉèʱÖÓGPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_15 ;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //×î¸ßÊä³öËÙÂÊ10MHzGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//ÉÏÀ­ÊäÈëGPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //×î¸ßÊä³öËÙÂÊ10MHzGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀ­ÊäÈëGPIO_Init(GPIOA, &GPIO_InitStructure);
}void lightOn(void)
{LED1( ON );			  // ÁÁ   D3Delay(0x200000);LED1( OFF );		  // ÃðLED2( ON );   //D2Delay(0x200000);LED2( OFF );LED3( ON );   //D2Delay(0x200000);LED3( OFF );      }/*¼ì²âÊÇ·ñÓа´¼ü°´ÏÂ*/
void  GetKey(void)
{if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)){Delay(1000000);//È¥¶¶¶¯if(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)){while(Bit_RESET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)){ ; } //µÈ´ý°´¼üÊÍ·Åcan_tx(0X11,0X33); lightOn();}}if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)){Delay(1000000);//È¥¶¶¶¯//È¥¶¶¶¯if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)){while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)){ ; }//µÈ´ý°´¼üÊÍ·Å                        can_tx(0X55,0X77); lightOn();}}if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)){Delay(1000000);//È¥¶¶¶¯//È¥¶¶¶¯if(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)){while(Bit_RESET == GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)){ ; }//µÈ´ý°´¼üÊÍ·Å                        can_tx(0X99,0Xbb); 	lightOn();}}       
}int main(void)
{   SystemInit(); //ÉèÖÃϵͳʱÖÓΪ72MKeyInit();	  //°´¼ü¹Ü½Å³õʼ»¯LED_GPIO_Config();//LED¹Ü½Å³õʼ»¯CAN_GPIO_Config();//CAN¹Ü½Å³õʼ» 1¯CAN_NVIC_Configuration(); //CANÖжϳõʼ»¯   CAN_INIT();//CA³õʼ»¯NÄ£¿é	while(1){GetKey();	//¼ì²âÊÇ·ñÓа´¼ü°´ÏÂ}
}
  1. led.c
/***************************************** ÎļþÃû  £ºled.c* ÃèÊö    £ºled Ó¦Óú¯Êý¿â*          * ʵÑéƽ̨£ºMINI STM32¿ª·¢°å »ùÓÚSTM32F103VET6* Ó²¼þÁ¬½Ó£º-----------------*          |   PB14 - LED1   |*          |   PB12 - LED2   |*          |   PB13 - LED3   |*           ----------------- * ¿â°æ±¾  £ºST3.0.0  																										  
*********************************************************/
#include "led.h"/***************  ÅäÖÃLEDÓõ½µÄI/O¿Ú *******************/
void LED_GPIO_Config(void)	
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // ʹÄÜPB¶Ë¿ÚʱÖÓ  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);  //³õʼ»¯PB¶Ë¿ÚGPIO_SetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14);	 // ¹Ø±ÕËùÓÐLED
}
  1. can.c
/***************************************** ÎļþÃû  £ºcan.c* ÃèÊö    £ºCAN²âÊÔÓ¦Óú¯Êý¿â¡£         * ʵÑéƽ̨£ºMINI STM32¿ª·¢°å »ùÓÚSTM32F103VET6* Ó²¼þÁ¬½Ó£º-----------------*          | 				  ||  PB8-CAN-RX     |*          |  PB9-CAN-TX     |*          |                 |*           -----------------* ¿â°æ±¾  £ºST3.0.0  																										  
*********************************************************/
#include "can.h"
#include "led.h"
#include "stdio.h"typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;/* ÔÚÖжϴ¦Àíº¯ÊýÖзµ»Ø */
__IO uint32_t ret = 0;volatile TestStatus TestRx;	/*CAN RX0 ÖжÏÓÅÏȼ¶ÅäÖà  */void CAN_NVIC_Configuration(void)
{NVIC_InitTypeDef NVIC_InitStructure;/* Configure the NVIC Preemption Priority Bits */  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);#ifdef  VECT_TAB_RAM  /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else  /* VECT_TAB_FLASH  *//* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   #endif/* enabling interrupt */NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);
}/*CAN GPIO ºÍʱÖÓÅäÖà */void CAN_GPIO_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure; /* ¸´Óù¦ÄܺÍGPIOB¶Ë¿ÚʱÖÓʹÄÜ*/	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);	                        											 /* CAN1 Ä£¿éʱÖÓʹÄÜ */RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* Configure CAN pin: RX */	 // PB8GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;	 // ÉÏÀ­ÊäÈëGPIO_Init(GPIOB, &GPIO_InitStructure);/* Configure CAN pin: TX */   // PB9GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³öGPIO_Init(GPIOB, &GPIO_InitStructure);//#define GPIO_Remap_CAN    GPIO_Remap1_CAN1 ±¾ÊµÑéûÓÐÓõ½ÖØÓ³ÉäI/OGPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);}/*	CAN³õʼ»¯ */void CAN_INIT(void)
{CAN_InitTypeDef        CAN_InitStructure;CAN_FilterInitTypeDef  CAN_FilterInitStructure;
//  CanTxMsg TxMessage;  /* CAN register init */CAN_DeInit(CAN1);	//½«ÍâÉèCANµÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵCAN_StructInit(&CAN_InitStructure);//°ÑCAN_InitStructÖеÄÿһ¸ö²ÎÊý°´È±Ê¡ÖµÌîÈë/* CAN cell init */CAN_InitStructure.CAN_TTCM=DISABLE;//ûÓÐʹÄÜʱ¼ä´¥·¢Ä£Ê½CAN_InitStructure.CAN_ABOM=DISABLE;//ûÓÐʹÄÜ×Ô¶¯ÀëÏß¹ÜÀíCAN_InitStructure.CAN_AWUM=DISABLE;//ûÓÐʹÄÜ×Ô¶¯»½ÐÑģʽCAN_InitStructure.CAN_NART=DISABLE;//ûÓÐʹÄÜ·Ç×Ô¶¯ÖØ´«Ä£Ê½CAN_InitStructure.CAN_RFLM=DISABLE;//ûÓÐʹÄܽÓÊÕFIFOËø¶¨Ä£Ê½CAN_InitStructure.CAN_TXFP=DISABLE;//ûÓÐʹÄÜ·¢ËÍFIFOÓÅÏȼ¶CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//CANÉèÖÃΪÕý³£Ä£Ê½CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //ÖØÐÂͬ²½ÌøÔ¾¿í¶È1¸öʱ¼äµ¥Î»CAN_InitStructure.CAN_BS1=CAN_BS1_3tq; //ʱ¼ä¶Î1Ϊ3¸öʱ¼äµ¥Î»CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; //ʱ¼ä¶Î2Ϊ2¸öʱ¼äµ¥Î»CAN_InitStructure.CAN_Prescaler=60;  //ʱ¼äµ¥Î»³¤¶ÈΪ60	CAN_Init(CAN1,&CAN_InitStructure);//²¨ÌØÂÊΪ£º72M/2/60(1+3+2)=0.1 ¼´100K/* CAN filter init */CAN_FilterInitStructure.CAN_FilterNumber=1;//Ö¸¶¨¹ýÂËÆ÷Ϊ1CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//Ö¸¶¨¹ýÂËÆ÷Ϊ±êʶ·ûÆÁ±ÎλģʽCAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//¹ýÂËÆ÷λ¿íΪ32λCAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;// ¹ýÂËÆ÷±êʶ·ûµÄ¸ß16λֵCAN_FilterInitStructure.CAN_FilterIdLow=0x0000;//	 ¹ýÂËÆ÷±êʶ·ûµÄµÍ16λֵCAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//¹ýÂËÆ÷ÆÁ±Î±êʶ·ûµÄ¸ß16λֵCAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//	¹ýÂËÆ÷ÆÁ±Î±êʶ·ûµÄµÍ16λֵCAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;// É趨ÁËÖ¸Ïò¹ýÂËÆ÷µÄFIFOΪ0CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;// ʹÄܹýÂËÆ÷CAN_FilterInit(&CAN_FilterInitStructure);//	°´ÉÏÃæµÄ²ÎÊý³õʼ»¯¹ýÂËÆ÷/* CAN FIFO0 message pending interrupt enable */ CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //ʹÄÜFIFO0ÏûÏ¢¹ÒºÅÖжÏ}  
/* ·¢ËÍÁ½¸ö×Ö½ÚµÄÊý¾Ý*/
void can_tx(u8 Data1,u8 Data2)
{ CanTxMsg TxMessage;  TxMessage.StdId=0x00;	//±ê×¼±êʶ·ûΪ0x00TxMessage.ExtId=0x0000; //À©Õ¹±êʶ·û0x0000TxMessage.IDE=CAN_ID_EXT;//ʹÓñê×¼±êʶ·ûTxMessage.RTR=CAN_RTR_DATA;//ΪÊý¾ÝÖ¡TxMessage.DLC=2;	//	ÏûÏ¢µÄÊý¾Ý³¤¶ÈΪ2¸ö×Ö½ÚTxMessage.Data[0]=Data1; //µÚÒ»¸ö×Ö½ÚÊý¾ÝTxMessage.Data[1]=Data2; //µÚ¶þ¸ö×Ö½ÚÊý¾Ý CAN_Transmit(CAN1,&TxMessage); //·¢ËÍÊý¾Ý}void Delay(vu32 nCount)
{for(; nCount != 0; nCount--);
}/* USBÖжϺÍCAN½ÓÊÕÖжϷþÎñ³ÌÐò£¬USB¸úCAN¹«ÓÃI/O£¬ÕâÀïÖ»Óõ½CANµÄÖжϡ£ */
void USB_LP_CAN1_RX0_IRQHandler(void)
{CanRxMsg RxMessage;RxMessage.StdId=0x00;RxMessage.ExtId=0x00;RxMessage.IDE=0;RxMessage.DLC=0;RxMessage.FMI=0;RxMessage.Data[0]=0x00;RxMessage.Data[1]=0x00;    CAN_Receive(CAN1,CAN_FIFO0, &RxMessage); //½ÓÊÕFIFO0ÖеÄÊý¾Ý  if((RxMessage.Data[0]==0x99)&&(RxMessage.Data[1]==0xbb))   // { LED1(0);LED2(1);LED3(1);}if((RxMessage.Data[0]==0x55)&&(RxMessage.Data[1]==0x77))   { 			LED1( ON );			  // ÁÁ   D3Delay(0x200000);LED1( OFF );		  // ÃðLED2( ON );   //D2Delay(0x200000);LED2( OFF );LED3( ON );   //D2Delay(0x200000);LED3( OFF );  }if((RxMessage.Data[0]==0x11)&&(RxMessage.Data[1]==0x33))   //{ LED1(1);LED2(1);LED3(0);}if((RxMessage.Data[0]==0x66)&&(RxMessage.Data[1]==0x77))   { 	LED1( ON );			  // ÁÁ   D3Delay(0x1000);LED1( OFF );		  // ÃðLED2( ON );   //D2Delay(0x1000);LED2( OFF );LED3( ON );   //D2Delay(0x1000);LED3( OFF );  }LED1( ON );			  // ÁÁ   D3Delay(0x50000);LED1( OFF );		  // ÃðLED2( ON );   //D2Delay(0x100000);LED2( OFF );LED3( ON );   //D2Delay(0x100000);LED3( OFF );  } 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269167.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring AOP 面向切面编程

AOP 在软件业&#xff0c;AOP为Aspect Oriented Programming的缩写&#xff0c;意为&#xff1a;面向切面编程&#xff0c;通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续&#xff0c;是软件开发中的一个热点&#xff0c;也是Spring框架中的…

遥感图像增强方法应用

黄永璘 &#xff08;广西气象减灾研究所 广西南宁 530022&#xff09; 1、引 言  人类从眼睛获取信息占人体器官获取信息的90%以上&#xff0c;虽然人眼能看到很多东西&#xff0c;但其实我们看到的只是波长为0.38&#xff5e;0.8μm可…

3D Mapping with an RGB-D Camera(RGBD SLAM V2 )论文笔记

这篇文章即是Felix Endres等人12年完成的RGB-D SLAM V2&#xff0c;是最早的为kinect风格传感器设计的SLAM系统之一 没看到有相关的论文解析 在Github上可找到开源代码&#xff0c;工程配置与运行参考http://www.cnblogs.com/voyagee/p/6898278.html 系统流程 系统分为前后端。…

mysql内连接

假设&#xff41;表和&#xff42;表的数据是这样的。 a表 id name   1  a 2 b 3 c b表 id stock  1 15 2 50   select * from …

制作新网络框架快速自动生成消息结构体的编辑器

这两天制作了一个自动生成代码脚本的窗体编辑器 目前还没有做过第一行可以选择生成的脚本类型&#xff0c;分请求消息和回应消息两种结构体脚本 第二行可以手动输入命名空间&#xff0c;第三行手动输入文件的名称&#xff0c;类名与文件名称相同 后面两个ID是消息的ID 下面的Po…

随心所欲的Web页面打印技术

一.概述 对基于B/S架构的应用程序而言&#xff0c;客户端的页面打印一直是比较头疼的问题&#xff0c;简单的做法是&#xff1a;1.使用IE的打印功能&#xff1b;2.使用水晶报表。但以上两种办法&#xff0c;都有很大的局限性&#xff0c;很难实现特殊要求的排版和精确的定位&…

OAuth2.0学习(2-1)Spring Security OAuth2.0 开发指南

开发指南&#xff1a;http://www.cnblogs.com/xingxueliao/p/5911292.htmlSpring OAuth2.0 提供者实现原理&#xff1a;Spring OAuth2.0提供者实际上分为&#xff1a;授权服务 Authorization Service.资源服务 Resource Service.虽然这两个提供者有时候可能存在同一个应用程序中…

SQL的连接分为三种:内连接、外连接、交叉连接

建了两张表&#xff0c;学生表student 课程表 class 一、内连接&#xff1a; 内连接&#xff08;INNER JOIN&#xff09;&#xff1a;有两种&#xff0c;显式的和隐式的&#xff0c;返回连接表中符合连接条件和查询条件的数据行。&#xff08;所谓的链接表就是数据库在做查询形…

jion连接总结

1:内连接 2:左外连接 3:右外连接 4:左外连接 where判断 5:右外连接 where 6:全外连接 7:全外连接 where

福禄克网络电缆测试仪LinkIQ网络电缆测试仪的使用方法

作为福禄克官方授权经销商—明辰智航将给你一个完整的LinkIQ有线网络测试仪概述。首先&#xff0c;让我们来看看基本工具包里的内容。 我们有&#xff1a; LinkIQ本身 福禄克随身携带便捷包 快速参考指南说明书 CAT6a接线 远程ID 1用于完整的导线图测试 RJ45-to-RJ45模块…

步步为营-77-Ajax简介

AJax:异步JavaScript和XML.Asynchronous JavaScript and XML 优点:无刷新 1 JavaScript下的Ajax 1.1 XMLHttpRequest对象  使用ajax有一个很重要的对象XMLHttpRequest,而该对象的创建方式 var xhr new XMLHttpRequest();//常用 var xhr new ActiveXObject("Microsoft…

福禄克Link IQ功能:PoE负载测试及故障排除

自从最初的IEEE 802.3af 1型以太网供电&#xff08;PoE&#xff09;标准于2003年首次推出以来&#xff0c;该技术已经发展到包括2型&#xff08;高达30瓦&#xff09;、3型&#xff08;高达60瓦&#xff09;和4型&#xff08;高达90瓦&#xff09;。这意味着PoE电压现在支持从手…

利用局域网测试仪进行网络性能测试

随着互联网科技的飞速发展&#xff0c;网络变的越来越庞大&#xff0c;数据量越来越高&#xff0c;网络速率成几何倍数增长&#xff0c;网络应用也越来越复杂&#xff0c;对网络的性能、稳定性和可靠性都提出了更高的要求。在日益复杂的网络环境中一旦发生网络故障&#xff0c;…

详解-制作根文件系统,并使用yaffs,jffs,nfs挂载系统(2)

1.安装mkyaffsimage, mkyaffs2image命令(用来制作yaffs文件系统) 第一个命令针对Flash小页512B,第二个针对Flash大页2KB首先下载压缩文件 yaffs_source_util_larger_small_page_nand.tar.bz2安装mkyaffs2image命令为例: tar –xjf yaffs_source_util_larger_small_page_nand.ta…

数据中心综合布线时,使用交叉连接和互连的好处?

数据中心中&#xff0c;在交换机之间或交换机与服务器之间&#xff0c;为了灵活性和方便管理&#xff0c;有源设备之间都是使用交叉连接和通过配线架互联。在一些场合&#xff0c;还有的要求在同一通道内使用多个交叉连接或互联。 大多数人都知道&#xff0c;永久链路是通道的…

Oracle 通过出生日期计算年龄

sysdate 为系统日期时间&#xff0c;BIRTH_DATE 为表中出生日期字段 SELECTCOUNT( * ) FROMBARN_SWALLOW_INFO WHEREVALID_FLAG 1 AND IS_JINQIAO 1 AND TRUNC( months_between( SYSDATE, BIRTH_DATE ) / 12 ) > 25 AND TRUNC( months_between( SYSDATE, BIRTH_DATE )…