键盘输入一个字符,字符进行+1,并在串口进行打印例如:键入输入一个‘a‘,串口打印‘b‘ 键盘输入一个字符串,串口回显字符串

 uart.c

#include "uart4.h"
extern void delay_ms(int ms);
//uart4初始化
void hal_uart4_init()
{
 /***RCC章节初始化******/
	//使能GPIOG和GPIOB
  RCC_MP_AHB4|=(0x1<<1);
  RCC_MP_AHB4|=(0x1<<6);	
  //使能uart4
  RCC_MP_APB1|=(0x1<<16);
  /****GPIO章节初始化***/
  //设置PB2和PG11引脚为复用功能
  GPIOB_MODER&=(~(0x3<<4));
  GPIOB_MODER|=(0x1<<5);
  GPIOG_MODER&=(~(0x3<<22));
  GPIOG_MODER|=(0x1<<23);
  //设置PB2引脚的复用功能为UART4_RX
  GPIOB_AFRL&=(~(0xF<<8));
  GPIOB_AFRL|=(0x1<<11);
  //设置PG11引脚的复用功能为UART4_TX
  GPIOG_AFPH&=(~(0xF<<12));
  GPIOG_AFPH|=(0x3<<13); 
	/***UART章节初始化******/
  //判断UE位是否使能
  if(USART4->CR1&0x1)
  {
	  delay_ms(500);
	  USART4->CR1&=(~(0x1));
  }
 //设置1位起始位,8位数据宽度
  USART4->CR1&=(~(0x1<<12));
  USART4->CR1&=(~(0x1<<28));
  //设置无奇偶校验位
  USART4->CR1&=(~(0x1<<10));
  //设置16倍采样率
  USART4->CR1&=(~(0x1<<15));
  //设置串口1位停止位 
  USART4->CR2&=(~(0x3<<12)); 
  //设置串口不分频
  USART4->PRESC&=(~(0xF));
  //设置波特率
  USART4->BRR&=0x22b;
  //串口发送寄存器使能
  USART4->CR1&=(0x1<<3);
  //串口接收寄存器使能
  USART4->CR1&=(0x1<<2);
  //串口使能
  USART4->CR1&=0x1; 
}
//发送一个字符
void put_char(const char str)
{ 
//读1:为空可以发送数据,读0:循环等待
 while(!(USART4->ISR&(0x1<<7)));
 //将要发送的字符,写入到发送数据寄存器中
  USART4->TDR=str;
 //判断发送数据是否完成:读0表示发送数据没有完成(循环等待数据发送数据完成),读1表示发送数据完成
 while(!(USART4->ISR&(0x1<<6)));  
}
   	
//发送一个字符串
void put_string(const char *string)
{
	//判断是否为'\0'
       while (*string)
		{
			put_char(*string++);
		}
		put_char('\n');

}

//接受一个字符
char get_char()
{
    char mych;
	//判断接收数据寄存器是否为空
	//读0:接收数据寄存器为空
	//读1:接收数据寄存器中有数据
	while(!(USART4->ISR & (1<<5)));
	mych = USART4->RDR ;	
	return mych ;	
}
char buf[50]={0};
//接受一个字符串
char *get_string()
{
	unsigned int i;
	//循环实现数据收发
	for(i=0 ;i<49;i++)
	{
		buf[i] = get_char();
		put_char(buf[i]);
		if(buf[i]=='\r')
		break;
	}
	buf[i] = '\0';
	return buf; 
}

uart.h 

#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#define RCC_MP_AHB4 (*(volatile unsigned int *)0x50000000)
#define RCC_MP_APB1 (*(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_AFPH   (*(volatile unsigned int *)0x50008024)
//uart4初始化
void hal_uart4_init();
//发送一个字符
void pu_char(const char str);
//发送一个字符串
void put_string(const char *string);
//接收一个字符
char get_char();
//接受一个字符穿
char* get_string();
#endif

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()
{
   hal_uart4_init();
   put_string("hello world\n");
	while (1)
	{
	//	put_char(get_char() + 1);
		put_string(get_string());
	}
	return 0;
}

 stm32mp1xx_uart.h 

#ifndef __STM32MP1XX_UART_H__
#define __STM32MP1XX_UART_H__

typedef struct {
	volatile unsigned int CR1;
	volatile unsigned int CR2;
	volatile unsigned int CR3;
	volatile unsigned int BRR;
	volatile unsigned int GTPR;
	volatile unsigned int RTOR;
	volatile unsigned int RQR;
	volatile unsigned int ISR;
	volatile unsigned int ICR;
	volatile unsigned int RDR;
	volatile unsigned int TDR;
	volatile unsigned int PRESC;
}uart_t;

#define USART1  ((uart_t *)0x5C000000)
#define USART2  ((uart_t *)0x4000E000)
#define USART3  ((uart_t *)0x4000F000)
#define USART4  ((uart_t *)0x40010000)
#define USART5  ((uart_t *)0x40011000)
#define USART6  ((uart_t *)0x44003000)
#define USART7  ((uart_t *)0x40018000)
#define USART8  ((uart_t *)0x40019000)

#endif // __STM32MP1XX_UART_H__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值