单片机如何非阻塞延时操作

       单片机中非阻塞延时,有时候整个while循环中不能使用delay sleep来进行延时,每时每刻都需要查询处理一些事情,此时应该如何处理了?
     在单片机中通过while循环,通过tick获取前一个时间,在大while循环中一直循环,直到再次获取到的时间大于特定时间,继续执行下一步

 


static uint32_t record_time = 0;

void app_loop(void)
{
	stat = CPU_NOT_POWER_ON;

	while (1) {
		app_watchdog_feed();
		
		switch(stat) {
			case CPU_NOT_POWER_ON
				app_cpu_not_poweron();
				break;
				
			case CPU_POWER_ON:
				app_cpu_power_on();
				break;
				
			case CPU_BOOTING:
				app_cpu_booting();
				break;
				
			case CPU_ALREADY_POWER_ON:
				app_cpu_already_power_on();
				break;

			case CPU_SHUTDOWN:
				app_cpu_shutdown();
				break;
				
			case CPU_POWER_OFF:
				app_cpu_power_off();
				break;
			
			default:
				break;
		}

	}
}


void app_cpu_power_on(void)
{
	static uint32_t step = FIRST_STEP;


	switch (step) {
		case FIRST_STEP:
			GPIO_PinSet(GPIO_1);
			record_time = drv_pit_get_ms();
			step = SECOND_STEP;
			break;

		case SECOND_STEP:
			if ((drv_pit_get_ms() - record_time) >= 20) {
				GPIO_PinSet(GPIO_2);
				record_time = drv_pit_get_ms();
				step = THIRD_STEP;
			}
			break;

		case THIRD_STEP:
			if ((drv_pit_get_ms() - record_time) >= 20) {
				GPIO_PinSet(GPIO_3);
				record_time = drv_pit_get_ms();
				step = FOUR_STEP;
			}
			break;

		case FOUR_STEP:
			if ((drv_pit_get_ms() - record_time) >= 20) {
				GPIO_PinSet(GPIO_4);
				record_time = drv_pit_get_ms();
				step = FIVE_STEP;
			}
			break;

		case FIVE_STEP:
			if ((drv_pit_get_ms() - record_time) >= 20) {
				GPIO_PinSet(GPIO_5);
				step		= FIRST_STEP;
				record_time = drv_pit_get_ms();
				stat        = CPU_BOOTING;
			}
			break;
	}

}

void app_cpu_booting(void)
{
	if ((drv_pit_get_ms() - record_time) >= 10000)	
	{
		stat = CPU_POWER_OFF;
		record_time = drv_pit_get_ms();
	}

	if (DRV_TYPE_GPIO_R(GPIO_XX) == 1)
	{
		stat = CPU_ALREADY_POWER_ON;
		record_time = drv_pit_get_ms();
		
	}
}

 

 

 

 

 

你可能感兴趣的:(单片机)