[单片机]单片机裸机程序开发方式

在不使用操作系统的单片机上,程序一般有以下几种常见的编写方式:

1. 轮询方式

轮询是一种简单直接的程序运行方式。主程序会不断地循环执行任务,每个任务按固定顺序依次被调用。这种方式通常用于任务少、对响应速度要求不高的应用场景。

  • 优点:简单易实现,占用资源少。
  • 缺点:不利于实时响应,可能造成较长的响应时间。
/*通过轮询按键状态来控制LED的开关*/

#include "stm32f10x.h"

#define LED_PIN GPIO_Pin_0
#define BUTTON_PIN GPIO_Pin_1

void setup()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

int main()
{
    setup();

    while (1)
    {
        if (GPIO_ReadInputDataBit(GPIOA, BUTTON_PIN) == Bit_RESET) // 按键按下
        {
            GPIO_SetBits(GPIOA, LED_PIN); // 打开LED
        }
        else
        {
            GPIO_ResetBits(GPIOA, LED_PIN); // 关闭LED
        }
    }
}

2. 中断方式

中断方式是指当某个特定事件发生(如按键按下、传感器信号变化、定时器溢出)时,单片机会暂时中断当前任务,转去执行中断服务程序 (ISR)。完成后,再返回主程序。

  • 优点:适用于对实时性要求高的任务,能快速响应外部事件。
  • 缺点:中断较多时可能引发响应优先级的问题,影响系统稳定性。
/*按键触发外部中断,切换LED状态 配置按键引脚为外部中断在中断处理函数中控制LED的状态*/
#include "stm32f10x.h"

#define LED_PIN GPIO_Pin_0
#define BUTTON_PIN GPIO_Pin_1

void setup()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 配置外部中断
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line = EXTI_Line1;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    // 配置中断优先级
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void EXTI1_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line1) != RESET)
    {
        GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, LED_PIN))); // 切换LED状态
        EXTI_ClearITPendingBit(EXTI_Line1);
    }
}

int main()
{
    setup();

    while (1)
    {
        // 主循环空转
    }
}

3. 状态机方式

状态机是一种基于不同状态和条件的程序结构。程序在执行过程中会根据输入或事件切换到不同的状态,每个状态完成特定任务。这种方式适用于逻辑较复杂的应用。

  • 优点:结构清晰,有利于控制复杂逻辑。
  • 缺点:实现过程相对复杂,需要清晰的状态转移设计。
/*使用状态机控制LED的亮度,通过按键在不同状态之间切换*/
#include "stm32f10x.h"

#define LED_PIN GPIO_Pin_0
#define BUTTON_PIN GPIO_Pin_1

typedef enum { LED_OFF, LED_DIM, LED_MEDIUM, LED_BRIGHT } LED_State;
LED_State ledState = LED_OFF;

void setup()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void updateLED(LED_State state)
{
    switch (state)
    {
    case LED_OFF:
        GPIO_ResetBits(GPIOA, LED_PIN);
        break;
    case LED_DIM:
        GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, LED_PIN)));
        for (int i = 0; i < 200000; i++); // 微亮延迟
        break;
    case LED_MEDIUM:
        GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, LED_PIN)));
        for (int i = 0; i < 100000; i++); // 中亮延迟
        break;
    case LED_BRIGHT:
        GPIO_SetBits(GPIOA, LED_PIN); // 最亮
        break;
    }
}

int main()
{
    setup();

    while (1)
    {
        if (GPIO_ReadInputDataBit(GPIOA, BUTTON_PIN) == Bit_RESET) // 按键按下
        {
            for (int i = 0; i < 50000; i++); // 去抖延迟
            ledState = (ledState + 1) % 4; // 切换到下一个状态
        }
        
        updateLED(ledState);
    }
}

4. 定时器+事件驱动方式

定时器定时触发,用于检查某些条件是否满足或执行某些任务。结合事件驱动机制,定时器可定期激活某些事件,实现更灵活的任务调度。

  • 优点:适合实现简单的多任务系统,可以保证一定的实时性。
  • 缺点:不适用于任务繁重、复杂的系统。
    /使用定时器中断每秒切换LED状态 配置定时器为1秒周期 在定时器中断函数中切换LED状态/
#include "stm32f10x.h"

#define LED_PIN GPIO_Pin_0

void setup()
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; // 1秒周期
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM2, ENABLE);
}

