ARM 练习

该代码实现了一个基于STM32MP1微处理器的UART4初始化函数,包括GPIO配置和波特率设置。同时提供字符发送、接收及字符串操作的函数。在`main.c`中,通过UART4进行串口通信,接收到字符串并回显到串口。测试结果显示串口通信功能正常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业:

 

uart4.h文件

#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"

void uart4_init();

void put_char(const char str);

char get_char();

void put_string(const char* str);

char* get_string();

#endif

uart.c文件

#include "uart4.h"

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

        //GPIO
        GPIOB->MODER &= (~(0x3 << 4));
        GPIOB->MODER |= (0x1 << 5);

        GPIOG->MODER &= (~(0x3 << 22));
        GPIOG->MODER |= (0x1 << 23);

        GPIOB->AFRL &= (~(0xF << 8));
        GPIOB->AFRL |= (0x1 << 11);

        GPIOG->AFRH &= (~(0xF << 12));
        GPIOG->AFRH |= (0x3 << 13);

        //UART
        if(USART4->CR1 & (0x1))
        {
                USART4->CR1 &= (~(0x1));
        }
        USART4->CR1 &= (~(0x1 << 28));
        USART4->CR1 &= (~(0x1 << 12));
        USART4->CR1 &= (~(0x1 << 10));
        USART4->CR1 &= (~(0x1 << 15));
        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)
{
        while(!(USART4->ISR & (0x1 << 7)));
        USART4->TDR = str;
        while(!(USART4->ISR & (0x1 << 6)));
}

void put_string(const char* str)
{
        while(*str!='\0')
        {
                put_char(*str);
                str++;
        }
}

char get_char()
{
        char ch;
        while(!(USART4->ISR & (0x1 << 5)));
        ch = USART4->RDR;
        return ch;
}

char buffer[50]={0};
char* get_string()
{
        while(!(USART4->ISR & (0x1 << 5)));
        int i=0;
        while((buffer[i]=get_char())!='\r')
        {
                put_char(buffer[i]);
                i++;
        }
        buffer[i]='\0';
        return buffer;
}

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

测试结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值