Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ele350 by
Revision 3:1ad50b4e51a6, committed 2015-11-05
- Comitter:
- GGHHHH
- Date:
- Thu Nov 05 12:01:26 2015 +0000
- Parent:
- 2:9a0770c4fdd6
- Child:
- 4:badd73a8d806
- Commit message:
- ku
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/microphone.cpp Thu Nov 05 12:01:26 2015 +0000 @@ -0,0 +1,100 @@ +#include "microphone.h" + +bool _mic_toggle = false; +uint8_t _mic_pulses= 0; +uint8_t _mic_pulses_buffered = 0; +uint8_t _mic_counter = 0; +DigitalIn* _mic_d; +DigitalOut* _mic_clk; + +void _mic_timer_int() +{ + // If clock output to mic is low, read data pin and increment + // pulse count if it's high. + if (!_mic_toggle) { + _mic_counter += 1; + if (_mic_counter == 0) { + _mic_pulses_buffered = _mic_pulses; + _mic_pulses = 0; + } + if (_mic_d->read()) _mic_pulses++; + } + + // Toggle clock output to mic + _mic_toggle = !_mic_toggle; + _mic_clk->write(_mic_toggle); + + // Clear interrupt + TIM3->SR &= ~TIM_SR_UIF; +} + +Microphone::Microphone() +{ + this->isStarted = false; +} + +void Microphone::start() +{ + if (!this->isStarted) { + // Two pins: clock to microphone and data from microphone. + _mic_d = new DigitalIn(PC_3); + _mic_clk = new DigitalOut(PB_10); + + // Use timer 3 to sample from the MP45DT02 microphone. + + // Enable timer. + RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; + + // Scale timer so ticks are 100ns + uint32_t PCLK = SystemCoreClock / 4; + uint32_t prescale = PCLK / 10000000; // 10Mhz (100ns ticks) + TIM3->PSC = prescale - 1; + + // Enable counter + TIM3->CR1 |= TIM_CR1_CEN; + + // Set auto-reset after five ticks (500ns). + TIM3->ARR = 4; + + // Re-initialize counter. + TIM3->EGR |= TIM_EGR_UG; + + // Set up and enable interrupt for when timer overflows. + NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int); + NVIC_EnableIRQ(TIM3_IRQn); + TIM3->DIER |= TIM_DIER_UIE; + this->isStarted = true; + } +} + +void Microphone::stop() // Not tested! +{ + if (this->isStarted) { + // Disable interrupts. + NVIC_SetVector(TIM3_IRQn, (uint32_t)_mic_timer_int); + NVIC_DisableIRQ(TIM3_IRQn); + TIM3->DIER &= ~TIM_DIER_UIE; + + // Disable counter + TIM3->CR1 &= ~TIM_CR1_CEN; + + // Disable timer. + RCC->APB1ENR &= ~RCC_APB1ENR_TIM3EN; + + // Clear interrupt + TIM3->SR &= ~TIM_SR_UIF; + + delete _mic_d; _mic_d = NULL; + delete _mic_clk; _mic_clk = NULL; + } +} + +int8_t Microphone::read() +{ + if (this->isStarted) { + return (int8_t)((uint16_t)_mic_pulses_buffered - 128); + } else { + return 0; + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/microphone.h Thu Nov 05 12:01:26 2015 +0000 @@ -0,0 +1,25 @@ +#ifndef _MICROPHONE_H_ +#define _MICROPHONE_H_ + +#include "mbed.h" + +class Microphone +{ + private: + bool isStarted; + public: + // Constructor. + Microphone(); + + // Begins sampling data from the microphone using timer 3. + void start(); + + // Ends sampling (UNTESTED!) + void stop(); + + // Returns a signed 8-bit integer containing the latest data from the microphone. + int8_t read(); +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/soft_pwm.cpp Thu Nov 05 12:01:26 2015 +0000 @@ -0,0 +1,42 @@ +#include "soft_pwm.h" + + +float SoftPwm::getPeriod() +{ + return this->period; +} + +float SoftPwm::getDutyCycle() +{ + return this->dutyCycle; +} + +void SoftPwm::setPeriod(float newPeriod) +{ + this->period = newPeriod; +} + +void SoftPwm::setDutyCycle(float newDutyCycle) +{ + this->dutyCycle = newDutyCycle; +} +SoftPwm::SoftPwm(float initialPeriod, float initialDutyCycle) +{ + + this->period = initialPeriod; + this->dutyCycle = initialDutyCycle; + + pwm.setDutyCycle((1.0f+sin(breathingTimer.read()*M_PI))/2.0f); + this->timer.start(); + +bool SoftPwm::isOn() + + float onPhaseDuration = this->dutyCycle * this->period; + float currentTime = this->timer.reset(); + + if (currentTime < onPhaseDuration){ + return true; + }else { + return false; + } +}