分类 嵌入式软件 下的文章

systick.c
定义1Hz

/*!
    \brief      configure systick
    \param[in]  none
    \param[out] none
    \retval     none
*/
void systick_config(void)
{
    /* setup systick timer for 1Hz interrupts */
    if (SysTick_Config(SystemCoreClock / 1000 / 1000)){
        /* capture error */
        while (1);
    }
    /* configure the systick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x00);
}

阻塞式延时函数

/*!
    \brief      delay a time in milliseconds
    \param[in]  count: count in milliseconds
    \param[out] none
    \retval     none
*/
void delay_1us(uint32_t count)
{
    delay = count;

    while(0 != delay);
}

void delay_1ms(uint32_t count)
{
    delay = count * 1000;

    while(0 != delay);
}

gd32f1x0_it.c
中断处理函数中调用led_spark(),实现led翻转

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SysTick_Handler(void)
{
    led_spark();
    delay_decrement();
}

main.c
led 翻转函数

/*!
    \brief      toggle the led every 500ms
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_spark(void)
{
    static __IO uint32_t timingdelaylocal = 0;

    if(timingdelaylocal){

        if(timingdelaylocal < 500*1000){
            gd_eval_ledon(LED2);
        }else{
            gd_eval_ledoff(LED2);
        }

        timingdelaylocal--;
    }else{
        timingdelaylocal = 1000*1000;
    }
}

日常阻塞式翻转

int main(void)
{
    gd_eval_ledinit(LED2);

    systick_config();

    while(1)
    {
      gd_eval_ledon(LED2);
      delay_1us(500*1000);
      gd_eval_ledoff(LED2);
      delay_1us(200*1000);
    }
}