金牌会员
- 积分
- 1000
- 威望
- 550
- 贡献
- 288
- 兑换币
- 335
- 注册时间
- 2013-12-3
- 在线时间
- 81 小时
- 毕业学校
- 上海工程技术大学
|
打算用STM32f103 和tja1050 can收发器 做汽车总线的通讯的
但是结果发现程序在回环模式测试下是可以正常工作的,但是进入正常模式后,也就是有两根线连接can高、can低之后没有收到任何数据,但是调式发现发送正常,发送寄存器有数据。
- uint8_t CAN_Mode_Init(uint8_t tsjw,uint8_t tbs2,uint8_t tbs1,u16 brp,uint8_t mode)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- #if CAN_RX0_INT_ENABLE
- NVIC_InitTypeDef NVIC_InitStructure;
- #endif
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ʹÄÜPORTAʱÖÓ
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ʹÄÜCAN1ʱÖÓ
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍì
- GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯IO
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//ÉÏÀÊäÈë
- GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯IO
-
- //CANµ¥ÔªÉèÖÃ
- CAN_InitStructure.CAN_TTCM=DISABLE; //·Çʱ¼ä´¥·¢Í¨ÐÅģʽ //
- CAN_InitStructure.CAN_ABOM=DISABLE; //Èí¼þ×Ô¶¯ÀëÏß¹ÜÀí //
- CAN_InitStructure.CAN_AWUM=DISABLE; //˯Ãßģʽͨ¹ýÈí¼þ»½ÐÑ(Çå³ýCAN->MCRµÄSLEEPλ)//
- CAN_InitStructure.CAN_NART=DISABLE;//ENABLE; //½ûÖ¹±¨ÎÄ×Ô¶¯´«ËÍ //
- CAN_InitStructure.CAN_RFLM=DISABLE; //±¨ÎIJ»Ëø¶¨,еĸ²¸Ç¾ÉµÄ //
- CAN_InitStructure.CAN_TXFP=DISABLE; //ÓÅÏȼ¶Óɱ¨Îıêʶ·û¾ö¶¨ //
- CAN_InitStructure.CAN_Mode= mode; //ģʽÉèÖ㺠mode:0,ÆÕͨģʽ;1,»Ø»·Ä£Ê½; //
- //ÉèÖò¨ÌØÂÊ
- CAN_InitStructure.CAN_SJW=tsjw; //ÖØÐÂͬ²½ÌøÔ¾¿í¶È(Tsjw)Ϊtsjw+1¸öʱ¼äµ¥Î» CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
- CAN_InitStructure.CAN_BS1=tbs1; //Tbs1=tbs1+1¸öʱ¼äµ¥Î»CAN_BS1_1tq ~CAN_BS1_16tq
- CAN_InitStructure.CAN_BS2=tbs2;//Tbs2=tbs2+1¸öʱ¼äµ¥Î»CAN_BS2_1tq ~ CAN_BS2_8tq
- CAN_InitStructure.CAN_Prescaler=brp; //·ÖƵϵÊý(Fdiv)Ϊbrp+1 //
- CAN_Init(CAN1, &CAN_InitStructure); // ³õʼ»¯CAN1
- CAN_FilterInitStructure.CAN_FilterNumber=0; //¹ýÂËÆ÷0
- CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
- CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ
- CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32λID
- CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32λMASK
- CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FilterFIFO0;//¹ýÂËÆ÷0¹ØÁªµ½FIFO0
- CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //¼¤»î¹ýÂËÆ÷0
- CAN_FilterInit(&CAN_FilterInitStructure);//Â˲¨Æ÷³õʼ»¯
- #if CAN_RX0_INT_ENABLE
-
- CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢¹ÒºÅÖжÏÔÊÐí.
-
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//1; // Ö÷ÓÅÏȼ¶Îª1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ´ÎÓÅÏȼ¶Îª0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- return 0;
- }
-
- #if CAN_RX0_INT_ENABLE //ʹÄÜRX0ÖжÏ
- //ÖжϷþÎñº¯Êý
- void USB_LP_CAN1_RX0_IRQnHandler(void)
- {
- CanRxMsg RxMessage;
-
- CAN_Receive(CAN1, 0, &RxMessage);
- //for(uint8_t i=0;i<8;i++)
- LCD_WriteEnglishSring(20,4,"succeed!");
-
- }
- #endif
- //can·¢ËÍÒ»×éÊý¾Ý(¹Ì¶¨¸ñʽ:IDΪ0X12,±ê×¼Ö¡,Êý¾ÝÖ¡)
- //len:Êý¾Ý³¤¶È(×î´óΪ8)
- //msg:Êý¾ÝÖ¸Õë,×î´óΪ8¸ö×Ö½Ú.
- //·µ»ØÖµ:0,³É¹¦;
- // ÆäËû,ʧ°Ü;
- uint8_t Can_Send_Msg(uint8_t* msg,uint8_t len)
- {
- uint8_t mbox;
- u16 i=0;
- CanTxMsg TxMessage;
- TxMessage.StdId=0x1BC; // ±ê×¼±êʶ·ûΪ0
- TxMessage.ExtId=0x12; // ÉèÖÃÀ©Õ¹±êʾ·û£¨29룩
- TxMessage.IDE=0; // ʹÓÃÀ©Õ¹±êʶ·û
- TxMessage.RTR=0; // ÏûÏ¢ÀàÐÍΪÊý¾ÝÖ¡£¬Ò»Ö¡8λ
- TxMessage.DLC=len; // ·¢ËÍÁ½Ö¡ÐÅÏ¢
- for(i=0;i<len;i++)
- TxMessage.Data[i]=msg[i]; // µÚÒ»Ö¡ÐÅÏ¢
- mbox= CAN_Transmit(CAN1, &TxMessage);
- i=0;
- while((CAN_TransmitStatus(CAN1, mbox)==CANTXFAILED)&&(i<0XFFF))i++; //µÈ´ý·¢ËͽáÊø
- if(i>=0XFFF)return 1;
- return 0;
- }
- //can¿Ú½ÓÊÕÊý¾Ý²éѯ
- //buf:Êý¾Ý»º´æÇø;
- //·µ»ØÖµ:0,ÎÞÊý¾Ý±»ÊÕµ½;
- // ÆäËû,½ÓÊÕµÄÊý¾Ý³¤¶È;
- uint8_t Can_Receive_Msg(uint8_t *buf)
- {
- uint32_t i;
- CanRxMsg RxMessage;
- if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //ûÓнÓÊÕµ½Êý¾Ý,Ö±½ÓÍ˳ö
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶ÁÈ¡Êý¾Ý
- for(i=0;i<8;i++)
- buf[i]=RxMessage.Data[i];
- return RxMessage.DLC;
- }
复制代码- int main(void)
- {
- delay_init();
-
- //Nokia5110 ³õʼ»¯
- GPIO_Configuration();
- LCD_Init();
- LCD_BL_ON();
- LCD_Clear();
-
- LCD_WriteEnglishSring(8,0,"Conditioner");
- LCD_WriteEnglishSring(40,1,"2015-07");
-
- LCD_WriteEnglishSring(0,2,"Tem:");
- LCD_WriteEnglishSring(0,3,"Hum:");
- LCD_WriteEnglishSring(50,2,"'C");
- LCD_WriteEnglishSring(50,3,"%");
- //TIM3_Int_Init(4999,7199);
-
- //CAN_DeInit(CAN1);
- CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_7tq,5,CAN_Mode_Normal);//CAN_Mode_LoopBack CAN³õʼ»¯»·»Øģʽ,²¨ÌØÂÊ450Kbps CAN_Mode_Normal
- //NVIC_Configuration(); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
-
-
- while(1)
- {
-
- for(i=0;i<8;i++)
- {
- canbuf[i]=i*2;//Ìî³ä·¢ËÍ»º³åÇø
- }
- res=Can_Send_Msg(canbuf,8);//·¢ËÍ8¸ö×Ö½Ú
- if(!res) GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET); //Ìáʾ·¢ËÍʧ°Ü
-
- key=Can_Receive_Msg(buf);
-
- }
- }
-
复制代码 有没有玩过的大神带带我。
|
|