Program Framework Concept (1)

Program Framework Concept (1)

my code history

When I first started learning microcontrollers, the code I wrote was like this:

void led_blink( void )
{
    led = ON;
    delay_ms(500);
    led = OFF;
    delay_ms(500);
}

Later, I found the problem when I used it. After all, writing the MCU in this way executes the delay_ms function most of the time, which will inevitably affect the normal operation of other programs. Then he started writing:

void led_blink( void )
{
    led = ~led;
}

void timer_handler( void )
{
    if( led_blink_flag == 0 && ++led_time >= led_blink_time )
    {
        led_blink_flag = 1;
        led_time = 0;
    }
}  

void main( void )
{
    while(1)
    {
        if( led_blink_flag == 1 )
        {
            led_blink();
            led_blink_flag = 0;
        }
    }
}

Let the timing program be executed in the timer interrupt, and the program with the flashing light is executed in the while(1) of the main function. This writing has found some problems. If there are many tasks involved in timing, then the timer is interrupted. The program will also increase accordingly, so some changes have been added on this basis:

void timer_handler( void )
{
    if( ++cnt_10ms >= time_10ms  )
    {
        led_blink_flag = 1;
        cnt_10ms = 0;
    }
} 

This can slightly reduce the interrupt running time of the microcontroller timer, but this does not reduce much time, so the concept of timestamp is added on this basis:

uint32_t getSysTick( void )
{
    return sysTick;
}

void sysTickHandler( void )
{
    sysTick++;
}

void timerHandler( void )
{
    sysTickHandler();
}

void ledBlink( void )
{
    led = ~led;
}

void ledBlinkTickInit( uint32_t tick, uint32_t blinktime )
{
    ledBlinkTick = tick + blinktime;    
}

bool getLedBlinkFlag( uint32_t tick )
{
    if( ledBlinkTick >= tick )
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main( void )
{
    while(1)
    {
        ledBlinkTickInit(getSysTick(), 500);

        if( getLedBlinkFlag( getSysTick() ) == true )
        {
            ledBlink();
            ledBlinkTickInit(getSysTick(), 500);
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721068&siteId=291194637