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.
SoftPdmOut.h
00001 /******************************************************************************************************/ 00002 /* Pulse Density Modulation driver. */ 00003 /* FILE: PDM.h */ 00004 /* */ 00005 /* Code based on an article and source code released by Ken Wada from Aurium Technologies Inc. */ 00006 /* http://www.embedded.com/electronics-blogs/embedded-round-table/4414405/Pulse-density-modulation */ 00007 /* */ 00008 /******************************************************************************************************/ 00009 00010 #ifndef __PDM_BB_H 00011 #define __PDM_BB_H 00012 00013 #include "mbed.h" 00014 00015 /** Class to use software defined Pulse Density Modulation (PDM). 00016 * 00017 * 'pdm' pin can be any digital pin. 00018 * 00019 \code 00020 //Example 00021 //------- 00022 #include "mbed.h" 00023 #include "SoftPdmOut.h" 00024 00025 SoftPdmOut pdm(LED1); 00026 00027 int main() 00028 { 00029 float pdmSet = 0.0f; 00030 float pdmAdd = 0.01f; 00031 00032 // Continuously cycle the output 00033 while(1) 00034 { 00035 pdm = pdmSet; 00036 wait_ms(10); 00037 if(pdmSet >= 1.0f) 00038 pdmAdd = -0.01f; 00039 if(pdmSet <= 0.0f) 00040 pdmAdd = 0.01f; 00041 pdmSet += pdmAdd; 00042 } 00043 } 00044 \endcode 00045 */ 00046 00047 class SoftPdmOut { 00048 public: 00049 /** Create a PDM object connected to a digital pin. 00050 * 00051 * @param pdm : Digital pin. 00052 * @param PulseWidth : Optional - The desired pulse width in microseconds (default = 500us). 00053 * @param Dmax : Optional - This is the total number of states in the DAC output (default = 64). 00054 * @param StartLevel : Optional - The DAC percentage (0.0 to 1.0) to be preprogrammed upon initialization (default = 0). 00055 * @return none 00056 */ 00057 SoftPdmOut(PinName pdm, uint32_t PulseWidth = 500, uint32_t Dmax = 64, float StartLevel = 0); 00058 00059 /** Start the PDM. 00060 * @param none 00061 * @return none 00062 */ 00063 void start(void); 00064 00065 /** Stop the PDM. 00066 * @param idleState (optional, allows the user to define the idle state - default = 0) 00067 * @return none 00068 */ 00069 void stop(bool idleState = 0); 00070 00071 /** Change the pulse width. 00072 * @param level : The desired pulse width in microseconds. 00073 * @return none 00074 */ 00075 void PulseWidth(uint32_t level); 00076 00077 /** Read the pulse width. 00078 * @param none. 00079 * @return current PulseWidth value 00080 */ 00081 uint32_t getPulseWidth(void); 00082 00083 /** Change the total number of states in the DAC output (DMAX). 00084 * @param level : The desired max. level. 00085 * @return none. 00086 */ 00087 void Dmax(uint32_t level); 00088 00089 /** Read the total number of states in the DAC output (DMAX). 00090 * @param none. 00091 * @return current DMAX value. 00092 */ 00093 uint32_t getDmax(void); 00094 00095 /** Set the PDM level, specified as a percentage (float). 00096 * @param level 00097 * @return none 00098 */ 00099 void write(float level); 00100 00101 /** Return the current PDM level as a percentage (float). 00102 * @param none 00103 * @return level 00104 */ 00105 float read(void); 00106 00107 /** 00108 * An operator shorthand for read() 00109 */ 00110 operator float(); 00111 00112 /** 00113 * An operator shorthand for write() 00114 */ 00115 SoftPdmOut& operator= (float value); 00116 00117 private: 00118 Ticker _pdmTicker; 00119 DigitalOut _pdm; 00120 uint32_t _PulseWidth; 00121 uint32_t _Level; 00122 uint32_t _Dmax; 00123 uint32_t _accumulator; 00124 bool _running; 00125 void pdmTick(void); 00126 }; 00127 #endif
Generated on Wed Jul 20 2022 00:19:18 by
1.7.2