1.实验要求:
头文件
#ifndef __GPIO_H__
#define __GPIO_H__
#include "stm32mp1xx_adc.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
//1.串口通信
void put_char(const char ch);
char get_char();
void put_string(const char *str);
char *get_string();
#define MAX 6
typedef enum{
LED1 = 1, //枚举成员默认从0开始,此处从1开始
LED2,
LED3
}led_t;
typedef enum{
LED_ON,
LED_OFF
}status_t;
typedef struct{
char* cmd_arr; //字符串
led_t led; //哪个灯
status_t status; //开灯或关灯
//函数指针,指向控制灯的函数
void (*leds_status_t)(led_t led,status_t status); //函数
}cmd_t;
cmd_t* find_command(const char* str);
//执行开灯或关灯,实现灯的控制
void led_status(led_t led,status_t status);
//字符串比较函数
int strcmp(const char * str1,const char * str2);
//灯初始化
void led1_init(void);
void led2_init(void);
void led3_init(void);
void led1_on(void);
void led1_off(void);
void led2_on(void);
void led2_off(void);
void led3_on(void);
void led3_off(void);
#endif
功能函数:
#include "gpio.h"
//定义结构体数组,并初始化内部成员
cmd_t cmd_arr[MAX] = {
[0] = {
.cmd_arr = "led1on",
.led = LED1,
.status = LED_ON,
.leds_status_t = led_status,
},
[1] = {
.cmd_arr = "led1off",
.led = LED1,
.status = LED_OFF,
.leds_status_t = led_status,
},
[2] = {
.cmd_arr = "led2on",
.led = LED2,
.status = LED_ON,
.leds_status_t = led_status,
},
[3] = {
.cmd_arr = "led2off",
.led = LED2,
.status = LED_OFF,
.leds_status_t = led_status,
},
[4] = {
.cmd_arr = "led3on",
.led = LED3,
.status = LED_ON,
.leds_status_t = led_status,
},
[5] = {
.cmd_arr = "led3off",
.led = LED3,
.status = LED_OFF,
.leds_status_t = led_status,
},
};
//查找用户命令
cmd_t* find_command(const char* str)
{
int i;
for(i=0;i<MAX;i++)
{
if(!strcmp(str,cmd_arr[i].cmd_arr))
{
return &cmd_arr[i];
}
}
return 0;
}
//比较函数
int strcmp(const char * str1,const char * str2)
{
//abcd---abd
while(*str1==*str2 && *str1!= 0 && *str2 !=0)
{
//找到不相等的那一位,跳出循环
str1++;
str2++;
}
return *str1 - *str2;
}
void led_status(led_t led,status_t status)
{
led1_init();
led2_init();
led3_init();
switch(led)
{
case LED1:
if(status == LED_ON)
{
//printf("led1点亮\n");
led1_on();
}
else {
//printf("led1熄灭\n");
led1_off();
}
break;
case LED2:
if(status == LED_ON)
{
//printf("led2点亮\n");
led2_on();
}
else {
//printf("led2熄灭\n");
led2_off();
}
break;
case LED3:
if(status == LED_ON)
{
led3_on();
}
else {
led3_off();
}
break;
}
}
/**************灯的初始化************/
//LED1初始化PE10
void led1_init(void)
{
// 设置GPIOE使能0x50000A28[4]=1
RCC->MP_AHB4ENSETR |=(0x1<<4);
//设置PE10引脚为输出模式MODER[21:20]=01
GPIOE->MODER &=(~(0x3<<20)); //清零
GPIOE->MODER |=(0x1<<20);
//设置PE10引脚为推挽模式OTYPER[10]=0
GPIOE->OTYPER &= (~(0x1<<10));
//设置PE10引脚为低速输出模式OSPEEDR[21:20]=00
GPIOE->OSPEEDR &= (~(0x3<<20));
//设置PE10引脚禁止上下拉PUPDR[21:20]=00
GPIOE->PUPDR &= (~(0x3<<20));
}
//LED1点亮
void led1_on(void)
{
//设置PE10引脚输出高电平ODDR[10]=1
GPIOE->ODR|=(0x1<<10);
}
//LED1熄灭,第十位设置1
void led1_off(void)
{
GPIOE->ODR &= (~(0x1<<10));
}
//灯2初始化PF10
void led2_init(void)
{
// 设置GPIOE使能0x50000A28[5]=1
RCC->MP_AHB4ENSETR |=(0x1<<5);
//GPIOF_MODER[21:20] = 01--设置PF10引脚为输出模式
GPIOF->MODER &= (~(0x3<<20));
GPIOF->MODER |=(0x1<<20);
//设置PF10引脚为推挽模式OTYPER[10]=0
GPIOF->OTYPER &= (~(0x1<<10));
//设置PF10引脚为低速输出模式OSPEEDR[21:20]=00
GPIOF->OSPEEDR &= (~(0x3<<20));
//设置PF10引脚禁止上下拉PUPDR[21:20]=00
GPIOF->PUPDR &= (~(0x3<<20));
}
//LED2点亮
void led2_on(void)
{
//设置PE10引脚输出高电平ODDR[10]=1
GPIOF->ODR|=(0x1<<10);
}
//LED2熄灭,第十位设置0,输出低电平,灯熄灭
void led2_off(void)
{
GPIOF->ODR &= (~(0x1<<10));
}
//LED3初始化PE8
void led3_init(void)
{
// 设置GPIOE使能0x50000A28[4]=1
RCC->MP_AHB4ENSETR |=(0x1<<4);
//设置PE8引脚为输出模式MODER[16:17]=01
GPIOE->MODER &=(~(0x3<<16)); //清零
GPIOE->MODER |=(0x1<<16);
//设置PE10引脚为推挽模式OTYPER[8]=0
GPIOE->OTYPER &= (~(0x1<<8));
//设置PE10引脚为低速输出模式OSPEEDR[16:17]=00
GPIOE->OSPEEDR &= (~(0x3<<16));
//设置PE10引脚禁止上下拉PUPDR[16:17]=00
GPIOE->PUPDR &= (~(0x3<<16));
}
//LED3点亮
void led3_on(void)
{
//设置PE8引脚输出高电平ODDR[8]=1灯点亮状态
GPIOE->ODR|=(0x1<<8);
}
//LED3熄灭,第8位引脚为输出低电平,灯熄灭
void led3_off(void)
{
GPIOE->ODR &= (~(0x1<<8));
}
//初始化串口;接收RD--PB2;发送TD--PG11
void uart_init()
{
//RCC使能GPIOB,GPIOG控制器
RCC->MP_AHB4ENSETR |= (0x1<<1);
RCC->MP_AHB4ENSETR |= (0x1<<6);
//RCC使能UART控制器
RCC->MP_APB1ENSETR |= (0x1<<16);
/******GPIO使能********/
//设置GPIOB---PB2引脚为复用功能
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |= (0x1<<5);
//设置GPIOG---PG11引脚为复用功能
GPIOG->MODER &= (~(0x3<<22));
GPIOG->MODER |= (0x1<<23);
//设置复用的功能为串口
GPIOB->AFRL &= (~(0xf<<8));
GPIOB->AFRL |= (0x1<<11); //AF8:1000
GPIOG->AFRH &= (~(0xf<<12));
GPIOG->AFRH |= (0x3<<13); //AF6:0110
/********UART4异步*********/
//设置数据宽度8bit传输[12][28]=00
USART4->CR1 &= (~(0x1<<12));
USART4->CR1 &= (~(0x1<<28));
//设置串口16倍采样率
USART4->CR1 &= (~(0x1<<15));
//设置串口无奇偶校验
USART4->CR1 &= (~(0x1<<10));
//设置串口发送数据寄存器使能
USART4->CR1 |= (0x1<<3);
//设置串口接收数据寄存器使能
USART4->CR1 |= (0x1<<2);
//设置串口使能*******************先判断似能,0禁用
USART4->CR1 |= (0x1<<0);
/***************/
//设置串口停止位为1位
USART4->CR2 &= (~(0x3<<12));
//设置串口波特率BRR115200
//系统提供的时钟源64MHZ*10^6/115200=0x22b
USART4->BRR = 0x22b;
//设置串口分频
USART4->PRESC &= (~0xf);
}
//发送一个字符
void put_char(const char ch)
{
//判断发送服务器状态位
//当发送寄存器发完当前的才能发下一个数据[7]=1可以继续发
while(!(USART4->ISR &(0x1<<7))); //!=1跳出循环
//将数据放到发送寄存器
USART4->TDR =ch;
//判断发送数据是否完成:[6]=0未完成,则一直发
while((USART4->ISR &(0x1<<6))==0);
}
//发送字符串
void put_string(const char *str)
{
while(*str)
{
//一个一个字符的发送
put_char(*str++);
put_char('\n');
//换行
put_char('\r'); //光标回到行首
}
}
//接收字符
char get_char()
{
//判断接收是否有数据,没有则等待
//[5]=0未收到数据
while((USART4->ISR &(0x1<<5))==0);
//收到的字符+1返回,收到a返回b
return USART4->RDR;
}
//接收字符串
char buf[30]="";
char *get_string()
{
//一个一个的接收
int i = 0;
//当收到字符串,则返回字符串的地址
while((buf[i]=get_char())!='\r')
{
put_char(buf[i]);
i++;
}
buf[i]='\0';
//终端换行
put_char('\n');
return buf;
}
测试代码:
#include "gpio.h"
//外部引用printf函数
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()
{
//定义数组存放用户输入的字符串首地址
char *str = 0;
//定义结构体指针存放查找到的结构体成员地址
cmd_t* cmd_string = 0;
while(1)
{
printf("\rplease input>");
str=get_string();
//调用查找比较函数,
cmd_string = find_command(str);
if(cmd_string == 0)
{
printf("command is not find\n");
}
else
{
//找到了就执行命令,调用灯的控制函数(此函数为结构体成员)
cmd_string->leds_status_t(cmd_string->led,cmd_string->status);
}
}
return 0;
}