|Click on our menu buttons immediately below to find MegaSquirt® information quickly:|
MegaShift™ and Bit-Banged Outputs
'Bit banging' means that an output port is turned off and on by specific instructions in the code that occur within normal program flow1.
Bit-banging can potentially be used for a number of functions (like dwell control, among many others), but it is not suitable for all purposes. The timing can be affected by the running of other interrupts, delaying the switching by up to milliseconds at times. However, for the purposes of producing pulse width modulated (PWM) outputs in a transmission control application, the PWM accuracy isn't as crucial, and bit-banging is more than adequate.
In GPIO based based systems (with the HCS12 processor running at 24 MHz), the chance to turn these outputs off or on occurs every ~1/8th of a millisecond (one 'tick' every 0.128 milliseconds) when the real time interrupt is called2.
In general, the slower the PWM frequency, the closer the actual the PWM frequency will be to your desired frequency and the more resolution you have in setting the PWM percent, so it is likely to be more accurate too.
The following calculator lets you see the effect of different frequencies on the resolution. It also calculates the the PWM period that you should enter into TunerStudioMS for any given frequency.
The PWM period is limited to 4 to 255 ticks long. This is from 0.512 to 32.64 milliseconds, which is a frequency range of 30.6 Hertz to 1953 Hertz.
Note that any output that is pulse width modulated must have a recirculation diode installed on the controlled device. It does not hurt to have such a diode installed on every output, even if it is ON/OFF.
2 The ~1/8 millisecond interrupt period is because the timer interrupt is called based on the 8 MHz crystal oscillator pulses, and we have the register RTICTL == 0x10 for the smallest possible timer interrupt period. RTICTL = 0x10 (= 16 decimal = 10000 binary) gives a prescale rate of 001 (210, which equals 1024) and a modulus counter of 0000 (÷1), so the divide rate is 210 ÷ 1 = 1024. This gives an RTI interrupt call frequency of 8000000/1024 = 7812.5 Hertz, or a period of 1/7812.5 = 0.000128 second = 0.128 milliseconds.
3 period = 1/frequency