高级会员
- 积分
- 944
- 金钱
- 944
- 注册时间
- 2014-10-9
- 在线时间
- 93 小时
|
5金钱
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
uint8_t ubKeyNumber = 0x0;
static void NVIC_Config(void);
static void CAN_Config(void);
static void Delay(void);
void bsp_can(void);
void Init_RxMes(CanRxMsg *RxMessage);
void main(void)
{
NVIC_Config();
CAN_Config();
bsp_can();
while(1)
{
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 12;
TxMessage.Data[1] = 12;
TxMessage.Data[2] = 12;
TxMessage.Data[3] = 12;
TxMessage.Data[4] = 12;
TxMessage.Data[5] = 12;
TxMessage.Data[6] = 12;
TxMessage.Data[7] = 12;
CAN_Transmit(CANx, &TxMessage);
Delay();
}
}
static void CAN_Config(void)
{
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
CAN_DeInit(CANx);
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_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(CANx, &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);
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}
static void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void bsp_can(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH|RCC_AHB1Periph_GPIOI, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //rx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOI, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //tx
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(GPIOH, &GPIO_InitStructure);
}
can配置,两个GPIO口的配置GPIO_Mode,GPIO_PuPd,GPIO_OType设的对吗,我是stm32f4的板子,还有帮我看看有错误吗,为什么单片机那两个教没输出,我用示波器量的,没任何输出? |
最佳答案
查看完整内容[请看2#楼]
回复【2楼】正点原子:
---------------------------------
恩恩,谢谢原子,问题已解决,我引脚弄错了。
|