Configure PWM timers for 25 KHz

This commit is contained in:
2024-09-02 23:12:19 +02:00
parent d60efc009e
commit 4453156641
3 changed files with 29 additions and 28 deletions

View File

@@ -3,7 +3,6 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
//#include <util/atomic.h>
#define USART_BAUDRATE 9600 #define USART_BAUDRATE 9600
#define USART_RXBUF_SIZE 512 #define USART_RXBUF_SIZE 512

View File

@@ -3,15 +3,14 @@
#include <stdarg.h> #include <stdarg.h>
#define BIT(n) (0x1U << (n))
#define UNUSED(s) (void)(s) #define UNUSED(s) (void)(s)
#define BIT(n) (0x1U << (n))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLAMP(n, hi, lo) (MIN(hi, MAX(n, lo))) #define CLAMP(n, hi, lo) (MIN((hi), MAX((n), (lo))))
void Info(const char *fmt, ...); void Info(const char *fmt, ...);
void Error(const char *fmt, ...); void Error(const char *fmt, ...);
double Clamp(double n, double upper, double lower);
#include <util/delay.h> #include <util/delay.h>
#define Sleep(ms) _delay_ms(ms) #define Sleep(ms) _delay_ms(ms)

View File

@@ -6,8 +6,8 @@
#include <assert.h> #include <assert.h>
// Minimum speed for Noctua fans is 20% // Minimum speed for Noctua fans is 20%
#define FAN_01_MIN_DUTY 255 / 100 * 20 #define FAN_01_MIN_DUTY (320 / 100 * 20)
#define FAN_02_MIN_DUTY 255 / 100 * 20 #define FAN_02_MIN_DUTY (320 / 100 * 20)
#define HEATER_MIN_DUTY 0 #define HEATER_MIN_DUTY 0
// ADS1115 (ADC) // ADS1115 (ADC)
@@ -44,12 +44,14 @@ int main(void)
sei(); sei();
Info("Initializing..."); Info("Initializing PWM devices...");
SetTWIChannel(0);
SetFan01Speed(51); SetFan01Speed(320); // PWM PB4
SetFan02Speed(51); SetFan02Speed(320); // PWM PB5
SetHeaterTemp(0); SetHeaterTemp(0); // PWM PB7
SetTWIChannel(0); // I2C MUX
Info("Running update loop...");
for (;;) { for (;;) {
Info("PING"); Info("PING");
@@ -69,7 +71,7 @@ static void SetTWIChannel(int channel)
// PCA9546 I2C Multiplexer // PCA9546 I2C Multiplexer
// ======================= // =======================
Info("Switching TWI channel to %d...", channel); Info("Switching I2C channel to %d...", channel);
// Excerpts taken from the PCA9546A datasheet: // Excerpts taken from the PCA9546A datasheet:
// https://www.ti.com/lit/ds/symlink/pca9546a.pdf // https://www.ti.com/lit/ds/symlink/pca9546a.pdf
@@ -131,7 +133,7 @@ static void SetPinDefaults(void)
// PB2: MOSFET #3 (Lights) // PB2: MOSFET #3 (Lights)
DDRB |= BIT(PB2); // Out DDRB |= BIT(PB2); // Out
PORTB &= ~BIT(PB2); // Low PORTB &= ~BIT(PB2); // High
// PD4: PWM Peltier Hot Side // PD4: PWM Peltier Hot Side
// PD5: PWM Peltier Cold Side // PD5: PWM Peltier Cold Side
@@ -139,19 +141,20 @@ static void SetPinDefaults(void)
DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7); DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7);
// Fast mode, 8-bit, non-inverting, no prescaling // Fast mode, non-inverting, no prescaling
TCCR1A = BIT(WGM10) | BIT(COM1A1); TCCR1A = BIT(WGM11) | BIT(COM1A1) | BIT(COM1B1);
TCCR1B = BIT(WGM12) | BIT(CS10); TCCR1B = BIT(WGM12) | BIT(WGM13) | BIT(CS10);
TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS20); TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS20);
ICR1 = 320; // 8000 MHz / 25000 KHz
// PD4 OC1B (Timer/Counter1 Output CompareB Match Output) // PD4 OC1A Timer1 CompareA
OCR1A = 51; // Minimum speed for Noctua fans at 20% PWM OCR1A = FAN_01_MIN_DUTY;
// PD5 OC1A (Timer/Counter1 Output CompareA Match Output) // PD5 OC1B Timer1 CompareB
OCR1B = 51; // Minimum speed for Noctua fans at 20% PWM OCR1B = FAN_02_MIN_DUTY;
// PD7 OC2 (Timer/Counter2 Output Compare Match Output) // PD7 OC2 Timer2 Compare
OCR2 = 0; // XXX: Set heater low to turn off? OCR2 = HEATER_MIN_DUTY;
// TODO: Update documentation below from ATMega32A sheet... // TODO: Update documentation below from ATMega32A sheet...
// Had to skip the TCR0 timer and use TCR1 and TCR2 instead. // Had to skip the TCR0 timer and use TCR1 and TCR2 instead.
@@ -195,24 +198,24 @@ static void SetPinDefaults(void)
static void SetFan01Speed(int n) static void SetFan01Speed(int n)
{ {
n = CLAMP(n, 0xFF, FAN_01_MIN_DUTY); // n = CLAMP(n, 320, FAN_01_MIN_DUTY);
Info("Setting FAN01 to PWM %d.", n); Info("Setting FAN01 to PWM %d...", n);
OCR1B = (unsigned char) n; // PD4 OCR1B = (unsigned char) n; // PD4
} }
static void SetFan02Speed(int n) static void SetFan02Speed(int n)
{ {
n = CLAMP(n, 0xFF, FAN_02_MIN_DUTY); // n = CLAMP(n, 320, FAN_02_MIN_DUTY);
Info("Setting FAN02 to PWM %d.", n); Info("Setting FAN02 to PWM %d...", n);
OCR1A = (unsigned char) n; // PD5 OCR1A = (unsigned char) n; // PD5
} }
static void SetHeaterTemp(int n) static void SetHeaterTemp(int n)
{ {
n = CLAMP(n, 0xFF, HEATER_MIN_DUTY); // n = CLAMP(n, 320, HEATER_MIN_DUTY);
Info("Setting HEATER to PWM %d.", n); Info("Setting HEATER to PWM %d...", n);
OCR2 = (unsigned char) n; // PD7 OCR2 = (unsigned char) n; // PD7
} }