使用单片机与手机进行通信
接线问题:
HC-05的RX,TX与单片机的PA9,PA10对应相接。至于连接在ATK MODULE 端口中下RX,TX相连无法接受,有待与进一步研究
待续...
下面带上可行的程序:
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_crc.h"
#include "system_stm32f10x.h"
#include "stdio.h"
#include "delay.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void delay_ms(u16 time);
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str);
int Putchar(int c);
int main(void)
{
SystemInit(); //这个函数在我们系统启动的时候都会调用,用来设置系统的整个时钟系统。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|
RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE); //GPIO 端口时钟使能
GPIO_Configuration(); //GPIO 端口初始化
USART_Configuration(); //USART 串口初始化,我们使用串口1
delay_init();
while(1)
{
delay_ms(500);
UART_PutStr(USART1,"hello2 world!\n");
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //传输速度为50Mhz
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //使用端口为9号位
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //使用PA系列端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //使用端口为10号位
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.10
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
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); //使能串口1
}
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}
}
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)
{
while (0 != *str)
{
UART_PutChar(USARTx, *str);
str++;
}
}