头文件
#ifndef __USART_H__
#define __USART_H__
#include "stm32f10x.h"
#include <stdio.h>
//串口1-USART1
#define CUSTOM_USART_APBxClkCmd RCC_APB2PeriphClockCmd
#define CUSTOM_USART_CLK RCC_APB2Periph_USART1
#define CUSTOM_USARTx USART1
#define CUSTOM_USART_BaudRate 115200
//USART_GPIO
#define CUSTOM_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
#define CUSTOM_USART_GPIO_CLK (RCC_APB2Periph_GPIOA)
#define CUSTOM_USART_TX_GPIO_PORT GPIOA
#define CUSTOM_USART_TX_GPIO_PIN GPIO_Pin_9
#define CUSTOM_USART_RX_GPIO_PORT GPIOA
#define CUSTOM_USART_RX_GPIO_PIN GPIO_Pin_10
#define CUSTOM_USART_IRQn USART1_IRQn
#define CUSTOM_USART_IRQHandler USART1_IRQHandler
#define USART_REC_LEN 200 //定义最大接收字节数 200
extern u8 USART_RX_Cmd[20]; //命令接收缓冲,最大USART_REC_LEN个字节
extern u8 USART_RX_BUF[USART_REC_LEN]; //数据接收缓冲,最大USART_REC_LEN个字节
void USART_Custom_Init(void);
void CUSTOM_USART_SendByte(USART_TypeDef* CUSTOM_USART, uint8_t data);
void CUSTOM_USART_SendHalfWord(USART_TypeDef* CUSTOM_USART, uint16_t data);
void CUSTOM_USART_SendArray(USART_TypeDef* CUSTOM_USART, uint8_t *data, uint8_t num);
void CUSTOM_USART_SendStr(USART_TypeDef* CUSTOM_USART, uint8_t *str);
void CUSTOM_USART_SendTem(USART_TypeDef* CUSTOM_USART, short data);
uint8_t CUSTOM_USART_ReceiveByte(USART_TypeDef* CUSTOM_USART);
void CUSTOM_USART_ReceiceArray(USART_TypeDef* CUSTOM_USART, uint8_t *data, uint8_t num);
#endif
函数相关
#include "usart.h"
void USART_Custom_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStrutre;
CUSTOM_USART_APBxClkCmd(CUSTOM_USART_CLK, ENABLE);
CUSTOM_USART_GPIO_APBxClkCmd(CUSTOM_USART_GPIO_CLK, ENABLE);
// 将USART Tx的GPIO配置为推挽复用模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = CUSTOM_USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CUSTOM_USART_TX_GPIO_PORT, &GPIO_InitStructure);
// 将USART Rx的GPIO配置为浮空输入模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = CUSTOM_USART_RX_GPIO_PIN;
GPIO_Init(CUSTOM_USART_RX_GPIO_PORT, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = CUSTOM_USART_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_InitStrutre.USART_BaudRate = CUSTOM_USART_BaudRate;
USART_InitStrutre.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStrutre.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStrutre.USART_Parity = USART_Parity_No;
USART_InitStrutre.USART_StopBits = USART_StopBits_1;
USART_InitStrutre.USART_WordLength = USART_WordLength_8b;
USART_Init(CUSTOM_USARTx, &USART_InitStrutre); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(CUSTOM_USARTx, ENABLE);// 使能串口
}
/* 发送一个字节 */
void CUSTOM_USART_SendByte(USART_TypeDef* CUSTOM_USART, uint8_t data)
{
USART_SendData(CUSTOM_USART, data);
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TXE) == RESET);
}
/* 接收一个字节 */
uint8_t CUSTOM_USART_ReceiveByte(USART_TypeDef* CUSTOM_USART)
{
uint8_t GetData;
if(USART_GetFlagStatus(CUSTOM_USART ,USART_FLAG_RXNE) == SET)
GetData = USART_ReceiveData(CUSTOM_USART);
return GetData;
}
/* 发送两个字节的数据 */
void CUSTOM_USART_SendHalfWord(USART_TypeDef* CUSTOM_USART, uint16_t data)
{
uint8_t temp_h,temp_l;
temp_h = (data & 0xff00) >> 8;
temp_l = data & 0xff;
USART_SendData(CUSTOM_USART, temp_h);
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TXE) == RESET);
USART_SendData(CUSTOM_USART, temp_l);
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TXE) == RESET);
}
/* 发送8位数据的数组 */
void CUSTOM_USART_SendArray(USART_TypeDef* CUSTOM_USART, uint8_t *data, uint8_t num)
{
uint8_t temp;
for(temp = 0; temp < num; temp++)
{
CUSTOM_USART_SendByte(CUSTOM_USART, data[temp]);
}
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TC) == RESET);
}
/* 接收多个字节的数组 */
void CUSTOM_USART_ReceiceArray(USART_TypeDef* CUSTOM_USART, uint8_t *data, uint8_t num)
{
uint8_t temp;
for(temp = 0; temp < num; temp++)
{
data[temp] = CUSTOM_USART_ReceiveByte(CUSTOM_USART);
}
}
/* 发送字符串 */
void CUSTOM_USART_SendStr(USART_TypeDef* CUSTOM_USART, uint8_t *str)
{
uint8_t i = 0;
do
{
CUSTOM_USART_SendByte(CUSTOM_USART, *(str+i));
i++;
}while(*(str+i) != '\0');
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TC) == RESET);
}
//重定向c库函数printf到串口,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到串口 */
USART_SendData(USART1, (uint8_t) ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(CUSTOM_USARTx, USART_FLAG_TXE) == RESET);
return ch;
}
//重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
/* 等待串口输入数据 */
while (USART_GetFlagStatus(CUSTOM_USARTx, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(CUSTOM_USARTx);
}
/* 发送数据 */
void CUSTOM_USART_SendTem(USART_TypeDef* CUSTOM_USART, short data)
{
uint8_t temp_h,temp_l;
temp_h = (data & 0xff00) >> 8;
temp_l = data & 0xff;
USART_SendData(CUSTOM_USART, temp_h);
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TXE) == RESET);
USART_SendData(CUSTOM_USART, temp_l);
while(USART_GetFlagStatus(CUSTOM_USART, USART_FLAG_TXE) == RESET);
}
中断服务函数
void CUSTOM_USART_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(CUSTOM_USARTx ,USART_IT_RXNE) == SET)
{
USART_ClearITPendingBit(CUSTOM_USARTx, USART_IT_RXNE);
USART_RX_Cmd[num_L] = USART_ReceiveData(CUSTOM_USARTx);
num_L ++;
}
if(USART_RX_Cmd[num_L - 1] == 0x5A)
num_S = num_L - 1;
if((USART_RX_Cmd[num_S] == 0x5A) && (USART_RX_Cmd[num_L - 1] == 0xA5))
{
Cmd_Judge();
for(num=0; num<=19; num++)
{
USART_RX_Cmd[num] = 0x00;
}
}
if(USART_GetFlagStatus(CUSTOM_USARTx,USART_FLAG_ORE) == SET) //溢出
{
USART_ClearFlag(CUSTOM_USARTx,USART_FLAG_ORE); //读SR
USART_ReceiveData(CUSTOM_USARTx); //读DR
}
}