【IAR工程】STM8S208RB基于ST标准库蜂鸣器(BEEP)驱动
- 🌾寄存器版本《STM8S系列基于IAR开发:蜂鸣器(BEEP)驱动功能模块示例》
- 🌿相关篇《【IAR工程】STM8S208RB基于ST标准库下GPIO点灯示例》
- 🌿《【IAR工程】STM8S208RB基于ST标准库下EXTI外部中断》
- 🌿《【IAR工程】STM8S208RB基于ST标准库下自动唤醒(AWU)》
- 🔖基于ST STM8S/A标准外设库:STSW-STM8069,版本号:2.3.1
- 📌STSW-STM8069官方资源下载地址:
https://www.st.com/zh/embedded-software/stsw-stm8069.html
- 🔧IAR编译器版本:
IAR Assembler for STMicroelectronics STM8 3.11.1
- 📌STM8S207/208RBT6最小系统板:
https://oshwhub.com/perseverance51/stm8s207rbt6-kai-fa-ban
📑蜂鸣器(BEEP)
当LS时钟工作在128kHz时可产生频率为1kHz,2 kHz或者是4 kHz的蜂鸣信号。
- 🌴DEEP输出引脚:
PD4
🚩注意事项
- 🔰在使用该功能时,需要配置
OPTION Byte
(选项字节)
📑DEEP引脚配置函数
void MyBEEP_Init(void)
{
//设置PD4为快速推挽输出
GPIO_Init(GPIOD , GPIO_PIN_4 , GPIO_MODE_OUT_PP_LOW_FAST);
}
📝主程序代码
- 🔖包含对内部低速时钟频率测量内容。
/**************************************************************************************
实验现象:打开串口调试助手,选择CH340对应串口号,波特率设置9600, 串口助手上会显示
lsi_freq_hz= 136898 。
接线说明: 1,STM8S单片机-->LED
PC7-->LED1
PC6-->LED2
--------------------------------------------------------------------
DEEP -> PD4
--------------------------------------------------------------------
OPTION Bytes选项:
AFR7 配置为“Alternate active”
注意事项: 1、点击“Download active application”按钮,程序下载完成后,即可运行程序。
2、串口1使用的是PA4和PA5引脚,所以这两个IO口不要被占用
***************************************************************************************/
#include "stm8s.h" /* 添加库函数头文件 */
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "usart.h"
#include <stdio.h>//包含此头文件调用printf函数串口才能有输出
uint32_t LSIMeasurment( void );
/* 主函数 */
int main( void )
{
u8 i = 0;
uint32_t lsi_freq_hz;
disableInterrupts(); //关闭系统中断
//内部时钟为1分频 = 16Mhz
CLK_SYSCLKConfig( CLK_PRESCALER_HSIDIV1 );
LED_Init();
USART1_Init( 9600 ); //初始化USART1 , 并设置波特率为9600
MyBEEP_Init();
/* BEEP calibration */
lsi_freq_hz = LSIMeasurment() ;
printf("lsi_freq_hz= %lu \r\n",lsi_freq_hz);
BEEP_LSICalibrationConfig(lsi_freq_hz);
enableInterrupts(); //使能系统中断
while( 1 )
{
i++;
if( i % 20 == 0 )
{
LED1_TOGGLE;
LED2_TOGGLE;
}
if( i % 100 == 0 )
{
BEEP_Cmd(DISABLE);
delay_ms(100);
BEEP_Init(BEEP_FREQUENCY_4KHZ);//1KHz,2KHz,4KHz
BEEP_Cmd(ENABLE);
}
delay_ms( 10 );
}
}
/**
* @brief Measure the LSI frequency using timer IC1 and update the calibration registers.
* @note It is recommended to use a timer clock frequency of at least 10MHz in order
* to obtain a better in the LSI frequency measurement.
* @param None
* @retval None
*/
uint32_t LSIMeasurment( void )
{
uint32_t lsi_freq_hz = 0x0;
uint32_t fmaster = 0x0;
uint16_t ICValue1 = 0x0;
uint16_t ICValue2 = 0x0;
/* Get master frequency */
fmaster = CLK_GetClockFreq();
/* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */
AWU->CSR |= AWU_CSR_MSR;
#if defined (STM8S903) || defined (STM8S103) || defined (STM8S003) || defined (STM8S001)
/* Measure the LSI frequency with TIMER Input Capture 1 */
/* Capture only every 8 events!!! */
/* Enable capture of TI1 */
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0 );
/* Enable TIM1 */
TIM1_Cmd( ENABLE );
/* wait a capture on cc1 */
while( ( TIM1->SR1 & TIM1_FLAG_CC1 ) != TIM1_FLAG_CC1 );
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag( TIM1_FLAG_CC1 );
/* wait a capture on cc1 */
while( ( TIM1->SR1 & TIM1_FLAG_CC1 ) != TIM1_FLAG_CC1 );
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag( TIM1_FLAG_CC1 );
/* Disable IC1 input capture */
TIM1->CCER1 &= ( uint8_t )( ~TIM1_CCER1_CC1E );
/* Disable timer2 */
TIM1_Cmd( DISABLE );
#else
/* Measure the LSI frequency with TIMER Input Capture 1 */
/* Capture only every 8 events!!! */
/* Enable capture of TI1 */
TIM3_ICInit( TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, TIM3_ICPSC_DIV8, 0 );
/* Enable TIM3 */
TIM3_Cmd( ENABLE );
/* wait a capture on cc1 */
while ( ( TIM3->SR1 & TIM3_FLAG_CC1 ) != TIM3_FLAG_CC1 );
/* Get CCR1 value*/
ICValue1 = TIM3_GetCapture1();
TIM3_ClearFlag( TIM3_FLAG_CC1 );
/* wait a capture on cc1 */
while ( ( TIM3->SR1 & TIM3_FLAG_CC1 ) != TIM3_FLAG_CC1 );
/* Get CCR1 value*/
ICValue2 = TIM3_GetCapture1();
TIM3_ClearFlag( TIM3_FLAG_CC1 );
/* Disable IC1 input capture */
TIM3->CCER1 &= ( uint8_t )( ~TIM3_CCER1_CC1E );
/* Disable timer3 */
TIM3_Cmd( DISABLE );
#endif /* (STM8S903) || (STM8S103) || (STM8S003) || (STM8S001) */
/* Compute LSI clock frequency */
lsi_freq_hz = ( 8 * fmaster ) / ( ICValue2 - ICValue1 );
/* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */
AWU->CSR &= ( uint8_t )( ~AWU_CSR_MSR );
return ( lsi_freq_hz );
}
//是一个宏定义;在固件库中,它的作用就是检测传递给函数的参数是否是有效的参数
void assert_failed( u8* file, u32 line )
{
while ( 1 )
{
}
}
-
📜串口打印内部LSI时钟频率:
-
🌿BEEP_FREQUENCY_4KHZ测量
-
🌿BEEP_FREQUENCY_1KHZ测量
-
🌿BEEP_FREQUENCY_2KHZ测量
📚工程源码
- 🔖IAR对中文路径不友好,不要将工程解压在带有中文字符路径的文件夹内直接打开工程编译,这样会导致IAR内部检索文件路径将消耗大量CPU资源。
链接: https://pan.baidu.com/s/1XkLkZm2VY9LR2gwMDOYn5A
提取码: spws