Buzzer.c 5.58 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6 7 8 9 10 11 12
/******************************************************************************
文 件 名:Buzzer.c
功能描述:蜂鸣器驱动库文件
作    者:张暄
版    本:V1.0
日    期:2016.5.18
******************************************************************************/

#include "Buzzer.h"

#include "TimerB.h"

13
#include "Sound_Tracks.h"
hu's avatar
hu committed
14

15
#define BUZZER_PWM_CHANNEL TIMERB_0_CH11
hu's avatar
hu committed
16

17
BuzzerPlayCtrlStruct BuzzerPlayCtrl;
hu's avatar
hu committed
18 19

/******************************************************************************
20 21 22 23
函数名:Buzzer_Start_Up
功  能:启动并初始化蜂鸣器
        蜂鸣器PWM使用CLKSA/CLKSB时钟源,推荐的时钟源频率为50kHz~100kHz
参  数:无
hu's avatar
hu committed
24 25
返回值:无
******************************************************************************/
26
void Buzzer_Start_Up(void)
hu's avatar
hu committed
27
{
28
    BuzzerPlayCtrl.Mode = BUZZER_MODE_IDLE;
hu's avatar
hu committed
29

30 31 32 33
    BuzzerPlayCtrl.Track = 0;
    BuzzerPlayCtrl.Note = 0;
    BuzzerPlayCtrl.Timer = 0;
    BuzzerPlayCtrl.Fade = 0;
hu's avatar
hu committed
34

35 36 37
    BUZZER_CTRL = 0;
    TimerB_PWM_Channel_Stop(BUZZER_PWM_CHANNEL);
    TimerB_PWM_Channel_Init(BUZZER_PWM_CHANNEL, TIMERB_CLOCK_0, TIMERB_HIGH);
hu's avatar
hu committed
38 39 40
}

/******************************************************************************
41 42
函数名:Buzzer_Shutdown
功  能:关闭蜂鸣器
hu's avatar
hu committed
43 44 45
参  数:无
返回值:无
******************************************************************************/
46
void Buzzer_Shutdown(void)
hu's avatar
hu committed
47
{
48
    BuzzerPlayCtrl.Mode = BUZZER_MODE_IDLE;
hu's avatar
hu committed
49

50 51 52 53
    BuzzerPlayCtrl.Track = 0;
    BuzzerPlayCtrl.Note = 0;
    BuzzerPlayCtrl.Timer = 0;
    BuzzerPlayCtrl.Fade = 0;
hu's avatar
hu committed
54

55 56
    BUZZER_CTRL = 0;
    TimerB_PWM_Channel_Stop(BUZZER_PWM_CHANNEL);
hu's avatar
hu committed
57 58 59
}

/******************************************************************************
60 61 62 63
函数名:Buzzer_Play_Track
功  能:单次播放指定音轨
参  数:TrackID - 音轨编号
返回值:无
hu's avatar
hu committed
64
******************************************************************************/
65
void Buzzer_Play_Track(uint8_t TrackID)
hu's avatar
hu committed
66
{
67 68
    if (BuzzerPlayCtrl.Mode)
        return;
hu's avatar
hu committed
69

70 71
    if (TrackID >= SND_TRACK_TOTAL_NUM)
        return;
hu's avatar
hu committed
72

73
    BUZZER_CTRL = 1;
hu's avatar
hu committed
74

75 76 77 78
    BuzzerPlayCtrl.Track = TrackID;
    BuzzerPlayCtrl.Note = 0;
    BuzzerPlayCtrl.Timer = (uint16_t)SndTracks[TrackID].PreCharge * 20;
    BuzzerPlayCtrl.Fade = 0;
hu's avatar
hu committed
79

80
    BuzzerPlayCtrl.Mode = BUZZER_MODE_SINGLE;
hu's avatar
hu committed
81 82 83
}

