我在CAN_TX线上看不到任何信号

时间:2019-06-27 14:09:38

标签: c stm32 can-bus

我尝试在STM32F303发现板上使用CAN总线,而我正在使用PD0→RX和PD1→TX引脚。

我想尝试发送消息并在示波器上进行分析。因此,我的配置显示在下面的代码块中。

当我用示波器分析哪些引脚是PD0和PD1时,我在示波器上看不到任何东西。

我的错在哪里?

这是我的代码:

#include <stm32f30x.h>
#include <stm32f30x_i2c.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_syscfg.h>
#include <stm32f30x_exti.h>
// #include <stm32f30x_misc.h>
#include "main.h"
#include <stdio.h>
#include "common.h"
#include "io_periph_defs.h"
#include "stm32f30x.h"
#include <stdio.h>
#include "conf.h"

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;

void Init_TxMes(CanTxMsg *TxMessage)
{
    /* Transmit INITIALIZATION*/

    TxMessage->IDE = CAN_ID_STD;

    TxMessage->DLC = 2;

    TxMessage->StdId = 0x321;

    TxMessage->RTR = CAN_RTR_DATA;
}

void Init_RxMes(CanRxMsg *RxMessage)
{
    uint8_t i = 0;

    RxMessage->StdId = 0;

    RxMessage->IDE = CAN_ID_STD;

    RxMessage->DLC = 0;

    RxMessage->FMI = 0;

    for (i = 0; i < 8; i++)
    {
        RxMessage->Data[i] = 0;
    }
}

void CAN_setup(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //Enable the clock for CAN

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);

    /* Configure CAN1 RX pin */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    /* Configure CAN1 TX pin */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_9);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_9);
    /* Enable the CAN global Interrupt */

    NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;

    NVIC_Init(&NVIC_InitStructure);

    CAN_DeInit(CAN1);

    /* Configure CAN1 **************************************************/

    /* Struct init*/

    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_Normal;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

    /* CAN Baudrate = 125kbps (CAN clocked at 36 MHz) */
    CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
    CAN_InitStructure.CAN_Prescaler = 16;
    CAN_Init(CAN1, &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);
}

int main(void)
{
    int i;

    uint8_t TransmitMailbox = 0;

    CAN_setup(); // Set up CAN interface

    Init_TxMes(&TxMessage);
    init_pin(GPIOA, GPIO_Pin_8, GPIO_Mode_OUT, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_UP);

    while (1)
    {
        TxMessage.Data[0] = 0xAA;
        GPIO_SetBits(GPIOA, GPIO_Pin_8); // Transmit data

        TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);

        i = 0;
        while ((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK))
            ;                              // I put a breakpoint here to check
                                           // and found the message is pending.

        GPIO_ResetBits(GPIOA, GPIO_Pin_8); // Transmit data

        //i++;
    }
}

我认为有问题,但是我该如何解决?

循环堆叠。

while ((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK)

0 个答案:

没有答案