我使用原子的CAN通信的代码,自己改成CAN2的,可是尝试了好久就是不合适,这是怎么回事呢?#include "can2.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
//CAN??????
//tsjw:?????????????±??????.·??§:1~3;
//tbs2:?±????2???±??????.·??§:1~8;
//tbs1:?±????1???±??????.·??§:1~16;
//brp :?¨????·????÷.·??§:1~1024;(????????1,??????1~1024) tq=(brp)*tpclk1
//×??????????????????????????è??0,·??ò?á??.
//?¨????=Fpclk1/((tbs1+tbs2+1)*brp);
//mode:0,???¨????;1,???·????;
//Fpclk1???±?????????????±?ò?è????42M,?????è??CAN2_Mode_Init(1,6,7,6,1);
//?ò?¨??????:42M/((6+7+1)*6)=500Kbps
//·?????:0,??????OK;
// ????,???????§°?;
u8 CAN2_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
u16 i=0;
if(tsjw==0||tbs2==0||tbs1==0||brp==0)return 1;
tsjw-=1;//??????1.???????è??
tbs2-=1;
tbs1-=1;
brp-=1;
RCC->AHB1ENR|=1<<1; //????PORTB???±??
GPIO_Set(GPIOB,PIN5|PIN6,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PA11,PA12,????????,????????
GPIO_AF_Set(GPIOB,5,9);//PB5,AF9
GPIO_AF_Set(GPIOB,6,9);//PB6,AF9
RCC->APB1ENR|=1<<26;//????CAN2?±?? CAN2????????APB1???±??(max:42M)
CAN2->MCR=0x0000; //????????????(???±?è???ù??????0)
CAN2->MCR|=1<<0; //???óCAN??????????????
while((CAN2->MSR&1<<0)==0)
{
i++;
if(i>100)return 2;//???????????????§°?
}
CAN2->MCR|=0<<7; //·??±????·??¨??????
CAN2->MCR|=0<<6; //?í??×??????????í
CAN2->MCR|=0<<5; //?????????¨???í??????(????CAN2->MCR??SLEEP??)
CAN2->MCR|=1<<4; //????±¨??×???????
CAN2->MCR|=0<<3; //±¨???????¨,????????????
CAN2->MCR|=0<<2; //????????±¨??±ê??·????¨
CAN2->BTR=0x00000000; //???????????è??.
CAN2->BTR|=mode<<30; //?????è?? 0,???¨????;1,???·????;
CAN2->BTR|=tsjw<<24; //?????????????í??(Tsjw)??tsjw+1???±??????
CAN2->BTR|=tbs2<<20; //Tbs2=tbs2+1???±??????
CAN2->BTR|=tbs1<<16; //Tbs1=tbs1+1???±??????
CAN2->BTR|=brp<<0; //·???????(Fdiv)??brp+1
//?¨????:Fpclk1/((Tbs1+Tbs2+1)*Fdiv)
CAN2->MCR&=~(1<<0); //???óCAN??????????????
while((CAN2->MSR&1<<0)==1)
{
i++;
if(i>0XFFF0)return 3;//???????????????§°?
}
//?????÷??????
CAN2->FMR|=1<<0; //?????÷×é?¤×÷????????????
CAN2->FA1R&=~(1<<0); //?????÷0???¤??
CAN2->FS1R|=1<<0; //?????÷???í??32??.
CAN2->FM1R|=0<<0; //?????÷0?¤×÷??±ê??·???±???????
CAN2->FFA1R|=0<<0; //?????÷0??????FIFO0
CAN2->sFilterRegister[0].FR1=0X00000000;//32??ID
CAN2->sFilterRegister[0].FR2=0X00000000;//32??MASK
CAN2->FA1R|=1<<0; //?¤???????÷0
CAN2->FMR&=0<<0; //?????÷×é????????????
#if CAN2_RX0_INT_ENABLE
//????????????
CAN2->IER|=1<<1; //FIFO0???????????????í.
MY_NVIC_Init(1,0,CAN2_RX0_IRQn,2);//×é2
#endif
return 0;
}
//id:±ê×?ID(11??)/?©??ID(11??+18??)
//ide:0,±ê×???;1,?©????
//rtr:0,??????;1,??????
//len:??·??????????¤??(???¨??8??×???,???±????·???????,???§??????6??×???)
//*dat:????????.
//·?????:0~3,????±à??.0XFF,?????§????.
u8 CAN2_Tx_Msg(u32 id,u8 ide,u8 rtr,u8 len,u8 *dat)
{
u8 mbox;
if(CAN2->TSR&(1<<26))mbox=0; //????0????
else if(CAN2->TSR&(1<<27))mbox=1; //????1????
else if(CAN2->TSR&(1<<28))mbox=2; //????2????
else return 0XFF; //????????,??·¨·???
CAN2->sTxMailBox[mbox].TIR=0; //?????®?°???è??
if(ide==0) //±ê×???
{
id&=0x7ff;//????11??stdid
id<<=21;
}else //?©????
{
id&=0X1FFFFFFF;//????32??extid
id<<=3;
}
CAN2->sTxMailBox[mbox].TIR|=id;
CAN2->sTxMailBox[mbox].TIR|=ide<<2;
CAN2->sTxMailBox[mbox].TIR|=rtr<<1;
len&=0X0F;//??????????
CAN2->sTxMailBox[mbox].TDTR&=~(0X0000000F);
CAN2->sTxMailBox[mbox].TDTR|=len; //?è??DLC.
//??·???????????????.
CAN2->sTxMailBox[mbox].TDHR=(((u32)dat[7]<<24)|
((u32)dat[6]<<16)|
((u32)dat[5]<<8)|
((u32)dat[4]));
CAN2->sTxMailBox[mbox].TDLR=(((u32)dat[3]<<24)|
((u32)dat[2]<<16)|
((u32)dat[1]<<8)|
((u32)dat[0]));
CAN2->sTxMailBox[mbox].TIR|=1<<0; //???ó·???????????
return mbox;
}
//????·???×???.
//mbox:????±à??;
//·?????:·???×???. 0,????;0X05,·????§°?;0X07,·???????.
u8 CAN2_Tx_Staus(u8 mbox)
{
u8 sta=0;
switch (mbox)
{
case 0:
sta |= CAN2->TSR&(1<<0); //RQCP0
sta |= CAN2->TSR&(1<<1); //TXOK0
sta |=((CAN2->TSR&(1<<26))>>24); //TME0
break;
case 1:
sta |= CAN2->TSR&(1<<8)>>8; //RQCP1
sta |= CAN2->TSR&(1<<9)>>8; //TXOK1
sta |=((CAN2->TSR&(1<<27))>>25); //TME1
break;
case 2:
sta |= CAN2->TSR&(1<<16)>>16; //RQCP2
sta |= CAN2->TSR&(1<<17)>>16; //TXOK2
sta |=((CAN2->TSR&(1<<28))>>26); //TME2
break;
default:
sta=0X05;//??????????,???¨?§°?.
break;
}
return sta;
}
//??????FIFO0/FIFO1??????????±¨??????.
//fifox:0/1.FIFO±à??;
//·?????:FIFO0/FIFO1????±¨??????.
u8 CAN2_Msg_Pend(u8 fifox)
{
if(fifox==0)return CAN2->RF0R&0x03;
else if(fifox==1)return CAN2->RF1R&0x03;
else return 0;
}
//????????
//fifox:??????
//id:±ê×?ID(11??)/?©??ID(11??+18??)
//ide:0,±ê×???;1,?©????
//rtr:0,??????;1,??????
//len:?????????????¤??(???¨??8??×???,???±????·???????,???§??????6??×???)
//dat:??????????
void CAN2_Rx_Msg(u8 fifox,u32 *id,u8 *ide,u8 *rtr,u8 *len,u8 *dat)
{
*ide=CAN2->sFIFOMailBox[fifox].RIR&0x04;//????±ê??·???????????
if(*ide==0)//±ê×?±ê??·?
{
*id=CAN2->sFIFOMailBox[fifox].RIR>>21;
}else //?©??±ê??·?
{
*id=CAN2->sFIFOMailBox[fifox].RIR>>3;
}
*rtr=CAN2->sFIFOMailBox[fifox].RIR&0x02; //????????·??????ó??.
*len=CAN2->sFIFOMailBox[fifox].RDTR&0x0F;//????DLC
//*fmi=(CAN2->sFIFOMailBox[FIFONumber].RDTR>>8)&0xFF;//????FMI
//????????
dat[0]=CAN2->sFIFOMailBox[fifox].RDLR&0XFF;
dat[1]=(CAN2->sFIFOMailBox[fifox].RDLR>>8)&0XFF;
dat[2]=(CAN2->sFIFOMailBox[fifox].RDLR>>16)&0XFF;
dat[3]=(CAN2->sFIFOMailBox[fifox].RDLR>>24)&0XFF;
dat[4]=CAN2->sFIFOMailBox[fifox].RDHR&0XFF;
dat[5]=(CAN2->sFIFOMailBox[fifox].RDHR>>8)&0XFF;
dat[6]=(CAN2->sFIFOMailBox[fifox].RDHR>>16)&0XFF;
dat[7]=(CAN2->sFIFOMailBox[fifox].RDHR>>24)&0XFF;
if(fifox==0)CAN2->RF0R|=0X20;//??·?FIFO0????
else if(fifox==1)CAN2->RF1R|=0X20;//??·?FIFO1????
}
#if CAN2_RX0_INT_ENABLE //????RX0????
//????·???????
void CAN2_RX0_IRQHandler(void)
{
u8 rxbuf[8];
u32 id;
u8 ide,rtr,len;
CAN2_Rx_Msg(0,&id,&ide,&rtr,&len,rxbuf);
printf("id:%d
",id);
printf("ide:%d
",ide);
printf("rtr:%d
",rtr);
printf("len:%d
",len);
printf("rxbuf[0]:%d
",rxbuf[0]);
printf("rxbuf[1]:%d
",rxbuf[1]);
printf("rxbuf[2]:%d
",rxbuf[2]);
printf("rxbuf[3]:%d
",rxbuf[3]);
printf("rxbuf[4]:%d
",rxbuf[4]);
printf("rxbuf[5]:%d
",rxbuf[5]);
printf("rxbuf[6]:%d
",rxbuf[6]);
printf("rxbuf[7]:%d
",rxbuf[7]);
}
#endif
//can·?????×é????(???¨????:ID??0X12,±ê×???,??????)
//len:?????¤??(×??ó??8)
//msg:????????,×??ó??8??×???.
//·?????:0,????;
// ????,?§°?;
u8 CAN2_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
mbox=CAN2_Tx_Msg(0X601,0,0,len,msg);
while((CAN2_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;//????·????á??
if(i>=0XFFF)return 1; //·????§°??
return 0; //·???????;
}
u8 CAN2_Send_MsgTEST(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
mbox=CAN2_Tx_Msg(0X701,0,0,len,msg);
while((CAN2_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;//????·????á??
if(i>=0XFFF)return 1; //·????§°??
return 0; //·???????;
}
//can·???????
//???????? ·?????×é?°ID
//·?????:0,·???????
// 1 ·????§°?
u8 CAN2_Send_Num(u8* msg,int ID)
{
u8 mbox;
u16 i=0;
mbox=CAN2_Tx_Msg(ID,0,0,8,msg);
while((CAN2_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;//????·????á??
if(i>=0XFFF)return 1; //·????§°??
return 0; //·???????;
}
//can???????????é??
//buf:??????????;
//·?????:0,??????±?????;
// ????,???????????¤??;
u8 CAN2_Receive_Msg(u8 *buf)
{
u32 id;
u8 ide,rtr,len;
if(CAN2_Msg_Pend(0)==0)return 0; //??????????????,?±??????
CAN2_Rx_Msg(0,&id,&ide,&rtr,&len,buf); //????????
if(id!=0x12||ide!=0||rtr!=0)len=0; //?????í?ó
return len;
}
帅哥可否分享一下工程文件啊,我can2打开can1就不通了,809440257@qq.com 多谢
一周热门 更多>