stm8s单片机(四)时钟输出与蜂鸣器实验

可配置的时钟输出功能使用户可在外部管脚CCO上输出指定的时钟
用户可选择下面6种时钟信号之一做为CCO时钟

  1. HSE
  2. HSI
  3. HSIDIV
  4. LSI
  5. MASTER
  6. CPU
    固件库中定义的枚举如下:
/**
   * @brief    CLK  Clock Output
   */
typedef enum {
  CLK_OUTPUT_HSI      = (uint8_t)0x00, /*!< Clock Output HSI */
  CLK_OUTPUT_LSI      = (uint8_t)0x02, /*!< Clock Output LSI */
  CLK_OUTPUT_HSE      = (uint8_t)0x04, /*!< Clock Output HSE */
  CLK_OUTPUT_CPU      = (uint8_t)0x08, /*!< Clock Output CPU */
  CLK_OUTPUT_CPUDIV2  = (uint8_t)0x0A, /*!< Clock Output CPU/2 */
  CLK_OUTPUT_CPUDIV4  = (uint8_t)0x0C, /*!< Clock Output CPU/4 */
  CLK_OUTPUT_CPUDIV8  = (uint8_t)0x0E, /*!< Clock Output CPU/8 */
  CLK_OUTPUT_CPUDIV16 = (uint8_t)0x10, /*!< Clock Output CPU/16 */
  CLK_OUTPUT_CPUDIV32 = (uint8_t)0x12, /*!< Clock Output CPU/32 */
  CLK_OUTPUT_CPUDIV64 = (uint8_t)0x14, /*!< Clock Output CPU/64 */
  CLK_OUTPUT_HSIRC    = (uint8_t)0x16, /*!< Clock Output HSI RC */
  CLK_OUTPUT_MASTER   = (uint8_t)0x18, /*!< Clock Output Master */
  CLK_OUTPUT_OTHERS   = (uint8_t)0x1A  /*!< Clock Output OTHER */
} CLK_Output_TypeDef;

相关api有

/**
  * @brief  Enables or disablle the Configurable Clock Output (CCO).
  * @param   NewState : New state of CCEN bit (CCO register).
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  */
void CLK_CCOCmd(FunctionalState NewState);

/**
  * @brief  Output the selected clock on a dedicated I/O pin.
  * @param   CLK_CCO : Specifies the clock source.
  * This parameter can be any of the  @ref CLK_Output_TypeDef enumeration.
  * @retval None
  * @par Required preconditions:
  * The dedicated I/O pin must be set at 1 in the corresponding Px_CR1 register \n
  * to be set as input with pull-up or push-pull output.
  */
void CLK_CCOConfig(CLK_Output_TypeDef CLK_CCO);

无源压电式蜂鸣器通常需要300~5kHz的脉冲才能发声
而且CCO与PE0是引脚复用的
stm8s单片机(四)时钟输出与蜂鸣器实验_第1张图片

切换系统到128kHz的低速时钟,再配置输出分频64,这样就可以在PE0引脚输出2kHz的方波脉冲。


int main(void)
{
	CLK_LSICmd(ENABLE);//使能LSI起振
	while(CLK_GetFlagStatus(CLK_FLAG_LSIRDY)== RESET);  //等待LSI准备就绪
	CLK_ClockSwitchCmd(ENABLE);   //使能时钟切换
	CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO ,    //
                CLK_SOURCE_LSI ,            //选择内部低速振荡器128K振作为系统时钟源
                DISABLE ,                   //不开启时钟切换中断
                CLK_CURRENTCLOCKSTATE_ENABLE //振荡器使能
                );
  	while(CLK_GetFlagStatus(CLK_FLAG_SWBSY) != RESET);
  	CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);   //如果切换成功,则设置CPU时钟为1分频,让128KHZ时钟作为系统时钟
  	CLK_ClockSwitchCmd(DISABLE);     //关闭切换
  	CLK_HSICmd(DISABLE);    //关闭内部高速HSI
  	GPIO_Init(GPIOE , GPIO_PIN_0 , GPIO_MODE_OUT_PP_LOW_FAST);
  	CLK_CCOConfig(CLK_OUTPUT_CPUDIV64);        // 配置输出为128kHz / 64 =2kHZ
 	CLK_CCOCmd(ENABLE);     //使能系统时钟输出
 	while(1)
    {	
    }
}

你可能感兴趣的:(嵌入式,单片机,嵌入式硬件)