UART ARM 3月28日

#ifndef __UART_H__
#define __UART_H__


//对RCC进行宏定义
#define RCC_AHB4 (*(volatile unsigned int*)0x50000A28)
#define RCC_MP_APB1 (*(volatile unsigned int*)0x50000A00)
#define GPIOB_MODER (*(volatile unsigned int*)0x50003000)
#define GPIOG_MODER (*(volatile unsigned int*)0x50008000)
#define GPIOB_AFRL (*(volatile unsigned int*)0x50003020)
#define GPIOG_AFRH (*(volatile unsigned int*)0x50008024)


typedef struct {                        
    volatile unsigned int CR1;          
    volatile unsigned int CR2;          
    volatile unsigned int CR3;          
    volatile unsigned int BRR;          
    volatile unsigned int GTPR;         
    volatile unsigned int RTOR;         
    volatile unsigned int RQR;          
    volatile unsigned int ISR;          
    volatile unsigned int ICR;          
    volatile unsigned int RDR;          
    volatile unsigned int TDR;          
    volatile unsigned int PRESC;        
}uart_t;                                
                                        
#define USART1  ((uart_t *)0x5C000000)  
#define USART2  ((uart_t *)0x4000E000)  
#define USART3  ((uart_t *)0x4000F000)  
#define USART4  ((uart_t *)0x40010000)  
#define USART5  ((uart_t *)0x40011000)  
#define USART6  ((uart_t *)0x44003000)  
#define USART7  ((uart_t *)0x40018000)  
#define USART8  ((uart_t *)0x40019000)  
                                                
//初始化串口函数
void hal_uart4_init();

//发送一个字符
void put_char(const char str);

//发送一个字符串
void put_string(const char* string);

//接收一个字符
char get_char();

//接收一个字符串
char* get_string();

#endif

#include"../include/uart.h"

//初始化串口函数
void hal_uart4_init()
{
    //rcc章节初始化
    RCC_AHB4|=(0x1<<1);
    RCC_AHB4|=(0x1<<6);
    RCC_MP_APB1|=(0x1<<16);

    //GPIO章节初始化
    GPIOB_MODER&=(~(0x3<<4));
    GPIOB_MODER|=(0x1<<5);
    GPIOG_MODER&=(~(0x3<<4));
    GPIOG_MODER|=(0X1<<23);
    GPIOB_AFRL&=(~(0xf<<8));
    GPIOB_AFRL|=(0x1<<11);
    GPIOG_AFRH&=(~(0xf<<12));
    GPIOG_AFRH|=(0x3<<13);

    //UART章节初始化
    USART4->CR1&=(~(0x1<<12));
    USART4->CR1&=(~(0x1<<28));
    USART4->CR1&=(~(0x1<<15));
    USART4->CR1&=(~(0x1<<10));
    USART4->CR1|=(0x1<<3);
    USART4->CR1|=(0x1<<2);
    USART4->CR1|=(0x1<<0);

    USART4->CR2&=(~(0x3<<12));
    USART4->BRR&=(~(0xffff<<0));
    USART4->BRR|=(0X22b);

    USART4->PRESC&=(~(0xf<<0));
}

//发送一个字符
void put_char(const char str)
{
    //判断发送数据寄存器是否为空,为空才可以发送下一帧数据
    //读0:发送数据寄存器满,需要等待
    //读1:发送数据寄存器空,可以发送
    while(!(USART4->ISR&(0x1<<7)));
    USART4->TDR=str;
    while(!(USART4->ISR&(0x1<<6)));
    
}

//发送一个字符串
void put_string(const char* string)
{
//判断字符串是否结束'\0'
    while(*string!=0)
    {
        put_char(*string++);
        
    }
    put_char('\n');
    

}

char get_char()
{
    //判断接收数据寄存器是否有数据
    //读0:没有接收到数据,
    //读1:接收到数据,有数据可读
    while(!(USART4->ISR&(0x1<<5)));
    static    char apple;
     apple=USART4->RDR;
    return apple;

}

char buf[128]={0};
char* get_string()
{
    unsigned int i;
    for(i=0;i<127;i++)
    {
        buf[i]=get_char();
        put_char(buf[i]);
        if(buf[i]=='r'){
            break;
        }
    }
        buf[i]='\0';
        put_char('\n');
        return buf;
    

    
}
 

#include"./include/uart.h"


int main()
{
hal_uart4_init();
put_string("apple");
/*
while(1)
{
    put_char(get_char()+1);

}*/
while(1)
{
    put_string(get_string());
}

return 0;

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值