-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (50 loc) · 1.73 KB
/
main.cpp
File metadata and controls
66 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2023, Christopher Durand
* Copyright (c) 2023, Luiz Carlos Gili
*
* 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>
using namespace Board;
// Complementary output on PWM0 Channel2 High / Low outputs
using OutH2 = GpioC19;
using OutL2 = GpioD26;
int main()
{
Board::initialize();
MODM_LOG_INFO << "PWM Test" << modm::endl;
Pwm0::connect<OutH2::PwmH2, OutL2::PwmL2>();
Pwm0::initialize();
// Period 1500 => MCLK / 1500 = 100 kHz
Pwm0::setPeriod(Pwm0::Channel::Ch2, 1500);
// 500/1500 = 33.3% duty-cycle
Pwm0::setDutyCycle(Pwm0::Channel::Ch2, 500);
constexpr auto mode = Pwm0::ChannelMode()
.setClock(Pwm0::ChannelClock::Mck)
.setDeadTimeGeneration(Pwm0::DeadTimeGeneration::Enabled);
Pwm0::setChannelMode(Pwm0::Channel::Ch2, mode);
// Set 50 ticks dead-time for both high and low output
const auto deadTimeL = 50; // ticks
const auto deadTimeH = 50; // ticks
Pwm0::setDeadTime(Pwm0::Channel::Ch2, deadTimeL, deadTimeH);
// Set all outputs to low in override mode
Pwm0::configureOutputOverrideValues(Pwm0::Outputs_t{});
Pwm0::enableChannelOutput(Pwm0::Channels::Ch2);
while (true)
{
Led0::toggle();
modm::delay(500ms);
// Activate override mode to force outputs to low when button is pressed
const auto outputs = Pwm0::Outputs::Ch2PwmH | Pwm0::Outputs::Ch2PwmL;
if (ButtonSW0::read())
Pwm0::setOutputOverride(outputs, false);
else
Pwm0::clearOutputOverride(outputs, false);
}
return 0;
}