aboutsummaryrefslogtreecommitdiff
path: root/hwdef-Noctigon_MD11.h
diff options
context:
space:
mode:
authorSelene ToyKeeper2021-09-24 05:45:26 -0600
committerSelene ToyKeeper2021-09-24 05:45:26 -0600
commitfd50e9006451b8cad45560b84aa9a40b39e88cca (patch)
tree5217f3e05e4cdc6af4d849bac36acfee8915301f /hwdef-Noctigon_MD11.h
parentjust a few adjustments to start with (diff)
downloadanduril-fd50e9006451b8cad45560b84aa9a40b39e88cca.tar.gz
anduril-fd50e9006451b8cad45560b84aa9a40b39e88cca.tar.bz2
anduril-fd50e9006451b8cad45560b84aa9a40b39e88cca.zip
added dynamic PWM and FET to MD11
Diffstat (limited to 'hwdef-Noctigon_MD11.h')
-rw-r--r--hwdef-Noctigon_MD11.h39
1 files changed, 26 insertions, 13 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 <avr/io.h>
-#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<<WGM11) | (1<<WGM10) // 10-bit (TOP=0x03FF) (DS table 12-5)
+ // COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
+ TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
- | (0<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
+ | (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
;
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
- | (0<<WGM13) | (0<<WGM12) // phase-correct PWM (DS table 12-5)
+ | (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
;
+ // set PWM resolution
+ PWM1_TOP = PWM_TOP;
+
// set up e-switch
//PORTA = (1 << SWITCH_PIN); // TODO: configure PORTA / PORTB / PORTC?
PUEA = (1 << SWITCH_PIN); // pull-up for e-switch