C语言 cortex-A7核UART总线实验

一、C

1)uart4.h

#ifndef __UART4_H__                       
#define __UART4_H__                       
                                          
#include "stm32mp1xx_rcc.h"               
#include "stm32mp1xx_gpio.h"              
#include "stm32mp1xx_uart.h"              
                                          
//PB2 ---> UART4_RX                       
//PG11 ---> UART4_TX                      
                                          
//RCC/GPIO/UART4章节初始化                
void hal_uart4_init();                    
//发送一个字符                            
void hal_put_char(const char ch);         
//发送一个字符串                          
void hal_put_string(const char* string);  
//接收一个字符                            
char hal_get_char();                      
//接收一个字符串                          
char* hal_get_string();                   
                                                                           
#endif

2)uart.c

#include "uart4.h"

//RCC/GPIO/UART4章节初始化
void hal_uart4_init()
{
	//RCC章节初始化
	//1.使能GPIOB组控制器  MP_AHB4ENSETR[1] = 1  0x50000A28[1]
	RCC->MP_AHB4ENSETR |= (0X1 << 1);
	//2.使能GPIOG组控制器  MP_AHB4ENSETR[6] = 1  0x50000a28[6]
	RCC->MP_AHB4ENSETR |= (0X1 << 6);
	//3.使能UART4组控制器  MP_AHB4ENSETR[11] = 1 PB2 PG11---> 0x500000A00[16] 
	RCC->MP_APB1ENSETR |= (0X1 << 16);

	//GPIO章节初始化
	//1.设置PB2引脚为复用功能模式 MODER[5:4] = 10
	GPIOB->MODER &= (~(0x1 << 4));
	GPIOB->MODER |= (0x1 << 5);
	//2.设置PB2引脚复用功能为UART4_RX功能 AFRL[11:8] = 1000
	GPIOB->AFRL &= (~(0xf << 8));
	GPIOB->AFRL |= (0x1 << 11);
	//设置PG11引脚为复用功能模式 MODER[23:22] = 10
	GPIOG->MODER &= (~(0x1 << 22));
	GPIOG->MODER |= (0x1 << 23);
	//设置PG11引脚复用功能为UART4_TX功能 AFRH[15:12] = 0110
	GPIOG->AFRH &= (~(0xf << 12));
	GPIOG->AFRH |= (0x6 << 12);

	//UART4章节初始化
	//8N1 115200 使能位 代码编写逻辑
	//0.设置串口UE=0 CR1[0] = 0
	USART4->CR1 &= (~(0x1 << 0));
	//1.设置1位起始位,8位数据位 CR1[28][12] = 00
	USART4->CR1 &= (~(0x1 << 12));
	USART4->CR1 &= (~(0x1 << 28));
	//2.设置串口1位停止位 CR2[13:12] = 00
	USART4->CR2 &= (~(0x3 << 12))	;
	//3.设置串口无校验位 CR1[10] = 0
	USART4->CR1 &= (~(0x1 << 10));
	//4.设置串口16倍采样率 CR1[15] = 0
	USART4->CR1 &= (~(0x1 << 15));
	//5.设置串口不分频 PRESC[3:0] = 0000
	USART4->PRESC &= (~(0xf << 0));
	//6.设置串口波特率为115200 BRR = 0X22b
	USART4->BRR &= (~(0xffff));
	USART4->BRR |= (0x22b);
	//7.设置串口发送位使能 CR1[3] = 1
	USART4->CR1 |= (0x1 << 3);
	//8.设置串口接受位使能 CR1[2] = 1
	USART4->CR1 |= (0x1 << 2);
	//9.设置串口使能 CR1[0] = 1
	USART4->CR1 |= (0x1 << 0);
}
//发送一个字符
void hal_put_char(const char ch)
{
	//1.判断发送数据寄存器是否为空 ISR[7]
	//读0:发送数据寄存器满,需要等待 for while
	//读1:发送数据寄存器空,可以发送数据
	while(!(USART4->ISR & (0X1 << 7)));
	//2.将参数ch传递过来的内容,赋值给TDR寄存器
	USART4->TDR = (volatile unsigned int)ch;	
	//3.判断一帧数据是否发送完成 ISR[6]
	while(!(USART4->ISR & (0x1 << 6)));
}

//发送一个字符串
void hal_put_string(const char* string)
{
	//判断字符串是否为'\0'
	int i=0;
	while(string[i] != '\0')
	{
		//一个字符一个字符进行发送
		hal_put_char(string[i]);
		i++;
	}
}

//接收一个字符
char hal_get_char()
{
	char ch;
	//1.判断接收数据寄存器是否不为空 ISR[5]
	//读0:没有接收到数据,需要等待 for while
	//读1:接收到数据,可以将数据读出
	while(!(USART4->ISR & (0x1 << 5)));
	//2.将接收数据寄存器中RDR,读出来
	ch = (char)USART4->RDR;
	return ch;
}


char buffer[50] = {0};
//接收一个字符串
char* hal_get_string()
{
	for(int i=0;buffer[i]!='\0'; i++)
	{
		buffer[i]='\0';
	}
	//判断键盘enter是否按下'\r'
	int i = 0;
	char ch;
	while(1)
	{
		ch = hal_get_char();
		hal_put_char(ch);
		buffer[i] = ch;
		i++;
		if(ch == '\r')
		{
			hal_put_char('\n');	
			break;			
		}
	}
	buffer[i] = '\n';
	return buffer;
}

3)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()                                                            
{                                                                     
    hal_uart4_init();                                                 
    while(1)                                                          
    {                                                                 
        //单个字符                                                    
        //hal_put_char(hal_get_char() + 1);                           
        //字符串                                                      
        hal_put_string(hal_get_string());                             
    }                                                                 
    return 0;                                                         
}                                                                    

4)效果

实验一:收发字符

C语言 cortex-A7核UART总线实验_第1张图片

实验二:收发字符串

C语言 cortex-A7核UART总线实验_第2张图片

二、函数实现

1)uart4.h

2)uart.c

3)main.c

4)效果

实验一:收发字符

实验二:收发字符串

你可能感兴趣的:(ARM,c语言,开发语言)