📄 can.c
字号:
#include "H256Port.h"
#include "mscan.h"
volatile unsigned char *can_periph[5] = {
&CAN0CTL0,
&CAN1CTL0
};
void MSCANInit(unsigned char can_num)
{
volatile unsigned char *can_pt;
can_pt = can_periph[can_num];
// If MSCAN peripheral is not in Initialization Mode, enables the Inizialization Mode Request
if(!(can_pt[CANCTL1]&CANCTL1_INITAK_MASK))
{
can_pt[CANCTL0] = CANCTL0_INITRQ_MASK;
while(!(can_pt[CANCTL1]&CANCTL1_INITAK_MASK))
;
}
// Enables MSCAN peripheral and chooses Oscillator Clock, Loop Disabled and Normal Operation
can_pt[CANCTL1] = 0xc0; //80????
// Configures SJW = 3Tq and Prescaler = 4
can_pt[CANBTR0] = 0x83; //0x82
// Configures One Sample, Time Segment 1 = 6Tq and Time Segment 2 = 3Tq
can_pt[CANBTR1] = 0x25;
// Disables all the Filters
can_pt[CANIDMR_1B+0] = 0xFF;
can_pt[CANIDMR_1B+1] = 0xFF;
can_pt[CANIDMR_1B+2] = 0xFF;
can_pt[CANIDMR_1B+3] = 0xFF;
can_pt[CANIDMR_2B+0] = 0xFF;
can_pt[CANIDMR_2B+1] = 0xFF;
can_pt[CANIDMR_2B+2] = 0xFF;
can_pt[CANIDMR_2B+3] = 0xFF;
// Restarts MSCAN peripheral and waits for Initialization Mode exit
can_pt[CANCTL0] = 0x00;
while(can_pt[CANCTL1]&CANCTL1_INITAK_MASK)
;
// Waits for MSCAN synchronization with the CAN bus
while(!(can_pt[CANCTL0]&CANCTL0_SYNCH_MASK))
;
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Send Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANSendMsg(unsigned char can_num, struct can_msg msg)
{
unsigned char n_tx_buf = 0, i;
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(msg.len > 8)
return(FALSE);
// if(!(can_pt[CANCTL0]&CANCTL0_SYNCH_MASK))
// return(FALSE);
while(!(can_pt[CANTFLG]&MaskOR(n_tx_buf)))
n_tx_buf = (n_tx_buf == MAX_TX_BUFFERS)? 0: (unsigned char)(n_tx_buf + 1);
can_pt[CANTBSEL] = MaskOR(n_tx_buf);
can_pt[CANTXIDR+0] = (unsigned char)(msg.id>>3);
can_pt[CANTXIDR+1] = (unsigned char)(msg.id<<5);
if(msg.RTR)
can_pt[CANTXIDR+1] |= 0x10;
for(i = 0; i < msg.len; i++)
can_pt[CANTXDSR+i] = msg.data[i];
can_pt[CANTXDLR] = msg.len;
can_pt[CANTXTBPR] = msg.prty;
can_pt[CANTFLG] = MaskOR(n_tx_buf);
return(TRUE);
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Get Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANGetMsg(unsigned char can_num, struct can_msg *msg)
{
unsigned char i;
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(!(can_pt[CANRFLG]&CANRFLG_RXF_MASK))
return(FALSE);
if(can_pt[CANRXIDR+1]&0x08)
return(FALSE);
msg->id = ((can_pt[CANRXIDR+0]<<3)&0x0700) | (unsigned char)(can_pt[CANRXIDR+0]<<3) | (unsigned char)(can_pt[CANRXIDR+1]>>5);
if(can_pt[CANRXIDR+1]&0x10)
msg->RTR = TRUE;
else
msg->RTR = FALSE;
msg->len = can_pt[CANRXDLR];
for(i = 0; i < msg->len; i++)
msg->data[i] = can_pt[CANRXDSR+i];
can_pt[CANRFLG] = CANRFLG_RXF_MASK;
return(TRUE);
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Check for Received Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANCheckRcvdMsg(unsigned char can_num)
{
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(can_pt[CANRFLG]&CANRFLG_RXF_MASK)
return(TRUE);
return(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -