stm32f103c8t6通过蓝牙模块手机控制点亮led灯PC13实验代码
时间: 2023-08-01 10:09:04 浏览: 99
好的,这是一个简单的蓝牙控制LED灯的实验代码,可以供您参考:
```c
#include "stm32f10x.h"
#include "usart.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void delay_us(u32 nus)
{
TIM2->CNT = 0;
while (TIM2->CNT < nus);
}
void delay_ms(u32 nms)
{
while (nms--)
{
delay_us(1000);
}
}
int main(void)
{
USART_Config();
LED_Init();
TIM2_Init();
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
uint8_t data = USART_ReceiveData(USART1);
if (data == '1')
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
delay_ms(500);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
delay_ms(500);
}
}
}
}
```
在这个代码中,我们通过蓝牙模块接收手机发送的数据,当接收到字符 '1' 时,点亮 LED 灯,然后延时 500ms,再熄灭 LED 灯,延时 500ms。
阅读全文