STM32蓝牙控制LED灯开关

介绍

STM32f103相关,USART1,USART2,led灯,蓝牙模块,通过手机app发送指令蓝牙转发实现对led的控制。

代码片段
#include "stm32f10x.h"
#include "GPIOLIKE51.h"
#include "stdio.h"

u8 usart1_buf[100]={0},usart3_buf[100]={0};
u16 index1=0,index3=0,flag1=0,flag3=0;


int fputc( int ch, FILE *f ){
		USART_SendData(USART1,(u8) ch );
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
		return ch;
}

void delay(uint32_t num){
	while(num--);
}

void delay_ms(uint32_t n){
	while(n--){
		SysTick->CTRL = 0;								//Disable SysTick 关闭系统定时器
		SysTick->LOAD = 9000;							//Count from 255 to 0 (256 cycles) 配置计数值
		SysTick->VAL = 0;								//CLear current value as well as count flag 清空当前寄存器计数值
		SysTick->CTRL = 1;								//Enable SysTick timer with processor clock 使能系统定时器
		while((SysTick->CTRL & (1<<16)) == 0);			//Wait until count flag is set
	}	
	SysTick->CTRL = 0;									//Disable SysTick 关闭系统定时器
}

void USART1_init(){
	GPIO_InitTypeDef GPIO_InitStructure;
	//配置串口结构体
	USART_InitTypeDef USART_InitStructure;
	//NVIC结构体
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//使能GPIO硬件时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	//使能串口1硬件时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	//USART1_TX   GPIOA.9
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 				//PA.9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			//复用推挽输出
	GPIO_Init(GPIOA, &GPIO_InitStructure);					//初始化GPIOA.9
	   
	//USART1_RX	  GPIOA.10初始化
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;				//PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;	//浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);					//初始化GPIOA.10  
	
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	/* Enable the USARTy Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		//子优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Init(USART1, &USART_InitStructure); //初始化串口1
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //配置为接收中断
	USART_Cmd(USART1, ENABLE);  //使能串口1
}

void USART3_init(){
	GPIO_InitTypeDef GPIO_InitStructure;
	//配置串口结构体
	USART_InitTypeDef USART_InitStructure;
	//NVIC结构体
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//使能GPIO硬件时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	//使能串口3硬件时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	//USART3_TX   GPIOB.10
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 				//PA.9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			//复用推挽输出
	GPIO_Init(GPIOB, &GPIO_InitStructure);					//初始化GPIOA.9
	   
	//USART3_RX	  GPIOB.11初始化
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;				//PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;	//浮空输入
	GPIO_Init(GPIOB, &GPIO_InitStructure);					//初始化GPIOA.10  
	
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	/* Enable the USARTy Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级2
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;		//子优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Init(USART3, &USART_InitStructure); //初始化串口3
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //配置为接收中断
	USART_Cmd(USART3, ENABLE);  //使能串口3
}

void USART1_SEND_STR(char *pstr){
	char *p = pstr;
	while(*p != '\0'){
		USART_SendData(USART1, *p);
		while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
		USART_ClearFlag(USART1, USART_FLAG_TXE);
		p++;
	}
}

void USART3_SEND_STR(char *pstr){
	char *p = pstr;
	while(*p != '\0'){
		USART_SendData(USART3, *p);
		while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
		USART_ClearFlag(USART3, USART_FLAG_TXE);
		p++;
	}
}

//蓝牙AT指令配置模块
void BLE_CONFIG_SET(){
	//发送AT测试指令
	USART3_SEND_STR("AT\r\n");
	delay_ms(500);
	//发送AT更改模块名指令
	USART3_SEND_STR("AT+NAMEble_01\r\n");
	delay_ms(600);
	//发送AT重启模块名指令
	USART3_SEND_STR("AT+RESET\r\n");
	delay_ms(2000);
}

//主函数,从这里执行
int main(void){
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	GPIO_SetBits(GPIOC, GPIO_Pin_13);
	PCout(14)=0;PCout(15)=0;
	//pc串口
	USART1_init();
	//蓝牙串口
	USART3_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
	
	//蓝牙模块配置
	BLE_CONFIG_SET();
	while(1){
		PCout(13)=1;
		delay_ms(1000);
		PCout(13)=0;
		delay_ms(1000);
	}
}

void USART1_IRQHandler(void)
{	
	
        u16 code;
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {   					
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);//Removal of receiving interrupt flag
                code=USART_ReceiveData(USART1);
				printf("%c", code);
                usart1_buf[index1] = code;
                index1++;      
            if(code == 0x0a)
            {
                index1 = 0;
                flag1 = 1;
            }
        }    
}

void USART3_IRQHandler(void)
{
        u16 code;
        if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
        {   
                USART_ClearITPendingBit(USART3, USART_IT_RXNE);
                code=USART_ReceiveData(USART3);
				printf("%c", code);
                usart3_buf[index3] = code;
                index3++;
                if(code == 0x0a)
                {
                    index3 = 0;
                    flag3 = 1;

                }
				
				//开灯
				if(code == 0x31)
                {
                   PCout(14)=1;

                }
				//关灯
				if(code == 0x32)
                {
                   PCout(14)=0;
                }
				//开灯
				if(code == 0x33)
                {
                   PCout(15)=1;

                }
				//关灯
				if(code == 0x34)
                {
                   PCout(15)=0;
                }
        }    
}

蓝牙串口app下载地址

  • 12
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "stm32f10x.h" #include "GPIOLIKE51.h" #define uint unsigned int #define uchar unsigned char #define CLOCK 72/8 //时钟=72M //函数声明 void GPIO_Configuration(void); /**************************************************************************** * 名 称:delay_us(u32 nus) * 功 能:微秒延时函数 * 入口参数:u32 us * 出口参数:无 * 说 明: * 调用方法:无 ****************************************************************************/ //void delay_us(u32 us) //{ // u32 temp; // SysTick->LOAD = 9*us; // SysTick->VAL=0X00;//清空计数器 // SysTick->CTRL=0X01;//使能,减到零是无动作,采用外部时钟源 // do // { // temp=SysTick->CTRL;//读取当前倒计数值 // }while((temp&0x01;)&&(!(temp&(1<CTRL=0x00; //关闭计数器 // SysTick->VAL =0X00; //清空计数器 //} /**************************************************************************** * 名 称:delay_ms(u16 ms) * 功 能:毫秒延时函数 * 入口参数:u16 nms * 出口参数:无 * 说 明: * 调用方法:无 ****************************************************************************/ void delay_ms(u16 ms) { u32 temp; SysTick->LOAD = 9000*ms; SysTick->VAL=0X00;//清空计数器 SysTick->CTRL=0X01;//使能,减到零是无动作,采用外部时钟源 do { temp=SysTick->CTRL;//读取当前倒计数值 }while((temp&0x01;)&&(!(temp&(1<CTRL=0x00; //关闭计数器 SysTick->VAL =0X00; //清空计数器 } //============================================================================= //文件名称:main //功能概要:主函数 //参数说明:无 //函数返回:int //============================================================================= int main(void) { GPIO_Configuration(); while (1) { // GPIO_SetBits(GPIOB,GPIO_Pin_9); // delay_ms(500);// 100=0.1S GPIO_ResetBits(GPIOB,GPIO_Pin_9); delay_ms(500); } } //============================================================================= //文件名称:GPIO_Configuration //功能概要:GPIO初始化 //参数说明:无 //函数返回:无 //============================================================================= void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE&#4

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值