Skip to content

Commit

Permalink
[example] Add MCP3008 SPI ADC example for SAMV71 Xplained Ultra
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Jun 1, 2023
1 parent ef0df70 commit eda224e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
58 changes: 58 additions & 0 deletions examples/samv71_xplained_ultra/mcp3008/main.cpp
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023, Christopher Durand
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/adc/mcp3008.hpp>
#include <utility>

using namespace Board;
using namespace modm::platform;

using SpiMaster = SpiMaster0;
using Sck = GpioD22;
using Mosi = GpioD21;
using Miso = GpioD20;
using Cs = GpioD25;

int main()
{
Board::initialize();

MODM_LOG_INFO << "MCP3004/8 ADC example" << modm::endl;

SpiMaster::connect<Sck::Spck, Mosi::Mosi, Miso::Miso>();
Cs::setOutput(true);

SpiMaster::initialize<SystemClock, 1500_kHz, 10_pct>();

modm::Mcp3008<SpiMaster, Cs> adc;
adc.initialize();

constexpr std::array channels = {
std::make_pair(0, modm::mcp3008::Channel::Ch0),
std::make_pair(1, modm::mcp3008::Channel::Ch1),
std::make_pair(2, modm::mcp3008::Channel::Ch2),
std::make_pair(3, modm::mcp3008::Channel::Ch3)
};

while (true) {
for (auto [i, ch] : channels) {
const auto value = RF_CALL_BLOCKING(adc.read(ch));
MODM_LOG_INFO << "channel " << i << ": " << value << '\n';
}

Led0::toggle();
Led1::toggle();
modm::delay(1s);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/samv71_xplained_ultra/mcp3008/project.xml
@@ -0,0 +1,11 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../build/samv71_xplained_ultra/mcp3008</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:driver:mcp3008</module>
<module>modm:platform:spi:0</module>
</modules>
</library>

0 comments on commit eda224e

Please sign in to comment.