main.c
#include "uart4.h"//封装延时函数void delay(int ms){int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++){}}}int main(){led_init();uart4_init();//char buf[128];char *str;//char i;while(1){/*i=getchar();putchar(i+1);putchar('\n');putchar('\r');*/str=gets();puts(str);if(strcmp(str,"led1_on")){GPIOE->ODR |= (0X1<<10); }else if(strcmp(str,"led1_off")){GPIOE->ODR &= ~(0X1<<10);}else if(strcmp(str,"led2_on")){GPIOE->ODR |= (0X1<<8);}else if(strcmp(str,"led2_off")){GPIOE->ODR &= ~(0X1<<8);}else if(strcmp(str,"led3_on")){GPIOF->ODR |= (0X1<<10);}else if(strcmp(str,"led3_off")){GPIOF->ODR &= ~(0X1<<10);}}return 0;}
src/uart4.c
#include "uart4.h"char buf[128]={0};
//led数据初始化
void led_init()
{//设置GPIOE/GPIOF时钟使能RCC->MP_AHB4ENSETR |=(0X3<<4);//设置PE10/PE8/PF10为输出模式//PE10GPIOE->MODER &=(~(0X3<<20));GPIOE->MODER |=(0X1<<20);//PE8GPIOE->MODER &=(~(0X3<<16));GPIOE->MODER |=(0X1<<16);//PF10GPIOF->MODER &=(~(0X3<<20));GPIOF->MODER |=(0X1<<20);//设置PE10/PE8/PF10为推完输出//PE10GPIOE->OTYPER &=(~(0X1<<10));//PE8GPIOE->OTYPER &=(~(0X1<<8));//PF10GPIOF->OTYPER &=(~(0X1<<10));//设置PE10/PE8/PF10为输出速度为低速//PE10GPIOE->OSPEEDR &=(~(0X3<<20));//PE8GPIOE->OSPEEDR &=(~(0X3<<16));//PF10GPIOF->OSPEEDR &=(~(0X3<<20));//设置PE10/PE8/PF10为无上拉无下拉GPIOE->PUPDR &=(~(0X3<<20));//PE8GPIOE->PUPDR &=(~(0X3<<20));//PE10GPIOE->PUPDR &=(~(0X3<<20));
}void uart4_init()
{//使能GPIOB GPIOG UART4外设时钟RCC->MP_AHB4ENSETR |= (0x1<<1);RCC->MP_AHB4ENSETR |= (0x1<<6);RCC->MP_APB1ENSETR |= (0x1<<16);//设置PB2和PG11管脚复用//PB2GPIOB->MODER &= (~(0X3<<4));GPIOB->MODER |=(0X2<<4);GPIOB->AFRL &=(~(0xf<<8));GPIOB->AFRL |=(0x8<<8);//PG11GPIOG->MODER &= (~(0X3<<22));GPIOG->MODER |=(0X2<<22);GPIOG->AFRL &=(~(0xf<<12));GPIOG->AFRL |=(0x6<<12);//设置串口不使能UE=OUSART4->CR1 &= (~(0X1));//设置8位数据位USART4->CR1 &=(~(0X1<<12));USART4->CR1 &= (~(0X1<<28));//设置没有奇偶校验位USART4->CR1 &=(~(0X1<<10));//设置1位停止位USART4->CR2 &= (~(0X3<<12));//设置16倍过采样USART4-> CR1 &= (~(0X1<<15));//设置时钟不分频USART4->PRESC &=(~0xf);//设置波特率为115200USART4->BRR=0X22B;//使能发送器USART4->CR1 |=(0X1<<3);//使能接收器USART4->CR1 |=(0X1<<2);//使能uart4USART4->CR1 |= (0X1);
}//发送一个字符
void putchar(char a)
{//先判断发送数据寄存器是否为空//不为空阻塞等待while(!(USART4->ISR &(0X1<<7)));//为空向发送数据寄存器写入a的数据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('\n');putchar('\r');
}
//接收字符串
char *gets()
{unsigned int i;for(i=0;i<50;i++){buf[i]=getchar();putchar(buf[i]);if((buf[i])=='\r')break;}buf[i]='\0';putchar('\n');putchar('\r');return buf;
}//比较两个字符串函数
int strcmp(char *dest, char *src)
{if(*dest == *src)return 0;while(*dest && *src){if(*dest != *src){return *dest - *src;}dest++;src++;}return 0;
}
src/uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
void led_init();
void uart4_init();
void putchar(char a);
char getchar();
void puts(char *s);
char *gets();
int strcmp(char *dest, char *src);#endif