yajuan yue / Mbed 2 deprecated ele350

Dependencies:   mbed

Fork of ele350 by JIAWEI ZHANG

Committer:
GGHHHH
Date:
Thu Nov 05 12:01:26 2015 +0000
Revision:
3:1ad50b4e51a6
ku

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GGHHHH 3:1ad50b4e51a6 1 #include "microphone.h"
GGHHHH 3:1ad50b4e51a6 2
GGHHHH 3:1ad50b4e51a6 3 bool _mic_toggle = false;
GGHHHH 3:1ad50b4e51a6 4 uint8_t _mic_pulses= 0;
GGHHHH 3:1ad50b4e51a6 5 uint8_t _mic_pulses_buffered = 0;
GGHHHH 3:1ad50b4e51a6 6 uint8_t _mic_counter = 0;
GGHHHH 3:1ad50b4e51a6 7 DigitalIn* _mic_d;
GGHHHH 3:1ad50b4e51a6 8 DigitalOut* _mic_clk;
GGHHHH 3:1ad50b4e51a6 9
GGHHHH 3:1ad50b4e51a6 10 void _mic_timer_int()
GGHHHH 3:1ad50b4e51a6 11 {
GGHHHH 3:1ad50b4e51a6 12 // If clock output to mic is low, read data pin and increment
GGHHHH 3:1ad50b4e51a6 13 // pulse count if it's high.
GGHHHH 3:1ad50b4e51a6 14 if (!_mic_toggle) {
GGHHHH 3:1ad50b4e51a6 15 _mic_counter += 1;
GGHHHH 3:1ad50b4e51a6 16 if (_mic_counter == 0) {
GGHHHH 3:1ad50b4e51a6 17 _mic_pulses_buffered = _mic_pulses;
GGHHHH 3:1ad50b4e51a6 18 _mic_pulses = 0;
GGHHHH 3:1ad50b4e51a6 19 }
GGHHHH 3:1ad50b4e51a6 20 if (_mic_d->read()) _mic_pulses++;
GGHHHH 3:1ad50b4e51a6 21 }
GGHHHH 3:1ad50b4e51a6 22
GGHHHH 3:1ad50b4e51a6 23 // Toggle clock output to mic
GGHHHH 3:1ad50b4e51a6 24 _mic_toggle = !_mic_toggle;
GGHHHH 3:1ad50b4e51a6 25 _mic_clk->write(_mic_toggle);
GGHHHH 3:1ad50b4e51a6 26
GGHHHH 3:1ad50b4e51a6 27 // Clear interrupt
GGHHHH 3:1ad50b4e51a6 28 TIM3->SR &= ~TIM_SR_UIF;
GGHHHH 3:1ad50b4e51a6 29 }
GGHHHH 3:1ad50b4e51a6 30
GGHHHH 3:1ad50b4e51a6 31 Microphone::Microphone()
GGHHHH 3:1ad50b4e51a6 32 {
GGHHHH 3:1ad50b4e51a6 33 this->isStarted = false;
GGHHHH 3:1ad50b4e51a6 34 }
GGHHHH 3:1ad50b4e51a6 35
GGHHHH 3:1ad50b4e51a6 36 void Microphone::start()
GGHHHH 3:1ad50b4e51a6 37 {
GGHHHH 3:1ad50b4e51a6 38 if (!this->isStarted) {
GGHHHH 3:1ad50b4e51a6 39 // Two pins: clock to microphone and data from microphone.
GGHHHH 3:1ad50b4e51a6 40 _mic_d = new DigitalIn(PC_3);
GGHHHH 3:1ad50b4e51a6 41 _mic_clk = new DigitalOut(PB_10);
GGHHHH 3:1ad50b4e51a6 42
GGHHHH 3:1ad50b4e51a6 43 // Use timer 3 to sample from the MP45DT02 microphone.
GGHHHH 3:1ad50b4e51a6 44
GGHHHH 3:1ad50b4e51a6 45 // Enable timer.
GGHHHH 3:1ad50b4e51a6 46 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
GGHHHH 3:1ad50b4e51a6 47
GGHHHH 3:1ad50b4e51a6 48 // Scale timer so ticks are 100ns
GGHHHH 3:1ad50b4e51a6 49 uint32_t PCLK = SystemCoreClock / 4;
GGHHHH 3:1ad50b4e51a6 50 uint32_t prescale = PCLK / 10000000; // 10Mhz (100ns ticks)
GGHHHH 3:1ad50b4e51a6 51 TIM3->PSC = prescale - 1;
GGHHHH 3:1ad50b4e51a6 52
GGHHHH 3:1ad50b4e51a6 53 // Enable counter
GGHHHH 3:1ad50b4e51a6 54 TIM3->CR1 |= TIM_CR1_CEN;
GGHHHH 3:1ad50b4e51a6 55
GGHHHH 3:1ad50b4e51a6 56 // Set auto-reset after five ticks (500ns).
GGHHHH 3:1ad50b4e51a6 57 TIM3->ARR = 4;
GGHHHH 3:1ad50b4e51a6 58
GGHHHH 3:1ad50b4e51a6 59 // Re-initialize counter.
GGHHHH 3:1ad50b4e51a6 60 TIM3->EGR |= TIM_EGR_UG;
GGHHHH 3:1ad50b4e51a6 61
GGHHHH 3:1ad50b4e51a6 62 // Set up and enable interrupt for when timer overflows.
GGHHHH 3:1ad50b4e51a6 63 NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int);
GGHHHH 3:1ad50b4e51a6 64 NVIC_EnableIRQ(TIM3_IRQn);
GGHHHH 3:1ad50b4e51a6 65 TIM3->DIER |= TIM_DIER_UIE;
GGHHHH 3:1ad50b4e51a6 66 this->isStarted = true;
GGHHHH 3:1ad50b4e51a6 67 }
GGHHHH 3:1ad50b4e51a6 68 }
GGHHHH 3:1ad50b4e51a6 69
GGHHHH 3:1ad50b4e51a6 70 void Microphone::stop() // Not tested!
GGHHHH 3:1ad50b4e51a6 71 {
GGHHHH 3:1ad50b4e51a6 72 if (this->isStarted) {
GGHHHH 3:1ad50b4e51a6 73 // Disable interrupts.
GGHHHH 3:1ad50b4e51a6 74 NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int);
GGHHHH 3:1ad50b4e51a6 75 NVIC_DisableIRQ(TIM3_IRQn);
GGHHHH 3:1ad50b4e51a6 76 TIM3->DIER &= ~TIM_DIER_UIE;
GGHHHH 3:1ad50b4e51a6 77
GGHHHH 3:1ad50b4e51a6 78 // Disable counter
GGHHHH 3:1ad50b4e51a6 79 TIM3->CR1 &= ~TIM_CR1_CEN;
GGHHHH 3:1ad50b4e51a6 80
GGHHHH 3:1ad50b4e51a6 81 // Disable timer.
GGHHHH 3:1ad50b4e51a6 82 RCC->APB1ENR &= ~RCC_APB1ENR_TIM3EN;
GGHHHH 3:1ad50b4e51a6 83
GGHHHH 3:1ad50b4e51a6 84 // Clear interrupt
GGHHHH 3:1ad50b4e51a6 85 TIM3->SR &= ~TIM_SR_UIF;
GGHHHH 3:1ad50b4e51a6 86
GGHHHH 3:1ad50b4e51a6 87 delete _mic_d; _mic_d = NULL;
GGHHHH 3:1ad50b4e51a6 88 delete _mic_clk; _mic_clk = NULL;
GGHHHH 3:1ad50b4e51a6 89 }
GGHHHH 3:1ad50b4e51a6 90 }
GGHHHH 3:1ad50b4e51a6 91
GGHHHH 3:1ad50b4e51a6 92 int8_t Microphone::read()
GGHHHH 3:1ad50b4e51a6 93 {
GGHHHH 3:1ad50b4e51a6 94 if (this->isStarted) {
GGHHHH 3:1ad50b4e51a6 95 return (int8_t)((uint16_t)_mic_pulses_buffered - 128);
GGHHHH 3:1ad50b4e51a6 96 } else {
GGHHHH 3:1ad50b4e51a6 97 return 0;
GGHHHH 3:1ad50b4e51a6 98 }
GGHHHH 3:1ad50b4e51a6 99 }
GGHHHH 3:1ad50b4e51a6 100