FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
square_wave.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef SQUARE_WAVE_HH
22#define SQUARE_WAVE_HH
23
24#include "timer.h"
25#include "pwm.h"
26
27namespace timer
28{
46 template<board::Timer NTIMER_, board::PWMPin OUTPUT_> class SquareWave
47 {
48 public:
49 SquareWave(const SquareWave&) = delete;
50 SquareWave& operator=(const SquareWave&) = delete;
51
53 static constexpr const board::Timer NTIMER = NTIMER_;
55 static constexpr const board::PWMPin OUTPUT = OUTPUT_;
57 static constexpr const board::DigitalPin PIN = board_traits::PWMPin_trait<OUTPUT>::ACTUAL_PIN;
58
65
70 SquareWave() : output_{timer_, timer::TimerOutputMode::TOGGLE}
71 {
72 using TRAIT = board_traits::PWMPin_trait<OUTPUT>;
73 static_assert(TRAIT::COM == 0, "Only OCnA pin is supported for wave generation");
74 }
75
79 const TIMER& timer() const
80 {
81 return timer_;
82 }
83
88 {
89 return timer_;
90 }
91
105 void start_frequency(uint32_t frequency)
106 {
107 const uint32_t period = ONE_SECOND / 2 / frequency;
108 typename TIMER::PRESCALER prescaler = CALC::CTC_prescaler(period);
109 start_frequency(prescaler, CALC::CTC_counter(prescaler, period));
110 }
111
129 void start_frequency(typename TIMER::PRESCALER prescaler, typename TIMER::TYPE counter)
130 {
131 timer_.end();
132 timer_.set_prescaler(prescaler);
133 timer_.begin();
134 output_.set_duty(counter);
135 }
136
140 void stop()
141 {
142 timer_.end();
143 output_.set_duty(0);
144 }
145
146 private:
147 TIMER timer_ = TIMER{timer::TimerMode::CTC, TIMER::PRESCALER::NO_PRESCALING};
148 PWMPIN output_;
149 };
150}
151
152#endif /* SQUARE_WAVE_HH */
void set_duty(TYPE duty)
Set the duty cycle for this PWM pin, from 0 (0% duty cycle) to MAX (100%), any value above MAX will b...
Definition: pwm.h:135
Simple API to generate a square wave to an output pin.
Definition: square_wave.h:47
void start_frequency(typename TIMER::PRESCALER prescaler, typename TIMER::TYPE counter)
Start producing, on the output pin, a square wave, which frequency matches the specified prescaler an...
Definition: square_wave.h:129
static constexpr const board::Timer NTIMER
The AVR timer used for the underlying Timer.
Definition: square_wave.h:53
void start_frequency(uint32_t frequency)
Start producing, on the ouput pin, a square wave with the specified frequency.
Definition: square_wave.h:105
static constexpr const board::DigitalPin PIN
The pin to which this SquareWave generator is connected.
Definition: square_wave.h:57
analog::PWMOutput< OUTPUT > PWMPIN
The analog::PWMOutput type for the OUTPUT board::PWMPin.
Definition: square_wave.h:64
const TIMER & timer() const
Return the underlying timer::Timer of this SquareWave generator.
Definition: square_wave.h:79
TIMER & timer()
Return the underlying timer::Timer of this SquareWave generator.
Definition: square_wave.h:87
static constexpr const board::PWMPin OUTPUT
The board::PWMPin connected to this SquareWave generator.
Definition: square_wave.h:55
timer::Timer< NTIMER > TIMER
The type of underlying timer::Timer used by this SquareWave generator.
Definition: square_wave.h:62
SquareWave()
Instantiate a SquareWave generator.
Definition: square_wave.h:70
void stop()
Stop square wave generation.
Definition: square_wave.h:140
void begin(TYPE max=0)
Start this timer in the currently selected mode, with the provided prescaler value and max value.
Definition: timer.h:948
typename TRAIT::TYPE TYPE
The type of this timer's counter (either uint8_t or uint16_t).
Definition: timer.h:725
typename PRESCALERS_TRAIT::TYPE PRESCALER
The enum type listing all available precaler values for this timer.
Definition: timer.h:735
void end()
Completely stop this timer: timer interrupts are disabled and counter is stopped.
Definition: timer.h:1185
void set_prescaler(PRESCALER prescaler)
Change prescaler for this timer.
Definition: timer.h:928
PWMPin
Defines all digital output pins of target MCU, capable of PWM output.
Definition: empty.h:84
DigitalPin
Defines all available digital input/output pins of the target MCU.
Definition: empty.h:56
Timer
Defines all timers available for target MCU.
Definition: empty.h:112
Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
TimerOutputMode
Defines the "connection" between this timer and specific PWM output pins.
Definition: timer.h:263
@ TOGGLE
Pin is toggled on Compare Match.
@ CTC
Timer "Clear Timer on Compare match" mode: counter is incremented until it reaches "TOP" (OCRxA regis...
PWM API.
Defines a set of calculation methods for the given NTIMER_ The behavior of these methods is specific ...
Definition: timer.h:302
static constexpr TYPE CTC_counter(PRESCALER prescaler, uint32_t us)
Computes the value of counter to use for this timer, in TimerMode::CTC mode, with prescaler,...
Definition: timer.h:423
static constexpr PRESCALER CTC_prescaler(uint32_t us)
Computes the ideal prescaler value to use for this timer, in TimerMode::CTC mode, in order to be able...
Definition: timer.h:390
Timer API.