PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
mcp3xxx.h
Go to the documentation of this file.
1 
27 #pragma once
28 
29 #include <PropWare/PropWare.h>
31 
32 namespace PropWare {
33 
49 class MCP3xxx {
50  public:
61  };
62 
73  };
74 
76  enum class PartNumber {MCP300x = 11,MCP320x = 13,MCP330x = 14
80  };
81 
82  public:
93  MCP3xxx (SPI &spi, const PropWare::Pin::Mask cs, MCP3xxx::PartNumber partNumber,
94  const bool alwaysSetSPIMode = false)
95  : m_spi(&spi),
96  m_alwaysSetMode(alwaysSetSPIMode),
97  m_dataWidth(static_cast<uint8_t>(partNumber)) {
98  this->m_spi->set_mode(SPI_MODE);
99  this->m_spi->set_bit_mode(SPI_BITMODE);
100 
101  this->m_cs.set_mask(cs);
102  this->m_cs.set();
103  this->m_cs.set_dir_out();
104  }
105 
113  void always_set_spi_mode (const bool alwaysSetMode) {
114  this->m_alwaysSetMode = alwaysSetMode;
115  }
116 
124  uint16_t read (const MCP3xxx::Channel channel) {
125  int8_t options;
126  uint16_t dat;
127 
128  options = START | static_cast<int8_t>(SINGLE_ENDED) | static_cast<int8_t>(channel);
129 
130  // Two dead bits between output and input - see page 19 of datasheet
131  options <<= 2;
132 
133  if (this->m_alwaysSetMode) {
134  this->m_spi->set_mode(MCP3xxx::SPI_MODE);
135  this->m_spi->set_bit_mode(MCP3xxx::SPI_BITMODE);
136  }
137 
138  this->m_cs.clear();
139  this->m_spi->shift_out(OPTION_WIDTH, (uint32_t) options);
140  dat = (uint16_t) this->m_spi->shift_in(this->m_dataWidth);
141  this->m_cs.set();
142 
143  return dat;
144  }
145 
155  uint16_t read_diff (const MCP3xxx::ChannelDiff channels) {
156  int8_t options;
157  uint16_t dat;
158 
159  options = START | static_cast<int8_t>(DIFFERENTIAL) | static_cast<int8_t>(channels);
160 
161  // Two dead bits between output and input - see page 19 of datasheet
162  options <<= 2;
163 
164  if (this->m_alwaysSetMode) {
165  this->m_spi->set_mode(SPI_MODE);
166  this->m_spi->set_bit_mode(SPI_BITMODE);
167  }
168 
169  this->m_cs.clear();
170  this->m_spi->shift_out(OPTION_WIDTH, (uint32_t) options);
171  dat = (uint16_t) this->m_spi->shift_in(this->m_dataWidth);
172  this->m_cs.set();
173 
174  return dat;
175  }
176 
177  protected:
178  static const SPI::Mode SPI_MODE = SPI::Mode::MODE_2;
179  static const SPI::BitMode SPI_BITMODE = SPI::BitMode::MSB_FIRST;
180 
181  static const uint8_t START = BIT_4;
182  static const uint8_t SINGLE_ENDED = BIT_3;
183  static const uint8_t DIFFERENTIAL = 0;
184  static const uint8_t OPTION_WIDTH = 7;
185 
186  protected:
187  SPI *m_spi;
188  PropWare::Pin m_cs;
189  bool m_alwaysSetMode;
190  uint8_t m_dataWidth;
191 };
192 
193 }
PropWare::MCP3xxx::Channel::CHANNEL_5
@ CHANNEL_5
PropWare::Port::set_dir_out
void set_dir_out() const
Set the port for output.
Definition: port.h:210
PropWare::SPI::set_bit_mode
void set_bit_mode(const BitMode bitmode)
Set the bitmode of SPI communication.
Definition: spi.h:181
PropWare::MCP3xxx::ChannelDiff::DIFF_6_7
@ DIFF_6_7
PropWare::Pin::set_mask
void set_mask(const Pin::Mask mask)
Definition: pin.h:80
PropWare::MCP3xxx::PartNumber::MCP320x
@ MCP320x
PropWare::MCP3xxx::Channel::CHANNEL_7
@ CHANNEL_7
PropWare::MCP3xxx::ChannelDiff::DIFF_2_3
@ DIFF_2_3
PropWare::MCP3xxx::read_diff
uint16_t read_diff(const MCP3xxx::ChannelDiff channels)
Read a specific axis's data in differential mode.
Definition: mcp3xxx.h:155
PropWare::MCP3xxx::ChannelDiff::DIFF_1_0
@ DIFF_1_0
PropWare::MCP3xxx::always_set_spi_mode
void always_set_spi_mode(const bool alwaysSetMode)
Choose whether to always set the SPI mode and bitmode before reading or writing to the ADC; Useful wh...
Definition: mcp3xxx.h:113
PropWare::MCP3xxx::Channel::CHANNEL_3
@ CHANNEL_3
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
PropWare::SPI::shift_in
uint32_t shift_in(const unsigned int bits) const
Read a value from the MISO line.
Definition: spi.h:242
PropWare::Port::Mask
Mask
Definition: port.h:43
spi.h
PropWare::MCP3xxx::PartNumber
PartNumber
Definition: mcp3xxx.h:76
PropWare::MCP3xxx::ChannelDiff::DIFF_7_6
@ DIFF_7_6
PropWare::MCP3xxx::Channel::CHANNEL_6
@ CHANNEL_6
PropWare::MCP3xxx::ChannelDiff::DIFF_3_2
@ DIFF_3_2
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::MCP3xxx::Channel
Channel
Definition: mcp3xxx.h:52
PropWare::MCP3xxx::read
uint16_t read(const MCP3xxx::Channel channel)
Read a specific channel's data in single-ended mode.
Definition: mcp3xxx.h:124
PropWare::SPI::Mode::MODE_2
@ MODE_2
PropWare::MCP3xxx::ChannelDiff::DIFF_4_5
@ DIFF_4_5
PropWare::MCP3xxx::Channel::CHANNEL_1
@ CHANNEL_1
PropWare.h
PropWare::SPI::shift_out
void shift_out(uint8_t bits, uint32_t value) const
Send a value out to a peripheral device.
Definition: spi.h:224
PropWare::MCP3xxx::Channel::CHANNEL_0
@ CHANNEL_0
PropWare::MCP3xxx::PartNumber::MCP300x
@ MCP300x
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::MCP3xxx
MCP3xxx-series ADC driver using SPI communication for the Parallax Propeller. Compatible with the fol...
Definition: mcp3xxx.h:49
PropWare::SPI::set_mode
void set_mode(const Mode mode)
Set the mode of SPI communication.
Definition: spi.h:166
PropWare::MCP3xxx::ChannelDiff::DIFF_0_1
@ DIFF_0_1
PropWare::MCP3xxx::Channel::CHANNEL_2
@ CHANNEL_2
PropWare::MCP3xxx::ChannelDiff::DIFF_5_4
@ DIFF_5_4
PropWare::MCP3xxx::PartNumber::MCP330x
@ MCP330x
PropWare::MCP3xxx::Channel::CHANNEL_4
@ CHANNEL_4
PropWare::SPI::BitMode
BitMode
Determine if data is communicated with the LSB or MSB sent/received first.
Definition: spi.h:78
PropWare::MCP3xxx::MCP3xxx
MCP3xxx(SPI &spi, const PropWare::Pin::Mask cs, MCP3xxx::PartNumber partNumber, const bool alwaysSetSPIMode=false)
Construction requires an instance of the SPI module; the SPI module does not need to be started.
Definition: mcp3xxx.h:93
PropWare::MCP3xxx::ChannelDiff
ChannelDiff
Definition: mcp3xxx.h:64
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249
PropWare::SPI::Mode
Mode
Descriptor for SPI signal as defined by Motorola modes.
Definition: spi.h:66