Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PwmOut.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_PWMOUT_H
  17. #define MBED_PWMOUT_H
  18. #include "platform.h"
  19. #if DEVICE_PWMOUT
  20. #include "pwmout_api.h"
  21. namespace mbed {
  22. /** A pulse-width modulation digital output
  23. *
  24. * Example
  25. * @code
  26. * // Fade a led on.
  27. * #include "mbed.h"
  28. *
  29. * PwmOut led(LED1);
  30. *
  31. * int main() {
  32. * while(1) {
  33. * led = led + 0.01;
  34. * wait(0.2);
  35. * if(led == 1.0) {
  36. * led = 0;
  37. * }
  38. * }
  39. * }
  40. * @endcode
  41. *
  42. * @note
  43. * On the LPC1768 and LPC2368, the PWMs all share the same
  44. * period - if you change the period for one, you change it for all.
  45. * Although routines that change the period maintain the duty cycle
  46. * for its PWM, all other PWMs will require their duty cycle to be
  47. * refreshed.
  48. */
  49. class PwmOut {
  50. public:
  51. /** Create a PwmOut connected to the specified pin
  52. *
  53. * @param pin PwmOut pin to connect to
  54. */
  55. PwmOut(PinName pin) {
  56. pwmout_init(&_pwm, pin);
  57. }
  58. /** Set the ouput duty-cycle, specified as a percentage (float)
  59. *
  60. * @param value A floating-point value representing the output duty-cycle,
  61. * specified as a percentage. The value should lie between
  62. * 0.0f (representing on 0%) and 1.0f (representing on 100%).
  63. * Values outside this range will be saturated to 0.0f or 1.0f.
  64. */
  65. void write(float value) {
  66. pwmout_write(&_pwm, value);
  67. }
  68. /** Return the current output duty-cycle setting, measured as a percentage (float)
  69. *
  70. * @returns
  71. * A floating-point value representing the current duty-cycle being output on the pin,
  72. * measured as a percentage. The returned value will lie between
  73. * 0.0f (representing on 0%) and 1.0f (representing on 100%).
  74. *
  75. * @note
  76. * This value may not match exactly the value set by a previous <write>.
  77. */
  78. float read() {
  79. return pwmout_read(&_pwm);
  80. }
  81. /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
  82. *
  83. * @note
  84. * The resolution is currently in microseconds; periods smaller than this
  85. * will be set to zero.
  86. */
  87. void period(float seconds) {
  88. pwmout_period(&_pwm, seconds);
  89. }
  90. /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
  91. */
  92. void period_ms(int ms) {
  93. pwmout_period_ms(&_pwm, ms);
  94. }
  95. /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
  96. */
  97. void period_us(int us) {
  98. pwmout_period_us(&_pwm, us);
  99. }
  100. /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
  101. */
  102. void pulsewidth(float seconds) {
  103. pwmout_pulsewidth(&_pwm, seconds);
  104. }
  105. /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
  106. */
  107. void pulsewidth_ms(int ms) {
  108. pwmout_pulsewidth_ms(&_pwm, ms);
  109. }
  110. /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
  111. */
  112. void pulsewidth_us(int us) {
  113. pwmout_pulsewidth_us(&_pwm, us);
  114. }
  115. #ifdef MBED_OPERATORS
  116. /** A operator shorthand for write()
  117. */
  118. PwmOut& operator= (float value) {
  119. write(value);
  120. return *this;
  121. }
  122. PwmOut& operator= (PwmOut& rhs) {
  123. write(rhs.read());
  124. return *this;
  125. }
  126. /** An operator shorthand for read()
  127. */
  128. operator float() {
  129. return read();
  130. }
  131. #endif
  132. protected:
  133. pwmout_t _pwm;
  134. };
  135. } // namespace mbed
  136. #endif
  137. #endif