#include "/home/ubuntu/diceng/common/include/stm32mp1xx_gpio.h"
#include "/home/ubuntu/diceng/common/include/stm32mp1xx_rcc.h"
#include "/home/ubuntu/diceng/common/include/stm32mp1xx_uart.h"
#include "/usr/include/myhead.h"
void uart_init();
char getchar_one();
void putchar_one(char a);
int main(int argc, const char *argv[])
{
//1.串口的初始化
uart_init();
while(1)
{
//2.接收一个字节数据
getchar_one();
//3/发送一个字节数据
char a;
scanf("%c",&a);
putchar_one(a);
}
return 0;
}
//封装串口初始化函数
void uart_init()
{
//1.使能GPIOB/GPIOG/UART4时钟 RCC_MP_AHB4ENSETR/RCC_MP_APB1ENSETR
RCC->MP_AHB4ENSETR |= 0x42;
RCC->MP_APB1ENSETR |= (0x1<<16);
//2.设置PG11/PB2为复用功能 GPIOX_MODER
GPIOG->MODER &=(~(0x3<<22));
GPIOB->MODER &=(~(0x3<<4));
GPIOG->MODER |= (0x2<<22);
GPIOB->MODER |= (0x2<<22);
//3.PG11/PB2为uart4复用功能 GPIOX_AFRL/GPIOX_AFRH
GPIOG->AFRH |= (0x6<<12);
GPIOB->AFRL |=(0x8<<8);
//4.设置数据位宽为8位 USART_CR1
//(unsigned int *)0x5C000000 &=(~(0x1<<28));
USART4->CR1 &=(~(0x1<<28));
//(unsigned int *)0x5C000000 &=(~(0x1<<12));
USART4->CR1 &=(~(0x1<<12));
//5.设置16倍过采样USART_CR1
//(unsigned int *)0x5C000000 &=(~(0x1<<15));
USART4->CR1 &=(~(0x1<<15));
//6.设置无校验 USART_CR1
//(unsigned int *)0x5C000000 &=(~(0x1<<10));
USART4->CR1 &=(~(0x1<<10));
//7.设置停止位 USART_CR2
//(unsigned int *)0x4000E000 &=(~(0x3<<12));
USART4->CR2 &=(~(0x3<<12));
//8.设置波特率 USART_BRR
//(unsigned int *)0x4000E004 |= 0x22B;
USART4->BRR |=0x22B;
//9.发送器使能 USART_CR1
//(unsigned int *)0x5C000000 |=(0x1<<3);
USART4->CR1 |= (0x1<<3);
//10.接收器使能 USART_CR1
//(unsigned int *)0x5C000000 |=(0x1<<2);
USART4->CR1 |= (0x1<<2);
//11.串口使能 USART_CR1
//(unsigned int *)0x5C000000 |=(0x1);
USART4->CR1 |= 0x1;
}
//接收一个字符
char getchar_one()
{
//1.判断接收数据寄存器数据是否准备好 USART_ISR[5]
char c;
while(!(USART4->ISR & (0x1<<5)));
//2.如果准备好就读取 USART_RDR
c = USART4->RDR;
//3.返回
return c;
}
//发送一个字符
void putchar_one(char a)
{
//1.判断发送数据寄存器是否不满USART_ISR[7]
while(!(USART4->ISR & (0x1<<7)));
//2.如果不满就写入 USART_TDR
USART4->TDR = a;
//3.等到发送完成后函数结束 USART_ISR[6]
while(!(USART4->ISR & (0x1<<6)));
}
11.13作业
最新推荐文章于 2023-11-17 22:48:51 发布