diff --git a/config/board.gen7-arm.h b/config/board.gen7-arm.h index 7c1239b..fe9fc26 100644 --- a/config/board.gen7-arm.h +++ b/config/board.gen7-arm.h @@ -211,17 +211,26 @@ DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, PIO1_0,THERMISTOR_EXTRUDER) device has the index 0 (zero). Set 'pwm' to ... - 1 for using PWM on a PWM-able pin and on/off on other pins. - 0 for using on/off on a PWM-able pin, too. + frequency in Hertz (Hz) on ARM based controllers to set PWM frequency of + this pin's output. Frequency isn't always accurate, Teacup + will choose the closest possible one. FAST_PWM is ignored + on such controllers. Valid range is 2 to 200'000 Hz. + 1 on AVR based controllers for using Pulse Width Modulation (PWM) + on a pin supporting it. PWM frequency can be influenced only + somewhat and only globally with FAST_PWM. + 0 for using a PWM-able pin in on/off mode. Using PWM usually gives smoother temperature control but can conflict - with slow switches, like solid state relays. PWM frequency can be - influenced globally with FAST_PWM, see below. + with slow switches, like solid state relays. A too high frequency can + overheat MOSFETs; a too low frequency can make your heater to emit audible + noise; so choose wisely. + + Pins which don't allow PWM are always operated in on/off mode. */ //DEFINE_HEATERS_START // name port pwm -DEFINE_HEATER(extruder, PIO0_10, 1) -//DEFINE_HEATER(bed, PIO0_11, 1) +DEFINE_HEATER(extruder, PIO0_10, 20000) +//DEFINE_HEATER(bed, PIO0_11, 10) #define HEATER_EXTRUDER HEATER_extruder //#define HEATER_BED HEATER_bed diff --git a/heater-arm.c b/heater-arm.c index ffc8942..5dcdd47 100644 --- a/heater-arm.c +++ b/heater-arm.c @@ -115,6 +115,7 @@ void heater_init() { PIO1_9 CT16B1_MAT0 0x1 --- */ if (NUM_HEATERS) { // At least one channel in use. + uint32_t freq; // Auto-generate pin setup. #undef DEFINE_HEATER @@ -132,10 +133,12 @@ void heater_init() { LPC_IOCON->pin ## _CMSIS = pin ## _PWM; /* Connect to timer. */ \ /*pin ## _TIMER->IR = 0; ( = reset value) No interrupts. */ \ pin ## _TIMER->TCR = (1 << 0); /* Enable counter. */ \ - /* TODO: set PWM frequency here according to configuration. */ \ - /* TODO: check wether configured freq. is within range (1..65536). */ \ - pin ## _TIMER->PR = /* Prescaler to */ \ - F_CPU / PWM_SCALE / 1000 - 1; /* 1000 Hz. */ \ + freq = F_CPU / PWM_SCALE / pwm; /* Figure PWM freq. */ \ + if (freq > 65535) \ + freq = 65535; \ + if (freq < 1) \ + freq = 1; \ + pin ## _TIMER->PR = freq - 1; /* Prescaler to freq. */ \ pin ## _TIMER->MCR = (1 << 10); /* Reset on Match 3. */ \ /* PWM_SCALE - 1, so match = 255 is full off. */ \ pin ## _TIMER->MR[3] = PWM_SCALE - 1; /* Match 3 at 254. */ \