1.实验目的:完成字符收发
led.h
cs
#ifndef __GPIO_H__
#define __GPIO_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
//RCC,GPIO,UART初始化
void init();
//字符数据发送
void set_tt(const char str);
//字符数据接收
char ret_tt();
//字符串数据发送
void seting_tt(const char* string);
//字符串数据接收
char reting_tt();
#endif
led.c
cs
#include "led.h"
//PB2,PG11,UART初始化
void init()
{
//PB2,PG11,UART使能
RCC-> MP_AHB4ENSETR |= (0X1<<1);
RCC-> MP_AHB4ENSETR |= (0X1<<6);
RCC-> MP_APB1ENSETR |= (0X1<<16);
//PB2
GPIOB->MODER |=(0X3<<4);
GPIOB->MODER &=(~(0X1<<4));
GPIOB->AFRL |=(0X15<<8);
GPIOB->AFRL &=(~(0X7<<8));
//PG11
GPIOG->MODER |=(0X3<<22);
GPIOG->MODER &=(~(0X1<<22));
GPIOG->AFRH |=(0X15<<12);
GPIOG->AFRH &=(~(0X9<<12));
}
//发送一个字符
void set_tt(const char data)
{
//数据位
USART4->CR1 &=(~(0X1<<12));
USART4->CR1 &=(~(0X1<<28));
USART4->CR1 &=(~(0X1<<10));
USART4->CR1 &=(~(0X1<<15));
//设置串口波特率
USART4->BRR =0X22b;
//设置停止位
USART4->CR2 &=(~(0X3<<12));
//使能USART
USART4->CR1 |=(0X1<<0);
//设置发送位使能
USART4->CR1 |=(0X1<<3);
//发送数据
if(USART4->ISR &(0X1<<7))
{
USART4->TDR=data;
if(USART4->ISR &(0X1<<6))
{
return;
}
}
}
//接受一个字符
char ret_tt()
{
//数据位
USART4->CR1 &=(~(0X1<<12));
USART4->CR1 &=(~(0X1<<28));
USART4->CR1 &=(~(0X1<<10));
USART4->CR1 &=(~(0X1<<15));
//设置串口波特率
USART4->BRR =0X22b;
//设置停止位
USART4->CR2 &=(~(0X3<<12));
//使能USART
USART4->CR1 |=(0X1<<0);
//设置接受位使能
USART4->CR1 |=(0X1<<2);
//接受数据
char data;
while(1)
{
if(USART4->ISR &(0X1<<5))
{
data=USART4->RDR;
return data;
}
}
return 0;
}
main.c
cs
#include "led.h"
int main()
{
char data;
init();
while(1)
{
data=ret_tt();
set_tt(data);
}
return 0;
}
2.实验目的:完成字符串收发