Click on our menu buttons immediately below to find MegaSquirt® information quickly:

Click here for MegaSquirt® MegaManual™ Information, Guides, and Links

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.

Enter your desired Frequency:Hertz
Number of 'ticks':
Period3 (for TunerStudioMS):milliseconds
Actual Frequency: Hertz
Number of PWM states: (incl. full-ON = 100% and full-OFF = 0%)

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.

1 The alternative to bit-banging is to use timer channels. These are built in processor hardware devices that allows the user to set the PWM frequency and duty cycle, and the hardware handles the PWM switching independently of the program code. This not only reduces computational overhead, but can be switched an much higher (or lower speeds) and is also is much more accurate. MS-II™ code from B&G uses the timer channels for fuel and ignition; other codes may use the more crude bit-banging to control the ignition and fuel (check with the code's developer to be sure).

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

MegaSquirt® and MicroSquirt® controllers are experimental devices intended for educational purposes.
MegaSquirt® and MicroSquirt® controllers are not for sale or use on pollution controlled vehicles. Check the laws that apply in your locality to determine if using a MegaSquirt® or MicroSquirt® controller is legal for your application.
© 2012 Bruce Bowling and Al Grippo. All rights reserved.