介绍
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 != ‘ ’){
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 != ‘ ’){
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(“ATrn”);
delay_ms(500);
//发送AT更改模块名指令
USART3_SEND_STR(“AT+NAMEble_01rn”);
delay_ms(600);
//发送AT重启模块名指令
USART3_SEND_STR(“AT+RESETrn”);
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;
}
}
}
介绍
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 != ‘ ’){
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 != ‘ ’){
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(“ATrn”);
delay_ms(500);
//发送AT更改模块名指令
USART3_SEND_STR(“AT+NAMEble_01rn”);
delay_ms(600);
//发送AT重启模块名指令
USART3_SEND_STR(“AT+RESETrn”);
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;
}
}
}
举报