heater-arm.c: respect configured PWM frequencies.

Especially at high frequencies the achieved one is only close,
but that's entirely sufficient for our purposes.

Test: the PWM frequency on the scope should be similar to the
one configures in the board file with DEFINE_HEATER().
This commit is contained in:
Markus Hitter 2015-08-09 17:10:53 +02:00
parent 857fef578b
commit 10310e9d19
2 changed files with 22 additions and 10 deletions

View File

@ -211,17 +211,26 @@ DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, PIO1_0,THERMISTOR_EXTRUDER)
device has the index 0 (zero). device has the index 0 (zero).
Set 'pwm' to ... Set 'pwm' to ...
1 for using PWM on a PWM-able pin and on/off on other pins. frequency in Hertz (Hz) on ARM based controllers to set PWM frequency of
0 for using on/off on a PWM-able pin, too. 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 Using PWM usually gives smoother temperature control but can conflict
with slow switches, like solid state relays. PWM frequency can be with slow switches, like solid state relays. A too high frequency can
influenced globally with FAST_PWM, see below. 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 //DEFINE_HEATERS_START
// name port pwm // name port pwm
DEFINE_HEATER(extruder, PIO0_10, 1) DEFINE_HEATER(extruder, PIO0_10, 20000)
//DEFINE_HEATER(bed, PIO0_11, 1) //DEFINE_HEATER(bed, PIO0_11, 10)
#define HEATER_EXTRUDER HEATER_extruder #define HEATER_EXTRUDER HEATER_extruder
//#define HEATER_BED HEATER_bed //#define HEATER_BED HEATER_bed

View File

@ -115,6 +115,7 @@ void heater_init() {
PIO1_9 CT16B1_MAT0 0x1 --- PIO1_9 CT16B1_MAT0 0x1 ---
*/ */
if (NUM_HEATERS) { // At least one channel in use. if (NUM_HEATERS) { // At least one channel in use.
uint32_t freq;
// Auto-generate pin setup. // Auto-generate pin setup.
#undef DEFINE_HEATER #undef DEFINE_HEATER
@ -132,10 +133,12 @@ void heater_init() {
LPC_IOCON->pin ## _CMSIS = pin ## _PWM; /* Connect to timer. */ \ LPC_IOCON->pin ## _CMSIS = pin ## _PWM; /* Connect to timer. */ \
/*pin ## _TIMER->IR = 0; ( = reset value) No interrupts. */ \ /*pin ## _TIMER->IR = 0; ( = reset value) No interrupts. */ \
pin ## _TIMER->TCR = (1 << 0); /* Enable counter. */ \ pin ## _TIMER->TCR = (1 << 0); /* Enable counter. */ \
/* TODO: set PWM frequency here according to configuration. */ \ freq = F_CPU / PWM_SCALE / pwm; /* Figure PWM freq. */ \
/* TODO: check wether configured freq. is within range (1..65536). */ \ if (freq > 65535) \
pin ## _TIMER->PR = /* Prescaler to */ \ freq = 65535; \
F_CPU / PWM_SCALE / 1000 - 1; /* 1000 Hz. */ \ if (freq < 1) \
freq = 1; \
pin ## _TIMER->PR = freq - 1; /* Prescaler to freq. */ \
pin ## _TIMER->MCR = (1 << 10); /* Reset on Match 3. */ \ pin ## _TIMER->MCR = (1 << 10); /* Reset on Match 3. */ \
/* PWM_SCALE - 1, so match = 255 is full off. */ \ /* PWM_SCALE - 1, so match = 255 is full off. */ \
pin ## _TIMER->MR[3] = PWM_SCALE - 1; /* Match 3 at 254. */ \ pin ## _TIMER->MR[3] = PWM_SCALE - 1; /* Match 3 at 254. */ \