Naze32 clone with Frysky receiver
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AnalogOut.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_ANALOGOUT_H
  17. #define MBED_ANALOGOUT_H
  18. #include "platform.h"
  19. #if DEVICE_ANALOGOUT
  20. #include "analogout_api.h"
  21. namespace mbed {
  22. /** An analog output, used for setting the voltage on a pin
  23. *
  24. * Example:
  25. * @code
  26. * // Make a sawtooth output
  27. *
  28. * #include "mbed.h"
  29. *
  30. * AnalogOut tri(p18);
  31. * int main() {
  32. * while(1) {
  33. * tri = tri + 0.01;
  34. * wait_us(1);
  35. * if(tri == 1) {
  36. * tri = 0;
  37. * }
  38. * }
  39. * }
  40. * @endcode
  41. */
  42. class AnalogOut {
  43. public:
  44. /** Create an AnalogOut connected to the specified pin
  45. *
  46. * @param AnalogOut pin to connect to (18)
  47. */
  48. AnalogOut(PinName pin) {
  49. analogout_init(&_dac, pin);
  50. }
  51. /** Set the output voltage, specified as a percentage (float)
  52. *
  53. * @param value A floating-point value representing the output voltage,
  54. * specified as a percentage. The value should lie between
  55. * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
  56. * Values outside this range will be saturated to 0.0f or 1.0f.
  57. */
  58. void write(float value) {
  59. analogout_write(&_dac, value);
  60. }
  61. /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
  62. *
  63. * @param value 16-bit unsigned short representing the output voltage,
  64. * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
  65. */
  66. void write_u16(unsigned short value) {
  67. analogout_write_u16(&_dac, value);
  68. }
  69. /** Return the current output voltage setting, measured as a percentage (float)
  70. *
  71. * @returns
  72. * A floating-point value representing the current voltage being output on the pin,
  73. * measured as a percentage. The returned value will lie between
  74. * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
  75. *
  76. * @note
  77. * This value may not match exactly the value set by a previous write().
  78. */
  79. float read() {
  80. return analogout_read(&_dac);
  81. }
  82. #ifdef MBED_OPERATORS
  83. /** An operator shorthand for write()
  84. */
  85. AnalogOut& operator= (float percent) {
  86. write(percent);
  87. return *this;
  88. }
  89. AnalogOut& operator= (AnalogOut& rhs) {
  90. write(rhs.read());
  91. return *this;
  92. }
  93. /** An operator shorthand for read()
  94. */
  95. operator float() {
  96. return read();
  97. }
  98. #endif
  99. protected:
  100. dac_t _dac;
  101. };
  102. } // namespace mbed
  103. #endif
  104. #endif