stm32f103c8t6 can例程
时间: 2023-08-27 08:05:17 浏览: 131
以下是一个基于STM32F103C8T6的CAN通信例程,你可以参考:
```c
#include "stm32f10x.h"
#include "stm32f10x_can.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define CANx CAN1
#define CAN_CLK RCC_APB1Periph_CAN1
#define CAN_RX_PIN GPIO_Pin_11
#define CAN_TX_PIN GPIO_Pin_12
#define CAN_GPIO_PORT GPIOA
#define CAN_GPIO_CLK RCC_APB2Periph_GPIOA
#define CAN_AF_PORT GPIO_AF_CAN1
#define CAN_RX_SOURCE GPIO_PinSource11
#define CAN_TX_SOURCE GPIO_PinSource12
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t flag = 0;
void CAN1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
RCC_APB2PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_CAN, 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_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 6;
CAN_Init(CANx, &CAN_InitStructure);
CAN_FilterInitTypeDef CAN_FilterInitStructure;
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)
{
uint8_t i = 0;
CAN1_Config();
TxMessage.StdId = 0x12;
TxMessage.ExtId = 0x12;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 2;
TxMessage.Data[0] = 'A';
TxMessage.Data[1] = 'B';
while (1)
{
CAN_Transmit(CANx, &TxMessage);
delay_ms(1000);
if(flag == 1)
{
flag = 0;
printf("Receive Data:");
for(i = 0; i < RxMessage.DLC; i++)
{
printf("%c", RxMessage.Data[i]);
}
printf("\r\n");
}
}
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
if(CAN_GetITStatus(CANx, CAN_IT_FMP0) != RESET)
{
CAN_Receive(CANx, CAN_FIFO0, &RxMessage);
CAN_ClearITPendingBit(CANx, CAN_IT_FMP0);
flag = 1;
}
}
```
这个例程演示了如何通过CAN1接口发送和接收数据。在这个例程中,我们使用CAN1接口和PA11(CAN_RX)和PA12(CAN_TX)引脚。
在主函数中,我们首先配置CAN1接口,并设置发送消息。然后,我们不断地发送消息,并等待接收消息。如果接收到消息,则打印接收到的数据。
在中断处理程序中,我们检查是否收到了一个新的消息。如果是,则接收消息并设置一个标志以指示主函数中有新数据可用。
请注意,这个例程还需要一个延迟函数,你需要自己实现。你可以使用定时器或其他方法来实现延迟函数。
阅读全文