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