实验一
键盘输入一个字符'a',串口工具显示'b'
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//初始化UART4
void hal_uart4_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char * str);
//接收一个字符
char hal_get_char();
//接收一个字符串
char* hal_get_string();
#endif
uart4.c
#include "uart4.h"
//初始化UART4
void hal_uart4_init()
{
/*************RCC*************/
//1.GPIOB使能 MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.GPIOG使能 MP_AHB4ENSETR[6] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//3.UART4使能 MP_APB1ENSETR[16] = 1
RCC->MP_APB1ENSETR |= (0x1 << 16);
/************GPIO*************/
//1.设置 FB2 引脚为复用模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0X3 << 4));
GPIOB->MODER |= (0x2 << 4);
//2.设置 FB2 引脚复用为 UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x8 << 8);
//1.设置 FG11 引脚为复用模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0X3 << 22));
GPIOG->MODER |= (0X2 << 22);
//2.设置 FG11 引脚复用为 UART4_TX AFRL[15:12] = 1000
GPIOG->AFRL &= (~(0XF << 12));
GPIOG->AFRL |= (0X6 << 12);
/***********UART4*************/
//0.设置串口UE = 0
USART4->CR1 &= (~0x1);
//1.设置1位起始位,8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//2.没有奇偶校验
USART4->CR1 &= (~(0x1 << 10));
//3.设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
//4.设置16倍采样位
USART4->CR1 &= (~(0x1 << 15));
//5.设置串口不分频
USART4->PRESC &= (~0xF);
//6.设置串口波特率为115200
USART4->BRR &= (~0xFFFF);
USART4->BRR |= (0x22B);
//7.设置串口发送器使能
USART4->CR1 |= (0x1 << 3);
//8.设置串口接收器使能
USART4->CR1 |= (0x1 << 2);
//9.设置串口使能
USART4->CR1 |= 0x1;
}
//发送一个字符
void hal_put_char(const char str)
{
//1.判断是否为空 ISR[7]
//0为满(堵塞),1为空(发送)
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送内容赋值寄存器
USART4->TDR = str;
//3.判断一帧数据是否发送完成ISR[6]
//0未完成,1已完成
while(!(USART4->ISR & (0x1 << 6)));
}
//接收一个字符
char hal_get_char()
{
//1.判断接收是否有数据ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将数据读取
return USART4->RDR;
}
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)
{
char arr = hal_get_char();
hal_put_char(arr+1);
}
return 0;
}
实现二
键盘输入一个字符串,串口工具回显输入的字符串
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//初始化UART4
void hal_uart4_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char * str);
//接收一个字符
char hal_get_char();
//接收一个字符串
char* hal_get_string();
#endif
uart4.c
#include "uart4.h"
extern void delay_ms(int ms);
//初始化UART4
void hal_uart4_init()
{
/*************RCC*************/
//1.GPIOB使能 MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.GPIOG使能 MP_AHB4ENSETR[6] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//3.UART4使能 MP_APB1ENSETR[16] = 1
RCC->MP_APB1ENSETR |= (0x1 << 16);
/************GPIO*************/
//1.设置 FB2 引脚为复用模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0X3 << 4));
GPIOB->MODER |= (0x2 << 4);
//2.设置 FB2 引脚复用为 UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x8 << 8);
//1.设置 FG11 引脚为复用模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0X3 << 22));
GPIOG->MODER |= (0X2 << 22);
//2.设置 FG11 引脚复用为 UART4_TX AFRL[15:12] = 1000
GPIOG->AFRL &= (~(0XF << 12));
GPIOG->AFRL |= (0X6 << 12);
/***********UART4*************/
//0.设置串口UE = 0
if(USART4->CR1 & 0x1)
{
delay_ms(500);
USART4->CR1 &= (~0x1);
}
//1.设置1位起始位,8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//2.没有奇偶校验
USART4->CR1 &= (~(0x1 << 10));
//3.设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
//4.设置16倍采样位
USART4->CR1 &= (~(0x1 << 15));
//5.设置串口不分频
USART4->PRESC &= (~0xF);
//6.设置串口波特率为115200
USART4->BRR &= (~0xFFFF);
USART4->BRR |= (0x22B);
//7.设置串口发送器使能
USART4->CR1 |= (0x1 << 3);
//8.设置串口接收器使能
USART4->CR1 |= (0x1 << 2);
//9.设置串口使能
USART4->CR1 |= 0x1;
}
//发送一个字符
void hal_put_char(const char str)
{
//1.判断是否为空 ISR[7]
//0为满(堵塞),1为空(发送)
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送内容赋值寄存器
USART4->TDR = str;
//3.判断一帧数据是否发送完成ISR[6]
//0未完成,1已完成
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void hal_put_string(const char* str)
{
//判断是否为‘\0'
//一个一个字符发送
while(*str)
{
hal_put_char(*str++);
}
hal_put_char('\n');
hal_put_char('\r');
}
//接收一个字符
char hal_get_char()
{
//1.判断接收是否有数据ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将数据读取
return USART4->RDR;
}
char arr[100] = {0};
//接收一个字符串
char* hal_get_string()
{
//循环接收
int i = 0;
while(1)
{
//'\n'回车表示接收完成
arr[i] = hal_get_char();
hal_put_char(arr[i]);
if(arr[i] == '\r')
{
break;
}
i++;
}
arr[i] = '\0';
hal_put_char('\n');
return arr;
}
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();
hal_put_string("UART4 TEXT\n");
hal_put_char(hal_get_char());
while(1)
{
hal_put_string(hal_get_string());
}
return 0;
}
文章来源:https://www.toymoban.com/news/detail-523940.html
文章来源地址https://www.toymoban.com/news/detail-523940.html
到了这里,关于ARM day7 (串口协议)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!