/******************************************************************************
84 85 86
函数名:Buzzer_Play_Track
功  能:循环播放指定音轨
参  数:TrackID - 音轨编号
hu's avatar
hu committed
87 88
返回值:无
******************************************************************************/
89
void Buzzer_Repeat_Play_Track(uint8_t TrackID)
hu's avatar
hu committed
90
{
91 92
    if (BuzzerPlayCtrl.Mode)
        return;
hu's avatar
hu committed
93

94 95
    if (TrackID >= SND_TRACK_TOTAL_NUM)
        return;
hu's avatar
hu committed
96

97
    BUZZER_CTRL = 1;
hu's avatar
hu committed
98

99 100 101 102
    BuzzerPlayCtrl.Track = TrackID;
    BuzzerPlayCtrl.Note = 0;
    BuzzerPlayCtrl.Timer = (uint16_t)SndTracks[TrackID].PreCharge * 20;
    BuzzerPlayCtrl.Fade = 0;
hu's avatar
hu committed
103

104
    BuzzerPlayCtrl.Mode = BUZZER_MODE_LOOP;
hu's avatar
hu committed
105 106 107
}

/******************************************************************************
108 109
函数名:Buzzer_Stop_Play
功  能:停止播放音轨
hu's avatar
hu committed
110 111 112
参  数:无
返回值:无
******************************************************************************/
113
void Buzzer_Stop_Play(void)
hu's avatar
hu committed
114
{
115
    BuzzerPlayCtrl.Mode = BUZZER_MODE_IDLE;
hu's avatar
hu committed
116

117 118 119 120 121 122
    BuzzerPlayCtrl.Track = 0;
    BuzzerPlayCtrl.Note = 0;
    BuzzerPlayCtrl.Timer = 0;
    BuzzerPlayCtrl.Fade = 0;

    BUZZER_CTRL = 0;
hu's avatar
hu committed
123 124 125 126 127 128 129 130 131
    TimerB_PWM_Channel_Stop(BUZZER_PWM_CHANNEL);
}

/******************************************************************************
函数名:Buzzer_Play_ISR
功  能:蜂鸣器播放中断服务函数
参  数:无
返回值:无
******************************************************************************
132
注  意:该服务函数必须嵌入到50us定时中断内
hu's avatar
hu committed
133
******************************************************************************/
134
void Buzzer_Play_ISR(void)
hu's avatar
hu committed
135
{
136
    if (BuzzerPlayCtrl.Mode)
hu's avatar
hu committed
137
    {
138 139 140 141
        if (BuzzerPlayCtrl.Timer)
            BuzzerPlayCtrl.Timer--;

        if (BuzzerPlayCtrl.Timer == 0)
hu's avatar
hu committed
142
        {
143
            if (BuzzerPlayCtrl.Note < SndTracks[BuzzerPlayCtrl.Track].NoteNum)
hu's avatar
hu committed
144
            {
145
                BUZZER_CTRL = 1;
hu's avatar
hu committed
146

147 148 149 150 151 152 153 154
                if (SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Freq)
                {
                    TimerB_PWM_Channel_Fre_Set(BUZZER_PWM_CHANNEL, SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Freq, SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Duty);
                    // TimerB_PWM_Channel_Duty_Set(BUZZER_PWM_CHANNEL, SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Duty);
                    TimerB_PWM_Channel_Start(BUZZER_PWM_CHANNEL);
                }
                else
                    TimerB_PWM_Channel_Stop(BUZZER_PWM_CHANNEL);
hu's avatar
hu committed
155

156 157 158 159 160
                BuzzerPlayCtrl.Timer = SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Interval * 20;
                BuzzerPlayCtrl.Fade = SndTracks[BuzzerPlayCtrl.Track].Note[BuzzerPlayCtrl.Note].Fade * 20;
                BuzzerPlayCtrl.Note++;
            }
            else
hu's avatar
hu committed
161
            {
162 163 164
                if (BuzzerPlayCtrl.Mode == BUZZER_MODE_LOOP)
                    BuzzerPlayCtrl.Note = 0;
                else
hu's avatar
hu committed
165
                {
166 167 168
                    BUZZER_CTRL = 0;
                    TimerB_PWM_Channel_Stop(BUZZER_PWM_CHANNEL);
                    BuzzerPlayCtrl.Mode = BUZZER_MODE_IDLE;
hu's avatar
hu committed
169 170 171
                }
            }
        }
172 173
        else if (BuzzerPlayCtrl.Timer < BuzzerPlayCtrl.Fade)
            BUZZER_CTRL = 0;
hu's avatar
hu committed
174
    }
175
}