Continuously read the ADC value from a channel of the MCP3000 and print it to the terminal.
cmake_minimum_required(VERSION 3.3)
project(MCP3000_Demo)
create_simple_executable(${PROJECT_NAME} MCP3000_Demo.cpp)
const uint16_t DIVISOR = 1024 / 8;
uint16_t data = 0;
uint32_t loopCounter;
uint8_t scaledValue, i;
uint32_t ledOutput;
pwOut <<
"Welcome to the MCP3000 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;
}
pwOut.
printf(
"Channel %d is reading: %d\n", CHANNEL, data);
}
}