实验要求:串口输入相应的命令,控制对应的硬件进行工作
例如:
在串口工具输入led1on----->板子led1点亮
在串口工具输入led1off----->板子led1熄灭
在串口工具输入led2on----->板子led2点亮
在串口工具输入led2off----->板子led2熄灭
在串口工具输入led3on----->板子led3点亮
在串口工具输入led3off----->板子led3熄灭
测试代码
- #include "port.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_led_init();
-
- cmd_t* cmd_string = 0;
-
- while(1)
- {
- cmd_string = find_command(get_string());
- if(cmd_string == 0)
- {
- printf("command is not find");
- }
- else
- {
- cmd_string->leds_status_t(cmd_string->led,cmd_string->status);
- }
- }
-
- return 0;
- }
- #include "port.h"
-
- char data[40];
- void hal_led_init()
- {
- RCC->MP_AHB4ENSETR |= (0x1<<4);
- RCC->MP_AHB4ENSETR |= (0x1<<5);
- RCC->MP_AHB4ENSETR |= (0x1<<1);
- RCC->MP_AHB4ENSETR |= (0x1<<6);
- RCC->MP_APB1ENSETR |= (0x1<<16);
-
- GPIOE->MODER &=(~(0x3<<20));
- GPIOE->MODER |=(0x1<<20);
- GPIOF->MODER &=(~(0x3<<20));
- GPIOF->MODER |=(0x1<<20);
- GPIOE->MODER &=(~(0x3<<16));
- GPIOE->MODER |=(0x1<<16);
- GPIOB->MODER &=(~(0x3<<4));
- GPIOB->MODER |=(0x1<<5);
- GPIOG->MODER &=(~(0x3<<22));
- GPIOG->MODER |=(0x1<<23);
-
- GPIOE->OTYPER &=(~(0x1<<10));
- GPIOF->OTYPER &=(~(0x1<<10));
- GPIOE->OTYPER &=(~(0x1<<8));
- GPIOE->OSPEEDR &=(~(0x3<<20));
- GPIOE->OSPEEDR &=(~(0x3<<16));
- GPIOF->OSPEEDR &=(~(0x3<<20));
- GPIOE->PUPDR &=(~(0x3<<20));
- GPIOE->PUPDR &=(~(0x3<<16));
- GPIOF->PUPDR &=(~(0x3<<20));
-
- GPIOB->AFRL &=(~(0xf<<8));
- GPIOB->AFRL |=(0x1<<11);
- GPIOG->AFRH &=(~(0xf<<12));
- GPIOG->AFRH |=(0x3<<13);
-
- /***********UART4章节初始化****/
- //0需要判断串口UE位是否使能
- if(USART4->CR1 & 0x1)
- {
- delay_ms(500);
- USART4->CR1 &= (~(0x1<<0));
- }
- //1、设置UART4串口8位数据位,无奇偶校验位,CR1[28][12]
- USART4->CR1 &=(~(0x1<<28));
- USART4->CR1 &=(~(0x1<<12));
- USART4->CR1 &=(~(0x1<<10));
- //2、设置uart4串口1为停止位
- USART4->CR2 &=(~(0x3<<12));
- //3、设置UART4串口16倍采样率
- USART4->CR1 &=(~(0x1<<15));
- //4、设置UART4串口不分频
- USART4->PRESC &=(~(0xf));
- //5、设置UART4波特率为115200
- USART4->BRR =0x22b;
- //6、设置UART4串口接收器/发送器使能
- USART4->CR1 |=(0x1<<3);
- USART4->CR1 |=(0x1<<2);
- //7、设置UART4串口使能
- USART4->CR1 |=(0x1);
-
-
- }
- void led_status(led_t led,status_t status)
- {
- switch(led)
- {
- case LED1:
- if(status == LED_ON)
- {
- GPIOE->ODR |=(0x1<<10);
- printf("led1点亮\n");
- }
- else
- {
- GPIOE->ODR &=(~(0x1<<10));
- printf("led1熄灭\n");
- }
- break;
- case LED2:
- if(status == LED_ON)
- {
- GPIOF->ODR|=(0x1<<10);
- printf("led2点亮\n");
- }
- else
- {
- GPIOF->ODR&=(~(0x1<<10));
- printf("led2熄灭\n");
- }
- break;
- case LED3:
- if(status == LED_ON)
- {
- GPIOE->ODR |=(0x1<<8);
- printf("led3点亮\n");
- }
- else {
- GPIOE->ODR &=(~(0x1<<8));
- printf("led3熄灭\n");
- }
-
- break;
- }
- }
-
- 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
- {
- if(!my_strcmp(str,cmd_arr[i].cmd_arr))
- {
- return &cmd_arr[i];
- }
- }
- return 0;
- }
- //比较函数
- int my_strcmp(const char *s1,const char *s2)
- {
- int result;
- while(*s1==*s2 &&*s1!='\0' &&*s2!='\0')
- {
- s1++;
- s2++;
-
- }
- result = *s1-*s2;
- return result;
- }
- //发送一个字符
- void put_char(const char ch)
- {
- //判断发送寄存器状态位
- while(!(USART4->ISR&(0x1<<7)));
- USART4->TDR = ch;
- //判断发送数据是否完成
- while(!(USART4->ISR&(0x1<<6)));
-
- }
- //发送一个字符串
- void put_string(const char* str)
- {
- while(*str)
- {
- put_char(*str++);
- }
- put_char('\n');
- put_char('\r');
-
- }
- //接收一个字符
- char get_char()
- {
- //判断接收数据是否有数据
- char ch;
- while(!(USART4->ISR&(0x1<<5)));
- ch = USART4->RDR;
- return ch;
-
- }
- //接收一个字符串
- char* get_string()
- {
- int i=0;
- for(i=0;i<sizeof(data)-1;i++)
- {
- data[i]=get_char();
- put_char(data[i]);
- if(data[i]=='\r')
- {
- break;
- }
- }
- data[i]='\0';
- put_char('\n');
- return data;
-
- }
- #ifndef __PORT_H__
- #define __PORT_H__
- #include "../common/include/stm32mp1xx_gpio.h"
- #include "../common/include/stm32mp1xx_rcc.h"
- #include "../common/include/stm32mp1xx_uart.h"
- #define MAX 6
- typedef enum
- {
- LED1=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;
- //初始化
- void hal_led_init();
- //比较函数
- //发送一个字符
- void put_char(const char ch);
- //发送一个字符串
- void put_string(const char* string);
- //接收一个字符
- char get_char();
-
- //接收一个字符串
- char *get_string();
- cmd_t* find_command(const char* str);
- void led_status(led_t led,status_t status);
-
- int my_strcmp(const char *s1,const char *s2);
- extern void printf(const char *fmt, ...);
- void delay_ms(int ms);
- #endif
