-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example] Add MCP3008 SPI ADC example for SAMV71 Xplained Ultra
- Loading branch information
1 parent
ef0df70
commit eda224e
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |