STM32F103VET6 的CAN初始化不能正常退出初始化的问题。
初始化能正常进入,但不能退出初始化状态,不知道问题在哪里,求做过CAN的师兄们指点一下,先多谢了
初始化代码如下:
u8 CAN_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->APB2ENR|=1<<2; // ʹÄÜPORTAʱÖÓ
GPIOA->CRH&=0XFFF00FFF;
GPIOA->CRH|=0X000B8000; // PA11 RX ,PA12 TX ÍÆÍìÊä³ö
GPIOA->ODR|=3<<11;
RCC->APB1ENR|=1<<25; // ʹÄÜCANʱÖÓ£¬Ê¹ÓõÄÊÇAPB1µÄʱÖÓ£¨max:36M£©
CAN->MCR=0X0000; // Í˳ö˯Ãßģʽ£¨Í¬Ê±ÉèÖÃËùÓÐλΪ0£©
CAN->MCR|=1<<0; // ÇëÇóCAN ½øÈë³õʼ»¯Ä£Ê½
while((CAN->MSR&1<<0)==0)
{
i++;
if(i>100)return 2 ; // ½øÈë³õʼ»¯Ê§°Ü
}
CAN->MCR|=0<<7; // ·Çʱ¼ä´¥·¢Í¨ÐÅģʽ
CAN->MCR|=0<<6; // Èí¼þ×Ô¶¯ÀëÏß¹ÜÀí
CAN->MCR|=0<<5; // ˯Ãßģʽͨ¹ýÈí¼þ»½ÐÑ£¨Çå³ýCAN->MCRµÄSLEEP룩
CAN->MCR|=1<<4; // ½ûÖ¹±¨ÎÄ×Ô¶¯´«ËÍ
CAN->MCR|=0<<3; // ±¨ÎIJ»Ëø¶¨£¬Ðµĸ²¸Ç¾ÉµÄ
CAN->MCR|=0<<2; // ÓÅÏȼ¶Óɱ¨Îıêʶ·û¾ö¶¨
CAN->BTR=0X00000000; // Çå³ýÔÀ´µÄÉèÖÃ
CAN->BTR|=mode<<30; // ģʽÉèÖà 0£¬ÆÕͨģʽ£»1£¬»Ø»·Ä£Ê½
CAN->BTR|=tsjw<<24; // ÖØÐÂͬ²½ÌøÔ¾¿í¶È£¨tsjw)Ϊtsjw+1¸öʱ¼äµ¥Î»
CAN->BTR|=tbs2<<20; // tbs2=tbs2+1¸öʱ¼äµ¥Î»
CAN->BTR|=tbs1<<16; // tbs1=tbs1+1¸öʱ¼äµ¥Î»
CAN->BTR|=brp<<0; // ·ÖƵϵÊý£¨Fdiv£©Îªbrp+1
// ²¨ÌØÂÊ£ºFpclk1/((Tbs1+Tbs2+1)*Fdiv)
CAN->MCR&=~(1<<0); // ÇëÇóCAN Í˳ö³õʼ»¯Ä£Ê½
while((CAN->MSR&(1<<0))==1)
{
i++;
if(i>0xFFF0)return 3; // Í˳ö³õʼ»¯Ä£Ê½Ê§°Ü
}
CAN->FMR|=1<<0; // ¹ýÂË×鹤×÷ÔÚ³õʼ»¯Ä£Ê½
CAN->FA1R&=~(1<<0); // ¹ýÂËÆ÷0²»¼¤»î
CAN->FS1R&=~(1<<0); // ¹ýÂËÆ÷λ¿íΪ2¸ö16λ
CAN->FM1R|=0<<0; // ¹ýÂËÆ÷0¹¤×÷ÔÚ±êʶ·ûÆÁ±Îλģʽ
CAN->FFA1R|=0<<0; // ¹ýÂËÆ÷0¹ØÁªµ½FIFO0
CAN->sFilterRegister[0].FR1=0X00000000; //32λID
CAN->sFilterRegister[0].FR2=0X00000000; //32λMASK
CAN->FA1R|=1<<0; //¼¤»î¹ýÂËÆ÷0
CAN->FMR&=0<<0; //¹ýÂËÆ÷×é½øÈëÕý³£Ä£Ê½
#if CAN_RX0_INT_ENABLE
// ʹÄÜÖжϽÓÊÕ
CAN->IER|=1<<1; // FIFO0ÏûÏ¢¹ÒºÅÖжÏÔÊÐí
MY_NVIC_Init(1,0,USB_LP_CAN_RX0_IRQChannel,2); //×é2
#endif
return 0;
}
这句是退出CAN初始化,结果返回3,不能退出初始化
CAN->MCR&=~(1<<0); // ÇëÇóCAN Í˳ö³õʼ»¯Ä£Ê½
while((CAN->MSR&(1<<0))==1)
{
i++;
if(i>0xFFF0)return 3; // Í˳ö³õʼ»¯Ä£Ê½Ê§°Ü
} |