日期:10月 28日
一、题目
1.
代码实现:
#ifndef __LED_H__
#define __LED_H__
#define MAX 6
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
typedef enum{
LED1 = 1 ,
LED2,
LED3
}led_t;
typedef enum{
LED_ON,
LED_OFF
}status_t;
//1、初始化串口
void hal_init();
//定义写函数
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,status_t status);
//发送一个字符
void put_char(const char ch );
//发送一个字符串
void put_string(const char* string);
//2、接收一个字符
char get_char();
//3、接收一个字符串
char* get_string();
//4.结构体封装
typedef struct{
char* cmd_arr;//命令符字符串
gpio_t *gpio;//gpio组号
unsigned int pin;//引脚编号
status_t status;//LED灯状态
void (*gpio_write_p)(gpio_t* gpio,unsigned int pin,status_t status);
}cmd_t;
//5.引脚进行封装
#define GPIO_PIN_0 0
#define GPIO_PIN_1 1
#define GPIO_PIN_2 2
#define GPIO_PIN_3 3
#define GPIO_PIN_4 4
#define GPIO_PIN_5 5
#define GPIO_PIN_6 6
#define GPIO_PIN_7 7
#define GPIO_PIN_8 8
#define GPIO_PIN_9 9
#define GPIO_PIN_10 10
#define GPIO_PIN_11 11
#define GPIO_PIN_12 12
#define GPIO_PIN_13 13
#define GPIO_PIN_14 14
#define GPIO_PIN_15 15
cmd_t* find_command(const char* str);
int my_strcmp(const char* str1,const char* str2);
#endif
#include "led.h"
extern void delay_ms(int ms);
//初始化结构体
cmd_t cmd_buf[MAX] = {
[0] = {
.cmd_arr = "led1on",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = LED_ON,
.gpio_write_p = hal_gpio_write,
},
[1] = {
.cmd_arr = "led1off",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = LED_OFF,
.gpio_write_p = hal_gpio_write,
},
[2] = {
.cmd_arr = "led2on",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = LED_ON,
.gpio_write_p = hal_gpio_write,
},
[3] = {
.cmd_arr = "led2off",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = LED_OFF,
.gpio_write_p = hal_gpio_write,
},
[4] = {
.cmd_arr = "led3on",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = LED_ON,
.gpio_write_p = hal_gpio_write,
},
[5] = {
.cmd_arr = "led3off",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = LED_OFF,
.gpio_write_p = hal_gpio_write,
},
};
int my_strcmp(const char* s1,const char* s2)
{
unsigned int res;
while(*s1 == *s2 && *s1 !='\0' && *s2 != '\0')
{
s1++;
s2++;
}
res = *s1- *s2;
return res;
}
cmd_t* find_command(const char* str)
{
int i = 0;
for(i = 0;i < MAX; i++)
{
if(!my_strcmp(str,cmd_buf[i].cmd_arr))
{
return &cmd_buf[i];
}
}
return 0;
}
//2.gpio写函数
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,status_t status)
{
if(status == LED_OFF)
{
gpiox->ODR &= (~(0x1 << pin));
}
else
{
gpiox->ODR |= (0x1 << pin);
}
}
//1、初始化串口
void hal_init()
{
/*******RCC章节初始化*********/
RCC->MP_AHB4ENSETR |=(0x1<<1);
RCC->MP_AHB4ENSETR |=(0x1<<6);
RCC->MP_APB1ENSETR |=(0x1<<16);
RCC->MP_AHB4ENSETR |=(0x1 << 4);
RCC->MP_AHB4ENSETR |=(0x1 << 5);
/******GPIO章节初始化*********/
//UART4_TX--->PG11
//UART4_RX--->PB2
//1.设置PG11引脚为复用功能模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x1 << 23);
//2.设置PG11引脚复用功能为UART4_TX AFRH[15:12] = 0110
GPIOG->AFRH &= (~(0xf << 12));
GPIOG->AFRH |= (0x3 << 13);
//3.设置PB2引脚为复用功能模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x1 << 5);
//4.设置PB2引脚复用功能为UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x1 << 11);
//LED1灯
//GPIOE_MODER [21:20]=01
GPIOE->MODER &=(~(0x3 << 20));
GPIOE->MODER |=(0x1<<20);
//GPIOE_OTYPER [10]=0
GPIOE->OTYPER &=(~(0x1 << 10));
//GPIOE_OSPEEDR [21:20]=00
GPIOE->OSPEEDR &=(~(0x3 << 20));
//GPIOE_PUPDR [21:20]=00
GPIOE->PUPDR &=(~(0x3 << 20));
//LED2
//GPIOE_MODER [21:20]=01
GPIOF->MODER &=(~(0x3 << 20));
GPIOF->MODER |=(0x1<<20);
//GPIOE_OTYPER [10]=0
GPIOF->OTYPER &=(~(0x1 << 10));
//GPIOE_OSPEEDR [21:20]=00
GPIOF->OSPEEDR &=(~(0x3 << 20));
//GPIOE_PUPDR [21:20]=00
GPIOF->PUPDR &=(~(0x3 << 20));
//LED3
//GPIOE_MODER [17:16]=01
GPIOE->MODER &=(~(0x3 << 16));
GPIOE->MODER |=(0x1<<16);
//GPIOE_OTYPER [8]=0
GPIOE->OTYPER &=(~(0x1 << 8));
//GPIOE_OSPEEDR [17:16]=00
GPIOE->OSPEEDR &=(~(0x3 << 16));
//GPIOE_PUPDR [17:16]=00
GPIOE->PUPDR &=(~(0x3 << 16));
/*******UART4章节初始化******/
//0.需要判断串口UE位是否使能 CR1[0] = 1
if(USART4->CR1 & 0x1)
{
delay_ms(500);
USART4->CR1 &= (~(0x1 << 0));
}
//1.设置UART4串口8为数据位,无奇偶校验位
//CR1[28][12] = 00 CR1[10] = 0
USART4->CR1 &= (~(0x1 << 12));
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 10));
//2.设置UART4串口1位停止位 CR2[13:12] = 00
USART4->CR2 &= (~(0x3 << 12));
//3.设置UART4串口16倍采样率 CR1[15] = 0
USART4->CR1 &= (~(0x1 << 15));
//4.设置UART4串口不分频 PRESC[3:0] = 0000
USART4->PRESC &= (~(0xf << 0));
//4.设置UART4串口波特率为115200 BRR = 0x22b
USART4->BRR |= 0x22b;
//5.设置UART4串口接受器使能 CR1[3] = 1
USART4->CR1 |= (0x1 << 3);
//6.设置UART4串口发送器使能 CR1[2] = 1
USART4->CR1 |= (0x1 << 2);
//7.设置UART4串口使能 CR1[0] = 1
USART4->CR1 |= (0x1 << 0);
}
//2.发送一个字符
void put_char(const char ch)
{
//1.判断发送寄存器是否有数据 ISR[7]
//读0:发送数据寄存器满,需要等待
//读1:发送数据寄存器为空,才可以发送下一个字节的数据
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送的字符,写到发送数据寄存器中
USART4->TDR = ch;
//3.判断发送数据是否完成 ISR[6]
while(!(USART4->ISR & (0x1 << 6)));
}
//3.发送一个字符串
void put_string(const char* string)
{
//一个字符一个字符进行发送
while(*string)
{
put_char(*string++);
}
put_char('\n');
put_char('\r');
}
//4.接收一个字符
char get_char()
{
char ch;
//1.判断接收数据是否有数据 ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
ch = USART4->RDR;
return ch;
}
//5.接收一个字符串
char buffer[50] = {0};
char* get_string()
{
//1.循环进行接收
unsigned int i;
//2.循环进行接收的时候,接收一个字符,发送一个字符
//当键盘的回车建按下之后,代表字符串结束了'\r'
for(i=0;i<49;i++)
{
buffer[i] = get_char();
put_char(buffer[i]);
if(buffer[i] == '\r')
break;
}
//3.字符串以'\0'结尾
buffer[i] = '\0';
put_char('\n');
return buffer;
}
#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
hal_init();
char* string = 0;
while(1)
{
string = get_string();
cmd_t* pa;
pa = find_command(string);
if(pa == 0)
{
put_string("Not find this command!!");
}
else
{
hal_gpio_write(pa->gpio,pa->pin,pa->status);
}
}
return 0;
}