完 成如下应用功能编程,并用嵌入式开发板验证是否正确: 1系统待机时,LED红灯常亮,其它功能均不工作。 2按下启动按键key0,设备启动,LED绿灯常亮,红灯灭。 3按下油泵当位调节按键key1,可模拟调节油泵档位,按一下油 泵输出一档(高电平占比10%),按两下油泵输出2档(高电平占比20% 按三下油泵输出3档(高电平占比30%),直到按+下油泵输出10档(高 电平占比20%),再次按下按键时油泵恢复到1档(高电平占比10%);(注:油泵控制采用引脚PB10) 4按下风机启动按键key2时,引脚PB11控制风机以50%占空比吹 风; 5为防止系统意外跑飞,设置5S定时看门狗,利用按键key_up触 发外部中断0,在外部中断0中实现看门狗喂狗。
时间: 2024-02-26 21:53:32 浏览: 112
通过开发板上的按键控制LED灯
好的,这是一道嵌入式系统的题目,需要用到开发板和编程语言来实现。
以下是参考代码,假设开发板使用的是STM32F103C8T6:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "misc.h"
#define LED_RED GPIO_Pin_13
#define LED_GREEN GPIO_Pin_14
#define KEY0 GPIO_Pin_0
#define KEY1 GPIO_Pin_1
#define KEY2 GPIO_Pin_2
#define OIL_PUMP GPIO_Pin_10
#define FAN GPIO_Pin_11
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LED_RED | LED_GREEN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY0 | KEY1 | KEY2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = OIL_PUMP | FAN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM3_PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
IWDG_ReloadCounter();
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM3_PWM_Init();
EXTI_Configuration();
TIM_SetCompare1(TIM3, 100);
GPIO_SetBits(GPIOB, LED_RED);
while (1)
{
if (GPIO_ReadInputDataBit(GPIOA, KEY0) == 0)
{
GPIO_ResetBits(GPIOB, LED_RED);
GPIO_SetBits(GPIOB, LED_GREEN);
}
else
{
GPIO_SetBits(GPIOB, LED_RED);
GPIO_ResetBits(GPIOB, LED_GREEN);
}
if (GPIO_ReadInputDataBit(GPIOA, KEY1) == 0)
{
static uint8_t oil_pump_state = 0;
oil_pump_state++;
if (oil_pump_state > 10)
{
oil_pump_state = 1;
}
uint16_t pulse_width = (uint16_t)(oil_pump_state * 100);
TIM_SetCompare1(TIM3, pulse_width);
Delay(1000000);
}
if (GPIO_ReadInputDataBit(GPIOA, KEY2) == 0)
{
GPIO_SetBits(GPIOB, FAN);
}
else
{
GPIO_ResetBits(GPIOB, FAN);
}
Delay(1000000);
}
}
```
请注意,这只是参考代码,您需要根据自己的开发板和需求来进行修改和适配。另外,如果您不熟悉嵌入式系统和编程,建议您先学习相关知识再进行实现。
阅读全文