2024-4-15-ARM作业

实现字符串数据收发函数的封装

源代码:

main.c

复制代码
#include "gpio.h"

#include "uart4.h"



int main()

{

	uart4_config();

	while (1)

	{

		// char a=getchar();

		// putchar(a+1);

		char s[20];

		gets(s);

		puts(s);

		//putchar('\n');

		putchar('\r');



	}



	return 0;

}

uart4.c

复制代码
#include "uart4.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_rcc.h"

void uart4_config()
{
    //使能
    RCC->MP_AHB4ENSETR |= (0x1 << 1);
    RCC->MP_AHB4ENSETR |= (0x1 << 6);
    RCC->MP_APB1ENSETR |= (0x1 << 16);

    //PB2管脚复用
    GPIOB->MODER &= (~(0x3 << 4));
    GPIOB->MODER |= (0x2 << 4);

    //PG11管脚复用
    GPIOG->MODER &= (~(0x3 << 22));
    GPIOG->MODER |= (0x2 << 22);
    // PG11 为 UART4_TX
    GPIOG->AFRH &= (~(0xF << 12));
    GPIOG->AFRH |= (0x6 << 12);
    // PB2 为 UART4_RX
    GPIOB->AFRH &= (~(0xF << 8));
    GPIOB->AFRH |= (0x8 << 8);
    //设置串口不使能
    USART4->CR1 &= (~0x1);
    //设置8位数据位
    USART4->CR1 &= (~(0x1 << 12));
    USART4->CR1 &= (~(0x1 << 28));
    //设置没有校验位
    USART4->CR1 &= (~(0x1 << 10));
    //设置时钟频率不分频
    USART4->PRESC &= (~0xf);
    //设置16倍过采样
    USART4->CR1 &= (~(0x1 << 15));
    //设置1位停止位
    USART4->CR2 &= (~(0x3 << 12));
    //设置波特率115200
    USART4->BRR = 0x22B;
    //使能发送器
    USART4->CR1 |= (0x1 << 3);
    //使能接收器
    USART4->CR1 |= (0x1 << 2);
    //使能串口
    USART4->CR1 |= (0x1 << 0);
}

void putchar(char dat)
{
    while (!(USART4->ISR & (0x1 << 7)))
        ;
    USART4->TDR = dat;
    //等待传输完成
    while (!(USART4->ISR & (0x1 << 6)))
        ;
}
char getchar()
{

    while (!(USART4->ISR & (0x1 << 5)))
        ;
    return USART4->RDR;
}
void puts(char *s)
{
    while (1)
    {
        if (*s == '\0')
            break;
        putchar(*s);
        s++;
    }
    putchar('\n');
    putchar('\r');
}
void gets(char *s)
{
    while (1)
    {
        *s = getchar();
        putchar(*s);
        if ((*s) == '\r')
            break;
        s++;
    }
    *s = '\0';
    putchar('\n');
}

实现效果:

相关推荐
雾削木42 分钟前
k230 Pyhton三角形识别
运维·服务器·网络·stm32·智能路由器
Jack电子实验室1 小时前
【杭电HDU】校园网(DeepL/Srun)自动登录教程
python·嵌入式硬件·计算机网络·自动化
QK_001 小时前
STM32--中断
stm32·单片机·嵌入式硬件
@good_good_study1 小时前
STM32 死区时间
stm32·单片机
SystickInt3 小时前
32 低功耗模式(睡眠 停机 待机 )
单片机·嵌入式硬件
brave and determined3 小时前
传感器学习(day08):加速度传感器:智能时代的感知利器
嵌入式硬件·传感器·工作原理·加速度传感器·嵌入式设计·三轴·计步算法
小π军3 小时前
51单片机第1讲:点亮LED
单片机·嵌入式硬件·51单片机
Syntech_Wuz4 小时前
从STM32 到SAM D21(二):EIC 外部中断控制器配置详解
stm32·外部中断·eic·sam d21·mplab x ide
JXNL@4 小时前
电压基准芯片核心知识解析:从原理到选型(含MAX6167A实例)
单片机·嵌入式硬件·电压基准芯片
boneStudent4 小时前
Day30:I2C 与其他通信协议对比
stm32·单片机·嵌入式硬件