1.实验要求:串口输入对应的命令,通过对应的命令,控制硬件工作 例子: 在串口工具输入led1on -------->硬件LED1点亮 在串口工具输入led1off -------->硬件LED1熄灭 在串口工具输入led2on -------->硬件LED2点亮 在串口工具输入led2off -------->硬件LED2熄灭 在串口工具输入led3on -------->硬件LED3点亮 在串口工具输入led3off -------->硬件LED3熄灭
#include "uart4.h"
#include "gpio.h"
#include "stdio.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++);
}
extern char buffer[LEN];
void led_init()
{
//RCC寄存器进行初始化
RCC_AHB4_ENSETR |= (0x3 << 4);
//GPIO结构体初始化
gpio_init_t init = {Output,Pp,Low,NO_pupd};
//对PE10引脚初始化
hal_gpio_init(GPIOE,&init,GPIO_PIN_10);
//对PF10引脚初始化
hal_gpio_init(GPIOF,&init,GPIO_PIN_10);
//对PE8引脚初始化
hal_gpio_init(GPIOE,&init,GPIO_PIN_8);
}
int main()
{
char *str;
cmd_t* p;
led_init();
hal_uart_init();
hal_put_string("usart4 test!!!!\n");
while(1)
{
hal_put_string("please input control command > ");
str = get_string(buffer);
p = find_command(str);
if(p == 0)
{
printf("command not found!\n");
}
else
{
p->gpio_write_p(p->gpio,p->pin,p->status);
}
}
return 0;
}
#ifndef __STDIO_H__
#define __STDIO_H__
#include "./gpio.h"
#define MAX 6
typedef struct{
char* cmd_arr;//命令字符串
gpio_t* gpio;//gpio组号
unsigned int pin;//对应引脚编号
gpio_status_t status; //LED灯状态
void (*gpio_write_p)(gpio_t* gpio,unsigned int pin,gpio_status_t status);
}cmd_t;
//查找输入命令与字符串比较的函数
cmd_t* find_command(const char* str);
//自己编写的strcmp
int my_strcmp (const char *s1, const char *s2);
#endif
#include "stdio.h"
#include "gpio.h"
cmd_t cmd[MAX] ={
[0] = {
.cmd_arr = "led1on",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[1] = {
.cmd_arr = "led1off",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
},
[2]= {
.cmd_arr = "led2on",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[3]= {
.cmd_arr = "led2off",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
},
[4]= {
.cmd_arr = "led3on",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[5]= {
.cmd_arr = "led3off",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
}
};
//查找输入命令与字符串比较的函数
cmd_t* find_command(const char* str)
{
int i=0;
for(i=0;i<MAX;i++)
{
if((my_strcmp(cmd[i].cmd_arr,str))==0)
{
return &cmd[i];
}
}
return 0;
}
int my_strcmp (const char *s1, const char *s2)
{
while (*s1 != '\0')
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
}
return (*s1 - *s2);\
}