帮我写一个STM32F103基于CAN通讯的按键点灯程序
时间: 2023-09-14 16:07:10 浏览: 85
STM32F103点亮一个LED的程序
好的,以下是一个简单的基于CAN通讯的按键点灯程序。这个程序通过CAN总线接收另一个节点发送过来的数据,如果数据为1,则点亮LED灯,如果数据为0,则熄灭LED灯。
```c
#include "stm32f10x.h"
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void CAN_Configuration(void);
void CAN_Send(uint8_t data);
void delay(uint32_t count);
int main(void)
{
uint8_t can_data;
GPIO_Configuration();
NVIC_Configuration();
CAN_Configuration();
while (1)
{
if ((GPIO_ReadInputData(GPIOA) & GPIO_Pin_0) == 0) // 如果按键按下
{
CAN_Send(1); // 发送数据1到CAN总线
}
else
{
CAN_Send(0); // 发送数据0到CAN总线
}
if (CAN_GetFlagStatus(CAN1, CAN_FLAG_FMP0) != RESET) // 如果接收到数据
{
CAN_Receive(CAN1, CAN_FIFO0, &can_data); // 读取接收到的数据
if (can_data == 1) // 如果接收到的数据为1
{
GPIO_SetBits(GPIOC, GPIO_Pin_13); // 点亮LED灯
}
else // 如果接收到的数据为0
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13); // 熄灭LED灯
}
}
delay(10000); // 延时一段时间
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // PA0为按键
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; // PC13为LED灯
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
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 CAN_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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_5tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
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 = 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);
}
void CAN_Send(uint8_t data)
{
CanTxMsg TxMessage;
TxMessage.StdId = 0x11;
TxMessage.ExtId = 0x00;
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.DLC = 1;
TxMessage.Data[0] = data;
CAN_Transmit(CAN1, &TxMessage);
}
void delay(uint32_t count)
{
while (count--);
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
if (CAN_GetITStatus(CAN1, CAN_IT_FMP0) != RESET)
{
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}
}
```
注意,这个程序中使用了CAN1和GPIOA、GPIOB、GPIOC。你需要根据自己的硬件连接情况进行修改。另外,这个程序中使用了STM32F10x标准库,如果你使用的是其他库,需要做相应的修改。
阅读全文