代码
1.HC05.C
#include "stm32f10x.h" // Device header
//1.开启时钟,把需要用的USART和GPIO口的时钟打开
//2.GPIO初始化,把TX设为复用输出,RX设为输入
//配置USART,配置参数
#include <stdio.h>
#include <stdarg.h>
#include "HC05.h"
char HC05_RxPacket[100]; //"@MSG\r\n"
uint8_t HC05_RxData;//接收数据
uint8_t HC05_RxFlag;//接收标志位
void HC05_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//初始化TX,把TX配置成复用推挽输出
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//TX是USART2控制的外设输出脚,选择复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART
USART_InitTypeDef USART_InitStructure;
//定义USART结构体
USART_InitStructure.USART_BaudRate = 9600;
//波特率数值
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//不使用流控
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
//TX发送功能
USART_InitStructure.USART_Parity = USART_Parity_No;
//校验:不需要
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//停止位:1位停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//字长:8位
USART_Init(USART1, &USART_InitStructure);
//初始化
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
//配置中断
USART_Cmd(USART1, ENABLE);
//供能
}
//发送一个字节数据的函数
void HC05_SendByte(uint8_t Byte)
{
USART_SendData(USART1, Byte);
//函数的功能是通过外设USARTx发送单个数据。
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//该标志位,当下一次再次使用SendData时,会自动清0
}
//发送多个字节数据(传递数组)
void HC05_SendArray(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i ++)
{
HC05_SendByte(Array[i]);
}
}
//发送多个字符数据
//string类型自带标志位,无需长度限制
void HC05_SendString(char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i ++)
{
HC05_SendByte(String[i]);
}
}
//求x的y次方函数
uint32_t HC05_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y --)
{
Result *= X;
}
return Result;
}
//发送数字(可显示成字符串形式)
//利用先相除得到首位,后取余,发送出该一个数字
void HC05_SendNumber(uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i ++)
{
HC05_SendByte(Number / HC05_Pow(10, Length - i - 1) % 10 + '0');
}
}
//重定向fputc
//printf的本质上是不断调用fputc()
//重定向后,只能该串口使用,其余串口无法使用
int fputc1(int ch, FILE *f)
{
HC05_SendByte(ch);
return ch;
}
void HC05_Printf(char *format, ...)
{
char String[100];
va_list arg;
va_start(arg, format);
vsprintf(String, format, arg);
va_end(arg);
HC05_SendString(String);
}
//接收标志位判断
uint8_t HC05_GetRxFlag(void)
{
if ( HC05_RxFlag == 1)
{
HC05_RxFlag = 0;
return 1;
}
return 0;
}
uint8_t HC05_GetRxData(void)
{
return HC05_RxData;
}
void HC05_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
HC05_RxData = USART_ReceiveData(USART1);
HC05_RxFlag = 1;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
|