software:firmware
MakAir Firmware
buzzer_control.cpp
Go to the documentation of this file.
1 
8 // INCLUDES ===================================================================
9 
10 #include "../includes/buzzer_control.h"
11 #include "../includes/config.h"
12 #include "../includes/parameters.h"
13 #include "Arduino.h"
14 
15 // INITIALISATION =============================================================
16 
18 #define BUZZER_FREQ 4000
19 
20 #define PERIOD_BUZZER_US (1000000 / BUZZER_FREQ)
21 
22 HardwareTimer* Buzzer_Hw_Timer;
24 
25 // FUNCTIONS ==================================================================
26 
27 void BuzzerControl_Init(void) {
28  TIM_TypeDef* Buzzer_Timer_Number = reinterpret_cast<TIM_TypeDef*>(
29  pinmap_peripheral(digitalPinToPinName(PIN_BUZZER), PinMap_PWM));
31  STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(PIN_BUZZER), PinMap_PWM));
32 
33  // Hardware 2: the buzzer has no internal oscillator. uC must generate a 4khz square on
34  // PIN_BUZZER (timer 2, channel 1)
35  Buzzer_Hw_Timer = new HardwareTimer(Buzzer_Timer_Number);
36  Buzzer_Hw_Timer->setMode(Buzzer_Timer_Channel, TIMER_OUTPUT_COMPARE_PWM1, PIN_BUZZER);
37  Buzzer_Hw_Timer->setOverflow(PERIOD_BUZZER_US, MICROSEC_FORMAT);
39  MICROSEC_COMPARE_FORMAT);
40  digitalWrite(PIN_BUZZER, LOW);
41 }
42 
43 void BuzzerControl_On(void) { Buzzer_Hw_Timer->resume(); }
44 
45 void BuzzerControl_Off(void) {
46  Buzzer_Hw_Timer->pause();
47  digitalWrite(PIN_BUZZER, LOW); // Stops current consumption in the buzzer
48 }
void BuzzerControl_Init(void)
Initialization of HardwareTimer for buzzer.
#define PERIOD_BUZZER_US
void BuzzerControl_On(void)
Switch buzzer ON.
HardwareTimer * Buzzer_Hw_Timer
void BuzzerControl_Off(void)
Switch buzzer OFF.
uint32_t Buzzer_Timer_Channel
#define PIN_BUZZER
Definition: parameters.h:274