用的cbt6调试can,可以帮我看一下驱动程序有什么问题吗?125MHZ的双机通信
单片机源程序如下:
- #include "stm32f10x.h"
- #include "./led/bsp_led.h"
- #include "./can/bsp_can.h"
- __IO uint32_t flag = 0;
- CanTxMsg TxMessage;
- CanRxMsg RxMessage;
- static void can_delay(__IO u32 nCount)
- {
- for(; nCount != 0; nCount--);
- }
- int main(void)
- {
- LED_GPIO_Config();
- CAN_Config();
- while(1)
- {
- uint8_t m;
-
- LED1_ON;
- LED3_ON;
- CAN_SetMsg(&TxMessage);
- m=CAN_Transmit(CANx, &TxMessage);
- if(CAN_TransmitStatus(CANx,m)==CAN_TxStatus_Ok)
- {
- LED1_OFF;
- }
- can_delay(10000);
- if(flag==1)
- {
- LED1_TOGGLE;
- flag=0;
- }
- }
- }
复制代码- #include "./can/bsp_can.h"
- static void CAN_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIO clock */
- RCC_APB2PeriphClockCmd(CAN_TX_GPIO_CLK|CAN_RX_GPIO_CLK, ENABLE);
- // //ÖØÓ³ÉäÒý½Å
- // GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
- /* Configure CAN TX pins */
- GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(CAN_TX_GPIO_PORT, &GPIO_InitStructure);
-
- /* Configure CAN RX pins */
- GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(CAN_RX_GPIO_PORT, &GPIO_InitStructure);
- }
- static void CAN_NVIC_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /*ÖжÏÉèÖÃ*/
- NVIC_InitStructure.NVIC_IRQChannel = CAN_RX_IRQ;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*
- * º¯ÊýÃû£ºCAN_Mode_Config
- * ÃèÊö £ºCANµÄģʽ ÅäÖÃ
- * ÊäÈë £ºÎÞ
- * Êä³ö : ÎÞ
- * µ÷Óà £ºÄÚ²¿µ÷ÓÃ
- */
- static void CAN_Mode_Config(void)
- {
- CAN_InitTypeDef CAN_InitStructure;
-
- /* Enable CAN clock */
- RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
- /*CAN¼Ä´æÆ÷³õʼ»¯*/
- CAN_DeInit(CANx);
- CAN_StructInit(&CAN_InitStructure);
- /*CANµ¥Ôª³õʼ»¯*/
- CAN_InitStructure.CAN_TTCM=DISABLE;
- CAN_InitStructure.CAN_ABOM=DISABLE;
- CAN_InitStructure.CAN_AWUM=ENABLE;
- CAN_InitStructure.CAN_NART=DISABLE;
- CAN_InitStructure.CAN_RFLM=DISABLE;
- CAN_InitStructure.CAN_TXFP=ENABLE;
- CAN_InitStructure.CAN_Mode = CAN_Mode_Normal
- CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
- CAN_InitStructure.CAN_BS1=CAN_BS1_10tq
- CAN_InitStructure.CAN_BS2=CAN_BS2_5tq
- CAN_InitStructure.CAN_Prescaler =18
- CAN_Init(CANx, &CAN_InitStructure);
- }
复制代码 在中断里接受
- extern __IO uint32_t flag ;
- extern CanRxMsg RxMessage;
- void CAN_RX_IRQHandler(void)
- {
-
- CAN_Receive(CANx, CAN_FIFO0, &RxMessage);
-
- if((RxMessage.ExtId==0x1314) && (RxMessage.IDE==CAN_ID_EXT) )//&& (RxMessage.DLC==8)
- {
- flag = 1;
- }
- else
- {
- flag = 0;
- }
- }
复制代码
|