66. Hardware Timer Default Constructor

#include <iostream>
using namespace std;

class HardwareTimer {
private:
    int frequency;
    bool enabled;
    int prescaler;

public:
    // Default constructor sets safe reset values
    HardwareTimer() : frequency(0), enabled(false), prescaler(1) {}

    void configure(int freq, int pre) {
        frequency = freq;
        prescaler = pre;
        enabled = true;
    }

    void stop() {
        enabled = false;
    }

    void print() {
        cout << "F=" << frequency
             << " P=" << prescaler
             << " EN=" << (enabled ? 1 : 0);
    }
};

int main() {
    HardwareTimer t;  // uses default constructor

    int f, p, x;
    cin >> f >> p >> x;

    t.configure(f, p);

    if (x == 0)
        t.stop();

    t.print();
    return 0;
}

Explanation & Logic Summary:

  • The default constructor initializes hardware timer fields to a safe reset-like state.
  • All members start with known values before configuration.
  • configure() activates the timer and sets operating parameters.
  • stop() disables the timer, simulating hardware shutdown behavior.

Firmware Relevance & Real-World Context:

  • MCU timers (STM32, AVR, ESP32, etc.) start in reset-safe states.
  • Constructors replicate power-on reset behavior.
  • Embedded firmware must avoid enabling peripherals with undefined values.
  • This exercise mirrors real initialization patterns for timers such as TIM, SysTick, and PWM units.

 

 

 

 

Loading...

Input

1000 8 1

Expected Output

F=1000 P=8 EN=1