一,续上篇文章,请参阅【MM32 eMiniBoard】+基于RT-Thread Nano移植+点灯,其中大部分代码是用上篇文章的代码内容,在这篇中主要使用uart.c及key.c内容,详细测试过程请阅览以下篇幅内容。
二,【MM32 eMiniBoard】按键,UART与MCU对应关系及按键原理图
1,MM32 eMiniBoard开发板编号为MB-021,查找官网给出的开发板资料。找出按键与MCU对应关系的表格如下:
2,UART串口对应MCU关系为:
3,按键原理图为如下,从原理图中看出,K1是为高电平有效,K2,K3,K4为低电平有效。
三,测试功能说明。
mcu初始化后,首先创建并启动按键线程,K1控制LED1开启,K2控制LED2开启,K3控制LED3开启,K4控制LED1,LED2,LED3关闭。并且由UART打印相关LED内容,例如:K1按下使能LED1,并打印“LED1 ON!”。按下K4则关闭LED1,并打印"LED1 OFF!"。只要是打开哪个通道的LED,关闭时会对应关闭已打开通道的LED。
四,代码编写并验证。
1,key.c对应的代码。
#include "key.h"#define KEY_DOWN_CHECK_COUNT_VALUE 2 //按键按下检测次数。 #define DELAY_TIME 1 //延时 void KEY_Init(void){ //使能端口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //GPIOB //设置端口参数 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //key2,key3,key4上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_10|GPIO_Pin_11; //key2,3,4 GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //key1 下接输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB,&GPIO_InitStructure); //设置电平 GPIO_ResetBits(GPIOB,GPIO_Pin_1); //KEY1 设置低电平 GPIO_SetBits(GPIOB,GPIO_Pin_2); //KEY2 设置高电平 GPIO_SetBits(GPIOB,GPIO_Pin_10);//KEY3 设置高电平 GPIO_SetBits(GPIOB,GPIO_Pin_11); //KEY4 设置高电平 } rt_uint8_t KEY_GET(void){ rt_uint8_t key_value = 0xff; rt_uint8_t forcount = 0; rt_uint8_t key_down_forcheck_count = 0; rt_uint8_t key_down_count = 0; for(forcount = 0;forcount < 4;forcount++){ key_down_count = 0; switch(forcount){ case KEY1: for(key_down_forcheck_count = 0;key_down_forcheck_count < KEY_DOWN_CHECK_COUNT_VALUE;key_down_forcheck_count++){ rt_thread_mdelay(DELAY_TIME); //延时防抖动 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 1){ key_down_count++; } } //检查次数与确认次数一致才能算有效 if(key_down_count == KEY_DOWN_CHECK_COUNT_VALUE){ return key_value = KEY1; } break; case KEY2: for(key_down_forcheck_count = 0;key_down_forcheck_count < KEY_DOWN_CHECK_COUNT_VALUE;key_down_forcheck_count++){ rt_thread_mdelay(DELAY_TIME); if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2) == 0){ key_down_count++; } } if(key_down_count == KEY_DOWN_CHECK_COUNT_VALUE){ return key_value = KEY2; } break; case KEY3: for(key_down_forcheck_count = 0;key_down_forcheck_count < KEY_DOWN_CHECK_COUNT_VALUE;key_down_forcheck_count++){ rt_thread_mdelay(DELAY_TIME); if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0){ key_down_count++; } } if(key_down_count == KEY_DOWN_CHECK_COUNT_VALUE){ return key_value = KEY3; } break; case KEY4: for(key_down_forcheck_count = 0;key_down_forcheck_count < KEY_DOWN_CHECK_COUNT_VALUE;key_down_forcheck_count++){ rt_thread_mdelay(DELAY_TIME); if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0){ key_down_count++; } } if(key_down_count == KEY_DOWN_CHECK_COUNT_VALUE){ return key_value = KEY4; } break; } } return key_value; }
复制代码#ifndef __KEY_H#define __KEY_H #include "HAL_gpio.h" #include "rtthread.h" enum KEY_ENUM_LIST{ KEY1 = 0, KEY2, KEY3, KEY4, }; void KEY_Init(void); rt_uint8_t KEY_GET(void); #endif
复制代码#include "uart.h"void uart_init(uint32_t bound){ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStructure; UART_InitTypeDef UART_InitSrtucture; /*uart2 tx-pa2*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOA,&GPIO_InitStructure); /*UART2 RX_PA3*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA,&GPIO_InitStructure); UART_InitSrtucture.UART_BaudRate = bound; UART_InitSrtucture.UART_WordLength = UART_WordLength_8b; UART_InitSrtucture.UART_StopBits = UART_StopBits_1; UART_InitSrtucture.UART_Parity = UART_Parity_No; UART_InitSrtucture.UART_HardwareFlowControl = UART_HardwareFlowControl_None; UART_InitSrtucture.UART_Mode = UART_Mode_Rx|UART_Mode_Tx; UART_Init(UART2,&UART_InitSrtucture); UART_ITConfig(UART2,UART_IT_RXIEN,ENABLE); UART_Cmd(UART2,ENABLE); } /******************************************************************************************************** **函数信息 :void UartSendByte(u8 dat) **功能描述 :UART发送数据 **输入参数 :dat **输出参数 : ** 备注 : ********************************************************************************************************/ void UartSendByte(uint8_t dat){ UART_SendData( UART2, dat); while(!UART_GetFlagStatus(UART2,UART_FLAG_TXEPT)); } /******************************************************************************************************** **函数信息 :void UartContinuousSend(u8 dat[]) **功能描述 :UART连续发送数据 **输入参数 :dat[],size **输出参数 : ** 备注 : ********************************************************************************************************/ void UartContinuousSend(u8 dat[],u16 size){ u16 forx = 0; for(forx = 0;forx < (size -1);forx++){ UartSendByte(dat[forx]); } }
复制代码#include "HAL_device.h" // Device header#include "rtthread.h" // RealThread.RT-Thread::RTOS:kernel #include "led.h" #include "uart.h" #include "delay.h" #include "key.h" #define THREAD_PRIORITY 15 //线程优先级 #define THREAD_STACK_SIZE 512 //线程堆大小 #define THREAD_TIMESLICE 10 //线程片时间 static rt_thread_t led1_thread = RT_NULL; static rt_thread_t led2_thread = RT_NULL; static rt_thread_t led3_thread = RT_NULL; static rt_thread_t key_thread = RT_NULL; static void led1_thread_entry(void *paramenter){ static rt_bool_t flag;// = RT_TRUE; while(1){ flag = ~flag; LEDx_SHOW(LED1,flag); rt_thread_mdelay(100); } } static void led2_thread_entry(void *paramenter){ static rt_bool_t flag; while(1){ flag = ~flag; LEDx_SHOW(LED2,flag); rt_thread_mdelay(100); } } static void led3_thread_entry(void *paramenter){ static rt_bool_t flag; while(1){ flag = ~flag; LEDx_SHOW(LED3,flag); rt_thread_mdelay(100); } } /***************** * key1控制led1线程开 * key2控制led2线程开 * key3控制led3线程开 * key4控制所有LED线程关 ******************/ void key_thread_entry(void *parameter){ u8 led1_on_array[] = "LED1 ON!\n"; u8 led2_on_array[] = "LED2 ON!\n"; u8 led3_on_array[] = "LED3 ON!\n"; u8 led1_off_array[] = "LED1 OFF!\n"; u8 led2_off_array[] = "LED2 OFF!\n" ; u8 led3_off_array[] = "LED3 OFF!\n"; while(1){ //key1 if(KEY_GET() == KEY1){ if(led1_thread == RT_NULL){ led1_thread = rt_thread_create("led1_thread", led1_thread_entry,RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); //创建线程 if(led1_thread != RT_NULL){ //打印led1信息 UartContinuousSend(led1_on_array,sizeof(led1_on_array)); rt_thread_startup(led1_thread);//启动线程 } } } //key2 if(KEY_GET() == KEY2){ if(led2_thread == RT_NULL){ led2_thread = rt_thread_create("led2_thread", led2_thread_entry,RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); //创建线程 if(led2_thread != RT_NULL){ //打印led2信息 UartContinuousSend(led2_on_array,sizeof(led2_on_array)); rt_thread_startup(led2_thread); //启动线程 } } } //key3 if(KEY_GET() == KEY3){ if(led3_thread == RT_NULL){ led3_thread = rt_thread_create("led3_thread", led3_thread_entry,RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY + 2, THREAD_TIMESLICE); //创建线程 if(led3_thread != RT_NULL){ //打印led3信息 UartContinuousSend(led3_on_array,sizeof(led3_on_array)); rt_thread_startup(led3_thread); //启动线程 } } } //key4 if(KEY_GET() == KEY4){ //led1_thread不为空,则关闭线程 if(led1_thread != RT_NULL){ rt_thread_delete(led1_thread); led1_thread = RT_NULL;; LEDx_SHOW(LED1,RT_FALSE); rt_thread_idle_excute();//释放线程资源 //打印led1信息 UartContinuousSend(led1_off_array,sizeof(led1_off_array)); } //led2_thread不为空,则关闭线程 if(led2_thread != RT_NULL){ rt_thread_delete(led2_thread); led2_thread = RT_NULL;; LEDx_SHOW(LED2,RT_FALSE); rt_thread_idle_excute();//释放线程资源 //打印led3信息 UartContinuousSend(led2_off_array,sizeof(led2_off_array)); } //led3_thread不为空,则关闭线程 if(led3_thread != RT_NULL){ rt_thread_delete(led3_thread); led3_thread = RT_NULL;; LEDx_SHOW(LED3,RT_FALSE); rt_thread_idle_excute();//释放线程资源 //打印led3信息 UartContinuousSend(led3_off_array,sizeof(led3_off_array)); } } } } int key_thread_init(void){ //创建key线程并启动 key_thread = rt_thread_create("key_thread", key_thread_entry,RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY -1, THREAD_TIMESLICE); if(key_thread != RT_NULL){ rt_thread_startup(key_thread); } return 0; } int main(void){ LED_Init(); KEY_Init(); key_thread_init(); //按键线程初始化 uart_init(115200); //uart带参数初始化 while(1){ } }
复制代码五,代码打包。
热门资料
热门活动
全部回复 0
暂无评论,快来抢沙发吧