【AT-START-F425测评】2、非阻塞方式点灯,blink,blink,blink…...
系列**:【AT-START-F425测评】1、初识AT32F425开发板(开箱)1、前言官方的demo用的阻塞方式点灯,即就是死等,在等待期间mcu干不了其他事情(中断除外),这种方式不太友好,本文使用非阻塞方式点灯。2、硬件连接3、思路
利用定时器中断,每1ms中断一次,整个系统维护一个tick计数;记录某一时刻的tick,用A表示,再获取当前的tick,用B表示,如果当前的B-A大于等于500(这里500ms闪烁一次),那么就执行一次led翻转,同时也更新A的值。4、软件实现(1)利用定时器中断,每1ms中断一次,整个系统维护一个tick计数static uint32_t SystemTick=0;
void Timer1Config(void)
{
/* tmr1 overflow interrupt nvic init */
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(TMR1_BRK_OVF_TRG_HALL_IRQn, 0, 0);
/* enable tmr1 clock */
crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
/* tmr1 configuration */
/* time base configuration */
/* 120 000 000/120/1000 = 1000hz */
tmr_base_init(TMR1, 999, 119);
tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
/* overflow interrupt enable */
tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);
/* enable tmr1 */
tmr_counter_enable(TMR1, TRUE);
}
void TMR1_BRK_OVF_TRG_HALL_IRQHandler(void)
{
if(tmr_flag_get(TMR1, TMR_OVF_FLAG) != RESET)
{
tmr_flag_clear(TMR1, TMR_OVF_FLAG);
SystemTick++;
}
}
uint32_t SystemGetTick(void)
{
return SystemTick;
}
(2)LED翻转void LedInit(void)
{
gpio_init_type gpio_init_struct;
/* enable the led clock */
crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
/* set default parameter */
gpio_default_para_init(&gpio_init_struct);
/* configure the led gpio */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type= GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_5;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOC, &gpio_init_struct);
}(3)主函数轮询int main(void)
{
system_clock_config();
LedInit();
Timer1Config();
while(1)
{
SystemRun();
}
}5、现象
这个定时器搞起来。
可以使用systick作为内部计时的时钟的。 操作系统不好用吗?操作系统可以实现非阻塞的。
页:
[1]