代码
uart4.c
#include "uart4.h"void uart4_config()
{//*****************************************//使能GPIOB|GPIOG|UART4外设时钟RCC->MP_AHB4ENSETR |=(0x1<<6);RCC->MP_AHB4ENSETR |=(0x1<<1);RCC->MP_APB1ENSETR |=(0x1<<16);RCC->MP_AHB4ENSETR |=(0x3<<4);//*****************************************//设置PB2|PG11用于UART4的管脚复用//PG11GPIOG->MODER &=(~(0x3<<22));//先清零GPIOG->MODER |=(0x2<<22);//再赋值GPIOG->AFRH &=(~(0xf<<12));GPIOG->AFRH |= (0x6<<12);//PB2GPIOB->MODER &=(~(0x3<<4));GPIOB->MODER |=(0x2<<4);GPIOB->AFRL &=(~(0xf<<8));GPIOB->AFRL |= (0x8<<8);//*****************************************//设置串口禁用USART4->CR1 &=(~0x1);//*****************************************//设置数据位宽为8位USART4->CR1 &=(~(0x1<<28));USART4->CR1 &=(~(0x1<<12));//*****************************************//设置无奇偶校验位USART4->CR1 &=(~(0x1<<10));//*****************************************//设置16倍过采样USART4->CR1 &=(~(0x1<<15));//*****************************************//设置1位的停止位USART4->CR2 &=(~(0x1<<12));//*****************************************//设置波特率为115200USART4->BRR =0x22B;//*****************************************//使能发送器USART4->CR1 |=(0x1<<3);//*****************************************//使能接收器USART4->CR1 |=(0x1<<2);//*****************************************//使能串口USART4->CR1 |=0x1;//*****************************************//LED1的操作GPIOE->MODER &=(~(0x3<<20));//设置PE10输出GPIOE->MODER |=(0x1<<20);GPIOE->OTYPER &=(~(0x1<<10));//设置PE10为推挽输出GPIOE->OSPEEDR &=(~(0x3<<20));//设置低速输出GPIOE->PUPDR &=(~(0x3<<20));//设置无上拉下拉//*****************************************//LED2的操作GPIOF->MODER &=(~(0x3<<20));//设置PF10输出GPIOF->MODER |=(0x1<<20);GPIOF->OTYPER &=(~(0x1<<10));//设置PF10为推挽输出GPIOF->OSPEEDR &=(~(0x3<<20));//设置低速输出GPIOF->PUPDR &=(~(0x3<<20));//设置无上拉下拉//*****************************************//LED3的操作GPIOE->MODER &=(~(0x3<<16));//设置PE10输出GPIOE->MODER |=(0x1<<16);GPIOE->OTYPER &=(~(0x1<<8));//设置PE10为推挽输出GPIOE->OSPEEDR &=(~(0x3<<16));//设置低速输出GPIOE->PUPDR &=(~(0x3<<16));//设置无上拉下拉}//发送一个单字符
void putchar(char a)
{//先判发送器是否为空,不为空阻塞等待while(!(USART4->ISR &(0x1<<7)));//向发送寄存器写入数据USART4->TDR=a;//等待发送完成while(!(USART4->ISR &(0x1<<6)));}
//接收一个单字符
char getchar()
{char a;//判断接收器是否有准备好的数据while(!(USART4->ISR &(0x1<<5)));//读取数据a=USART4->RDR;//返回return a;}//发送一个字符串
void puts(char *s)
{while(*s){putchar(*s);s++;}putchar('\r');putchar('\n');
}
//接受一个字符串
void gets(char *s)
{while(1){*s=getchar();putchar(*s);//键盘输入的内容在串口上回显if(*s=='\r')break;s++;}*s='\0';putchar('\n');
}
//字符串比较
int strcmp(char *src,char *dst)
{while(*src!='\0' || *dst!='\0'){if(*src!=*dst){return *src - *dst;}src++;dst++;}return 0;
}
main.c
//#include "gpio.h"#include "uart4.h"int main(){// char a;char src[128];uart4_config();while(1){/* //接受一个字符数据a=getchar();//发送接收到的字符+1putchar(a);putchar('\r');//回车,切换到下一行putchar('\n');//换行*/ gets(src);puts(src);if(strcmp(src,"led1_on")==0){GPIOE->ODR |=(0x1<<10);}else if(strcmp(src,"led1_off")==0){GPIOE->ODR &=(~(0x1<<10));}else if(strcmp(src,"led2_on")==0){GPIOF->ODR |=(0x1<<10);}else if(strcmp(src,"led2_off")==0){GPIOF->ODR &=(~(0x1<<10));}else if(strcmp(src,"led3_on")==0){GPIOE->ODR |=(0x1<<8);}else if(strcmp(src,"led3_off")==0){GPIOE->ODR &=(~(0x1<<8));}}return 0;}
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"void uart4_config();
void putchar(char a);
char getchar();
void gets(char *s);
void puts(char *s);
int strcmp(char *src,char *dst);#endif
效果图