我用还回模式自测。我在PA11,PA12,没接线的情况下收到了讯息。我有些不解,不知道哪里错了,或者哪里理解错了。还望指教一下谢谢。(发送信息成功点亮PD12,收到成功点亮PD13)。代码借用了你的上面这一段。
#include "bsp.h"
#include "stdio.h"
#include "rcs.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_flash.h"
#include "system_stm32f4xx.h"
void RCC_Configuration(void);
// void CAN_Config(void);
void GPIO_CAN_init(void);
CanTxMsg TxMessage;
CanRxMsg RxMessage;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
int main(void)
{
uint32_t state = 0;
uint8_t transmit_mailbox = 0;
RCC_Configuration();
//CAN_Config();
GPIO_CAN_init();
while (1)
{
state = 0;
RxMessage.DLC = 0;
transmit_mailbox = CAN_Transmit(CAN1, &TxMessage);
state = CAN_TransmitStatus(CAN1, transmit_mailbox);
//CAN_FIFORelease(CAN1, CAN_FIFO0);
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if (state != 0)
{
GPIO_SetBits(GPIOD, GPIO_Pin_12);
}
if (RxMessage.DLC < 9)
{
GPIO_SetBits(GPIOD, GPIO_Pin_13);
}
//OSTimeDly(3000);
//GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13);
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* 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(ENABLE);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK */
RCC_PCLK1Config(RCC_HCLK_Div1);
/* Select HSE as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Wait till HSE is used as system clock source */
while (RCC_GetSYSCLKSource() != 0x04)
{
}
}
}
// void CAN_Config(void)
// {
// GPIO_InitTypeDef GPIO_InitStructure;
// /* GPIO clock enable */
// GPIO_PinAFConfig (GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
// GPIO_PinAFConfig (GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
// /* Configure CAN pin: RX */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//| GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// /* Configure CAN pin: TX */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// GPIO_StructInit(&GPIO_InitStructure); //初始化GPIO_InitStructure变量
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; //定义为12管脚号
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式设置为输出
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度为50MHz
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //内部上拉
// GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化IO口
// //Configure CAN pin: RX
// /* CANx Periph clock enable */
// /* CAN register init */
// CAN_DeInit(CAN1);
// 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 Baudrate = 1MBps*/
// CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
// CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
// CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
// CAN_InitStructure.CAN_Prescaler = 4;
// 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);
// /* Transmit */
// TxMessage.StdId = 0x321;
// TxMessage.ExtId = 0x01;
// TxMessage.IDE = CAN_ID_STD;
// TxMessage.RTR = CAN_RTR_DATA;
// TxMessage.DLC = 1;
// }
void GPIO_CAN_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_StructInit(&GPIO_InitStructure); //初始化GPIO_InitStructure变量
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;//| GPIO_Pin_14 | GPIO_Pin_15; //定义为12管脚号
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式设置为输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度为50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //内部上拉
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化IO口
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //CAN IO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //CAN 时钟
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //RX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //输入模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //设置为推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //设置上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //TX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //设置为推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //其他未置引脚悬空
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1); //启动IO复用功能
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
/*-----------CAN控制器初始化 ------------------------------------------*/
CAN_DeInit(CAN1); //复位所有设置
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE ; //使能离线管理
// CAN_InitStructure.CAN_ABOM = ENABLE ; //使能离线管理
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE ; //使能自动重传
// CAN_InitStructure.CAN_NART = ENABLE ; //使能自动重传
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE; //复位所有设置
CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; //设置为正常模式
/* CAN Baudrate = 125kbps (CAN clocked at 30 MHz) */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //波特率相关设置
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
while (CAN_Init(CAN1, &CAN_InitStructure) != CANINITOK); //此处为在线调试断点,程序卡在这里,初始化成功则继续往下
/*-----------初始化滤波设置--------------------------------------------*/
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); /* 载入设置 */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
/* Transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA; //数据帧
TxMessage.DLC = 1;
TxMessage.Data[0] = 0xCA;
/* RECIEVE */
RxMessage.StdId = 0x00;
RxMessage.ExtId = 0x01;
RxMessage.IDE = CAN_ID_STD;
RxMessage.DLC = 0;
RxMessage.Data[0] = 0x00;
}
|