39 lines
1.1 KiB
C++
39 lines
1.1 KiB
C++
LP_TIMER(1000, []() {
|
|
int power = Output * 255 / 100;
|
|
setPWMDutyCycle(power);
|
|
});
|
|
|
|
// Function to configure Timer1 for PWM with the specified window duration
|
|
void configureTimer1(unsigned long windowMillis) {
|
|
// Disable interrupts while configuring the timer
|
|
noInterrupts();
|
|
|
|
// Reset Timer/Counter1 Control Registers
|
|
TCCR1A = 0;
|
|
TCCR1B = 0;
|
|
TCNT1 = 0;
|
|
|
|
// Set the prescaler to 256
|
|
TCCR1B |= (1 << CS12); // Set prescaler to 256
|
|
|
|
// Calculate the top value for Timer1 based on the window duration
|
|
unsigned long ticks = (62500 * windowMillis) / 1000 / 2;
|
|
|
|
// Set the top value for Timer1 (ICR1) based on the calculated ticks
|
|
ICR1 = ticks - 1; // Subtract 1 because the counter starts at 0
|
|
|
|
// Configure Timer1 for Phase Correct PWM with ICR1 as TOP
|
|
// Enable non-inverted PWM on OC1B (Pin 10)
|
|
TCCR1A = (1 << WGM11) | (1 << COM1B1); // Only configure OC1B for PWM
|
|
|
|
// Set Phase Correct PWM mode with ICR1 as TOP
|
|
TCCR1B |= (1 << WGM13); // No WGM12
|
|
|
|
// Enable interrupts again
|
|
interrupts();
|
|
}
|
|
|
|
void setPWMDutyCycle(uint8_t duty) {
|
|
OCR1B = (unsigned long)duty * ICR1 / 255; // Set the duty cycle for Pin 10 (OC1B)
|
|
}
|