实验准备:
两块带有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 */
- 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 */
- 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 */
- ST 老版本库
- 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(); //¼ì²âÊÇ·ñÓа´¼ü°´ÏÂ}
}
- 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
}
- 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 ); }