燃油热风锅炉是典型的嵌入式设备,根据所学嵌入式知识,完 成如下应用功能编程,并用嵌入式开发板验证是否正确: 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-25 15:58:01 浏览: 64
这是一个热风控制系统嵌入式项目,基于STM32F1芯片和RT-Thread实时系统开发出温度闭环控制和风速控制.zip
这是一个很好的嵌入式开发问题,我可以为您提供参考代码,但是需要知道您使用的开发板型号和开发环境。
下面是一个基于STM32F407开发板的示例代码,使用Keil MDK作为开发环境:
```c
#include "stm32f4xx.h"
#define LED_RED_PIN GPIO_Pin_12
#define LED_GREEN_PIN GPIO_Pin_13
#define OIL_PUMP_PIN GPIO_Pin_10
#define FAN_PIN GPIO_Pin_11
void init_gpio(void);
void delay(uint32_t time);
void feed_dog(void);
int main(void)
{
init_gpio();
// 系统待机
GPIO_SetBits(GPIOD, LED_RED_PIN);
while(1)
{
feed_dog(); // 喂狗
}
}
void init_gpio(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIO时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// 配置PD12和PD13为输出模式
GPIO_InitStructure.GPIO_Pin = LED_RED_PIN | LED_GREEN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// 配置PB10和PB11为输出模式
GPIO_InitStructure.GPIO_Pin = OIL_PUMP_PIN | FAN_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置PA0为输入模式,用于外部中断触发看门狗喂狗
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置外部中断0
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_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);
// 配置看门狗
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_32); // 分频系数为32
IWDG_SetReload(0xfff); // 设置计数器重装值为0xfff,约为5s
IWDG_ReloadCounter();
IWDG_Enable();
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET) // 判断是否为PA0的中断
{
feed_dog(); // 喂狗
EXTI_ClearITPendingBit(EXTI_Line0); // 清除中断标志位
}
}
void delay(uint32_t time)
{
uint32_t i;
for(i=0; i<time; i++);
}
void feed_dog(void)
{
IWDG_ReloadCounter();
}
void set_oil_pump(uint8_t level)
{
uint8_t i;
for(i=0; i<level; i++)
{
GPIO_SetBits(GPIOB, OIL_PUMP_PIN);
delay(100000);
GPIO_ResetBits(GPIOB, OIL_PUMP_PIN);
delay(100000);
}
}
void set_fan(uint8_t duty)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置TIM4时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// 配置TIM4为PWM模式,频率为20kHz
TIM_TimeBaseStructure.TIM_Period = 839; // 时钟周期为1us,所以计数器为840-1,频率为1MHz/840=1.19kHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// 配置TIM4通道2为PWM输出
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (duty*839)/100; // 占空比为duty%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE);
}
void start(void)
{
GPIO_ResetBits(GPIOD, LED_RED_PIN);
GPIO_SetBits(GPIOD, LED_GREEN_PIN);
}
int main(void)
{
init_gpio();
while(1)
{
feed_dog(); // 喂狗
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == RESET) // 油泵调节按键
{
static uint8_t level = 1;
level++;
if(level > 10)
{
level = 1;
}
set_oil_pump(level);
}
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == RESET) // 风机启动按键
{
set_fan(50); // 占空比50%
}
}
}
```
以上代码仅提供参考,具体实现还需要根据您的开发板型号和开发环境进行相应的调整和修改。
阅读全文