Move PWM and MOSFET implementations into separate module files

This commit is contained in:
2024-09-03 02:24:31 +02:00
parent cd1a481ef6
commit 0e15d5b1e2
8 changed files with 186 additions and 169 deletions

44
src/bus/pwm.c Normal file
View File

@@ -0,0 +1,44 @@
#include "common.h"
#include "bus/pwm.h"
int PWM_Init(void)
{
// PD4: PWM NF-12 Fan Peltier Hot Side
// PD5: PWM NF-A8 Fan Peltier Cold Side
// PD7: PWM NF-R8 Fan Heating Element
DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7);
// Fast mode, non-inverting, no prescaling
TCCR1A = BIT(WGM11) | BIT(COM1A1) | BIT(COM1B1);
TCCR1B = BIT(WGM12) | BIT(WGM13) | BIT(CS10);
TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS20);
ICR1 = PWM_CYCLE_TOP; // 8000 MHz / 25000 KHz
// TODO: Enable 25 KHz frequency for timer 3
OCR1A = FAN01_MIN_DUTY;
OCR1B = FAN02_MIN_DUTY;
OCR2 = FAN03_MIN_DUTY;
return 0;
}
// Port must be PD4, PD5 or PD7 and the value is
// expected to be in the range between 0 and 100
void PWM_SetValue(int port, int value)
{
int n;
if (port != PD4 && port != PD5 && port != PD7)
return; // Invalid port
n = CLAMP(value, 100, 0) * (PWM_CYCLE_TOP / 100.0f);
Info("Setting PWM value to %d...", n);
switch (port) {
case PD4: OCR1B = n; break;
case PD5: OCR1A = n; break;
case PD7: OCR2 = n; break;
}
}