打印
[STM32F1]

F103使用CAN死活不通,求助各位

[复制链接]
1825|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
老牛09|  楼主 | 2015-11-5 15:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
CAN, pi, IO, ni, GPIO
自己做的F103 + MCP2551的板,CAN死活不通,以为硬件设计有问题,又单独用焊片2551小板, 接到一个F405的板子上,能正常发送数据给别的CAN设备了,在接到103上,还是不通, 怀疑做的板有问题,又找一个F103的开发板来测,接这个2551小板,还是不通,现在软件硬件都查了无数回了,还是没找到原因:'(,
103回环模式,引脚有波形,正常下无,总线上以无波形。

测试代码:
//can function ------------------------------------------------
#define CAN_REMAP
#define USE_CAN
int canStat = 0;
static void can_rcc_config()
{
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
    //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
        RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE);
   
#ifdef CAN_REMAP   
    //Remap  to PB8, PB9
        GPIO_PinRemapConfig( GPIO_Remap1_CAN1, ENABLE);
#endif
}
static void can_gpio_config()
{
    GPIO_InitTypeDef initStructure;

#ifdef CAN_REMAP
        //CAN RX
    initStructure.GPIO_Pin  = GPIO_Pin_8;
    initStructure.GPIO_Speed = GPIO_Speed_50MHz;
    initStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init( GPIOB, &initStructure );

    //CAN TX
    initStructure.GPIO_Pin  = GPIO_Pin_9;
    initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init( GPIOB, &initStructure );
#else
    //CAN RX
    initStructure.GPIO_Pin  = GPIO_Pin_11;
    initStructure.GPIO_Speed = GPIO_Speed_50MHz;
    initStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init( GPIOA, &initStructure );

    //CAN TX
    initStructure.GPIO_Pin  = GPIO_Pin_12;
    initStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init( GPIOA, &initStructure );
#endif       
    //?
        //GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN0);
    //GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
}
static void can_config()
{
    CAN_InitTypeDef initStructure;
    CAN_FilterInitTypeDef filterInitStructure;

    //CAN_DeInit( CAN1 );
    CAN_StructInit(&initStructure );
    initStructure.CAN_TTCM = DISABLE;
    initStructure.CAN_ABOM = DISABLE;  //如果ABOM位为’1’,bxCAN进入离线状态后,就自动开启恢复过程
    initStructure.CAN_AWUM = DISABLE;
    initStructure.CAN_NART = DISABLE;  // DISABLE;  //禁止自动重发
    initStructure.CAN_RFLM = DISABLE;
    initStructure.CAN_TXFP = DISABLE;
    initStructure.CAN_Mode = CAN_Mode_Normal; //CAN_Mode_LoopBack , CAN_Mode_Normal;
    initStructure.CAN_SJW  = CAN_SJW_1tq;  // CAN_SJW_1tq;
    initStructure.CAN_BS1  = CAN_BS1_8tq; //CAN_BS1_5tq; // CAN_BS1_8tq;
    initStructure.CAN_BS2  = CAN_BS2_7tq;// CAN_BS2_3tq;  // CAN_BS2_7tq;
   
    //36M / ( 1 + 5 + 3 ) / 16 = 250kbps
    //2,1,9 = 1M;   8,7,9 = 250k
    initStructure.CAN_Prescaler = 9; //16
    CAN_Init(CAN1, &initStructure );

    filterInitStructure.CAN_FilterNumber = 0;
    filterInitStructure.CAN_FilterMode   = CAN_FilterMode_IdMask;
    filterInitStructure.CAN_FilterScale  = CAN_FilterScale_32bit;
    filterInitStructure.CAN_FilterIdHigh = 0x0000;// << 3;
    filterInitStructure.CAN_FilterIdLow  = 0x0000;
    filterInitStructure.CAN_FilterMaskIdHigh = 0x0000;// << 3;
    filterInitStructure.CAN_FilterMaskIdLow  = 0x0000;
    filterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
    filterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit( &filterInitStructure );

}


void can_sendData()
{
    static uint32_t count = 0;
    int tryCount = 0;
    uint8_t ret = 0;

    uint8_t transmitMailbox = 0;
    CanTxMsg txMessage;
    CanRxMsg rxMessage;

    //txMessage.ExtId = 0x00AA0000;
    txMessage.StdId = 0x123;
    txMessage.ExtId = 0x12345;
    txMessage.RTR = CAN_RTR_DATA;
    txMessage.IDE = CAN_ID_EXT;
    //txMessage.IDE = CAN_ID_STD;
    txMessage.DLC = 8;
    txMessage.Data[0] = 0x00;
    txMessage.Data[1] = 0x12;
    txMessage.Data[2] = 0x34;
    txMessage.Data[3] = 0x56;
    txMessage.Data[4] = ((char*)&count)[0];// 0x78;
    txMessage.Data[5] = ((char*)&count)[1];//0xAB;
    txMessage.Data[6] = ((char*)&count)[2];//0xCD;
    txMessage.Data[7] = ((char*)&count)[3];//0xEF;
    count++;

    transmitMailbox = CAN_Transmit(CAN1, &txMessage );
   
    if( transmitMailbox < 3 )
    {
        ret = CAN_TransmitStatus(CAN1, transmitMailbox);
        while( tryCount < 1000 && (ret) != CANTXOK )
        {
            ret = CAN_TransmitStatus(CAN1, transmitMailbox);
            tryCount++;
        };
    }

}



