STM8S时钟配置CLK_HSIPrescalerConfig与CLK_SYSCLKConfig区别

STM8S的时钟配置通过:CLK_CKDIVR寄存器,而CLK_CKDIVR一个是配置HSI分频,另一个是配置CPU的分频

 

static void CLK_Config(void)
{
CLK_DeInit();

/* Clock divider to HSI/1 */
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);  // 只配置HSI分频率,CPU分频率1   

/* Output Fcpu on CLK_CCO pin */
CLK_CCOConfig(CLK_OUTPUT_MASTER);
}

 


/**
* @brief Configure system clock to run at Maximum clock speed and output the
* system clock on CCO pin
* @param None
* @retval None
*/
static void CLK_Config(void)
{
ErrorStatus status = FALSE;

CLK_DeInit();

/* Configure the Fcpu to DIV1*/
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);   // 配置CPU分频

/* Configure the HSI prescaler to the optimal value */
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);  // 配置HSI分频

/* Output Fcpu on CLK_CCO pin */
CLK_CCOConfig(CLK_OUTPUT_CPU);

/* Configure the system clock to use HSE clock source and to run at 24Mhz */
status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);

while (ButtonPressed == FALSE)
{
}
/* Configure the system clock to use HSI clock source and to run at 16Mhz */
status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
}

 

 

 

// CLK_SYSCLKConfig通过CLK_Prescaler值的最高位来区分是配置HSI分频,还是CPU分频的。这个函数有两个功能。而CLK_HSIPrescalerConfig是直接配置HSI分频的,CPU分频配置成1

void CLK_SYSCLKConfig(CLK_Prescaler_TypeDef CLK_Prescaler)
{
/* check the parameters */
assert_param(IS_CLK_PRESCALER_OK(CLK_Prescaler));

if (((uint8_t)CLK_Prescaler & (uint8_t)0x80) == 0x00) /* Bit7 = 0 means HSI divider */
{
CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_HSIDIV);
CLK->CKDIVR |= (uint8_t)((uint8_t)CLK_Prescaler & (uint8_t)CLK_CKDIVR_HSIDIV);
}
else /* Bit7 = 1 means CPU divider */
{
CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_CPUDIV);
CLK->CKDIVR |= (uint8_t)((uint8_t)CLK_Prescaler & (uint8_t)CLK_CKDIVR_CPUDIV);
}
}

 

posted @ 2017-02-12 11:42  未命名blogs  阅读(5748)  评论(0编辑  收藏  举报