initialise PWM properly

This commit is contained in:
Michael Moon 2010-11-07 16:45:34 +11:00
parent 162a7b2442
commit d57dc22c04
2 changed files with 39 additions and 14 deletions

View File

@ -46,18 +46,40 @@ typedef struct {
int16_t EE_i_limit;
} EE_factor;
EE_factor EEMEM EE_factors[NUM_HEATERS];
EE_factor EEMEM EE_factors[NUM_HEATERS] = {
{
DEFAULT_P,
DEFAULT_I,
DEFAULT_D,
DEFAULT_I_LIMIT
}
};
void heater_init() {
#if NUM_HEATERS > 0
uint8_t i;
// setup pins
for (i = 0; i < NUM_HEATERS; i++) {
*(heaters[i].heater_port) &= ~heaters[i].heater_pin;
*(heaters[i].heater_port) &= ~MASK(heaters[i].heater_pin);
// DDR is always 1 address below PORT. ugly code but saves ram and an extra field in heaters[] which will never be used anywhere but here
*((volatile uint8_t *) (heaters[i].heater_port - 1)) |= heaters[i].heater_pin;
if (heaters[i].heater_pwm)
*((volatile uint8_t *) (heaters[i].heater_port - 1)) |= MASK(heaters[i].heater_pin);
if (heaters[i].heater_pwm) {
*heaters[i].heater_pwm = 0;
switch((uint16_t) heaters[i].heater_pwm) {
case (uint16_t) &OCR0A:
TCCR0A |= MASK(COM0A1);
break;
case (uint16_t) &OCR0B:
TCCR0A |= MASK(COM0B1);
break;
case (uint16_t) &OCR2A:
TCCR2A |= MASK(COM2A1);
break;
case (uint16_t) &OCR2B:
TCCR2A |= MASK(COM2B1);
break;
}
}
}
// read factors from eeprom
@ -112,11 +134,11 @@ void heater_tick(uint8_t h, uint16_t current_temp, uint16_t target_temp) {
// combine factors
int32_t pid_output_intermed = (
(
(((int32_t) heaters_runtime[h].heater_p) * heaters_pid[h].p_factor) +
(((int32_t) heaters_runtime[h].heater_i) * heaters_pid[h].i_factor) +
(((int32_t) heaters_runtime[h].heater_d) * heaters_pid[h].d_factor)
) / PID_SCALE
(
(((int32_t) heaters_runtime[h].heater_p) * heaters_pid[h].p_factor) +
(((int32_t) heaters_runtime[h].heater_i) * heaters_pid[h].i_factor) +
(((int32_t) heaters_runtime[h].heater_d) * heaters_pid[h].d_factor)
) / PID_SCALE
);
// rebase and limit factors
@ -138,13 +160,15 @@ void heater_tick(uint8_t h, uint16_t current_temp, uint16_t target_temp) {
void heater_set(uint8_t index, uint8_t value) {
#if NUM_HEATERS > 0
if (heaters[index].heater_pwm) {
*heaters[index].heater_pwm = value;
*(heaters[index].heater_pwm) = value;
if (debug_flags & DEBUG_PID)
sersendf_P(PSTR("PWM{%u = %u}\n"), index, OCR0A);
}
else {
if (value >= 8)
*heaters[index].heater_port |= MASK(heaters[index].heater_pin);
*(heaters[index].heater_port) |= MASK(heaters[index].heater_pin);
else
*heaters[index].heater_port &= ~MASK(heaters[index].heater_pin);
*(heaters[index].heater_port) &= ~MASK(heaters[index].heater_pin);
}
#endif
}

View File

@ -17,6 +17,7 @@
#include "heater.h"
#include "analog.h"
#include "pinio.h"
#include "arduino.h"
void io_init(void) {
// disable modules we don't use
@ -65,12 +66,12 @@ void io_init(void) {
WRITE(E_STEP_PIN, 0); SET_OUTPUT(E_STEP_PIN);
WRITE(E_DIR_PIN, 0); SET_OUTPUT(E_DIR_PIN);
// setup PWM timer: fast PWM, no prescaler
// setup PWM timers: fast PWM, no prescaler
TCCR0A = MASK(WGM01) | MASK(WGM00);
TCCR0B = MASK(CS00);
TIMSK0 = 0;
OCR0A = 0;
OCR0B = 255;
OCR0B = 0;
TCCR2A = MASK(WGM21) | MASK(WGM20);
TCCR2B = MASK(CS20);