1.在键盘输入一个字符,字符+1,并且打印在串口工具上 键盘输入’a’----->串口工具打印’b’
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
//初始化串口
void surt_init();
//发送一个字符
void put_char(const char ch);
//发送一个字符串
void put_string(const char* string);
//接收一个字符
char get_char();
//接收一个字符串
char* get_string();
#endif
uart.c
#include "uart4.h"
//初始化串口
void surt_init()
{
/******RCC章节初始化****/
//1.使能GPIO控制器
RCC->MP_AHB4ENSETR |= (0x1<<1);
RCC->MP_AHB4ENSETR |= (0x1<<6);
//使能SUART寄存器
RCC->MP_APB1ENSETR |= (0x1<<16);
//******GPIO章节初始化****/
//设置PB2引脚为复用功能
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |= (0x1<<5);
//设置PG11引脚为复用功能
GPIOG->MODER &= (~(0x3<<22));
GPIOG->MODER |= (0x1<<23);
/******UART4章节初始化****/
//设置PB2引脚复用功能为UART4_RX功能
GPIOB->AFRL &= (~(0xf<<8));
GPIOB->AFRL |= (0x1<<11);
//设置PG11引脚复用功能为UART4_TX功能
GPIOG->AFRH &= (~(0xf<<12));
GPIOG->AFRH |= (0x6<<12);
/******UART4章节初始化****/
if(USART4->CR1 & (0x1<<0))
{
USART4->CR1 &= (~(0x1<<0));
}
//设置串口八位数据位宽度
USART4->CR1 &= (0x1<<12);
USART4->CR1 &= (0x1<<28);
//设置串口16倍采样率
USART4->CR1 &= (0x1<<15);
//设置出口无奇数偶数位
USART4->CR1 &= (0x1<<10);
//设置串口发送数据寄存器使能
USART4->CR1 |= (0x1<<3);
//设置串口结束数据寄存器使能
USART4->CR1 |= (0x1<<2);
//设置出口1位停止位
USART4->CR2 &= (~(0x3<<12));
//设置波特率为115200
USART4->BRR &= (~(0xffff));
USART4->BRR = 0x22b;
//设置串口与分频
USART4->PRESC &= (~(0xf));
//设置串口使能
USART4->CR1 |= 0x1;
}
//发送一个字符
void put_char(const char ch)
{
while(!(USART4->ISR & (0x1<<7)));
USART4->TDR &= (~(0xff));
USART4->TDR = ch;
while(!(USART4->ISR & (0x1<<6)));
}
}
//发送一个字符串
void put_string( const char* string)
{
// while(!(USART4->ISR & (0x1<<7)));
// USART4->TDR &= (~(0xff));
while(1)
{
if(*string == '\0')
{
break;
}
put_char(*string);
string++;
}
put_char('\n');
put_char('\r');
}
//接收一个字符
char get_char()
{
char ch;
while(!(USART4->ISR & (0x1<<5)));
ch = USART4->RDR;
return ch;
}
//接收一个字符串
char buf[20]={0};
char* get_string()
{
int i=0;
// while(!(USART4->ISR & (0x1<<5)));
while(1)
{
buf[i]=get_char();
put_char(buf[i]);
if(buf[i]=='\r')
{
break;
}
i++;
}
buf[i]='\0';
put_char('\n');
return buf;
}
main.c
#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++);
}
int main()
{
surt_init();
while(1)
{
put_char(get_char()+1);
}
return 0;
}