Skip to content

Commit

Permalink
[example] Add MCP990x temperature sensor example
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Aug 10, 2022
1 parent 5bdc8a5 commit 0fd53a8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
64 changes: 64 additions & 0 deletions examples/nucleo_f303re/temperature_mcp990x/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2022, 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/processing.hpp>
#include <modm/driver/temperature/mcp990x.hpp>

using namespace Board;
using namespace std::literals;

using I2c = I2cMaster2;
using Sda = GpioA10;
using Scl = GpioOutputA9;

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

I2c::connect<Sda::Sda, Scl::Scl>();
I2c::initialize<Board::SystemClock, 400_kHz>();

MODM_LOG_INFO << "Welcome to MCP9902/3/4 Test" << modm::endl;

modm::mcp990x::Data data{};
// I2C address MCP990xT-1: 0x4c, -2: 0x4d, -A: adjustable, see datasheet
modm::Mcp990x<I2c> sensor{data, 0x4d};

// wait for sensor boot-up time
modm::delay(15ms);

bool success = RF_CALL_BLOCKING(sensor.initialize());
if (!success)
{
MODM_LOG_ERROR << "Initialization failed" << modm::endl;
}

modm::PeriodicTimer timer{500ms};
while (true)
{
if (timer.execute())
{
if (RF_CALL_BLOCKING(sensor.readInternalTemperature()))
{
MODM_LOG_INFO.printf("temperature: %3.3f\n °C\n", data.getTemperature());
}
else
{
MODM_LOG_INFO << "Reading temperature failed!\n";
}
}
}

return 0;
}

12 changes: 12 additions & 0 deletions examples/nucleo_f303re/temperature_mcp990x/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-f303re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f303re/temperature_mcp990x</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:i2c:2</module>
<module>modm:driver:mcp990x</module>
<module>modm:processing:timer</module>
</modules>
</library>

0 comments on commit 0fd53a8

Please sign in to comment.