From fd50e9006451b8cad45560b84aa9a40b39e88cca Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Fri, 24 Sep 2021 05:45:26 -0600 Subject: added dynamic PWM and FET to MD11 --- hwdef-Noctigon_MD11.h | 39 ++++++++++++++++++--------- spaghetti-monster/anduril/cfg-noctigon-md11.h | 39 +++++++++++++++++---------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/hwdef-Noctigon_MD11.h b/hwdef-Noctigon_MD11.h index ecd9f10..334e17b 100644 --- a/hwdef-Noctigon_MD11.h +++ b/hwdef-Noctigon_MD11.h @@ -5,7 +5,7 @@ * (based on Noctigon K1) * * Pin / Name / Function - * 1 PA6 (none) (PWM1B) (reserved for DD drivers) + * 1 PA6 FET PWM (direct drive) (PWM1B) (on some models) * 2 PA5 R: red aux LED (PWM0B) * 3 PA4 G: green aux LED * 4 PA3 B: blue aux LED @@ -29,9 +29,10 @@ * * Main LED power uses one pin to turn the Opamp on/off, * and one pin to control Opamp power level. - * All brightness control uses the power level pin, with 4 kHz 10-bit PWM. + * Linear brightness control uses the power level pin, with dynamic PWM. * The on/off pin is only used to turn the main LED on and off, * not to change brightness. + * Some models also have a direct-drive FET for turbo. */ #ifdef ATTINY @@ -40,9 +41,10 @@ #define ATTINY 1634 #include -#define PWM_CHANNELS 1 -#define PWM_BITS 10 // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz -#define PWM_TOP 1023 +#define PWM_CHANNELS 2 // override this for the no-FET version +#define PWM_BITS 16 // data type needs 16 bits, not 8 +#define PWM_TOP 255 // highest value used in top half of ramp +#define USE_DYN_PWM // dynamic frequency and speed #define SWITCH_PIN PA7 // pin 20 #define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt @@ -52,6 +54,13 @@ #define PWM1_PIN PB3 // pin 16, Opamp reference #define PWM1_LVL OCR1A // OCR1A is the output compare register for PB3 +#define PWM1_CNT TCNT1 // for dynamic PWM, reset phase + +#define PWM2_PIN PA6 // pin 1, DD FET PWM +#define PWM2_LVL OCR1B // OCR1B is the output compare register for PA6 + +// PWM parameters of both channels are tied together because they share a counter +#define PWM1_TOP ICR1 // holds the TOP value for for variable-resolution PWM #define LED_ENABLE_PIN PB0 // pin 19, Opamp power #define LED_ENABLE_PORT PORTB // control port for PB0 @@ -76,7 +85,7 @@ // Raw ADC readings at 4.4V and 2.2V // calibrate the voltage readout here // estimated / calculated values are: -// (voltage - D1) * (R2/(R2+R1) * 256 / 1.1) +// (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1) // D1, R1, R2 = 0, 330, 100 #ifndef ADC_44 //#define ADC_44 981 // raw value at 4.40V @@ -107,8 +116,9 @@ inline void hwdef_setup() { // Opamp level and Opamp on/off DDRB = (1 << PWM1_PIN) | (1 << LED_ENABLE_PIN); - // aux R/G/B - DDRA = (1 << AUXLED_R_PIN) + // DD FET PWM, aux R/G/B, button LED + DDRA = (1 << PWM2_PIN) + | (1 << AUXLED_R_PIN) | (1 << AUXLED_G_PIN) | (1 << AUXLED_B_PIN) | (1 << BUTTON_LED_PIN) @@ -117,18 +127,21 @@ inline void hwdef_setup() { // configure PWM // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter // pre-scale for timer: N = 1 - // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5) + // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5) // CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6) // COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4) - // COM1B[1:0]: 0,0: PWM OC1B disabled (DS table 12-4) - TCCR1A = (1<