用固件库函数方式写个Stm32f103c8t6 写个程序PA4外部上拉,外部8M晶体,PA11为CAN RX,PA12为CAN TX ,当CAN 接收到数据00 00 00 01 00 00 00 00时PA4输出低电平,波特率为500K,帧ID为0XB4
时间: 2023-08-22 08:10:15 浏览: 80
GD32GD32F103C8T6 GD库编写的通过CAN控制4位数码管程序,数码管驱动MAX7219M
好的,我了解了你的要求。下面是代码,你可以参考一下:
```c
#include "stm32f10x.h"
#include "stm32f10x_can.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define CAN_RX_PIN GPIO_Pin_11
#define CAN_TX_PIN GPIO_Pin_12
void GPIO_Configuration(void);
void CAN_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
GPIO_Configuration();
CAN_Configuration();
NVIC_Configuration();
while (1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// PA4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// PA11,PA12
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
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_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 4;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000B400;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x00000000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000FFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000FFFF;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if (RxMessage.StdId == 0xB4 && RxMessage.Data[0] == 0x00 && RxMessage.Data[1] == 0x00 && RxMessage.Data[2] == 0x00 && RxMessage.Data[3] == 0x01 && RxMessage.Data[4] == 0x00 && RxMessage.Data[5] == 0x00 && RxMessage.Data[6] == 0x00 && RxMessage.Data[7] == 0x00)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
}
```
在这个程序中,我们首先进行了 GPIO 和 CAN 的初始化配置,然后在主循环中一直等待接收到 CAN 信息。当接收到 CAN 信息时,我们会检查其帧 ID 和数据是否满足要求,如果满足,就将 PA4 输出低电平。
这个程序的波特率为 500K,帧 ID 为 0xB4,数据为 00 00 00 01 00 00 00 00。你可以根据需要自行修改这些参数。
阅读全文