金牌会员
- 积分
- 1014
- 威望
- 500
- 贡献
- 342
- 兑换币
- 317
- 注册时间
- 2013-9-6
- 在线时间
- 86 小时
- 毕业学校
- 江苏理工学院
|
- int main(void)
- {
- u8 buf[100];
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
- CAN1_Mode_Init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 6, CAN_Mode_Normal);//CAN3õê¼»ˉÕy3£Ä£ê½,2¨ìØÂê500Kbps
- uart1_init(115200); //′®¿ú3õê¼»ˉ2¨ìØÂêÎa115200
- delay_init(); //Ñóê±3õê¼»ˉ
-
- // FSMC_SRAM_Init();
- // Memory_Init(EXSRAM); //3õê¼»ˉÄú2¿Äú′æ3Ø
-
- uart1SendChars("yyy",3);
-
- while(1)
- {
- u8 buf[100];
- memset(buf, 0, 100);
- CAN1_Receive_Msg(buf);
- }
-
- }
复制代码- //CAN3õê¼»ˉ
- //tsjw:ÖØDÂí¬2½ìøԾ걼äμ¥Ôa. @ref CAN_synchronisation_jump_width ·¶Î§: CAN_SJW_1tq~ CAN_SJW_4tq
- //tbs2:ê±¼ä¶Î2μÄê±¼äμ¥Ôa. @ref CAN_time_quantum_in_bit_segment_2 ·¶Î§: CAN_BS2_1tq~CAN_BS2_8tq
- //tbs1:ê±¼ä¶Î1μÄê±¼äμ¥Ôa. @refCAN_time_quantum_in_bit_segment_1 ·¶Î§: CAN_BS1_1tq ~CAN_BS1_16tq
- //brp :2¨ìØÂê·ÖÆμÆ÷.·¶Î§:1~1024;(êμ¼êòa¼ó1,ò2¾íêÇ1~1024) tq=(brp)*tpclk1
- //2¨ìØÂê=Fpclk1/((tsjw+tbs1+tbs2+3)*brp);
- //mode: @ref CAN_operating_mode ·¶Î§£oCAN_Mode_Normal,ÆÕí¨Ä£ê½;CAN_Mode_LoopBack,»Ø»·Ä£ê½;
- //Fpclk1μÄê±ÖóÔú3õê¼»ˉμÄê±oòéèÖÃÎa36M,èç1ûéèÖÃCAN_Normal_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_LoopBack);
- //Ôò2¨ìØÂêÎa:42M/((1+6+7)*6)=500Kbps
- //·μ»ØÖμ:0,3õê¼»ˉOK;
- // ÆäËû,3õê¼»ˉ꧰ü;
- u8 CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
-
- #if CAN1_RX0_INT_ENABLE
- NVIC_InitTypeDef NVIC_InitStructure;
- #endif
-
- //ê1ÄüÏà1Øê±Öó
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüPORTAê±Öó
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ê1ÄüCAN1ê±Öó
-
- //3õê¼»ˉGPIO
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
- GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉPA11,PA12
-
- //òy½Å¸′óÃó3éäÅäÖÃ
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11¸′óÃÎaCAN1
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12¸′óÃÎaCAN1
-
- //CANμ¥ÔaéèÖÃ
- CAN_InitStructure.CAN_TTCM=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½
- CAN_InitStructure.CAN_ABOM=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí
- CAN_InitStructure.CAN_AWUM=DISABLE;//ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
- CAN_InitStructure.CAN_NART=ENABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
- CAN_InitStructure.CAN_RFLM=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ
- CAN_InitStructure.CAN_TXFP=DISABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
- CAN_InitStructure.CAN_Mode= mode; //Ä£ê½éèÖÃ
- CAN_InitStructure.CAN_SJW=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq~CAN_SJW_4tq
- CAN_InitStructure.CAN_BS1=tbs1; //Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
- CAN_InitStructure.CAN_BS2=tbs2;//Tbs2·¶Î§CAN_BS2_1tq ~ CAN_BS2_8tq
- CAN_InitStructure.CAN_Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
- CAN_Init(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1
- //ÅäÖÃ1yÂËÆ÷
- CAN_FilterInitStructure.CAN_FilterNumber=0; //1yÂËÆ÷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_Filter_FIFO0;//1yÂËÆ÷01Øáaμ½FIFO0
- CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //¼¤»î1yÂËÆ÷0
- CAN_FilterInit(&CAN_FilterInitStructure);//ÂË2¨Æ÷3õê¼»ˉ
-
- #if CAN1_RX0_INT_ENABLE
-
- CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
-
- NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ′ÎóÅÏ輶Îa0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- return 0;
- }
复制代码- //can¿ú½óêÕêy¾Y2éÑˉ
- //buf:êy¾Y»o′æÇø;
- //·μ»ØÖμ:0,ÎTêy¾Y±»êÕμ½;
- // ÆäËû,½óêÕμÄêy¾Y3¤¶è;
- u8 CAN1_Receive_Msg(u8 *buf)
- {
- /*
- CanRxMsg RxMessage;
- if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //ûóD½óêÕμ½êy¾Y,Ö±½óíË3ö
-
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶áè¡êy¾Y
- dataFuc(RxMessage);*/
- u8 data[100];
- u32 i,j;
- CanRxMsg rx;
- CanRxMsg RxMessage;
- // CanRxMsg *msgs;
- canmsgs_t msgs;
- // msgs=(CanRxMsg*)Mem_malloc(EXSRAM,sizeof(CanRxMsg)*43);
- // if (msgs == NULL)
- // {
- // uart1SendChars("malloc error!",strlen("malloc error!"));
- // return 1;/* ?????????,????????? */
- // }
- memset(data, 0, 100);
- for(i = 0;i<21;)
- {
-
- if( CAN_MessagePending(CAN1,CAN_FIFO0) == 3)
- {
-
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶áè¡êy¾Y
-
- sprintf(data, "CAN_RF0R_FOVR0 = %d\r\n", (CAN1->RF0R &= CAN_RF0R_FOVR0)>>4);
- uart1SendChars(data,strlen(data));
-
- sprintf(data, "CAN_RF0R_FULL0=%d\r\n", (CAN1->RF0R &= CAN_RF0R_FULL0)>>3);
- uart1SendChars(data,strlen(data));
-
- sprintf(data, "CAN_RF0R_RFOM0 =%d\r\n", (CAN1->RF0R &= CAN_RF0R_RFOM0)>>5);
- uart1SendChars(data,strlen(data));
-
- // msgs[i] = RxMessage;
- msgs.mmsg[i++] = RxMessage;
- }
-
- // delay_us(500);
- }
- // for(j=0;j<21;j++)
- // {
- // rx = msgs.mmsg[j];
- //// rx = msgs[j];
- // sprintf(data, "%02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n", j,rx.Data[0],rx.Data[1],rx.Data[2],rx.Data[3],rx.Data[4],rx.Data[5],rx.Data[6],rx.Data[7]);
- // uart1SendChars(data,strlen(data));
- // }
-
- // Mem_free(EXSRAM,msgs);
- // for(i=0;i<RxMessage.DLC;i++)
- // buf[i]=RxMessage.Data[i];
- // sprintf(data, "%02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n",CAN_MessagePending(CAN1, CAN_FIFO0), buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
- // uart1SendChars(data,strlen(data));
- return 0;
复制代码 上面三段代码是 主函数,can初始化,和can接收数据的程序,问题是:
传感器是以83帧数据为周期发送连续的can数据帧,我是连续读取21帧数据到can数据帧结构体数组中,通过串口发送到上位机,查看id是否一致。发现数据帧并不是传感器发送出来的,而是一些固定的未知数据帧。对于can的接收是FIFO0的三个接收邮箱,我怎么分别读取这三个邮箱的数据呢?我把接收到的的邮箱数打印出来观察一直都是现实三帧,也就是硬件每次读到了三帧数据到邮箱。传感器是每60ms发送83帧数据。
我弄了几天了,也没找到原因,望指点小弟一二?谢谢
|
|