void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, LED_PIN))); // 切换LED状态
    }
}

int main()
{
    setup();

    while (1)
    {
        // 主循环空转
    }
}

5. 前后台系统方式

这种方式将系统分为前台和后台任务。前台任务由中断服务程序处理,而后台任务在主循环中执行。后台任务通常处理一些非紧急任务,前台通过中断处理紧急事件,最为常用的就是时间片轮询,用来处理简单的多任务场景。

  • 优点:中断能优先响应紧急任务,同时后台任务在主程序中循环执行。
  • 缺点:适合中小型系统,任务较多时可能影响系统响应。
/* 使用定时器中断定时计数并置位标志位 再在主循环中循环判断 触发标志则允许对应任务 
   也可中断只更新一个标志位 在主循环中计数并执行
*/

#include "stm32f10x.h"

#define LED_PIN GPIO_Pin_0
#define BUTTON_PIN GPIO_Pin_1

// 定义时间标志结构体
typedef struct {
    uint8_t flag_10ms;
    uint8_t flag_100ms;
    uint8_t flag_200ms;
    uint8_t flag_500ms;
    uint8_t flag_1s;
    uint8_t flag_2s;
    uint8_t flag_5s;
} TimeFlags;

volatile TimeFlags timeFlags = {0}; // 全局时间标志

// 计数器,用于每10ms增加并判断是否达到对应时间周期
volatile uint16_t counter_100ms = 0;
volatile uint16_t counter_200ms = 0;
volatile uint16_t counter_500ms = 0;
volatile uint16_t counter_1s = 0;
volatile uint16_t counter_2s = 0;
volatile uint16_t counter_5s = 0;

void setup()
{
    // 初始化GPIO
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 配置LED引脚
    GPIO_InitStructure.GPIO_Pin = LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 配置按键引脚
    GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 初始化定时器
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 10000 - 1;   // 10ms时间片
    TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; // 1kHz计数频率
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    // 开启定时器中断
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_Cmd(TIM2, ENABLE);
}

void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        
        // 每10ms触发一次
        timeFlags.flag_10ms = 1;

        // 更新计数器
        counter_100ms++;
        counter_200ms++;
        counter_500ms++;
        counter_1s++;
        counter_2s++;
        counter_5s++;

        // 根据计数器设置相应的标志位
        if (counter_100ms >= 10) { // 10 * 10ms = 100ms
            timeFlags.flag_100ms = 1;
            counter_100ms = 0;
        }
        if (counter_200ms >= 20) { // 20 * 10ms = 200ms
            timeFlags.flag_200ms = 1;
            counter_200ms = 0;
        }
        if (counter_500ms >= 50) { // 50 * 10ms = 500ms
            timeFlags.flag_500ms = 1;
            counter_500ms = 0;
        }
        if (counter_1s >= 100) { // 100 * 10ms = 1s
            timeFlags.flag_1s = 1;
            counter_1s = 0;
        }
        if (counter_2s >= 200) { // 200 * 10ms = 2s
            timeFlags.flag_2s = 1;
            counter_2s = 0;
        }
        if (counter_5s >= 500) { // 500 * 10ms = 5s
            timeFlags.flag_5s = 1;
            counter_5s = 0;
        }
    }
}

// 任务1:每10ms执行
void Task_10ms(void)
{
    // 例如:闪烁LED
    GPIO_WriteBit(GPIOA, LED_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, LED_PIN)));
}

// 任务2:每100ms检查按键状态
void Task_100ms(void)
{
    if (GPIO_ReadInputDataBit(GPIOA, BUTTON_PIN) == Bit_RESET)
    {
        // 按键按下时执行的操作
    }
}

// 任务3:每1s执行其他任务
void Task_1s(void)
{
    // 模拟计算任务或其他功能
}

int main()
{
    setup();

    while (1)
    {
        // 轮询检查标志位并调用相应的任务
        if (timeFlags.flag_10ms) {
            timeFlags.flag_10ms = 0;
            Task_10ms();
        }

        if (timeFlags.flag_100ms) {
            timeFlags.flag_100ms = 0;
            Task_100ms();
        }

        if (timeFlags.flag_1s) {
            timeFlags.flag_1s = 0;
            Task_1s();
        }

        // 其他时间周期的任务可以依此类推
    }
}

总结

裸机开发一般使用时间片轮询的前后台系统即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值