📄 pic18f2480.c
字号:
/**********************initcan***************************************/
void initcan(char CAN_ADRESS_H, char CAN_ADRESS_L)
{
/**********config the model of CAN********************************************/
trisb = (trisb | 0x08) & 0xfb;//config the rb3 for receive config the rb2 for send
ecancon = 0x00;//config the legacy mode
cancon= 0x80;//turn to config mode
while((canstat&0x80)==0)//wait turn to the config mode success state
{
nop();
}
write_dbc(0,0,"config mode!");
/******************config the baud rate 80k*****************************************/
brgcon1 = 0x09;// config the SJW=1TQ, BRP=0x09
brgcon2 = 0X98;//config the Phase_Seg1 = 4TQ, Prog_Seg = 1TQ
brgcon3 = 0x83;//config the Phase_Seg2 = 4TQ
//在这里需要注意到的是18F2480的指令周期是没有4分频的
/*********************
the time = TQ*(Sync_Seg + Prop_Seg + Phase_seg1 + Phae_seg2)
it must greater or equeal to 8TQ,
TQ = (2 * (BRP +1))/Fosc(Mhz)
************************************/
/************config the send mailbox and init the data for send********************/
txb0con = 0x03;//config the send priority for highest
txb0sidl = CAN_ADRESS_L & 0xe0;//config the three low bit of adress the CAN_ADRESS_L is define by macro
txb0sidh = CAN_ADRESS_H;//config the high eight bit of adreess the CAN_ADRESS_H is defined by marco
txb0dlc = 0x08;//config the send code is 8 byte
txb0d0 = 0xf0;//config the buff of sender
txb0d1 = 0xf1;
txb0d2 = 0xf2;
txb0d3 = 0xf3;
txb0d4 = 0xf4;
txb0d5 = 0xf5;
txb0d6 = 0xf6;
txb0d7 = 0xf7;
/*************config the recever mailbox and init the data of receibe buffer**************************************************************/
rxb0sidl = CAN_ADRESS_L & 0xe0;
rxb0sidh = CAN_ADRESS_H;
rxb0con = 0x20;//only receive the corresponding ID filhit0=0 means thar the rxb0 use the filer0
rxb0dlc = 0x08;//config the length of the data
rxb0d0 = 0x00;
rxb0d1 = 0x01;
rxb0d2 = 0x02;
rxb0d3 = 0x03;
rxb0d4 = 0x04;
rxb0d5 = 0x05;
rxb0d6 = 0x06;
rxb0d7 = 0x07;
/*******init the filter 0 and mask*******************************************/
rxf0sidh = CAN_ADRESS_H;
rxf0sidl = CAN_ADRESS_L;
rxm0sidh = 0xff;
rxm0sidl = 0xe0;
/**********config the work mode of the CAN****************************************/
cancon = 0x40;//when the bit6==1,it will be in the test mode,when be 0 is in normal mode
while((canstat & 0x40) == 0)//wait for turn to loopback mode
{
nop();
}
write_dbc(1,0,"work mode!");
/*****turn to normal mode*******************************************/
cancon = 0x00;
while(canstat != 0x00)
{
nop();
}
write_dbc(2,0,"normal mode!");
/************init the interrupt of CAN*****************************************/
pie3 = pie3 | 0x01;
ipr3 = ipr3 | 0x01;//config the interrupt to high priority
pir3 = 0x00;
}
void send_msg(char *msg)
{
txb0d0 = msg[0];//config the buff of sender
txb0d1 = msg[1];
txb0d2 = msg[2];
txb0d3 = msg[3];
txb0d4 = msg[4];
txb0d5 = msg[5];
txb0d6 = msg[6];
txb0d7 = msg[7];
txb0con = txb0con | 0x08;
while((txb0con & 0x08) == 0)
{
nop();
}
write_dbc(3,7," M");
//delay_s(4);
}
void interrupt(void)
{
if(pir3.RXB0IF)
{
receive_flag = 1;
if(can_point <= &can_buff[48])//防止接收溢出缓冲区
{
*(can_point++) = rxb0d0;
*(can_point++) = rxb0d1;
*(can_point++) = rxb0d2;
*(can_point++) = rxb0d3;
*(can_point++) = rxb0d4;
*(can_point++) = rxb0d5;
*(can_point++) = rxb0d6;
*(can_point++) = rxb0d7;
rxb0con.RXFUL = 0;//清除接收BUFF满标志使接收BUFFER能够再次接收信息
}
pir3.RXB0IF = 0;
}
}
void main()
{
int ID;
char ID1,ID2;
ID1 = ID >> 3;//CAN_H
ID2 = ID << 5;//CAN_L
initcan(i,tmp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -