/*********************************************************************
*
* Function: Initialize the CAN Module
*
*********************************************************************/
void ECAN_Init(void)
{
CAN_STB_SetDigitalOutput();//CAN收发器待机模式端配置为输出
CAN_TX_SetDigitalOutput();
CAN_RX_SetDigitalInput();
// Place CAN module in configuration mode, see CANCON register data
CANCON = 0x80; //REQOP bits <2:0> = 0b100
// while(!(CANSTATbits.OPMODE ==0x04)); //Wait for op-mode bits in the
while(CANSTAT != 0X80); //CANSTAT register to = 0b100
//to indicate config mode OK
// Enter CAN module into Mode 0, standard legacy mode; see ECANCON register
ECANCON = 0x00;
// See Microchip application note AN754, Understanding Microchip's
// CAN Module Bit Timing." See also: Microchip Controller Area Network
//(CAN) Bit Timing Calculator, available at Intrepid Control Systems:
//www.intrepidcs.com/support/mbtime.htm.
// Initialize CAN Bus bit rate timing. Assumes only four standard rates.
// Initialize CAN Timing
if (F_ECAN_100==1)
{
// 100 Kbps @ 32MHz
BRGCON1 = 0x13; //SJW=1TQ BRP 19
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_125==1)
{
// 125 Kbps @ 32MHz
BRGCON1 = 0x0F; //SJW=1TQ BRP 15
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_500==1)
{
// 125 Kbps @ 32MHz
BRGCON1 = 0x03; //SJW=1TQ BRP 3
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_1000==1)
{
// 1000 Kbps @ 32MHz
BRGCON1 = 0x01; //SJW=1TQ BRP 1
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
// Initialize Receive Masks, see registers RXMxEIDH, RXMxEIDL, etc...
// Mask 0 (M0) will accept NO extended addresses, but any standard address
RXM0EIDH = 0x00; // Extended Address receive acceptance mask, high byte
RXM0EIDL = 0x00; // Extended Address receive acceptance mask, low byte
RXM0SIDH = 0xFF; // Standard Address receive acceptance mask, high byte
RXM0SIDL = 0xE0; // Standard Address receive acceptance mask, low byte
// Mode 0 allows use of receiver filters RXF0 through RXF5. Enable filters
// RXF0 and RXF1, all others disabled. See register RXFCONn.
// Only using two filters
RXFCON0 = 0x01; //Enable Filter-0; disable others
RXFCON1 = 0x00; //Disable Filters 8 through 15
// Initialize Receive Filters
// Filter 0 = 0x312
RXF0EIDH = 0x00; //Extended Address Filter-0 unused, set high byte to 0
RXF0EIDL = 0x00; //Extended Address Filter-0 unused, set low byte to 0
RXF0SIDH = (uint8_t)(0x312>>3); //Standard Address Filter-0 high byte set to xx
RXF0SIDL = (uint8_t)(0x312<<5)&0xE0; //Standard Address Filter-0 low byte set to xx
TXB0CON=0X03;//TXB0为最高优先级3
// After configuring CAN module with above settings, return it
// to Normal mode
CANCON = 0x00;
// while(CANSTATbits.OPMODE!=0x00); //Wait for op-mode bits in the
while(CANSTAT != 0X00); //CANSTAT register to = 0b000
//to indicate Normal mode OK
RXB0CON = 0x20;//只接收有效的标准标识符信息
/* 初始化CAN的中断,PIR5为CAN的外围中断标志寄存器 */
PIR5=0X00; // 清所有CAN中断标志
PIE5bits.RXB0IE = 1; //使能接收缓冲器0的接收中断
IPR5bits.RXB0IP = 1; // 接收缓冲器0的接收中断为高优先级
// PIR5bits.WAKIF = 0; //清除CAN总线活动唤醒中断标志
// PIE5bits.WAKIE = 1; //允许CAN总线活动唤醒中断
// IPR5bits.WAKIP = 1; //CAN总线活动唤醒中断为高优先级
}
|