|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Raphael Lehmann |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <modm/board.hpp> |
| 12 | + |
| 13 | +#undef MODM_LOG_LEVEL |
| 14 | +#define MODM_LOG_LEVEL modm::log::INFO |
| 15 | + |
| 16 | +int |
| 17 | +main() |
| 18 | +{ |
| 19 | + Board::initialize(); |
| 20 | + |
| 21 | + MODM_LOG_INFO << "STM32G4 ADC basic example" << modm::endl; |
| 22 | + MODM_LOG_INFO << " running on Nucleo-G474RE" << modm::endl << modm::endl; |
| 23 | + |
| 24 | + MODM_LOG_INFO << "Configuring ADC ..."; |
| 25 | + Adc1::initialize( |
| 26 | + Adc1::ClockMode::SynchronousPrescaler1, |
| 27 | + Adc1::ClockSource::SystemClock, |
| 28 | + Adc1::Prescaler::Disabled, |
| 29 | + Adc1::CalibrationMode::SingleEndedInputsMode, |
| 30 | + true); |
| 31 | + Adc1::connect<GpioA0::In1>(); |
| 32 | + Adc1::setPinChannel<GpioA0>(Adc1::SampleTime::Cycles13); |
| 33 | + |
| 34 | + while (true) |
| 35 | + { |
| 36 | + Adc1::startConversion(); |
| 37 | + while(!Adc1::isConversionFinished) |
| 38 | + ; |
| 39 | + int adcValue = Adc1::getValue(); |
| 40 | + MODM_LOG_INFO << "adcValue=" << adcValue; |
| 41 | + float voltage = adcValue * 3.3 / 0xfff; |
| 42 | + MODM_LOG_INFO << " voltage="; |
| 43 | + MODM_LOG_INFO.printf("%.3f\n", voltage); |
| 44 | + modm::delay(500ms); |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
0 commit comments