STM32F407ZE开发板实现使用串口USART1接收、发送字符串实例
具体代码及解析如下:
main.c部分
#include <stm32f4xx.h>
#include "led.h"
#include "ustart.h"
#include <string.h>
int main()
{
LED_Init();
USART1_Init();
USART_SendString(USART1, "Hello world!\r\n");
while(1)
{
if(Receive_Flag == 1) //接收数据标志位等于1(接收完毕,停止接收)
Receive_Flag = 0; //接收数据标志位置0(可以开始接收)
}
}
ustart.h部分
#ifndef USTART_H
#define USTART_H
#include <stm32f4xx.h>
#include <stm32f4xx_usart.h>
#include <stdio.h>
#include "sys.h"
extern char USART1_ReceiveData[50]; //接收PC端发送过来的字符
extern int Receive_Flag;
void USART1_Init();
void USART_SendString(USART_TypeDef* USARTx, char *DataString);
#endif
ustart.c部分
#include "ustart.h"
#include <string.h>
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到串口 */
USART_SendData(USART1, (uint8_t) ch); //程序开始时,会发送一次数据,ch是系统分配的(可能是0),串口会显示大概两个空格的内容
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
void USART1_Init()
{
GPIO_InitTypeDef GPIOInit_Struct;
USART_InitTypeDef USARTInit_Struct;
NVIC_InitTypeDef USARTNVIC_Struct;
//1、使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//2、初始化对应的IO引脚复用为USART1功能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIOInit_Struct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIOInit_Struct.GPIO_Mode = GPIO_Mode_AF;
GPIOInit_Struct.GPIO_OType = GPIO_OType_PP;
GPIOInit_Struct.GPIO_Speed = GPIO_Fast_Speed;
GPIOInit_Struct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIOInit_Struct);
//将PA9 PA10复用为USART1功能
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//3、USART1初始化
USARTInit_Struct.USART_BaudRate = 115200; //波特率
USARTInit_Struct.USART_Parity = USART_Parity_No; //无校验位
USARTInit_Struct.USART_StopBits = USART_StopBits_1; //1位停止位
USARTInit_Struct.USART_WordLength = USART_WordLength_8b; //8位数据位
USARTInit_Struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USARTInit_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件控制流
USART_Init(USART1,&USARTInit_Struct);
//开启串口终端
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USARTNVIC_Struct.NVIC_IRQChannel = USART1_IRQn;//stm32f4xx.h
USARTNVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;
USARTNVIC_Struct.NVIC_IRQChannelSubPriority = 0;
USARTNVIC_Struct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&USARTNVIC_Struct);
//4、开启串口
USART_Cmd(USART1,ENABLE);
}
void USART_SendString(USART_TypeDef* USARTx, char *DataString)
{
int i = 0;
USART_ClearFlag(USARTx,USART_FLAG_TC); //发送字符前清空标志位(否则缺失字符串的第一个字符)
while(DataString[i] != '\0') //字符串结束符
{
USART_SendData(USARTx,DataString[i]); //每次发送字符串的一个字符
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC) == 0); //等待数据发送成功
USART_ClearFlag(USARTx,USART_FLAG_TC); //发送字符后清空标志位
i++;
}
}
char USART_ReceiveString[50]; //接收PC端发送过来的字符
int Receive_Flag = 0; //接收消息标志位
int Receive_sum = 0; //数组下标
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE) == 1) //USART_FLAG_RXNE判断数据,== 1则有数据
{
if(Receive_sum > 49) //数组能存放50个字节的数据
{
USART_ReceiveString[49] = '\0'; //数据字节超过50位时,将最后一位设置为\0
Receive_Flag = 1; //接收标志位置1,停止接收数据
Receive_sum = 0; //数组下标置0
}
if(Receive_Flag == 0) //接收标志位等于0,开始接收数据
{
USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART1); //通过USART1串口接收字符
Receive_sum++; //数组下标++
}
if(Receive_sum >= 2) //数组下标大于2
{
if(USART_ReceiveString[Receive_sum-2] == '\r' && USART_ReceiveString[Receive_sum-1] == '\n' )
{
USART_ReceiveString[Receive_sum-1] = '\0';
USART_ReceiveString[Receive_sum-2] = '\0';
Receive_Flag = 1; //接收标志位置1,停止接收数据
Receive_sum = 0; //数组下标置0
printf("%s\r\n",USART_ReceiveString);
if(strcmp(USART_ReceiveString,"hello") == 0)
{
PFout(9) = !PFout(9);
}
if(strcmp(USART_ReceiveString,"world") == 0)
{
PFout(10) = !PFout(10);
}
if(strcmp(USART_ReceiveString,"jiajia") == 0)
{
PEout(13) = !PEout(13);
}
if(strcmp(USART_ReceiveString,"haha") == 0)
{
PEout(14) = !PEout(14);
}
}
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //接收后先清空标志位
}
}
led.h部分
#ifndef _LED_H_
#define _LED_H_
#include <stm32f4xx.h>
#include "sys.h"
void LED_Init(void);
#endif
led.c部分
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef aaa;
//1、先开启对应用到的模块时钟节拍
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);//PE组时钟
//2、可以初始化配置GPIO F组的9号引脚
aaa.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
aaa.GPIO_Mode = GPIO_Mode_OUT;//输出模式
aaa.GPIO_Speed = GPIO_Fast_Speed;//快速 点灯和引脚速度无关
aaa.GPIO_OType = GPIO_OType_PP;//推挽输出
aaa.GPIO_PuPd = GPIO_PuPd_UP;//内部上拉
GPIO_Init(GPIOF,&aaa);
aaa.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOE,&aaa);
//初始化完成 灭掉4盏灯
PFout(9) = 1;
PFout(10) = 1;
PEout(13) = 1;
PEout(14) = 1;
}
sys.h部分
#ifndef __SYS_H
#define __SYS_H
#include "stm32f4xx.h"
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr & 0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
//STM32中 对寄存器的访问 是不能单独访问寄存器的单个bit 只能以32bit地址访问寄存器
//这些位为只写形式,只能在字(word)--4byte、半字2byte 或字节模式下访问
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
#define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n) //输出
#define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n) //输入
#define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n) //输出
#define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n) //输入
#endif
版权声明:本文为CSDN博主「佳佳鸽」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43793181/article/details/109134705
暂无评论