uart.h
#ifndef __UART__H__#define __UART__H__#include "stm32mp1xx_gpio.h"#include "stm32mp1xx_rcc.h"#include "stm32mp1xx_uart.h" void all_set();void putchar(char a);char getchar();void puts(char *s);void gets(char *s);#endif
led.h
#ifndef __LED_H__#define __LED_H__#define RCC_GPIO (*(unsigned int *)0x50000a28)#define GPIOE_MODER (*(unsigned int *)0x50006000)#define GPIOF_MODER (*(unsigned int *)0x50007000)#define GPIOE_OTYPER (*(unsigned int *)0x50006004)#define GPIOF_OTYPER (*(unsigned int *)0x50007004)#define GPIOE_OSPEEDR (*(unsigned int *)0x50006008)#define GPIOF_OSPEEDR (*(unsigned int *)0x50007008)#define GPIOE_PUPDR (*(unsigned int *)0x5000600C)#define GPIOF_PUPDR (*(unsigned int *)0x5000700c)#define GPIOE_ODR (*(unsigned int *)0x50006014)#define GPIOF_ODR (*(unsigned int *)0x50007014)void all_led_init();void led1_on();void led1_off();void led2_on();void led2_off();void led3_on();void led3_off();void delay(int ms);void feng_init();void feng_off();void feng_on();#endif
led.c
#include "led.h"#include"uart4.h"void all_led_init(){RCC_GPIO |= (0X3<<4);//时钟使能GPIOE_MODER &=(~(0X3<<20));//设置PE10输出GPIOE_MODER |= (0X1<<20);//设置PE10为推挽输出GPIOE_OTYPER &=(~(0x1<<10));//PE10为低速输出GPIOE_OSPEEDR &= (~(0x3<<20));//设置无上拉下拉GPIOE_PUPDR &= (~(0x3<<20));//LED2GPIOF_MODER &=(~(0X3<<20));//设置Pf10输出GPIOF_MODER |= (0X1<<20);//设置Pf10为推挽输出GPIOF_OTYPER &=(~(0x1<<10));//Pf10为低速输出GPIOF_OSPEEDR &= (~(0x3<<20));//设置无上拉下拉GPIOF_PUPDR &= (~(0x3<<20));//LED3GPIOE_MODER &=(~(0X3<<16));//设置PE8输出GPIOE_MODER |= (0X1<<16);//设置PE8为推挽输出GPIOE_OTYPER &=(~(0x1<<8));//PE8为低速输出GPIOE_OSPEEDR &= (~(0x3<16));//设置无上拉下拉GPIOE_PUPDR &= (~(0x3<<16));}void led1_on(){GPIOE_ODR |= (0x1<<10);}void led1_off(){GPIOE_ODR &= (~(0x1<<10));}void led2_on(){GPIOF_ODR |= (0x1<<10);}void led2_off(){GPIOF_ODR &= (~(0x1<<10));}void led3_on(){GPIOE_ODR |= (0x1<<8);}void led3_off(){GPIOE_ODR &= (~(0x1<<8));}void delay(int ms){int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++){}}}void feng_init(){GPIOE_MODER &=(~(0X3<<18));//设置PE9输出GPIOE_MODER |= (0X1<<18);//设置PE9为推挽输出GPIOE_OTYPER &=(~(0x1<<9));//PE9为低速输出GPIOE_OSPEEDR &= (~(0x3<<18));//设置无上拉下拉GPIOE_PUPDR &= (~(0x3<<18));}void feng_on(){ GPIOE_ODR |= (0x1<<9);}void feng_off(){ GPIOE_ODR&= (~(0x1<<9));}
uart4.c
#include "uart4.h"#include "led.h"void all_set(){//1.使能GPIOB\GPIOG\UART4外设时钟RCC->MP_AHB4ENSETR |=(0x1<MP_AHB4ENSETR |=(0x1<MP_APB1ENSETR |=(0x1<MODER &=(~(0x3<MODER |=(0x2<AFRL &=(0xf<AFRL |=(0x8<MODER &=(~(0x3<MODER |=(0x2<AFRH &=(0xf<AFRH |=(0x6<在UE为0时,初始化系统设置USART4->CR1 &=(~0x1);//3.设置数据位宽为8位USART4->CR1 &=(~(0x1<CR1 &=(~(0x0<CR1 &=(~(0x1<CR1 &=(~(0x1<CR2 &=(~(0x3<PRESC &=(~0xf);//8.设置波特率为115200USART4->BRR = 0x22b;//9.使能发送器USART4->CR1 |=(0x1<CR1 |=(0x1<CR1 |=(0x1);}void putchar(char a){//1.先判断发送器是否为空,不为空等待while(!(USART4->ISR &(0X1<TDR = a;if(a == 'a'){led1_on();led2_off();led3_off();}else if(a == 's'){led1_off();led2_on();led3_off();}else if(a == 'd'){led1_off();led2_off();led3_on();}else if(a == 'f'){feng_on();}else if(a == 'g'){feng_off();} //3.等待发送完成 while(!(USART4->ISR &(0x1<ISR &(0x1<RDR;//3.返回 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');}
man.c
#include "gpio.h"#include"uart4.h"#include "led.h"int main(){ char a;//char buf[128];all_set();all_led_init();feng_init();while(1){a=getchar();putchar(a);putchar('\r');putchar('\n'); //gets(buf); //puts(buf);}return 0;}