arm_uart4实验

cs 复制代码
#include "uart4.h"                       
                                         
//UART                                   
                                         
//初始化                                 
void hal_uart4_init()                    
{                                        
    //rcc_init                           
        //使能GPIOB组控制器[1]=1         
        RCC->MP_AHB4ENSETR |= (0x1 << 1);
        //使能GPIOG组控制器[6]=1         
        RCC->MP_AHB4ENSETR |= (0x1 << 6);
        //使能uart组控制器[16]=1  
        RCC->MP_APB1ENSETR |=(0x1 << 16);       
    //gpio_init  
        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_init
        if(USART4->CR1 & 0x1)
        {
            USART4->CR1 &= (~(0x1 << 0));
        }
        USART4->CR1 &=(~(0x1 << 28));
        USART4->CR1 &=(~(0x1 << 12));
        USART4->CR1 &=(~(0x1 << 15));
        USART4->CR1 &=(~(0x1 << 10));
        USART4->CR1 |=(0x1 << 3);
        USART4->CR1 |=(0x1 << 2) ;
        USART4->CR1 |= 0x1;
        
        USART4->CR2 &=(~(0x1 << 12));
        USART4->PRESC &= (~(0xf<<0));  
        USART4->BRR=0x22b;                    
}                                        
//发送一个字符                           
void hal_put_char(const char str)        
{                                        
     
    while (!(USART4->ISR &(1<<7)));
    USART4->TDR=str;
    if(str=='\n'){
        hal_put_char('\r');
    }
                                    
}                                        
//接受一个字符                           
char hal_get_char()                      
{                                        
      
    char data;
    while(!(USART4->ISR &(1<<5)));
    data=(char)USART4->RDR;
    return data;
                                   
}                                        
//发送一个字符串                         

void hal_put_string(const char* string)  
{                                        
     
    while(*string !='\0'){
        hal_put_string(*string);
        string++;
    }
                                    
}                                        
//接一个字符串                         
char* hal_get_string()                   
{                                        
    int i=0;
    char buff[50];
    for(;i<49;i++){
        buff[i]=hal_get_char();
        hal_put_char(buff[i]);
        if (buff[i] == '\r')
        {
            break;
        }
    }
    buff[i]='\0';
    hal_put_char('\n');
    return buff;
                                   
}                                        
cs 复制代码
#include "uart4.h"

int main()
{
    hal_uart4_init();
    //hal_put_string("uart4 test");
    while(1)
    {
        hal_put_char(hal_get_char()+1);
        //hal_put_string(hal_get_string());
    }
}
cs 复制代码
#ifndef __UART_H__
#define __UART_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
void hal_uart4_init();

void hal_put_char(const char str);

char hal_get_char();

void hal_put_string(const char* string);
char* hal_get_string();
#endif
相关推荐
MaoXian_n15 小时前
[IMX] 05.串口 - UART
汇编·arm开发·驱动开发·单片机·嵌入式硬件
notion20251 天前
Keil MDK-ARM与C51双环境安装指南(2023最新版)
arm开发·其他
亿道电子Emdoor1 天前
【Arm】应用ArmDS移植最小FreeRTOS系统
arm开发·arm
半吊子全栈工匠1 天前
智能体间协作的“巴别塔困境“如何破解?解读Agent通信4大协议:MCP/ACP/A2A/ANP
arm开发
交叉编译之王 hahaha2 天前
OpenHarmony 5.1.0 Release目录结构详细解析(3级目录)
arm开发·华为·harmonyos
{⌐■_■}3 天前
【go】binary包,大小端理解,read,write使用,自实现TCP封包拆包案例
arm开发·tcp/ip·golang
亿道电子Emdoor4 天前
【ARM】MDK如何将变量存储到指定内存地址
arm开发·stm32·单片机
nuoyigui98894 天前
keil 解决 Error: CreateProcess failed, Command: ‘XXX\ARM\ARMCC\bin\fromelf.exe
arm开发
charlie1145141915 天前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
MaoXian_n5 天前
[IMX] 03.时钟树 - Clock Tree
arm开发·驱动开发·单片机·嵌入式硬件