[mw_shl_code=c,true]#include "stm32f4xx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "misc.h"
/*
*
*/
void My_Usart2_Printf(char *string){
while(*string){
USART_SendData(USART2, (unsigned short int) *string++);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
}
/*
*/
void My_Uart2_Init(){
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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;
USART_Init(USART2, &USART_InitStructure);
/****************LED***************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOG, &GPIO_InitStructure); // Save
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_EnableIRQ(USART2_IRQn);
}
/*
*/
char buff [100] = "";
char * My_Usart2_ReadLine(){
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
char c = USART_ReceiveData(USART2);
if(c != '\r' && c != '\n'){
sprintf (buff, "%s%c", buff, c);
}else{
char buff2 [100] = "";
memcpy(buff2,buff, strlen(buff));
memset(buff, 0, strlen(buff));
return buff2;
}
}
return "";
}
/*
*
*/
void USART2_IRQHandler(){
char * data = My_Usart2_ReadLine();
if(!strcmp(data, "on")){
GPIO_SetBits(GPIOG,GPIO_Pin_14);
}
if(!strcmp(data, "off")){
GPIO_ResetBits(GPIOG,GPIO_Pin_14);
}
}
/*
*
*/
int main(){
My_Uart2_Init();
while(1){
}
}
[/mw_shl_code]
我们使用HC-06蓝芽模组连接在板子上,并使用有蓝芽的手机连线试图透过APP点亮LED,但是失败,请问是什么问题? |