蜂鸣器操作代码(实现多种状态提示)

一、

.h

#define  BEEP  PCout(14)


typedef struct 
{
    bool isActive;
    bool isLoop;
    u16 waitMS;
}beepSeq_t;


enum beepAction
{
    TRIM_BEEP = 0,
    FLIP_BEEP,
    LOWPOWER_BEEP,
    CANFLY_BEEP,
};


void beepInit(void);
void runBeepAcktion(enum beepAction runaction);
void stopBeepAcktion(enum beepAction runaction);
#endif
 

二、

.c

/*蜂鸣器对应报警的延时时间(ms)*/
#define  TRIM            100
#define  FLIP            50 
#define  LOWPOWER        500
#define  CANFLY            100

beepSeq_t beepAction[] =
{
    {false, true, TRIM},
    {false, true, FLIP},
    {false, true, LOWPOWER},
    {false, false, CANFLY},
};

#define  ACTION_NUM    (sizeof(beepAction)/sizeof(beepSeq_t))//动作个数


static bool isInit;
static bool beepEnable;
static TimerHandle_t beepTimer;
enum beepAction currentRun;
    
/* 蜂鸣器IO口初始化 */
static void beepIO_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    
     /* 初始化蜂鸣器(PC14) */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
    GPIO_ResetBits(GPIOC,GPIO_Pin_14);
}

static void runBeepseq(xTimerHandle xTimer)
{
    u8 i;
    if(beepAction[currentRun].isLoop == false)
    {
        beepAction[currentRun].isActive = false;
    }    
    for(i=0; i<ACTION_NUM; i++)
    {
        if(beepAction[i].isActive == true) 
        {
            xTimerChangePeriod(beepTimer, beepAction[i].waitMS, 0);
            break;
        }
    }
    if(i == ACTION_NUM)/*当前无动作运行*/
    {
        beepEnable = false;
    }
    if(beepEnable)
        BEEP = !BEEP;
    else
        BEEP = 0;
}

void beepInit(void)
{
    if(isInit) return;
    beepIO_Init();
    beepTimer = xTimerCreate("beepTimer", 1000, pdTRUE,
                        0, runBeepseq);
    isInit = true;
}

void runBeepAcktion(enum beepAction action)
{
    currentRun = action;
    beepAction[action].isActive = true;
    xTimerChangePeriod(beepTimer, beepAction[action].waitMS, portMAX_DELAY);
    BEEP = 1;
    beepEnable = true;
}

void stopBeepAcktion(enum beepAction action)
{
    beepAction[action].isActive = false;
    for(u8 i=0; i<ACTION_NUM; i++)
    {
        if(beepAction[i].isActive == true)
        {
            currentRun = (enum beepAction)i;
            xTimerChangePeriod(beepTimer, beepAction[currentRun].waitMS, portMAX_DELAY);
            return;
        }    
    }
    beepEnable = false;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值