[活动专区] 【AT-START-F425测评】2、非阻塞方式点灯,blink,blink,blink…...

[复制链接]
 楼主| freeelectron 发表于 2022-3-3 19:20 | 显示全部楼层 |阅读模式

系列**:

【AT-START-F425测评】1、初识AT32F425开发板(开箱)


1、前言

官方的demo用的阻塞方式点灯,即就是死等,在等待期间mcu干不了其他事情(中断除外),这种方式不太友好,本文使用非阻塞方式点灯。

2、硬件连接

7190362209f868c667.png

3、思路


利用定时器中断,每1ms中断一次,整个系统维护一个tick计数;

记录某一时刻的tick,用A表示,再获取当前的tick,用B表示,如果当前的B-A大于等于500(这里500ms闪烁一次),那么就执行一次led翻转,同时也更新A的值。

4、软件实现

(1)利用定时器中断,每1ms中断一次,整个系统维护一个tick计数

  1. static uint32_t SystemTick=0;

  2. void Timer1Config(void)
  3. {
  4.         /* tmr1 overflow interrupt nvic init */
  5.         nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  6.         nvic_irq_enable(TMR1_BRK_OVF_TRG_HALL_IRQn, 0, 0);
  7.        
  8.         /* enable tmr1 clock */
  9.         crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);

  10.     /* tmr1 configuration */
  11.     /* time base configuration */
  12.         /* 120 000 000/120/1000 = 1000hz */

  13.         tmr_base_init(TMR1, 999, 119);
  14.         tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);

  15.         /* overflow interrupt enable */
  16.         tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);
  17.         /* enable tmr1 */
  18.         tmr_counter_enable(TMR1, TRUE);
  19. }


  20. void TMR1_BRK_OVF_TRG_HALL_IRQHandler(void)
  21. {
  22.         if(tmr_flag_get(TMR1, TMR_OVF_FLAG) != RESET)
  23.         {
  24.                 tmr_flag_clear(TMR1, TMR_OVF_FLAG);
  25.                
  26.                 SystemTick++;
  27.         }
  28. }


  29. uint32_t SystemGetTick(void)
  30. {
  31.         return SystemTick;
  32. }

(2)LED翻转

  1. void LedInit(void)
  2. {
  3.         gpio_init_type gpio_init_struct;

  4.         /* enable the led clock */
  5.         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);

  6.         /* set default parameter */
  7.         gpio_default_para_init(&gpio_init_struct);

  8.         /* configure the led gpio */
  9.         gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  10.         gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  11.         gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  12.         gpio_init_struct.gpio_pins = GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_5;
  13.         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  14.         gpio_init(GPIOC, &gpio_init_struct);
  15. }

(3)主函数轮询

  1. int main(void)
  2. {
  3.         system_clock_config();
  4.         LedInit();
  5.         Timer1Config();

  6.         while(1)
  7.         {
  8.                 SystemRun();
  9.         }
  10. }

5、现象

65396220a44adc640.gif









pmp 发表于 2022-12-4 22:43 | 显示全部楼层
这个定时器搞起来。
              
lzmm 发表于 2022-12-6 12:49 | 显示全部楼层
可以使用systick作为内部计时的时钟的。
cashrwood 发表于 2022-12-6 14:30 | 显示全部楼层
操作系统不好用吗?操作系统可以实现非阻塞的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:stm32/LoRa物联网:304350312

65

主题

785

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部