CAN->RF0R |= 1<5; //释放接收邮箱0 407
}
////////////////////////设置标示符过滤器组/////////////////////////////////////////////////////////////////////////////
void CAN_wrFilter (unsigned int id, unsigned char format)
{
static unsigned short CAN_filterIdx = 0; //标示符设置数量索引
unsigned int CAN_msgId = 0; //变量用于存储需要设定的标示符
if (CAN_filterIdx > 13) //检查CAN过滤器是否已满,最高到13
{
return; //如果已满-返回
}
if (format == STANDARD_FORMAT) //如果是标准帧,标准帧是11位ID(报文识别码)
{ // Standard ID
CAN_msgId |= (unsigned int)(id < 21) | CAN_ID_STD;//标示符左移21位并添加帧识别码,得到32位的标示符字
}
else //如果是扩展帧,扩展帧是29位ID(报文识别码)
{ // Extended ID
CAN_msgId |= (unsigned int)(id 3) | CAN_ID_EXT;//标示符左移3位并添加帧识别码,得到32位的标示符字
}
// set Initialisation mode for filter banks
//CAN过滤器组工作在初始化模式
CAN->FMR |= 1<0;
// deactivate filter
CAN->FA1R &= ~(unsigned int)(1 < CAN_filterIdx);
// initialize filter
// set 32-bit scale configuration
//初始化过滤器
//过滤器位宽为单个32位
CAN->FS1R |= (unsigned int)(1 < CAN_filterIdx);
// set 2 32-bit identifier list mode
//过滤器组x的2个32位寄存器工作在标识符列表模式
CAN->FM1R |= (unsigned int)(1 < CAN_filterIdx);
// 32-bit identifier
//32位标识符
CAN->sFilterRegister[CAN_filterIdx].FR1 = CAN_msgId;
// 32-bit identifier
//32位标识符
CAN->sFilterRegister[CAN_filterIdx].FR2 = CAN_msgId;
// assign filter to FIFO 0
//过滤器被关联到FIFO0
CAN->FFA1R &= ~(unsigned int)(1 < CAN_filterIdx);
// activate filter
//过滤器被激活
CAN->FA1R |= (unsigned int)(1 < CAN_filterIdx);
// reset Initialisation mode for filter banks
//CAN过滤器组工作在正常模式
CAN->FMR &= ~(1<0); // 该位置0为正常模式 415
// increase filter index
//设置的过滤器数量增加
CAN_filterIdx += 1;
}
void USB_HP_CAN_TX_IRQHandler (void)
{
// request completed mbx 0
//邮箱0请求完成 当发送完成后硬件对该位置1407
if (CAN->TSR & (1<0)) //检测是否置1来判断发送已完成
{
// reset request complete mbx 0
//复位邮箱0请求,对该位写1可以清0该位 407
CAN->TSR |= 1<0;
// disable TME interrupt
CAN->IER &= ~(1<0); //禁止发送邮箱空中断
CAN_TxRdy = 1;
}
}
void USB_LP_CAN_RX0_IRQHandler (void) {
// message pending ?
//接收到CAN报文,通过判断报文数目判断是否有报文
if (CAN->RF0R & (1<0))
{
// read the message
//接收报文
CAN_rdMsg (&CAN_RxMsg);
CAN_RxRdy = 1; // set receive flag
}
}
////////////////////////CAN初始化////////////////////////////////////////////////////////////////////////
void can_Init (void)
{
CAN_setup (); // setup CAN interface
CAN_wrFilter (33, STANDARD_FORMAT); // Enable reception of messages,filter set
//CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM); // Loopback, Silent Mode (self-test)
//软件仿真使用环回模式
//CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM);
//硬件仿真使用正常模式
CAN_testmode(); //正常模式
// leave init mode
//进入正常模式
CAN_start ();
// wait til mbx is empty
//等待CAN就绪
CAN_waitReady ();
}
以下是.H文件
#ifndef _CAN_H_
#define _CAN_H_
#define STANDARD_FORMAT 0
#define EXTENDED_FORMAT 1
#define DATA_FRAME 0
#define REMOTE_FRAME 1
#define CAN_ID_STD ((u32)0x00000000)
#define CAN_ID_EXT ((u32)0x00000004)
typedef struct {
unsigned int id; // 29 bit identifier
unsigned char data[8]; // Data field
unsigned char len; // Length of data field in bytes
unsigned char format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER
unsigned char type; // 0 - DATA FRAME, 1 - REMOTE FRAME
} CAN_msg;
void CAN_setup (void);
//void CAN_init (void);
void CAN_start (void);
void CAN_waitReady (void);
void CAN_wrMsg (CAN_msg *msg);
void CAN_rdMsg (CAN_msg *msg);
void CAN_wrFilter (unsigned int id, unsigned char filter_type);
void can_Init (void);
void CAN_testmode (void);
e