STM 32 can example code
#include "sysdef.h"
#define MAX_MAIL_NUM 3
//CAN bus debugging: 0=run 1=self-loop debugging
#define CAN_DEBUG 0
//CAN bus baud rate:0=250kbps,1=500kbps, 2=1Mbps
#define CAN1_BPS 0
unsigned char can1_addr = 0;
unsigned short Can1_Tx_Count =0;
unsigned short Can1_Rx_Count =0;
unsigned short Can1_Send_Delay =0;
unsigned char Can1_Send_Buf[10]={0xeb,0x90,0x01,0x55,0xAA};
unsigned char Can1_Recv_Buf[10]={0};
extern int angle_num ;
extern unsigned int angle_data ;
static u8 CAN_msg_num[MAX_MAIL_NUM]; // Send mailbox flag
void CAN1_Config_init(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */ //36MHz 500Kbps
CAN_InitStructure.CAN_TTCM=DISABLE;//Disable time trigger communication mode
CAN_InitStructure.CAN_ABOM=DISABLE; //After the software sets the INRQ bit of the CAN_MCR register to 1 and then clears it to 0, once the hardware detects
//11 consecutive recessive bits for 128 times, it will exit the offline state
CAN_InitStructure.CAN_AWUM=DISABLE;//Sleep mode by clearing CAN_MCR The SLEEP bit of the register is woken up by software
CAN_InitStructure.CAN_NART=DISABLE;//Whether the CAN message is only sent once, regardless of the result of the transmission (success/error or arbitration loss)
CAN_InitStructure.CAN_RFLM=DISABLE;//When an overflow is received When the FIFO is not locked, when the received FIFO message is not read out, the next received message will overwrite the original message
CAN_InitStructure.CAN_TXFP=DISABLE;//The sent FIFO priority is determined by the message identifier to decide
#if CAN_DEBUG
CAN_InitStructure.CAN_Mode= CAN_Mode_LoopBack;
#else
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;
#endif
//Transmission baud rate
if(CAN1_BPS == 0)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resync jump width 1 time unit
CAN_InitStructure .CAN_BS1=CAN_BS1_12tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;//Time period 2 is 8 time units
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9 = 250kbps
//36M/(1+5+2)/9 = 500kbps
//36M(1+2+1)/9 = 1M
}
else if(CAN1_BPS == 1)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resynchronization jump width is 1 time unit
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//Time period 2 is 8 time units
CAN_InitStructure. CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
else
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resync jump width 1 time unit
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;//Time period 2 is 8 time units
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
CAN_Init(CAN1,&CAN_InitStructure);
/ * CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0; //Specifies the filter 0 to be initialized
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//Specifies the mode that the filter will be initialized to is the identifier mask bit pattern
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;;//Give A 32-bit filter with a filter bit width
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//It is used to set the filter identifier (the high segment when the bit width is 32 bits, and the first one when the bit width is 16 bits)
CAN_FilterInitStructure .CAN_FilterIdLow=0x0000;;//Used to set the filter identifier (32-bit width is the low segment, 16-bit width is the second one)
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//Used to set the filter Filter mask identifier or filter identifier (the high-order bit when the bit width is 32 bits, the first one when the bit width is 16 bits)
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//Used to set the filter mask identifier or filter Identifier (32-bit width is the low-order bit, 16-bit width is the second one)
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;;//The FIFO0 pointing to the filter is set
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//Enable filter
CAN_FilterInit(&CAN_FilterInitStructure);
}
// ******************************** *********************************
// BaudRate = 1 / NominalBitTime
// NominalBitTime = 1tq + tBS1 + tBS2
/ / tq = (BRP[9:0] + 1) x tPCLK
// tPCLK = CAN's clock = APB1's clock
// At 1Mbps rate, the position of the adopted point is at 6tq position, BS1=5, BS2=2
// 500kbps rate Below, the position of the adopted point is at the 8tq position, BS1=7, BS2=3
// At 250kbps rate, the position of the adopted point is at the 14tq position, BS1=13, BS2=2
// 125k, 100k, 50k, 20k, 10k adopts the same spot position as 250K
// **************************************** ************************
void CAN1_Com_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable CAN RX0 interrupt IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
//Can Rx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//Can Tx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
CAN1_Config_init();
CAN_ITConfig(CAN1,CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE); // fifo0中断
CAN_ITConfig(CAN1,CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1, ENABLE); // fifo1中断
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE); // 发送中断
CAN_ITConfig(CAN1,CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR | CAN_IT_WKU | CAN_IT_SLK, ENABLE); // ERR中断
// CAN缓存初始化
memset(CAN_msg_num,0,MAX_MAIL_NUM);
}
int CAN1_Tx_msg(CanTxMsg TxMessage)
{
u8 TransmitMailbox = 0;
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
if(CAN_NO_MB == TransmitMailbox)
{
//发送失败
return 0;
}
else
{
CAN_msg_num[TransmitMailbox] = 1;
}
CAN_ITConfig(CAN1,CAN_IT_TME, ENABLE);
return 1;
}
u16 angle=0,angle_h=0,angle_l=0;
extern unsigned char angle_dir;
int CAN1_Tx_data(void)
{
CanTxMsg TxMessage;
u8 TransmitMailbox = 0,i=0;
/* transmit */
TxMessage.StdId=0x6f1;//Set the standard identifier
TxMessage.ExtId=0x1234;//Set the extended identifier
TxMessage.RTR=CAN_RTR_DATA;//Set the frame type of the message to be transmitted
TxMessage.IDE=CAN_ID_STD;//Set Determine the type of message identifier
TxMessage.DLC=6; //Data length
angle=angle_data/10;
if(angle>9999) angle=9999;
angle_h=angle/100;
angle_h=angle_h/10*16+angle_h%10;
angle_l=angle%100;
angle_l=angle_l/10*16+angle_l%10;
Can1_Send_Buf[0]=angle_num; //Number of
turns Can1_Send_Buf[1]=angle_h; //Total angle
Can1_Send_Buf[2]=angle_l;
Can1_Send_Buf[3 ]=0;
Can1_Send_Buf[4]=0;
Can1_Send_Buf[5]=angle_dir;
for(i=0;i < TxMessage.DLC;i++)
{
TxMessage.Data[i] = Can1_Send_Buf[i];
}
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
if(CAN_NO_MB == TransmitMailbox)
{
//发送失败,没有空邮箱
return 0;
}
else
{
CAN_msg_num[TransmitMailbox] = 1;
}
CAN_ITConfig(CAN1,CAN_IT_TME, ENABLE);
Can1_Tx_Count++;
if(Can1_Tx_Count > 10000)
Can1_Tx_Count =0;
Can1_Send_Delay =200;
return 1;
}
//解析数据
void CAN1_Rx_Data(CanRxMsg RxMessage)
{
u8 i =0;
if((RxMessage.StdId==0x6f1) && (RxMessage.IDE==CAN_ID_STD) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xEB90))
{
for(i=0;i < RxMessage.DLC;i++)
{
Can1_Recv_Buf[i] = RxMessage.Data[i];
}
Can1_Rx_Count++;
if(Can1_Rx_Count > 10000)
Can1_Rx_Count =0;
}
}
//发送完中断函数
void CAN1_Send(void)
{
if(CAN_msg_num[0])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP0);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[0] = 0;
}
}
if(CAN_msg_num[1])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP1);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[1] = 0;
}
}
if(CAN_msg_num[2])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP2))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP2);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[2] = 0;
}
}
}
//接收中断函数
void CAN1_Recv(unsigned char num)
{
CanRxMsg RxMessage;
switch(num)
{
case 0:
if(CAN_GetITStatus(CAN1,CAN_IT_FF0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FF0);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_FOV0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FOV0);
}
else
{
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
//解析数据:
CAN1_Rx_Data(RxMessage);
}
break;
case 1:
if(CAN_GetITStatus(CAN1,CAN_IT_FF1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FF1);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_FOV1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FOV1);
}
else
{
CAN_Receive(CAN1,CAN_FIFO1, &RxMessage);
//解析数据
CAN1_Rx_Data(RxMessage);
}
break;
}
}
void CAN1_Main(unsigned char flg )
{
if(flg)
{
if(Can1_Send_Delay == 0)
CAN1_Tx_data();
}
}
#include "sysdef.h"
#define MAX_MAIL_NUM 3
//CAN bus debugging: 0=run 1=self-loop debugging
#define CAN_DEBUG 0
//CAN bus baud rate:0=250kbps,1=500kbps, 2=1Mbps
#define CAN1_BPS 0
unsigned char can1_addr = 0;
unsigned short Can1_Tx_Count =0;
unsigned short Can1_Rx_Count =0;
unsigned short Can1_Send_Delay =0;
unsigned char Can1_Send_Buf[10]={0xeb,0x90,0x01,0x55,0xAA};
unsigned char Can1_Recv_Buf[10]={0};
extern int angle_num ;
extern unsigned int angle_data ;
static u8 CAN_msg_num[MAX_MAIL_NUM]; // Send mailbox flag
void CAN1_Config_init(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */ //36MHz 500Kbps
CAN_InitStructure.CAN_TTCM=DISABLE;//Disable time trigger communication mode
CAN_InitStructure.CAN_ABOM=DISABLE; //After the software sets the INRQ bit of the CAN_MCR register to 1 and then clears it to 0, once the hardware detects
//11 consecutive recessive bits for 128 times, it will exit the offline state
CAN_InitStructure.CAN_AWUM=DISABLE;//Sleep mode by clearing CAN_MCR The SLEEP bit of the register is woken up by software
CAN_InitStructure.CAN_NART=DISABLE;//Whether the CAN message is only sent once, regardless of the result of the transmission (success/error or arbitration loss)
CAN_InitStructure.CAN_RFLM=DISABLE;//When an overflow is received When the FIFO is not locked, when the received FIFO message is not read out, the next received message will overwrite the original message
CAN_InitStructure.CAN_TXFP=DISABLE;//The sent FIFO priority is determined by the message identifier to decide
#if CAN_DEBUG
CAN_InitStructure.CAN_Mode= CAN_Mode_LoopBack;
#else
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;
#endif
//Transmission baud rate
if(CAN1_BPS == 0)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resync jump width 1 time unit
CAN_InitStructure .CAN_BS1=CAN_BS1_12tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;//Time period 2 is 8 time units
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9 = 250kbps
//36M/(1+5+2)/9 = 500kbps
//36M(1+2+1)/9 = 1M
}
else if(CAN1_BPS == 1)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resynchronization jump width is 1 time unit
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//Time period 2 is 8 time units
CAN_InitStructure. CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
else
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//Resync jump width 1 time unit
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;//Time period 1 is 9 time units
CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;//Time period 2 is 8 time units
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
CAN_Init(CAN1,&CAN_InitStructure);
/ * CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0; //Specifies the filter 0 to be initialized
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//Specifies the mode that the filter will be initialized to is the identifier mask bit pattern
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;;//Give A 32-bit filter with a filter bit width
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//It is used to set the filter identifier (the high segment when the bit width is 32 bits, and the first one when the bit width is 16 bits)
CAN_FilterInitStructure .CAN_FilterIdLow=0x0000;;//Used to set the filter identifier (32-bit width is the low segment, 16-bit width is the second one)
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//Used to set the filter Filter mask identifier or filter identifier (the high-order bit when the bit width is 32 bits, the first one when the bit width is 16 bits)
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//Used to set the filter mask identifier or filter Identifier (32-bit width is the low-order bit, 16-bit width is the second one)
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;;//The FIFO0 pointing to the filter is set
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//Enable filter
CAN_FilterInit(&CAN_FilterInitStructure);
}
// ******************************** *********************************
// BaudRate = 1 / NominalBitTime
// NominalBitTime = 1tq + tBS1 + tBS2
/ / tq = (BRP[9:0] + 1) x tPCLK
// tPCLK = CAN's clock = APB1's clock
// At 1Mbps rate, the position of the adopted point is at 6tq position, BS1=5, BS2=2
// 500kbps rate Below, the position of the adopted point is at the 8tq position, BS1=7, BS2=3
// At 250kbps rate, the position of the adopted point is at the 14tq position, BS1=13, BS2=2
// 125k, 100k, 50k, 20k, 10k adopts the same spot position as 250K
// **************************************** ************************
void CAN1_Com_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable CAN RX0 interrupt IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
//Can Rx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//Can Tx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
CAN1_Config_init();
CAN_ITConfig(CAN1,CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE); // fifo0中断
CAN_ITConfig(CAN1,CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1, ENABLE); // fifo1中断
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE); // 发送中断
CAN_ITConfig(CAN1,CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR | CAN_IT_WKU | CAN_IT_SLK, ENABLE); // ERR中断
// CAN缓存初始化
memset(CAN_msg_num,0,MAX_MAIL_NUM);
}
int CAN1_Tx_msg(CanTxMsg TxMessage)
{
u8 TransmitMailbox = 0;
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
if(CAN_NO_MB == TransmitMailbox)
{
//发送失败
return 0;
}
else
{
CAN_msg_num[TransmitMailbox] = 1;
}
CAN_ITConfig(CAN1,CAN_IT_TME, ENABLE);
return 1;
}
u16 angle=0,angle_h=0,angle_l=0;
extern unsigned char angle_dir;
int CAN1_Tx_data(void)
{
CanTxMsg TxMessage;
u8 TransmitMailbox = 0,i=0;
/* transmit */
TxMessage.StdId=0x6f1;//Set the standard identifier
TxMessage.ExtId=0x1234;//Set the extended identifier
TxMessage.RTR=CAN_RTR_DATA;//Set the frame type of the message to be transmitted
TxMessage.IDE=CAN_ID_STD;//Set Determine the type of message identifier
TxMessage.DLC=6; //Data length
angle=angle_data/10;
if(angle>9999) angle=9999;
angle_h=angle/100;
angle_h=angle_h/10*16+angle_h%10;
angle_l=angle%100;
angle_l=angle_l/10*16+angle_l%10;
Can1_Send_Buf[0]=angle_num; //Number of
turns Can1_Send_Buf[1]=angle_h; //Total angle
Can1_Send_Buf[2]=angle_l;
Can1_Send_Buf[3 ]=0;
Can1_Send_Buf[4]=0;
Can1_Send_Buf[5]=angle_dir;
for(i=0;i < TxMessage.DLC;i++)
{
TxMessage.Data[i] = Can1_Send_Buf[i];
}
TransmitMailbox = CAN_Transmit(CAN1,&TxMessage);
if(CAN_NO_MB == TransmitMailbox)
{
//发送失败,没有空邮箱
return 0;
}
else
{
CAN_msg_num[TransmitMailbox] = 1;
}
CAN_ITConfig(CAN1,CAN_IT_TME, ENABLE);
Can1_Tx_Count++;
if(Can1_Tx_Count > 10000)
Can1_Tx_Count =0;
Can1_Send_Delay =200;
return 1;
}
//解析数据
void CAN1_Rx_Data(CanRxMsg RxMessage)
{
u8 i =0;
if((RxMessage.StdId==0x6f1) && (RxMessage.IDE==CAN_ID_STD) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xEB90))
{
for(i=0;i < RxMessage.DLC;i++)
{
Can1_Recv_Buf[i] = RxMessage.Data[i];
}
Can1_Rx_Count++;
if(Can1_Rx_Count > 10000)
Can1_Rx_Count =0;
}
}
//发送完中断函数
void CAN1_Send(void)
{
if(CAN_msg_num[0])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP0);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[0] = 0;
}
}
if(CAN_msg_num[1])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP1);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[1] = 0;
}
}
if(CAN_msg_num[2])
{
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP2))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP2);
CAN_ITConfig(CAN1,CAN_IT_TME, DISABLE);
CAN_msg_num[2] = 0;
}
}
}
//接收中断函数
void CAN1_Recv(unsigned char num)
{
CanRxMsg RxMessage;
switch(num)
{
case 0:
if(CAN_GetITStatus(CAN1,CAN_IT_FF0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FF0);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_FOV0))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FOV0);
}
else
{
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
//解析数据:
CAN1_Rx_Data(RxMessage);
}
break;
case 1:
if(CAN_GetITStatus(CAN1,CAN_IT_FF1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FF1);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_FOV1))
{
CAN_ClearITPendingBit(CAN1,CAN_IT_FOV1);
}
else
{
CAN_Receive(CAN1,CAN_FIFO1, &RxMessage);
//解析数据
CAN1_Rx_Data(RxMessage);
}
break;
}
}
void CAN1_Main(unsigned char flg )
{
if(flg)
{
if(Can1_Send_Delay == 0)
CAN1_Tx_data();
}
}