PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
MCP3xxx_Demo.cpp

Continuously read the ADC value from a channel of the MCP3xxx and print it to the terminal.

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(MCP3xxx_Demo)
create_simple_executable(${PROJECT_NAME} MCP3xxx_Demo.cpp)
static const MCP3xxx::PartNumber PART_NUMBER = MCP3xxx::PartNumber::MCP300x;
static const MCP3xxx::Channel CHANNEL = MCP3xxx::Channel::CHANNEL_1;
static const Port::Mask MOSI = Port::Mask::P0;
static const Port::Mask MISO = Port::Mask::P1;
static const Port::Mask SCLK = Port::Mask::P2;
static const Port::Mask CS = Port::Mask::P3;
int main () {
const uint16_t DIVISOR = 1024 / 8;
uint16_t data = 0;
uint32_t loopCounter;
uint8_t scaledValue, i;
uint32_t ledOutput;
SPI spi(MOSI, MISO, SCLK);
MCP3xxx adc(spi, CS, PART_NUMBER);
// Set the Quickstart LEDs for output (used as a secondary display)
SimplePort scale(Port::P16, 8, Pin::Dir::OUT);
// Though this functional call is not necessary (default value is 0), I
// want to bring attention to this function. It will determine whether the
// adc.read* functions will always explicitly set the SPI modes before
// each call, or assume that the SPI cog is still running in the proper
// configuration
adc.always_set_spi_mode(0);
pwOut << "Welcome to the MCP3xxx demo!\n";
while (1) {
loopCounter = SECOND / 2 + CNT;
// Loop over the LED output very quickly, until we are within 1
// millisecond of total period
while (abs(loopCounter - CNT) > MILLISECOND) {
data = adc.read(CHANNEL);
// Turn on LEDs proportional to the analog value
scaledValue = (uint8_t) ((data + DIVISOR / 2 - 1) / DIVISOR);
ledOutput = 0;
for (i = 0; i < scaledValue; ++i)
ledOutput = (ledOutput << 1) | 1;
scale.write(ledOutput);
}
pwOut.printf("Channel %d is reading: %d\n", static_cast<int>(CHANNEL), data);
}
}
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::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
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
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33