Continuously read the ADC value from a channel of the MCP3xxx and print it to the terminal.
cmake_minimum_required(VERSION 3.12)
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;
const uint16_t DIVISOR = 1024 / 8;
uint16_t data = 0;
uint32_t loopCounter;
uint8_t scaledValue, i;
uint32_t ledOutput;
SimplePort scale(Port::P16, 8, Pin::Dir::OUT);
adc.always_set_spi_mode(0);
pwOut <<
"Welcome to the MCP3xxx demo!\n";
while (1) {
loopCounter = SECOND / 2 +
CNT;
while (abs(loopCounter -
CNT) > MILLISECOND) {
data = adc.read(CHANNEL);
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);
}
}