Configure PWM timers for 25 KHz
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
//#include <util/atomic.h>
|
||||
|
||||
#define USART_BAUDRATE 9600
|
||||
#define USART_RXBUF_SIZE 512
|
||||
|
||||
@@ -3,15 +3,14 @@
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#define BIT(n) (0x1U << (n))
|
||||
#define UNUSED(s) (void)(s)
|
||||
#define BIT(n) (0x1U << (n))
|
||||
#define MAX(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 Error(const char *fmt, ...);
|
||||
double Clamp(double n, double upper, double lower);
|
||||
|
||||
#include <util/delay.h>
|
||||
#define Sleep(ms) _delay_ms(ms)
|
||||
|
||||
51
src/main.c
51
src/main.c
@@ -6,8 +6,8 @@
|
||||
#include <assert.h>
|
||||
|
||||
// Minimum speed for Noctua fans is 20%
|
||||
#define FAN_01_MIN_DUTY 255 / 100 * 20
|
||||
#define FAN_02_MIN_DUTY 255 / 100 * 20
|
||||
#define FAN_01_MIN_DUTY (320 / 100 * 20)
|
||||
#define FAN_02_MIN_DUTY (320 / 100 * 20)
|
||||
#define HEATER_MIN_DUTY 0
|
||||
|
||||
// ADS1115 (ADC)
|
||||
@@ -44,12 +44,14 @@ int main(void)
|
||||
|
||||
sei();
|
||||
|
||||
Info("Initializing...");
|
||||
SetTWIChannel(0);
|
||||
Info("Initializing PWM devices...");
|
||||
|
||||
SetFan01Speed(51);
|
||||
SetFan02Speed(51);
|
||||
SetHeaterTemp(0);
|
||||
SetFan01Speed(320); // PWM PB4
|
||||
SetFan02Speed(320); // PWM PB5
|
||||
SetHeaterTemp(0); // PWM PB7
|
||||
SetTWIChannel(0); // I2C MUX
|
||||
|
||||
Info("Running update loop...");
|
||||
|
||||
for (;;) {
|
||||
Info("PING");
|
||||
@@ -69,7 +71,7 @@ static void SetTWIChannel(int channel)
|
||||
// PCA9546 I2C Multiplexer
|
||||
// =======================
|
||||
|
||||
Info("Switching TWI channel to %d...", channel);
|
||||
Info("Switching I2C channel to %d...", channel);
|
||||
|
||||
// Excerpts taken from the PCA9546A datasheet:
|
||||
// https://www.ti.com/lit/ds/symlink/pca9546a.pdf
|
||||
@@ -131,7 +133,7 @@ static void SetPinDefaults(void)
|
||||
// PB2: MOSFET #3 (Lights)
|
||||
|
||||
DDRB |= BIT(PB2); // Out
|
||||
PORTB &= ~BIT(PB2); // Low
|
||||
PORTB &= ~BIT(PB2); // High
|
||||
|
||||
// PD4: PWM Peltier Hot Side
|
||||
// PD5: PWM Peltier Cold Side
|
||||
@@ -139,19 +141,20 @@ static void SetPinDefaults(void)
|
||||
|
||||
DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7);
|
||||
|
||||
// Fast mode, 8-bit, non-inverting, no prescaling
|
||||
TCCR1A = BIT(WGM10) | BIT(COM1A1);
|
||||
TCCR1B = BIT(WGM12) | BIT(CS10);
|
||||
// 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 = 320; // 8000 MHz / 25000 KHz
|
||||
|
||||
// PD4 OC1B (Timer/Counter1 Output CompareB Match Output)
|
||||
OCR1A = 51; // Minimum speed for Noctua fans at 20% PWM
|
||||
// PD4 OC1A Timer1 CompareA
|
||||
OCR1A = FAN_01_MIN_DUTY;
|
||||
|
||||
// PD5 OC1A (Timer/Counter1 Output CompareA Match Output)
|
||||
OCR1B = 51; // Minimum speed for Noctua fans at 20% PWM
|
||||
// PD5 OC1B Timer1 CompareB
|
||||
OCR1B = FAN_02_MIN_DUTY;
|
||||
|
||||
// PD7 OC2 (Timer/Counter2 Output Compare Match Output)
|
||||
OCR2 = 0; // XXX: Set heater low to turn off?
|
||||
// PD7 OC2 Timer2 Compare
|
||||
OCR2 = HEATER_MIN_DUTY;
|
||||
|
||||
// TODO: Update documentation below from ATMega32A sheet...
|
||||
// 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)
|
||||
{
|
||||
n = CLAMP(n, 0xFF, FAN_01_MIN_DUTY);
|
||||
Info("Setting FAN01 to PWM %d.", n);
|
||||
// n = CLAMP(n, 320, FAN_01_MIN_DUTY);
|
||||
Info("Setting FAN01 to PWM %d...", n);
|
||||
|
||||
OCR1B = (unsigned char) n; // PD4
|
||||
}
|
||||
|
||||
static void SetFan02Speed(int n)
|
||||
{
|
||||
n = CLAMP(n, 0xFF, FAN_02_MIN_DUTY);
|
||||
Info("Setting FAN02 to PWM %d.", n);
|
||||
// n = CLAMP(n, 320, FAN_02_MIN_DUTY);
|
||||
Info("Setting FAN02 to PWM %d...", n);
|
||||
|
||||
OCR1A = (unsigned char) n; // PD5
|
||||
}
|
||||
|
||||
static void SetHeaterTemp(int n)
|
||||
{
|
||||
n = CLAMP(n, 0xFF, HEATER_MIN_DUTY);
|
||||
Info("Setting HEATER to PWM %d.", n);
|
||||
// n = CLAMP(n, 320, HEATER_MIN_DUTY);
|
||||
Info("Setting HEATER to PWM %d...", n);
|
||||
|
||||
OCR2 = (unsigned char) n; // PD7
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user