以下 基于STM8S库使用例
#ifndef __BEEP_H
#define __BEEP_H
#include "stm8s.h"
#define LSI_128kHz 128000
void BEEP_Config(BEEP_Frequency_TypeDef BEEP_Frequency, FunctionalState NewState);
void Set_Beep_OptionByte(void);
#endif
---------------------------------------------------------------------------
#include "beep.h"
#define Beep_OptionAdd 0x4803
void BEEP_Config(BEEP_Frequency_TypeDef BEEP_Frequency, FunctionalState NewState) {
if (NewState) {
BEEP_Init(BEEP_Frequency);
BEEP_LSICalibrationConfig(LSI_128kHz);
CLK_LSICmd(ENABLE);
BEEP_Cmd(ENABLE);
} else {
BEEP_Cmd(DISABLE);
//CLK_BEEPClockConfig(CLK_BEEPCLKSource_Off);
}
}
void Set_Beep_OptionByte(void) {
uint16_t Beep_Option_status;/*记录激活备选功能Beep的状态*/
Beep_Option_status = FLASH_ReadOptionByte(Beep_OptionAdd);
/*Beep_Option_status的最高位为1激活了Beep,否则不激活*/
if(!(Beep_Option_status & 0x8000)) {
FLASH_ProgramOptionByte(Beep_OptionAdd, (uint8_t)(Beep_Option_status | 0x8000));
/*向Beep_OptionAdd 0x4803 置1,激活了Beep*/
}
}
-------------------------------------------------------------------------------------------------
#include "stm8s.h"
#include "stm8s_clk.h"
#include "intrinsics.h"
#include "beep.h"
void BEEP_Alarm(u32 n) {
BEEP_Config(BEEP_FREQUENCY_2KHZ, ENABLE);
delay_ms(n);
BEEP_Config(BEEP_FREQUENCY_2KHZ, DISABLE);
}
int main(void) {
Set_Beep_OptionByte();
while (1) {
BEEP_Alarm(100);
delay_ms(500);
}
} |