PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
MCP3xxx_Demo.cpp
Go to the documentation of this file.
1 
26 #include <PropWare/PropWare.h>
30 
31 using PropWare::MCP3xxx;
32 using PropWare::Port;
33 using PropWare::SPI;
35 using PropWare::Pin;
36 
38 static const MCP3xxx::PartNumber PART_NUMBER = MCP3xxx::PartNumber::MCP300x;
39 static const MCP3xxx::Channel CHANNEL = MCP3xxx::Channel::CHANNEL_1;
40 
42 static const Port::Mask MOSI = Port::Mask::P0;
44 static const Port::Mask MISO = Port::Mask::P1;
46 static const Port::Mask SCLK = Port::Mask::P2;
48 static const Port::Mask CS = Port::Mask::P3;
49 
57 int main () {
58  const uint16_t DIVISOR = 1024 / 8;
59  uint16_t data = 0;
60  uint32_t loopCounter;
61  uint8_t scaledValue, i;
62  uint32_t ledOutput;
63  SPI spi(MOSI, MISO, SCLK);
64  MCP3xxx adc(spi, CS, PART_NUMBER);
65 
66  // Set the Quickstart LEDs for output (used as a secondary display)
67  SimplePort scale(Port::P16, 8, Pin::Dir::OUT);
68 
69  // Though this functional call is not necessary (default value is 0), I
70  // want to bring attention to this function. It will determine whether the
71  // adc.read* functions will always explicitly set the SPI modes before
72  // each call, or assume that the SPI cog is still running in the proper
73  // configuration
74  adc.always_set_spi_mode(0);
75 
76  pwOut << "Welcome to the MCP3xxx demo!\n";
77 
78  while (1) {
79  loopCounter = SECOND / 2 + CNT;
80 
81  // Loop over the LED output very quickly, until we are within 1
82  // millisecond of total period
83  while (abs(loopCounter - CNT) > MILLISECOND) {
84  data = adc.read(CHANNEL);
85 
86  // Turn on LEDs proportional to the analog value
87  scaledValue = (uint8_t) ((data + DIVISOR / 2 - 1) / DIVISOR);
88  ledOutput = 0;
89  for (i = 0; i < scaledValue; ++i)
90  ledOutput = (ledOutput << 1) | 1;
91  scale.write(ledOutput);
92  }
93 
94  pwOut.printf("Channel %d is reading: %d\n", static_cast<int>(CHANNEL), data);
95  }
96 }
printer.h
PropWare::Printer::printf
void printf(const char fmt[], const T first, const Targs... remaining) const
Similar in functionality to the C-standard function printf.
Definition: printer.h:497
mcp3xxx.h
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::SimplePort::write
void write(uint32_t value) const
Allow easy writing to a port w/o destroying data elsewhere in the port; A shift is performed before w...
Definition: simpleport.h:105
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
CS
static const Port::Mask CS
Definition: MCP3xxx_Demo.cpp:48
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::MCP3xxx::read
uint16_t read(const MCP3xxx::Channel channel)
Read a specific channel's data in single-ended mode.
Definition: mcp3xxx.h:124
SCLK
static const Port::Mask SCLK
Definition: MCP3xxx_Demo.cpp:46
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
simpleport.h
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
PropWare::MCP3xxx
MCP3xxx-series ADC driver using SPI communication for the Parallax Propeller. Compatible with the fol...
Definition: mcp3xxx.h:49
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
MISO
static const Port::Mask MISO
Definition: MCP3xxx_Demo.cpp:44
PropWare::SimplePort
The PropWare::SimplePort is the recommended way to use data ports on the Propeller....
Definition: simpleport.h:36
PART_NUMBER
static const MCP3xxx::PartNumber PART_NUMBER
Definition: MCP3xxx_Demo.cpp:38
SPI
Definition: SPI.h:11
MOSI
static const Port::Mask MOSI
Definition: MCP3xxx_Demo.cpp:42