新手上路
- 积分
- 47
- 金钱
- 47
- 注册时间
- 2017-2-13
- 在线时间
- 14 小时
|
10金钱
本帖最后由 迷路的麋鹿 于 2017-3-20 15:51 编辑
为什么我的蓝牙模块接收到数据发送给单片机以后,单片机读不到
#include "stm32f10x_exti.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#include "misc.h"
void USART_RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void USART_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void USART_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_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(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void USART_SendStr(u8 *s)
{
u8 i;
for(i=0;s!=0;i++){
USART_SendData(USART1,s);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
USART_ClearFlag(USART1,USART_FLAG_TXE);
}
}
#include "stm32f10x_it.h"
extern u8 temp;
void USART1_IRQHandler(void)
{
// u8 i=0;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET){
temp=USART_ReceiveData(USART1);
// USART_SendData(USART1,i);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// USART_ClearFlag(USART1,USART_FLAG_TXE);
USART_ClearFlag(USART1,USART_IT_RXNE);
// GPIO_Write(GPIOB,0xaaaa);
}
}
#include "stm32f10x.h"
void USART_Configuration(void);
void USART_NVIC_Configuration(void);
void USART_GPIO_Configuration(void);
void USART_RCC_Configuration(void);
void USART_SendStr(u8 *s);
u8 temp=0;
void Delay(u32 t)
{
while(t--);
}
int main(void)
{
/* 设置系统时钟为72M */
SystemInit();
USART_RCC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
USART_GPIO_Configuration();
USART_NVIC_Configuration();
USART_Configuration();
USART_SendStr("AT+NAMEB-watch\r\n");
Delay(6000000);
USART_SendStr("AT\r\n");
Delay(6000000);
while(1){
if(temp!=0){
USART_SendData(USART1,temp);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
USART_ClearFlag(USART1,USART_FLAG_TXE);
temp=0;
}
}
}
|
|