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
相关推荐
AndyHeee16 天前
【SVC、PendSV(系统异常) 与 外设 IRQ 、NVIC笔记】
arm开发
暮云星影16 天前
瑞芯微rk3588利用Rockchip NPU运行大语言模型(LLM)
arm开发·人工智能·语言模型·自然语言处理
techdashen16 天前
绕过系统 ICMP:用 rawsock、Npcap 和 WMI 找到默认网卡
开发语言·arm开发·rust
振南的单片机世界16 天前
ARM中断比51快在哪?硬件压栈+NVIC集中管理
arm开发·stm32·单片机·嵌入式硬件
墨绿色的摆渡人16 天前
论文笔记(一百三十七)Learning Dual-Arm Push and Grasp Synergy in Dense Clutter
arm开发·论文阅读
暮云星影17 天前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发
m0_5474866617 天前
《ARM Cortex-M4嵌入式应用技术——基于STM32F407、STM32CubeMX与Proteus》全套PPT课件
arm开发·stm32·proteus
Lanceli_van17 天前
SQLite 3.45.2(sqlite-autoconf-3450200)ARM 交叉编译完整步骤
arm开发·sqlite
暮云星影17 天前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发
暮云星影17 天前
瑞芯微rk3566开发FIT Secure Boot
linux·arm开发·驱动开发·安全