OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 7150|回复: 1

STM32F429 USART 对蓝芽传输

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
25
金钱
25
注册时间
2015-5-18
在线时间
0 小时
发表于 2015-5-18 18:55:02 | 显示全部楼层 |阅读模式
5金钱
[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,但是失败,请问是什么问题?

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165371
金钱
165371
注册时间
2010-12-1
在线时间
2110 小时
发表于 2015-5-19 00:37:01 | 显示全部楼层
仿真看下串口接收到的数据,分析下即可。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-24 15:17

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表