沙发
icecut| | 2015-11-5 16:17 | 只看该作者
你先找个别人用着没问题的板子和代码.

使用特权

评论回复
板凳
老牛09|  楼主 | 2015-11-6 22:17 | 只看该作者
icecut 发表于 2015-11-5 16:17
你先找个别人用着没问题的板子和代码.

我已经用了两个不同的板了,代码已都反复查过了。还是这样啊

使用特权

评论回复
地板
457344370| | 2015-11-7 07:37 | 只看该作者
以前写的,当年调OSEK的时候随手写的,在C8T6和VET6上使用正常,波特率125K,中断接收

#include "stm32f10x.h"
#include "sys_can.h"

CanTxMsg CAN_TxMessage;
CanRxMsg CAN_RxMessage;

static void CAN_GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

        GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
        /* Configure CAN pin: RX */                                                                                       // PB8
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        /* Configure CAN pin: TX */                                                                                       // PB9
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
}

static void CAN_NVIC_Config(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
       
        NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

static void CAN_Mode_Config(void)
{
        CAN_InitTypeDef CAN_InitStructure;

        CAN_DeInit(CAN1);
        CAN_StructInit(&CAN_InitStructure);

        CAN_InitStructure.CAN_TTCM=DISABLE;                           //MCR-TTCM  关闭时间触发通信模式使能
        CAN_InitStructure.CAN_ABOM=ENABLE;                           //MCR-ABOM  自动离线管理
        CAN_InitStructure.CAN_AWUM=ENABLE;                           //MCR-AWUM  使用自动唤醒模式
        CAN_InitStructure.CAN_NART=DISABLE;                           //MCR-NART  禁止报文自动重传          DISABLE-自动重传
        CAN_InitStructure.CAN_RFLM=DISABLE;                           //MCR-RFLM  接收FIFO 锁定模式  DISABLE-溢出时新报文会覆盖原有报文  
        CAN_InitStructure.CAN_TXFP=DISABLE;                           //MCR-TXFP  发送FIFO优先级 DISABLE-优先级取决于报文标示符
        CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
        CAN_InitStructure.CAN_SJW=CAN_SJW_3tq;
        CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
        CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
        CAN_InitStructure.CAN_Prescaler =6;
        CAN_Init(CAN1, &CAN_InitStructure);
}

static void CAN_Filter_Config(void)
{
        CAN_FilterInitTypeDef  CAN_FilterInitStructure;

        CAN_FilterInitStructure.CAN_FilterNumber=0;
        CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList;        //工作在标识符掩码模式
        CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;        //过滤器位宽为单个32位。

        CAN_FilterInitStructure.CAN_FilterIdHigh= 0x400<<5;                                //要过滤的ID高位
        CAN_FilterInitStructure.CAN_FilterIdLow= CAN_RTR_Data|CAN_Id_Standard; //要过滤的ID低位
        CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xffff;                                                                //屏蔽0x400-0x4ff以外的所有ID
        CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xffff;                                                                        //屏蔽非标准数据帧
        CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ;                                //过滤器被关联到FIFO0
        CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;                        //使能过滤器
        CAN_FilterInit(&CAN_FilterInitStructure);
       
        CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}

void sys_canInit(void)
{
  CAN_GPIO_Config();
  CAN_NVIC_Config();
  CAN_Mode_Config();
  CAN_Filter_Config();
}


//#define USE_UCOS_II
#ifdef USE_UCOS_II
  extern void OSIntEnter(void);
  extern void OSIntExit(void);
  extern short OSTimeDlyResume(short);
#else
  #define OSIntEnter()
  #define OSIntExit()
  #define OSTimeDlyResume(x)
#endif


//extern void APP_CanMsg_Handler(void);//在CAN_Msg中定义
extern CanRxMsg CAN_RxMessage;

void USB_LP_CAN1_RX0_IRQHandler(void)
{
        OSIntEnter();
        if(CAN_GetITStatus(CAN1, CAN_IT_FMP0)!=RESET)
        {
//                APP_CanMsg_Handler();
    CAN_Receive(CAN1, CAN_FIFO0, &CAN_RxMessage);
//    OSTimeDlyResume(10);
        }
//        do
//        {
//                CAN_FIFORelease(CAN1,CAN_FIFO0);
//        }while(CAN_MessagePending(CAN1, CAN_FIFO0));
        OSIntExit();
}

void CAN1_RX1_IRQHandler(void)
{
        OSIntEnter();
        CAN_FIFORelease(CAN1,CAN_FIFO1);
        OSIntExit();
}

void CAN1_SCE_IRQHandler(void)
{
        OSIntEnter();
        if(CAN_GetITStatus(CAN1, CAN_IT_ERR)!=RESET)
        {
                CAN_ClearITPendingBit(CAN1, CAN_IT_ERR);
        }
        OSIntExit();
}

使用特权

评论回复
5
huangcunxiake| | 2015-11-7 20:13 | 只看该作者
这也太费劲了,上面这位朋友的代码可以正常使用吗?硬件配置有问题没?

使用特权

评论回复
6
老鼠不怕猫| | 2015-11-9 11:40 | 只看该作者
本公司代理STM32&STM8全系列MCU,意法半导体指定认证代理商,有需要报价或要货的可以与我联络。qq2355925821,电话13802244205。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

22

帖子

0

粉丝