实验要求:
1、键盘输入一个字符,字符进行+1,并在串口进行打印
例如:键盘输入一个'a',串口打印'b'
2、键盘输入一个字符串,串口回显字符串
头文件
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#define RCC_AHB4_ENSETR (*(volatile unsigned int *)0x50000A28)
#define RCC_MP_APB1ENSETR (*(volatile unsigned int *)0x50000A00)
#define GPIOB_MODER (*(volatile unsigned int *)0x50003000)
#define GPIOG_MODER (*(volatile unsigned int *)0x50008000)
#define GPIOB_AFRL (*(volatile unsigned int *)0x50003020)
#define GPIOG_AFRH (*(volatile unsigned int *)0x50008024)
//初始化串口函数
void hal_uart4_init();
//发送一个字符
void put_char(const char str);
//发送一个字符串
void put_string(const char* string);
//接收一个字符
char get_char();
//接收一个字符串
char* get_string();
#endif
功能函数
#include "uart4.h"
//初始化串口函数
void hal_uart4_init()
{
/*******RCC章节初始化******/
RCC_AHB4_ENSETR |= (0x1 << 1);//使能GPIOB组控制器
RCC_AHB4_ENSETR |= (0x1 << 6);//使能GPIOG组控制器
//对UART4使能
RCC_MP_APB1ENSETR |= (0x1 << 16);//使能UART4组控制器
/*******GPIO章节初始化******/
//设置PB2引脚为复用模式功能
GPIOB_MODER &= (~(0x3 << 4));
GPIOB_MODER |= (0x2 << 4);
//设置PG11引脚为复用模式功能
GPIOG_MODER &= (~(0x3 << 22));
GPIOG_MODER |= (0x1 << 23);
//设置GPIOB引脚为复用功能
GPIOB_AFRL &= (~(0xf << 8));
GPIOB_AFRL |= (0x8 << 8);
//设置GPIOG引脚为复用功能
GPIOG_AFRH &= (~(0xf << 12));
GPIOG_AFRH |= (0x6 << 12);
/*******UART章节初始化******/
//1.USART_CR1:设置数据位,设置串口使能,设置发送位使能,设置串口接收位使能
//USART_CR1[28][12] = 00 ------>设置串口1位起始位,8位数据位
USART4->CR1 &= (~(0x3 << 12));
USART4->CR1 &= (~(0x3 << 28));
//USART_CR1[15] = 0 ------>设置串口16倍采样率
USART4->CR1 &= (~(0x1 << 15));
//USART_CR1[10] = 0 ------>设置串口无奇偶校验位
USART4->CR1 &= (~(0x1 << 10));
//USART_CR1[3] = 1 ------>设置串口发送器位使能
USART4->CR1 |= (0x1 << 3);
//USART_CR1[2] = 1 ------>设置串口接收器位使能
USART4->CR1 |= (0x1 << 2);
//USART_CR1[0] = 1 ------>设置串口位使能
USART4->CR1 |= (0x1);
//2.USART_BRR:设置串口波特率
USART4->BRR &= ~0xffff;
USART4->BRR = 0x22B;
//3.USART_CR2:设置串口停止位位数
USART4->CR2 &= (~(0x3 << 12));
//7.USART_PRESC:设置串口分频器
USART4->PRESC &= (~(0xf));
}
//发送一个字符
void put_char(const char str)
{
//判断发送数据寄存器是否为空,为空才可以发送下一个字节数据
//读0:发送数据寄存器满,需要等待
//读1:发送数据寄存器空,可以发送下一个字节数据
while(!(USART4->ISR & (0x1 << 7)));
USART4->TDR = str;
//判断发送数据是否完成:ISR[6]
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void put_string(const char* string)
{
//判断字符串是否结束
while(*string)
{
if(USART4->ISR & (0x1 << 6))
put_char(*string);
string++;
}
put_char('\n');
put_char('\r');
}
//接收一个字符
char get_char()
{
char ch;
//1.判断接收数据寄存器是否有数据
//2.读0:表示没有接收到数据,没有数据可读
//3.读1:接收到数据,有数据可读
while(!(USART4->ISR & (0x1 << 5)));
//定义一个字符接收把数据读出来
ch = USART4->RDR;
return ch;
}
char buf[50] = {0}; //不能返回局部变量的地址
//接收一个字符串
char* get_string()
{
//循环进行接收
//循环实现:接收一个字符,回显一个字符
//键盘的回车键按下之后,对应字符的是'\r'
int i = 0;
for(i=0;i<49;i++)
{
buf[i] = get_char();
put_char(buf[i]);
if(buf[i] == '\r')
{
break;
}
}
buf[i] = '\0';
put_char('\n');
return buf;
}
主函数
#include "./include/uart4.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
char *string;
int main()
{
//串口初始化
hal_uart4_init();
//实现串口数据接收发
while(1)
{
//put_char(get_char()+1);
put_string(get_string());
}
return 0;
}
结果测试
1、输入abcd...... 输出bcde......
2、输入abc 串口回显abc