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');
}

实现效果:

相关推荐
小范馆1 小时前
ESP各模组的引脚图-小智接线图
stm32
松涛和鸣1 小时前
DAY63 IMX6ULL ADC Driver Development
linux·运维·arm开发·单片机·嵌入式硬件·ubuntu
想放学的刺客5 小时前
单片机嵌入式试题(第23期)嵌入式系统电源管理策略设计、嵌入式系统通信协议栈实现要点两个全新主题。
c语言·stm32·单片机·嵌入式硬件·物联网
猫猫的小茶馆5 小时前
【Linux 驱动开发】五. 设备树
linux·arm开发·驱动开发·stm32·嵌入式硬件·mcu·硬件工程
YouEmbedded6 小时前
解码内部集成电路(IIC)与OLED屏
stm32·0.96寸oled·硬件iic·软件模拟iic·图片取模·汉字取模
jghhh016 小时前
基于上海钜泉科技HT7017单相计量芯片的参考例程实现
科技·单片机·嵌入式硬件
恶魔泡泡糖7 小时前
51单片机外部中断
c语言·单片机·嵌入式硬件·51单片机
意法半导体STM327 小时前
【官方原创】如何基于DevelopPackage开启安全启动(MP15x) LAT6036
javascript·stm32·单片机·嵌入式硬件·mcu·安全·stm32开发
v_for_van7 小时前
STM32低频函数信号发生器(四通道纯软件生成)
驱动开发·vscode·stm32·单片机·嵌入式硬件·mcu·硬件工程
电化学仪器白超7 小时前
③YT讨论
开发语言·python·单片机·嵌入式硬件