io口翻转控制(led,马达控制等等)

//leds

static uint16_t led_on_ms, led_off_ms,led_times;
static uint8_t led_sta=0,led_timer_tick;
static void led_timer_timeout(void * p_context)
{
    led_timer_tick++;
}
APP_TIMER_DEF(m_led_timer);

void init_led_timer(void)
{
    ret_code_t err_code;

    err_code = app_timer_create(&m_led_timer,
                                APP_TIMER_MODE_REPEATED,
                                led_timer_timeout);
    APP_ERROR_CHECK(err_code);

}



void start_led_timer(uint16_t ms,bool start_flg)
{
    if(start_flg==true)
    {
        app_timer_start(m_led_timer,
                        APP_TIMER_TICKS(ms),
                        NULL);
    }
    else
    {
        app_timer_stop(m_led_timer);
    }


}



void led_ctrl(uint16_t on_ms,uint16_t off_ms,uint16_t times)
{
    led_on_ms=on_ms;
    led_off_ms=off_ms ;
    led_times=times;
    led_sta=0;
    start_led_timer(100,true);
    led_timer_tick=0;
}

//static uint16_t led_on_ms, led_off_ms ,led_times;
//static uint8_t led_sta=0,led_timer_tick;
void led_timer_manage(void)
{
    if(led_timer_tick==0) return;
    led_timer_tick--;
    static uint16_t endtick=0;
    switch(led_sta)
    {
        case 0:
            if(led_times)
            {

                endtick=led_on_ms;
                LED_ON();
                led_sta++;
            }
            break;
        case 1:
            endtick--;
            if(0==endtick)
            {
                endtick=led_off_ms;
                LED_OFF();
                led_sta++;
            }
            break;
        case 2:
            endtick--;
            if(0==endtick)
            {
                led_times--;
                if(0==led_times)
                {
                    led_sta=0;
                    start_led_timer(0,false);
                    break;
                }
                else
                {
                    endtick=led_on_ms;
                    LED_ON();
                    led_sta=1;
                }
            }
            break;

    }
}

你可能感兴趣的:(io口翻转控制(led,马达控制等等))