STM32 can 實(shí)例代碼
#include "sysdef.h"
#define MAX_MAIL_NUM 3
//CAN總線調(diào)試:0=運(yùn)行 1=自環(huán)調(diào)試
#define CAN_DEBUG 0
//CAN總線波特率: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]; // 發(fā)送郵箱標(biāo)記
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;//禁止時(shí)間觸發(fā)通信模式
CAN_InitStructure.CAN_ABOM=DISABLE;//軟件對(duì)CAN_MCR寄存器的INRQ位進(jìn)行置1隨后清0后,一旦硬件檢測(cè)
//到128次11位連續(xù)的隱性位,就退出離線狀態(tài)
CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通過清除CAN_MCR寄存器的SLEEP位,由軟件喚醒
CAN_InitStructure.CAN_NART=DISABLE;//CAN報(bào)文是否只發(fā)1次,不管發(fā)送的結(jié)果如何(成功/出錯(cuò)或仲裁丟失)
CAN_InitStructure.CAN_RFLM=DISABLE;//在接收到溢出時(shí)FIFO未被鎖定,當(dāng)接收到FIFO報(bào)文未被讀出,下一個(gè)收到的報(bào)文會(huì)覆蓋原有的報(bào)文
CAN_InitStructure.CAN_TXFP=DISABLE;//發(fā)送的FIFO優(yōu)先級(jí)由報(bào)文的標(biāo)識(shí)符來決定
#if CAN_DEBUG
CAN_InitStructure.CAN_Mode= CAN_Mode_LoopBack;
#else
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;
#endif
//傳輸波特率
if(CAN1_BPS == 0)
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳躍寬度1個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS1=CAN_BS1_12tq;//時(shí)間段1為9個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;//時(shí)間段2為8個(gè)時(shí)間單位
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;//重新同步跳躍寬度1個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//時(shí)間段1為9個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//時(shí)間段2為8個(gè)時(shí)間單位
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
else
{
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳躍寬度1個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;//時(shí)間段1為9個(gè)時(shí)間單位
CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;//時(shí)間段2為8個(gè)時(shí)間單位
CAN_InitStructure.CAN_Prescaler= 9;//36M/(1+12+3)/9= 250kbps
}
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0; //指定了待初始化的過濾器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定了過濾器將被初始化到的模式為標(biāo)識(shí)符屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;;//給出了過濾器位寬1個(gè)32位過濾器
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//用來設(shè)定過濾器標(biāo)識(shí)符(32位位寬時(shí)為其高段位,16位位寬時(shí)為第一個(gè))
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;;//用來設(shè)定過濾器標(biāo)識(shí)符(32位位寬時(shí)為其低段位,16位位寬時(shí)為第二個(gè))
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//用來設(shè)定過濾器屏蔽標(biāo)識(shí)符或者過濾器標(biāo)識(shí)符(32位位寬時(shí)為其高段位,16位位寬時(shí)為第一個(gè))
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//用來設(shè)定過濾器屏蔽標(biāo)識(shí)符或者過濾器標(biāo)識(shí)符(32位位寬時(shí)為其低段位,16位位寬時(shí)為第二個(gè))
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;;//設(shè)定了指向過濾器的FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能過濾器
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
// 1Mbps 速率下,采用點(diǎn)的位置在6tq位置處,BS1=5, BS2=2
// 500kbps 速率下,采用點(diǎn)的位置在8tq位置處,BS1=7, BS2=3
// 250kbps 速率下,采用點(diǎn)的位置在14tq位置處,BS1=13, BS2=2
// 125k, 100k, 50k, 20k, 10k 的采用點(diǎn)位置與 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); // 發(fā)送中斷
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)
{
//發(fā)送失敗
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 Transmit