The program worked so well with the PICmicro development board that I thought I would try it on a faster PICmicro which was clocked at 20Mhz rather than the 3.2768Mhz which the PICmicro development board runs at. Oh tragedy! Nothing worked, with the screen starting up the wrong mode and all sorts. After some considerable torment, worrying about delay loops and such, I found the problem.

It seems that sometimes the PICmicro can go faster than its input/output ports. If I set a bit in a port high, and then low immediately afterwards the port doesn't have enough time to react to the change and the signal does not behave correctly. This is a problem of timing which is sometimes called a "glitch". A glitch is engineer's jargon for a pulse which isn't present for a full clock cycle, or which shouldn't be there at all. You must be careful when using PORTA or PORTB in your C because you do not know the order in which the compiler will evaluate an expression. If it uses the port as a register variable it may cause changes to the PORT which will produce glitchy outputs:

PORTB = PORTB + a + b ;

You intend the code above to add a and b to the value in PORTB and then set PORTB to the result. However, the compiler may use PORTB to work out the result. If it does this there may be an instant where PORTB is equal to just PORTB + a. Normally this would not matter, but PORTB is actually providing output. The result of this would be that the signals would not change exactly when you think they should, and you might get tiny pulses (or glitches) as a result of this. If you look at my code you will find that I don't write any code like this, instead I assemble the value in a temporary variable and then assign it to PORTB. I would advise you to follow my example because timing glitches are around the nastiest problems you can hit.

As for my particular glitches, I have added a call to delay after operations which set the values of the ports. This made all my glitch problems go away!