各位,小弟刚刚开始学习STM32,现在正在调试STM103VBT6上的CAN接口,我把ST的库中的CAN例子放在自己的板子上验证了一下,测试通过。我自己的板子上使用的CAN端口被映射到D0,D1上了,我把CAN通信模式从Loopback模式修改成normal,用示波器在CANRX(PD0,已经与CAN收发器断开了)和CANTX(PD1,已经与CAN收发器断开了)引脚上查看了,结果CANTX引脚电平为0,CANRX引脚为3.3V,CAN通信模式换成原来的Loopback模式,在CANTX引脚上仍然看不到任何东西,请大家帮忙分析一下我的问题出来哪里了。
下面时我的代码
int main(void) {
#ifdef DEBUG debug(); #endif
/* System Clocks Configuration */ RCC_Configuration();
/* NVIC Configuration */ NVIC_Configuration(); /* GPIO ports pins Configuration */ GPIO_Configuration(); while (1) { /* CAN transmit at 100Kb/s and receive by polling in loopback mode */ TestRx = CAN_Polling(); if (TestRx == FAILED) { /* Turn on led */ GPIO_SetBits(GPIOE, GPIO_Pin_2); } else { /* Turn on led */ GPIO_SetBits(GPIOE, GPIO_Pin_3); } } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* CAN Periph clock enable */ // Enable GPIOD clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // 必要么 }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
// Configure LED pin PE.02 -- PE.03 as Output push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure); // Configure CAN pin: RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure);
// Configure CAN pin: TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure);
// CAN pin remap to PD0/PD1 GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE); }
/******************************************************************************* * Function Name : CAN_Polling * Description : Configures the CAN, transmit and receive by polling * Input : None * Output : None * Return : PASSED if the reception is well done, FAILED in other case *******************************************************************************/ TestStatus CAN_Polling(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; CanTxMsg TxMessage; CanRxMsg RxMessage; u32 i = 0; u8 TransmitMailbox = 0;
/* CAN register init */ CAN_DeInit(); CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */ CAN_InitStructure.CAN_TTCM=DISABLE; CAN_InitStructure.CAN_ABOM=DISABLE; CAN_InitStructure.CAN_AWUM=DISABLE; CAN_InitStructure.CAN_NART=DISABLE; CAN_InitStructure.CAN_RFLM=DISABLE; CAN_InitStructure.CAN_TXFP=DISABLE; // CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; CAN_InitStructure.CAN_BS2=CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler=5; CAN_Init(&CAN_InitStructure);
/* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber=0; CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; 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=0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure);
/* transmit */ TxMessage.StdId=0x11; TxMessage.RTR=CAN_RTR_DATA; TxMessage.IDE=CAN_ID_STD; TxMessage.DLC=2; TxMessage.Data[0]=0xCA; TxMessage.Data[1]=0xFE;
TransmitMailbox=CAN_Transmit(&TxMessage); i = 0; while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF)) { i++; }
i = 0; while((CAN_MessagePending(CAN_FIFO0) < 1) && (i != 0xFF)) { i++; }
/* receive */ RxMessage.StdId=0x00; RxMessage.IDE=CAN_ID_STD; RxMessage.DLC=0; RxMessage.Data[0]=0x00; RxMessage.Data[1]=0x00; CAN_Receive(CAN_FIFO0, &RxMessage);
if (RxMessage.StdId!=0x11) { return FAILED; }
if (RxMessage.IDE!=CAN_ID_STD) { return FAILED; }
if (RxMessage.DLC!=2) { return FAILED; }
if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE) { return FAILED; } return PASSED; /* Test Passed */ }
|