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.

76 lines
1.9KB

  1. // Copyright 2009 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Driver for a MCP492x DAC (SPI single/dual 12-bits DAC).
  19. #ifndef AVRLIB_DEVICES_MCP492X_H_
  20. #define AVRLIB_DEVICES_MCP492X_H_
  21. #include "avrlib/spi.h"
  22. #include "avrlib/op.h"
  23. using namespace avrlib;
  24. namespace avrlib {
  25. static const uint8_t kDacSpeed = 2;
  26. enum DacVoltageReference {
  27. BUFFERED_REFERENCE,
  28. UNBUFFERED_REFERENCE
  29. };
  30. template<typename Interface,
  31. DacVoltageReference voltage_reference = UNBUFFERED_REFERENCE,
  32. uint8_t gain = 1>
  33. class Dac {
  34. public:
  35. enum {
  36. buffer_size = 0,
  37. data_size = 8,
  38. };
  39. Dac() { }
  40. static void Init() {
  41. Interface::Init();
  42. }
  43. static inline void Write(uint8_t value) {
  44. Write(value, 0);
  45. }
  46. static inline void Write(uint8_t value, uint8_t channel) {
  47. value = U8Swap4(value);
  48. uint8_t command;
  49. command = (value & 0x0f) | 0x10;
  50. if (channel) {
  51. command |= 0x80;
  52. }
  53. if (voltage_reference == BUFFERED) {
  54. command |= 0x40;
  55. }
  56. if (gain == 1) {
  57. command |= 0x20;
  58. }
  59. Interface::WriteWord(command, value & 0xf0);
  60. }
  61. };
  62. } // namespace avrlib
  63. #endif // AVRLIB_DEVICES_MCP492X_H_