main.c
#include"led.h"
//手动封装一个延时函数
void delay_ms(int ms)
{int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++){}}
}
int strcmp(char *s1,char *s2)
{while(*s1&&(*s1==*s2)){*s1++;*s2++;}return *s1-*s2;
}
int main()
{//使能GPIO的外设时钟*((unsigned int *)0X50000A28) |= (0X3<<4);led1_init();// led2_init();// led3_init();uart4_config();char a;char buf[32];char auf[32]="yes";char cuf[32]="no";while(1){/*a=getchar();//读取一个字符putchar(a+1);//将读取到的字符+1返回 win下换行符是'\n',回车键对应的字符是'\r'putchar('\n');putchar('\r');*/gets(buf);//读取字符串if (strcmp(buf,auf)==0){led1_ctl(1);}else if(strcmp(buf,cuf)==0){led1_ctl(0);}puts(buf);}return 0;
}
led.h
#ifndef __LED_H__
#define __LED_H__typedef struct{unsigned int MODER;unsigned int OTYPER;unsigned int OSPEEDR;unsigned int PUPDR;unsigned int IDR;unsigned int ODR;unsigned int BSRR;
}gpio1_t;#define GPIOE ((gpio1_t *)0x50006000)
#define GPIOF ((gpio1_t *)0x50007000)void led1_init();
//void led2_init();
//void led3_init();
void led1_ctl(int flag);
#endif#ifndef __UART4_H__
#define __UART4_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_uart.h"
void uart4_config();
void putchar(char c);
char getchar();
void puts(char *s);
void gets(char *s);#endif
led.c
#include "led.h"void led1_init()
{// 设置PE10为输出模式GPIOE->MODER &= (~(0x3 << 20));GPIOE->MODER |= (0x1 << 20);// 设置PE10为推挽输出GPIOE->OTYPER &= (~(0x1 << 10));// 设置PE10为低速输出GPIOE->OSPEEDR &= (~(0x3 << 20));// 设置PE10无上拉下拉GPIOE->PUPDR &= (~(0x3 << 20));
}
/*void led2_init()
{// 设置Pf10为输出模式GPIOF->MODER &= (~(0x3 << 20));GPIOF->MODER |= (0x1 << 20);// 设置PF10为推挽输出GPIOF->OTYPER &= (~(0x1 << 10)); // 设置PF10为低速输出GPIOF->OSPEEDR &= (~(0x3 << 20));// 设置PF10无上拉下拉GPIOF->PUPDR &= (~(0x3 << 20));
}*/
/*void led3_init()
{// 设置PE8为输出模式GPIOE->MODER &= (~(0x3 << 16));GPIOE->MODER |= (0x1 << 16);// 设置PE8为推挽输出GPIOE->OTYPER &= (~(0x1 << 8));// 设置PE8为低速输出GPIOE->OSPEEDR &= (~(0x3 << 16));// 设置PE8无上拉下拉GPIOE->PUPDR &= (~(0x3 << 16));
}*/
//LED1控制
void led1_ctl(int flag)
{if(flag==1){GPIOE->ODR |= (0x1<<10);}else if(flag==0){GPIOE->ODR &= (~(0x1<<10));}
}
//LED2控制
/*void led2_ctl(int flag)
{if(flag==1){GPIOF->ODR |= (0x1<<10);}else if(flag==0){GPIOF->ODR &= (~(0x1<<10));}
}*/
//LED3控制
/*void led3_ctl(int flag)
{if(flag==1){GPIOE->ODR |= (0x1<<8);}else if(flag==0){GPIOE->ODR &= (~(0x1<<8));}*/
//
void uart4_config()
{//使能UART4、GPIOG\GPIOB的外设时钟RCC->MP_APB1ENSETR |= (0x1<<16);RCC->MP_AHB4ENSETR |= (0x1<<1);RCC->MP_AHB4ENSETR |= (0x1<<6);//设置PB2为复用功能GPIOB->MODER &= (~(0x3<<4));GPIOB->MODER |= ((0x2<<4));//设置PB2为UART_RX复用功能GPIOB->AFRL &= (~(0xf<<8));GPIOB->AFRL |= (0x8<<8);//设置PG11为复用功能GPIOG->MODER &= (~(0x3<<22));GPIOG->MODER |= ((0x2<<22));//设置PG11为UART_TX复用功能GPIOB->AFRL &= (~(0xf<<12));GPIOB->AFRL |= (0x6<<12);//禁用串口,UE=0USART4->CR1 &=(~0x1);//设置8位数据位USART4->CR1 &= (~(0x1<<12));USART4->CR1 &= (~(0x1<<28));//设置无奇偶校验USART4->CR1 &= (~(0x1<<10));//设置1位停止位USART4->CR2 &= (~(0x3<<12));//设置波特率为115200USART4->BRR=0x22b;//设置频率不分频USART4->PRESC &= (~(0XF));//设置16倍过采样倍数USART4->CR1 &= (~(0X1<<15));//使能发送器USART4->CR1 |= (0x1<<3);//使能接收器USART4->CR1 |= (0x1<<2);//使能串口USART4->CR1 |= (0x1);}//封装单个字符发送函数
void putchar(char c)
{//判断发送数据寄存器是否为空,不为空则阻塞等待while(!(USART4->ISR&(0X1<<7)));//当为空时将数据写入到数据发送寄存器USART4->TDR=c;//阻塞等待数据发送完毕,函数结束while(!(USART4->ISR&(0X1<<6)));
}//封装单个字符接收的函数
char getchar()
{//判断接收数据寄存器中有没有有效数据。如果没有就阻塞等待,如果有直接读取while(!(USART4->ISR&(0X1<<5)));//将读取的数据返回return USART4->RDR;
}//发送一个字符串
void puts(char *s)
{while(*s){putchar(*s);s++;}//发送完字符串让光标显示在下一行最开头putchar('\n');putchar('\r');
}//接收字符串
void gets(char *s)
{while(1){*s=getchar();putchar(*s);//将读取到的字符回显到串口if(*s=='\r'){break;}s++;}*s='\0';//手动补上一个'\0'putchar('\n');//让光标显示在下一行
}