stm32实现按键控制,短按控制灯开关,长按控制灯亮暗
时间: 2023-05-12 11:03:52 浏览: 852
5、POLLING-按键.zip_stm32 按键_按键灯
可以使用定时器来实现按键的检测,当检测到按键按下时,可以通过判断按键按下的时间来确定是短按还是长按。对于短按,可以直接控制灯的开关;对于长按,可以通过改变灯的亮度来实现灯的亮暗控制。具体实现可以参考以下代码:
```
#include "stm32f10x.h"
#define KEY_GPIO_PORT GPIOA
#define KEY_GPIO_PIN GPIO_Pin_0
#define LED_GPIO_PORT GPIOB
#define LED_GPIO_PIN GPIO_Pin_5
#define SHORT_PRESS_TIME 20 // 短按时间,单位为ms
#define LONG_PRESS_TIME 1000 // 长按时间,单位为ms
void GPIO_Configuration(void);
void TIM_Configuration(void);
int main(void)
{
GPIO_Configuration();
TIM_Configuration();
while (1)
{
// 等待按键按下
while (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == 1);
// 开始计时
TIM_Cmd(TIM2, ENABLE);
// 等待按键松开
while (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == 0);
// 停止计时
TIM_Cmd(TIM2, DISABLE);
// 判断按键按下的时间
if (TIM_GetCounter(TIM2) < SHORT_PRESS_TIME)
{
// 短按控制灯开关
GPIO_WriteBit(LED_GPIO_PORT, LED_GPIO_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(LED_GPIO_PORT, LED_GPIO_PIN)));
}
else if (TIM_GetCounter(TIM2) >= LONG_PRESS_TIME)
{
// 长按控制灯亮暗
GPIO_WriteBit(LED_GPIO_PORT, LED_GPIO_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(LED_GPIO_PORT, LED_GPIO_PIN)));
TIM_SetCompare1(TIM3, TIM_GetCapture1(TIM3) + 100); // 增加灯的亮度
}
// 复位计时器
TIM_SetCounter(TIM2, 0);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOA和GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置KEY_GPIO_PIN为输入模式
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
// 配置LED_GPIO_PIN为输出模式
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// 使能TIM2和TIM3时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
// 配置TIM2为计数模式,时钟频率为72MHz
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 配置TIM3为PWM模式,时钟频率为72MHz
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// 配置TIM3的通道1为PWM输出模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// 使能TIM3的通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
// 启动TIM2和TIM3
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
```
对于lua closure factory 完整代码的问题,可以参考以下代码:
```
function createClosure(x)
return function(y)
return x + y
end
end
closure1 = createClosure(1)
closure2 = createClosure(2)
print(closure1(2)) -- 输出3
print(closure2(2)) -- 输出4
```
对于中文加密的问题,可以使用简单的替换加密算法,将明文中的每个中文字符替换成另一个中文字符。具体实现可以参考以下代码:
```
function encrypt(str)
local result = ""
for i = 1, #str do
local c = string.sub(str, i, i)
if c >= "一" and c <= "龥" then -- 判断是否为中文字符
c = string.char(string.byte(c) + 1) -- 替换为下一个中文字符
end
result = result .. c
end
return result
end
function decrypt(str)
local result = ""
for i = 1, #str do
local c = string.sub(str, i, i)
if c >= "一" and c <= "龥" then -- 判断是否为中文字符
c = string.char(string.byte(c) - 1) -- 替换为上一个中文字符
end
result = result .. c
end
return result
end
plaintext = "这是一段明文"
ciphertext = encrypt(plaintext)
print(ciphertext) -- 输出"這是丁段明文"
print(decrypt(ciphertext)) -- 输出"这是一段明文"
```
阅读全文