这是can的配置文件,一块贴上来。
#include "can.h"
#include "stm32f10x_can.h"
unsigned int CAN_ID;
unsigned char CAN_DATA0,CAN_DATA1,CAN_DATA2,CAN_DATA3,CAN_DATA4,CAN_DATA5,CAN_DATA6,CAN_DATA7;
unsigned char CanFlag,Display;
//extern void GPIO_Configuration(void);
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* CAN Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
/* Configure CAN pin : RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable CAN1 RX0 interrupt IRQ channel */
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);
}
void CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_Configuration();
GPIO_Configuration();
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* 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;
CAN_InitStructure.CAN_TXFP = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 45;
if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED)
{
}
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; /* ÆÁ±Öģʽ */
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /* 32λ */
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; /* ÒÔÏÂËĸö¶¼Îª0, ±íÃ÷²»¹ýÂËÈκÎid */
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0; /* Äܹ»Í¨¹ý¸Ã¹ýÂËÆ÷µÄ±¨ÎÄ´æµ½fifo0ÖÐ */
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); /* ¹ÒºÅÖжÏ, ½øÈëÖжϺó¶ÁfifoµÄ±¨Îĺ¯ÊýÊͷű¨ÎÄÇåÖжϱêÖ¾ */
CAN_FilterInit(&CAN_FilterInitStructure);
}
void CANWriteData(unsigned int ID)
{
CanTxMsg TxMessage;
CAN_DATA0=0x01; CAN_DATA1=0x02;
CAN_DATA2=0x03; CAN_DATA3=0x04;
CAN_DATA4=0x05; CAN_DATA5=0x06;
CAN_DATA6=0x07; CAN_DATA7=0x08;
/* transmit */
TxMessage.StdId = ID; /* ÉèÖñê×¼id ×¢Òâ±ê×¼idµÄ×î¸ß7λ²»ÄÜÈ«ÊÇÒþÐÔ(1)¡£¹²11λ */
//TxMessage.ExtId = 0x00; //ÉèÖÃÀ©Õ¹id À©Õ¹id¹²18λ
TxMessage.RTR = CAN_RTR_DATA; /* ÉèÖÃΪÊý¾ÝÖ¡ */
TxMessage.IDE = CAN_ID_STD; /* ʹÓñê×¼id */
TxMessage.DLC = 8; /* Êý¾Ý³¤¶È, can±¨ÎĹ涨×î´óµÄÊý¾Ý³¤¶ÈΪ8×Ö½Ú */
TxMessage.Data[0] = CAN_DATA0;
TxMessage.Data[1] = CAN_DATA1;
TxMessage.Data[2] = CAN_DATA2;
TxMessage.Data[3] = CAN_DATA3;
TxMessage.Data[4] = CAN_DATA4;
TxMessage.Data[5] = CAN_DATA5;
TxMessage.Data[6] = CAN_DATA6;
TxMessage.Data[7] = CAN_DATA7;
CAN_Transmit(CAN1,&TxMessage); /* ·µ»ØÕâ¸öÐÅÏ¢ÇëÇó·¢Ë͵ÄÓÊÏäºÅ0,1,2»òûÓÐÓÊÏäÉêÇë·¢ËÍno_box */
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage); /* ´Ëº¯Êý°üº¬ÊÍ·ÅÌá³ö±¨ÎÄÁ˵Ä,ÔڷDZØҪʱ,²»ÐèÒª×Ô¼ºÊÍ·Å */
CAN_ID=RxMessage.StdId;
CAN_DATA0=RxMessage.Data[0];
CAN_DATA1=RxMessage.Data[1];
CAN_DATA2=RxMessage.Data[2];
CAN_DATA3=RxMessage.Data[3];
CAN_DATA4=RxMessage.Data[4];
CAN_DATA5=RxMessage.Data[5];
CAN_DATA6=RxMessage.Data[6];
CAN_DATA7=RxMessage.Data[7];
CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0); /* Çå³ý¹ÒÆðÖÐ¶Ï */
CanFlag = ENABLE;
}
|