Let me try to explain the dutycycle algorithm in English.
First i'll provide some context for the algorithm:
The PIC handles temperature control via its built-in PWM. The PWM (pulse width modulator) functions as a current control, switching the current on and off. There is a "frequency" and a "dutycycle" that control the PWM. Frequency is somewhat arbitrary -- in this case i set it to 1000 cycles per second. The "dutycycle" is a 10-bit value that gets loaded into the PWM register, and it functions like a dimmer switch. It specifies how much of each frequency cycle is supplying current to the heater coil : 0 is no current, 1023 is full current for the entire cycle, and a value in between is the fraction of the cycle that the current is ON, in 1/10 % steps. Pretty cool.
i have the PWM configured to run at maximum frequency (with respect to the 4 MHz PIC system clock). My goal was to make sure the audio sound from the switching of the PWM is not audible. Evidently i have failed in this goal -- although i can't hear it (except very rarely), other people say it is very noticable (the sound may prevent teenagers from using this vape! - a built-in underage restricter!), and it certainly annoys the cats.
Here is the basic scenario for a cold start-up sequence:
Load the bud into the vial
Insert vial into oven
Apply battery power to the vape (BLUE led comes on)
(GREEN led flashes 3 times every 5 seconds to remind you the vape is powered)
Hold BUTTON3 for 2 seconds: GREEN led goes to ON until button is released.
Release BUTTON3 - PIC initializes and enters the main loop.
The 7 minute count-down timer starts running.
Every 100 msec (a "tick"), the clock interrupts the main loop to:
--- read the COILTEMP (only every third tick),
--- test for a BUTTON press, and
--- reset the Watch Dog Timer.
(The interrupt code is listed in post #91.)
(It always grates on me that Neo is called a "program writer" by Mr. Rineheart, his manager (as if!). Coder, hacker, programmer, software engineer, even "weenie", would all be more "natural" to my ear. Oh well.)
If for whatever reason the WDT is not reset within 1/4 second, the vape shuts off.
In the main loop, we're waiting for the clock tick.
When the clock interrupt returns to the main loop,
1 - check if maximum run time (7 minutes) has elapsed,
and if so, shut off (damn distracted stoners!)
2 - check for a button press and takes the appropriate action
- Button 1, increase SETPOINT by 10F;
- Button 3, decrease SETPOINT by 10F;
- Button 2, use current COILTEMP as the new SETPOINT.
3 - update the duty cycle value
4 - turn ON the GREEN led if COILTEMP is within 0.5F of SETPOINT
end loop
Anyway, here is a chart i used to think about the algorithm that calculates the next value to load into the dutycycle register:
i had a conceptual breakthrough when i changed the focus of the algorithm from trying to maintain COILTEMP at a SETPOINT temperature, to making the error between COILTEMP and SETPOINT be zero. It's the same thing of course (right?), but it shifted my thinking enough to break through writers block and get the algorithm finished in Feb 2009.