#include <TinyDebug.h>
#undef F_CPU
#define F_CPU 10000000UL
constexpr uint16_t HERTZ {20};
constexpr uint8_t CALC_PSCIDX() {
return (F_CPU / (1UL * HERTZ)) <= 65535U ? 0
: (F_CPU / (2UL * HERTZ)) <= 65535U ? 1
: (F_CPU / (4UL * HERTZ)) <= 65535U ? 2
: (F_CPU / (8UL * HERTZ)) <= 65535U ? 3
: (F_CPU / (16UL * HERTZ)) <= 65535U ? 4
: (F_CPU / (64UL * HERTZ)) <= 65535U ? 5
: (F_CPU / (256UL * HERTZ)) <= 65535U ? 6
: (F_CPU / (1024UL * HERTZ)) <= 65535U ? 7
: 8;
}
constexpr uint16_t PRESCALER[] {1, 2, 4, 8, 16, 64, 256, 1024, 0};
constexpr uint16_t CMPA { F_CPU / (HERTZ * PRESCALER[CALC_PSCIDX()])};
void setCMPA() {
uint32_t tempperiod {F_CPU / HERTZ};
uint8_t presc {0};
while (tempperiod > 65535 && presc < 7) {
presc++;
Debug.print(presc); Debug.print(" ");
tempperiod = tempperiod >> (presc > 4 ? 2 : 1);
Debug.println(tempperiod);
}
Debug.println(tempperiod);
Debug.println(presc << 1);
Debug.println(presc);
// cmpA = tempperiod / 2;
}
void setup() {
Debug.begin();
setCMPA();
Debug.println("----------------");
Debug.println(CMPA);
Debug.println(CALC_PSCIDX() << 1);
Debug.println(CALC_PSCIDX());
}
void loop() {}