[活动专区] 【AT-START-WB415测评】外部时钟PLL到144MHZ,非阻塞点灯,定时器

[复制链接]
1882|4
 楼主| freeelectron 发表于 2022-8-25 16:00 | 显示全部楼层 |阅读模式

本文主要实现以下功能:


系统配置为外部16MHZ时钟,并且PLL到144MHZ;

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

普通定时器1ms一次中断,为整个系统应用层提供时钟,


代码实现:

1、系统配置为外部16MHZ时钟,并且PLL到144MHZ

  1.   /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  system clock config program
  3.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   the system clock is configured as follow:
  4.   *         - system clock        = heck  * pll_mult
  5.   *         - system clock source = pll (hick)
  6.   *         - heck                = 16000000
  7.   *         - sclk                = 144000000
  8.   *         - ahbdiv              = 1
  9.   *         - ahbclk              = 144000000
  10.   *         - apb2div             = 2
  11.   *         - apb2clk             = 72000000
  12.   *         - apb1div             = 2
  13.   *         - apb1clk             = 72000000
  14.   *         - pll_mult            = 9
  15.   *         - flash_wtcyc         = 4 cycle
  16.   * @param  none
  17.   * @retval none
  18.   */
  19. void system_clock_config(void)
  20. {
  21.         /* config flash psr register */
  22.         flash_psr_set(FLASH_WAIT_CYCLE_4);

  23.         /* reset crm */
  24.         crm_reset();

  25.         crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);

  26.         /* wait till heck is ready */
  27.         while(crm_flag_get(CRM_HEXT_STABLE_FLAG) != SET)
  28.         {
  29.         }
  30.         /* config pll clock resource */
  31.         crm_pll_config(CRM_PLL_SOURCE_HEXT, CRM_PLL_MULT_9);

  32.         /* enable pll */
  33.         crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);

  34.         /* wait till pll is ready */
  35.         while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
  36.         {
  37.         }
  38.         /* config ahbclk */
  39.         crm_ahb_div_set(CRM_AHB_DIV_1);

  40.         /* config apb2clk */
  41.         crm_apb2_div_set(CRM_APB2_DIV_2);
  42.         /* config apb1clk */
  43.         crm_apb1_div_set(CRM_APB1_DIV_2);
  44.         /* enable auto step mode */
  45.         crm_auto_step_mode_enable(TRUE);
  46.         /* select pll as system clock source */
  47.         crm_sysclk_switch(CRM_SCLK_PLL);
  48.         /* wait till pll is used as system clock source */
  49.         while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
  50.         {
  51.         }
  52.         /* disable auto step mode */
  53.         crm_auto_step_mode_enable(FALSE);
  54.         /* update system_core_clock global variable */
  55.         system_core_clock_update();
  56. }

2、定时器3配置为1ms中断一次

  1. static uint32_t SystemTick=0;

  2. void TimerInit(void)
  3. {
  4.         crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
  5.        
  6.         tmr_base_init(TMR3, 71, 999); //72000000/72/999
  7.     tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
  8.        
  9.         tmr_interrupt_enable(TMR3, TMR_OVF_INT, TRUE);

  10.         /* tmr3 overflow interrupt nvic init */
  11.         nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  12.         nvic_irq_enable(TMR3_GLOBAL_IRQn, 0, 0);

  13.         /* enable tmr3 */
  14.         tmr_counter_enable(TMR3, TRUE);
  15. }


  16. uint32_t SystemGetTick(void)
  17. {
  18.         return SystemTick;
  19. }


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

3、非阻塞点灯,500ms闪烁一次

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

  4.         /* enable the led clock */
  5.         crm_periph_clock_enable(CRM_GPIOB_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_7|GPIO_PINS_8|GPIO_PINS_9;
  13.         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  14.         gpio_init(GPIOB, &gpio_init_struct);
  15.        
  16. }

  17. void McuRun(void)
  18. {
  19.         static uint32_t curtick=0;
  20.        
  21.         if(SystemGetTick()-curtick>=500)
  22.         {
  23.                 gpio_bits_write(GPIOB, GPIO_PINS_7|GPIO_PINS_8|GPIO_PINS_9, !gpio_output_data_bit_read(GPIOB, GPIO_PINS_7));

  24.                 curtick=SystemGetTick();
  25.         }
  26. }

blink.gif



hilahope 发表于 2022-9-3 17:03 | 显示全部楼层
144hz频率速度可以啊
sanxingnote7 发表于 2022-9-3 18:45 | 显示全部楼层
AT-START-WB415非常棒呢。
burgessmaggie 发表于 2022-9-3 21:12 | 显示全部楼层
是pwm控制的吗
Henryko 发表于 2022-9-7 21:30 | 显示全部楼层
114,可以啊!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

66

主题

786

帖子

11

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