在键盘输入一个字符,串口工具打印内容为:加1后的内容
头文件
#ifndef __GPIO_H__
#define __GPIO_H__
//#include "../common/include/ctype.h"
#include "../common/include/stm32mp1xx_adc.h"
#include "../common/include/stm32mp1xx_gic.h"
#include "../common/include/stm32mp1xx_i2c.h"
#include "../common/include/stm32mp1xx_spi.h"
#include "../common/include/stm32mp1xx_uart.h"
#include "../common/include/stdarg.h"
#include "../common/include/stm32mp1xx_exti.h"
#include "../common/include/stm32mp1xx_gpio.h"
#include "../common/include/stm32mp1xx_rcc.h"
#include "../common/include/stm32mp1xx_tim.h"
#include "../common/include/uart.h"
//1.初始化串口
void hal_uart_init();
//2.发送一个字符
void put_char(const char ch);
//3.发送一个字符串
void put_string(const char* str);
//4.接收一个字符
char get_char();
//5.接收一个字符串
char *get_string();
#endif
功能文件:
#include "gpio.h"
//1.初始化串口
void hal_uart_init()
{
/*******RCC章节初始化*******/
//1.GPIOB使能
(RCC->MP_AHB4ENSETR)|=(0x1<<1);
//2.GPIOG使能
(RCC->MP_AHB4ENSETR)|=(0x1<<6);
//3.USART4使能
(RCC->MP_APB1ENSETR)|=(0x1<<16);
/*******GPIO章节初始化*******/
//1.设置PB2和PG11复用功能模式
(GPIOB->MODER)&=(~(0x3<<4));
(GPIOB->MODER)|=(0x2<<4);
(GPIOG->MODER)&=(~(0x3<<22));
(GPIOG->MODER)|=(0x2<<22);
//2.设置PB2的复用功能为UART4_RX
(GPIOB->AFRL)&=(~(0xf<<8));
(GPIOB->AFRL)|=(0x8<<8);
//3.设置PG11的复用功能为UART4_TX
(GPIOG->AFRH)&=(~(0xf<<12));
(GPIOG->AFRH)|=(0x6<<12);
/*******UART4章节初始化*******/
//CR1部分
//0.设置串口不使能
(USART4->CR1)&=(~(0x1<<0));
//1.设置串口8位数据位宽度
(USART4->CR1)&=(~(0x1<<12));
(USART4->CR1)&=(~(0x1<<28));
//2.设置串口16倍采样率
(USART4->CR1)&=(~(0x1<<15));
//3.设置串口无奇偶校验位
(USART4->CR1)&=(~(0x1<<10));
//4.设置串口发送数据寄存器使能
(USART4->CR1)|=(0x1<<3);
//5.设置串口接收数据寄存器使能
(USART4->CR1)|=(0x1<<2);
//CR2部分
//1.设置串口1为停止位
(USART4->CR2)&=(~(0x3<<12));
//6.设置串口使能
(USART4->CR1)|=(0x1<<0);
//BRR部分
//1.波特率设置
USART4->BRR=0x22b;
//PRESC部分
//1.设置串口不分频
(USART4->PRESC)&=(~(0xf<<0));
}
//TDR部分
//2.发送一个字符
void put_char(const char ch)
{
//判断发送寄存器状态位
while(1)
{
if((USART4->ISR) & (0x1<<7))
{
break;
}
}
//先清空数据
//以下注释写法有错误
/*for(int i=0;i<8;i++)
{
(USART4->TDR)&=(~(0x1<<i));
}*/
USART4->TDR &=(~(0xff));
//设置数据并发送
USART4->TDR=ch;
//判断发送数据是否完成
while(1)
{
if((USART4->ISR) & (0x1<<6))
{
break;
}
}
}
/*//3.发送一个字符串
void put_string(const char* str)
{
//定义保存数据变量
static char arr[100]="";
static char *p=arr;
//判断发送寄存器状态位
while(1)
{
if((USART4->ISR) & (0x1<<7))
{
break;
}
}
//先清空数据
USART4->TDR &=(~(0xff));
//设置数据并发送
if(*str==10)
{
char *q=arr;
USART4->TDR=10;
while(q<=p)
{
USART4->TDR=*q;
q++;
}
}
*p++=*str;
USART4->TDR=*str;
//判断发送数据是否完成
while(1)
{
if((USART4->ISR) & (0x1<<6))
{
break;
}
}
}*/
//RDR部分
//4.接收一个字符
char get_char()
{
char ch=0;
//判断接收数据是否有数据
while(1)
{
if((USART4->ISR) & (0x1<<5))
{
break;
}
}
ch=USART4->RDR;
return ch;
}
/*//5.接收一个字符串
char *get_string()
{
static char ch=0;
//判断接收数据是否有数据
while(1)
{
if((USART4->ISR) & (0x1<<5))
{
break;
}
}
ch=USART4->RDR;
return &ch;
}*/
测试文件:
#include "gpio.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()
{
while(1)
{
put_char(get_char()+1);
//put_string(get_string());
};
return 0;
}
测试结果: