本文主要实现以下功能:
系统配置为外部16MHZ时钟,并且PLL到144MHZ; 官方的demo用的阻塞方式点灯,即就是死等,在等待期间mcu干不了其他事情(中断除外),这种方式不太友好,本文使用非阻塞方式点灯; 普通定时器1ms一次中断,为整个系统应用层提供时钟,
代码实现: 1、系统配置为外部16MHZ时钟,并且PLL到144MHZ - /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] system clock config program
- * [url=home.php?mod=space&uid=536309]@NOTE[/url] the system clock is configured as follow:
- * - system clock = heck * pll_mult
- * - system clock source = pll (hick)
- * - heck = 16000000
- * - sclk = 144000000
- * - ahbdiv = 1
- * - ahbclk = 144000000
- * - apb2div = 2
- * - apb2clk = 72000000
- * - apb1div = 2
- * - apb1clk = 72000000
- * - pll_mult = 9
- * - flash_wtcyc = 4 cycle
- * @param none
- * @retval none
- */
- void system_clock_config(void)
- {
- /* config flash psr register */
- flash_psr_set(FLASH_WAIT_CYCLE_4);
- /* reset crm */
- crm_reset();
- crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
- /* wait till heck is ready */
- while(crm_flag_get(CRM_HEXT_STABLE_FLAG) != SET)
- {
- }
- /* config pll clock resource */
- crm_pll_config(CRM_PLL_SOURCE_HEXT, CRM_PLL_MULT_9);
- /* enable pll */
- crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
- /* wait till pll is ready */
- while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
- {
- }
- /* config ahbclk */
- crm_ahb_div_set(CRM_AHB_DIV_1);
- /* config apb2clk */
- crm_apb2_div_set(CRM_APB2_DIV_2);
- /* config apb1clk */
- crm_apb1_div_set(CRM_APB1_DIV_2);
- /* enable auto step mode */
- crm_auto_step_mode_enable(TRUE);
- /* select pll as system clock source */
- crm_sysclk_switch(CRM_SCLK_PLL);
- /* wait till pll is used as system clock source */
- while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
- {
- }
- /* disable auto step mode */
- crm_auto_step_mode_enable(FALSE);
- /* update system_core_clock global variable */
- system_core_clock_update();
- }
2、定时器3配置为1ms中断一次 - static uint32_t SystemTick=0;
- void TimerInit(void)
- {
- crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
-
- tmr_base_init(TMR3, 71, 999); //72000000/72/999
- tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
-
- tmr_interrupt_enable(TMR3, TMR_OVF_INT, TRUE);
- /* tmr3 overflow interrupt nvic init */
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- nvic_irq_enable(TMR3_GLOBAL_IRQn, 0, 0);
- /* enable tmr3 */
- tmr_counter_enable(TMR3, TRUE);
- }
- uint32_t SystemGetTick(void)
- {
- return SystemTick;
- }
- void TMR3_GLOBAL_IRQHandler(void)
- {
- if(tmr_flag_get(TMR3, TMR_OVF_FLAG) != RESET)
- {
- SystemTick++;
-
- tmr_flag_clear(TMR3, TMR_OVF_FLAG);
- }
- }
3、非阻塞点灯,500ms闪烁一次 - void LedInit(void)
- {
- gpio_init_type gpio_init_struct;
- /* enable the led clock */
- crm_periph_clock_enable(CRM_GPIOB_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_7|GPIO_PINS_8|GPIO_PINS_9;
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
- gpio_init(GPIOB, &gpio_init_struct);
-
- }
- void McuRun(void)
- {
- static uint32_t curtick=0;
-
- if(SystemGetTick()-curtick>=500)
- {
- gpio_bits_write(GPIOB, GPIO_PINS_7|GPIO_PINS_8|GPIO_PINS_9, !gpio_output_data_bit_read(GPIOB, GPIO_PINS_7));
- curtick=SystemGetTick();
- }
- }
|