描述问题:当发送三帧数据的时候,利用串口调试助手发现结果正确,如图1;但是当发送四帧数据时候,第四帧与前三帧之间添加延时,串口调试助手如图2.
图片1(串口结果,三帧): 图片2(串口结果,四帧):
图2:四帧
代码如下:
CAN配置:
tatic void CAN_NVIC_Configuration(void){
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable CAN1 RX0 interrupt IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // ′ÎóÅÏ輶Îa3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* oˉêyÃû£oCAN_GPIO_Config
* Ãèêö £oCAN GPIO oíê±ÖóÅäÖÃ
* êäèë £oÎT
* êä3ö : ÎT
* μ÷óÃ £oÄú2¿μ÷óÃ
*/
static void CAN_GPIO_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* CAN1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* Configure CAN pin: RX */ // PA11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; // GPIO_Mode_IN_FLOATING IPU // éÏà-êäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */ // PA12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USER_CAN_Config(void){
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; // ê±¼ä′¥·¢í¨DŽûÖ1
CAN_InitStructure.CAN_ABOM=ENABLE; // àëÏßíË3öêÇÔúÖD¶ÏÖÃλÇå0oóíË3ö
CAN_InitStructure.CAN_AWUM=ENABLE; // ×Ô¶ˉ»½DÑģ꽣oó2¼t»½DÑ
CAN_InitStructure.CAN_NART=DISABLE; // ½ûÖ1ÖØDÂ′«Ëí
CAN_InitStructure.CAN_RFLM=DISABLE; // FIFOûóDËø¶¨£¬D±¨Îĸ2¸Ç¾é±¨ÎÄ
CAN_InitStructure.CAN_TXFP=DISABLE; // ·¢Ëí±¨ÎÄóÅÏ輶跶¨£o±êÖ¾·û;(IDÄ£ê½ó鱨ÎÄμÄID¾ö¶¨£¬¼′IDÖμÔ½D¡£¬óÅÏ輶Խ¸ß.)
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; // Õy3£Ä£ê½ CAN_Mode_Normal
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; // 1tq¡¢BS1¡¢BS2μÄÖμ¸ú2¨ìØÂêóD1Ø
CAN_InitStructure.CAN_BS1=CAN_BS1_3tq; // ¿éòÔéèÖÃ16¸öê±¼äμ¥λ £¬êμ¼êD′èëμÄÖμÎa5.
CAN_InitStructure.CAN_BS2=CAN_BS2_5tq; // ¿éòÔéèÖÃ8¸öê±¼äμ¥λ£¬êμ¼êD′èëμÄÖμÎa1.
CAN_InitStructure.CAN_Prescaler=16; // ·ÖÆμÏμêyÎa16 36/((1+3+5)*16)=250KHz£¬·¶Î§1μ½1024,D′3éê2Ã′¾íêÇê2Ã′£¬Ôú·a×°oˉêyàïÃæòѾ-¼õ1.
CAN_Init(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1; //Ñ¡Ôñ1yÂËÆ÷1£¬éèÖÃÆäËû1yÂËÆ÷μÄê±oò£¬DèòaÖØDÂÅäÖã¬2»Äüê1óûòóï¾äà′ÅäÖöà¸ö1yÂËÆ÷
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //Æá±Îλģ꽣¨CAN_FilterMode_IdList ±ê궷ûáD±íģ꽣©
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; //½óêÕμ½μÄêy¾Y′æμ½FIFO0£¬μ½ê±oòÖ±½ó¶á
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); //ÖD¶Ïê1Äü£¬óêÏä0μıê־λ
}
回环测试四帧数据代码:
void CAN_SdPl_One(uint16_t ID){
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t TransmitMailbox;
uint8_t i,n;
TxMessage.StdId=ID; // ±ê×¼±ê궷ûÎa0x0011,±íê¾μúò»×éμÄμúò»Ö¡êy¾Y£¬óÃμúò»¸öËÄλ±íê¾Ö¡êy£¬óÃμú¶t¸öËÄλ±íê¾×éêy¡£0000 0001 0001
TxMessage.IDE=CAN_ID_STD; // ê1óñê×¼±ê궷û
TxMessage.RTR=CAN_RTR_DATA; // Ö¡ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=8; // ·¢Ëí8¸ö×Ö½úDÅÏ¢,êy¾Y3¤¶èÂ룬ָ¶¨′«êäÖ¡μÄ3¤¶è
// for(i=0;i<8;i++){
// TxMessage.Data
=CAN_SendArray;
// }
TxMessage.Data[0]=0x11;
TxMessage.Data[1]=0x12;
TxMessage.Data[2]=0x13;
TxMessage.Data[3]=0x14;
TxMessage.Data[4]=0x15;
TxMessage.Data[5]=0x16;
TxMessage.Data[6]=0x17;
TxMessage.Data[7]=0x18;
TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); //¿aê¼′«êäò»¸öÏûÏ¢£¬ oˉêyóD¸ö·μ»ØÖ죬·μ»ØóêÏäoÅ0£¬1£¬2
while((CAN_TransmitStatus(CAN1, TransmitMailbox)!= CANTXOK)&&(n!=0xFF)){ //í¨1y¼ì2éCANTXOKà′è·¶¨·¢ËíêÇ·ñ3é1|£¬CANTXPENDING£oÏûÏ¢êÇ·ñ1òoÅ£»CANTXFAILEDÆäËû
n++;
}
n=0;
while((CAN_MessagePending(CAN1,CAN_FIFO0)==0)&&(n!=0xFF)){
n++;
}
RxMessage.StdId=0x00; //é趨±ê×¼±ê궷û
RxMessage.IDE=CAN_ID_STD; //ê1óñê×¼±ê궷û
RxMessage.DLC=0; //óÃà′é趨′y′«êäμÄÖ¡3¤¶è
for(i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if(RxMessage.StdId == ID){
for(i=0;i<RxMessage.DLC;i++){
printf(" %d",RxMessage.Data);
}
printf(" The first frame has sent and got!
");
}
}
void CAN_SdPl_Two(uint16_t ID){
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t TransmitMailbox;
uint8_t i,n;
TxMessage.StdId=ID; // ±ê×¼±ê궷ûÎa0x0011,±íê¾μúò»×éμÄμúò»Ö¡êy¾Y£¬óÃμúò»¸öËÄλ±íê¾Ö¡êy£¬óÃμú¶t¸öËÄλ±íê¾×éêy¡£0000 0001 0001
TxMessage.IDE=CAN_ID_STD; // ê1óñê×¼±ê궷û
TxMessage.RTR=CAN_RTR_DATA; // Ö¡ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=8; // ·¢Ëí8¸ö×Ö½úDÅÏ¢,êy¾Y3¤¶èÂ룬ָ¶¨′«êäÖ¡μÄ3¤¶è
// for(i=0;i<8;i++){
// TxMessage.Data=CAN_SendArray;
// }
TxMessage.Data[0]=0x21;
TxMessage.Data[1]=0x22;
TxMessage.Data[2]=0x23;
TxMessage.Data[3]=0x24;
TxMessage.Data[4]=0x25;
TxMessage.Data[5]=0x26;
TxMessage.Data[6]=0x27;
TxMessage.Data[7]=0x28;
TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); //¿aê¼′«êäò»¸öÏûÏ¢£¬ oˉêyóD¸ö·μ»ØÖ죬·μ»ØóêÏäoÅ0£¬1£¬2
while((CAN_TransmitStatus(CAN1, TransmitMailbox)!= CANTXOK)&&(n!=0xFF)){ //í¨1y¼ì2éCANTXOKà′è·¶¨·¢ËíêÇ·ñ3é1|£¬CANTXPENDING£oÏûÏ¢êÇ·ñ1òoÅ£»CANTXFAILEDÆäËû
n++;
}
n=0;
while((CAN_MessagePending(CAN1,CAN_FIFO0)==0)&&(n!=0xFF)){
n++;
}
RxMessage.StdId=0x00; //é趨±ê×¼±ê궷û
RxMessage.IDE=CAN_ID_STD; //ê1óñê×¼±ê궷û
RxMessage.DLC=0; //óÃà′é趨′y′«êäμÄÖ¡3¤¶è
for(i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if(RxMessage.StdId == ID){
for(i=0;i<RxMessage.DLC;i++){
printf(" %d",RxMessage.Data);
}
printf(" The second frame has sent and got!
");
}
}
void CAN_SdPl_Three(uint16_t ID){
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t TransmitMailbox;
uint8_t i,n;
TxMessage.StdId=ID; // ±ê×¼±ê궷ûÎa0x0011,±íê¾μúò»×éμÄμúò»Ö¡êy¾Y£¬óÃμúò»¸öËÄλ±íê¾Ö¡êy£¬óÃμú¶t¸öËÄλ±íê¾×éêy¡£0000 0001 0001
TxMessage.IDE=CAN_ID_STD; // ê1óñê×¼±ê궷û
TxMessage.RTR=CAN_RTR_DATA; // Ö¡ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=8; // ·¢Ëí8¸ö×Ö½úDÅÏ¢,êy¾Y3¤¶èÂ룬ָ¶¨′«êäÖ¡μÄ3¤¶è
// for(i=0;i<8;i++){
// TxMessage.Data=CAN_SendArray;
// }
TxMessage.Data[0]=0x31;
TxMessage.Data[1]=0x32;
TxMessage.Data[2]=0x33;
TxMessage.Data[3]=0x34;
TxMessage.Data[4]=0x35;
TxMessage.Data[5]=0x36;
TxMessage.Data[6]=0x37;
TxMessage.Data[7]=0x38;
TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); //¿aê¼′«êäò»¸öÏûÏ¢£¬ oˉêyóD¸ö·μ»ØÖ죬·μ»ØóêÏäoÅ0£¬1£¬2
while((CAN_TransmitStatus(CAN1, TransmitMailbox)!= CANTXOK)&&(n!=0xFF)){ //í¨1y¼ì2éCANTXOKà′è·¶¨·¢ËíêÇ·ñ3é1|£¬CANTXPENDING£oÏûÏ¢êÇ·ñ1òoÅ£»CANTXFAILEDÆäËû
n++;
}
n=0;
while((CAN_MessagePending(CAN1,CAN_FIFO0)==0)&&(n!=0xFF)){
n++;
}
RxMessage.StdId=0x00; //é趨±ê×¼±ê궷û
RxMessage.IDE=CAN_ID_STD; //ê1óñê×¼±ê궷û
RxMessage.DLC=0; //óÃà′é趨′y′«êäμÄÖ¡3¤¶è
for(i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if(RxMessage.StdId == ID){
for(i=0;i<RxMessage.DLC;i++){
printf(" %d",RxMessage.Data);
}
printf(" The thrid frame has sent and got!
");
}
}
void CAN_SdPl_Four(uint16_t ID){
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t TransmitMailbox;
uint8_t i,n;
TxMessage.StdId=ID; // ±ê×¼±ê궷ûÎa0x0011,±íê¾μúò»×éμÄμúò»Ö¡êy¾Y£¬óÃμúò»¸öËÄλ±íê¾Ö¡êy£¬óÃμú¶t¸öËÄλ±íê¾×éêy¡£0000 0001 0001
TxMessage.IDE=CAN_ID_STD; // ê1óñê×¼±ê궷û
TxMessage.RTR=CAN_RTR_DATA; // Ö¡ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=8; // ·¢Ëí8¸ö×Ö½úDÅÏ¢,êy¾Y3¤¶èÂ룬ָ¶¨′«êäÖ¡μÄ3¤¶è
// for(i=0;i<8;i++){
// TxMessage.Data=CAN_SendArray;
// }
TxMessage.Data[0]=0x41;
TxMessage.Data[1]=0x42;
TxMessage.Data[2]=0x43;
TxMessage.Data[3]=0x44;
TxMessage.Data[4]=0x45;
TxMessage.Data[5]=0x46;
TxMessage.Data[6]=0x47;
TxMessage.Data[7]=0x48;
TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); //¿aê¼′«êäò»¸öÏûÏ¢£¬ oˉêyóD¸ö·μ»ØÖ죬·μ»ØóêÏäoÅ0£¬1£¬2
while((CAN_TransmitStatus(CAN1, TransmitMailbox)!= CANTXOK)&&(n!=0xFF)){ //í¨1y¼ì2éCANTXOKà′è·¶¨·¢ËíêÇ·ñ3é1|£¬CANTXPENDING£oÏûÏ¢êÇ·ñ1òoÅ£»CANTXFAILEDÆäËû
n++;
}
n=0;
while((CAN_MessagePending(CAN1,CAN_FIFO0)==0)&&(n!=0xFF)){
n++;
}
RxMessage.StdId=0x00; //é趨±ê×¼±ê궷û
RxMessage.IDE=CAN_ID_STD; //ê1óñê×¼±ê궷û
RxMessage.DLC=0; //óÃà′é趨′y′«êäμÄÖ¡3¤¶è
for(i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if(RxMessage.StdId == ID){
for(i=0;i<RxMessage.DLC;i++){
printf(" %d",RxMessage.Data);
}
printf(" The forth frame has sent and got!
");
}
}
写入三帧数据代码:
void CAN_SdPl_Temp(void){
CAN_SdPl_One(0x0011);
CAN_SdPl_Two(0x0012);
CAN_SdPl_Three(0x0013);
}
写入四帧数据代码:
void CAN_SdPl_Temp(void){
CAN_SdPl_One(0x0011);
CAN_SdPl_Two(0x0012);
CAN_SdPl_Three(0x0013);
Delay_ms(100);
CAN_SdPl_Four(0x0014);
}
主函数:
while(1){
if(FLAGS0_CANSdTp){
CAN_SdPl_Temp();
FLAGS0_CANSdTp = FALSE;
}
}
问题出现了很久,一直没有解决多出来,求各位大佬帮忙解答!
一周热门 更多>