作业要求:
代码效果图:
代码:
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();#endif
led.c
#include "led.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));
}
uart.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 *s1,char *s2);
#endif
uart.c
#include"uart4.h"//uart4的相关配置
void uart4_config()
{//1.使能GPIOB\GPIOG\UART4外设时钟RCC->MP_AHB4ENSETR |= (0x1<<1);//gpiobRCC->MP_AHB4ENSETR |= (0x1<<6);//gpiogRCC->MP_APB1ENSETR |= (0x1<<16);//uart4//2.设置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);//3.设置数据位宽为8位USART4->CR1 &= (~(0x1<<12));USART4->CR1 &= (~(0x1<<28));//4.设置无奇偶校验位USART4->CR1 &= (~(0x1<<10));//5.设置16倍过采样USART4->CR1 &= (~(0x1<<15));//6.设置1位停止位USART4->CR2 &= (~(0x3<<12));//7.设置不分频USART4->PRESC &= (~0xf);//8.设置波特率为115200USART4->BRR=0X22B;//9.使能发送器USART4->CR1 |= (0x1<<3);//10.使能接收器USART4->CR1 |= (0x1<<2);//11.使能串口USART4->CR1 |= (0x1);
}void putchar(char a)
{//1.先判断发送器是否为空,不为空等待while(!(USART4->ISR &(0x1<<7)));//2.向发送寄存器写入数据USART4->TDR=a;//3.等待发送完成while(!(USART4->ISR &(0x1<<6)));
}char getchar()
{char a;//1.判断接收器是否有准备好的数据,没有就等待while(!(USART4->ISR &(0x1<<5)));//2.读取数据a=USART4->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';}//实现字符串比较
int strcmp(char *s1,char *s2)
{int i = 0;while(((*(s1+i))==(*(s2+i)))){i++;if( (*(s1+i)=='\0')){break;}}int sub = ((*(s1+i))-(*(s2+i)));if(sub>0){return sub;}else if(sub<0){return sub;}else{return 0; }
}
main.c
#include "uart4.h"
#include "led.h"int main()
{char buf[128];char *ledon1 = "led1on";char *ledon2 = "led2on";char *ledon3 = "led3on";char *ledoff1 = "led1off";char *ledoff2 = "led2off";char *ledoff3 = "led3off";uart4_config();all_led_init();while (1){gets(buf); // 读取字符串puts(buf); // 打印字符串//判断字符串,确定灯的亮与灭if((strcmp(buf,ledon1) == 0)) //一号灯亮{led1_on();}else if((strcmp(buf,ledoff1) == 0)) //一号灯灭{led1_off();}else if((strcmp(buf,ledon2) == 0)) //二号灯亮{led2_on();}else if((strcmp(buf,ledoff2) == 0)) //二号灯灭{led2_off();}else if((strcmp(buf,ledon3) == 0)) //三号灯亮{led3_on();}else if((strcmp(buf,ledoff3) == 0)) //三号灯灭{led3_off();} }
}