Frequently Asked Questions
Why does a program work with standard I/O but not with fast I/O?First remember that the fast I/O mode does nothing except the I/O. The programmer must set the tri-state registers to establish the direction via SET_TRIS_X(). The SET_TRIS_X() function will set the direction for the entire port (8 bits). A bit set to 1 indicates input and 0 is an output.
For example, to set all pins of port B to outputs except the B7 pin, use the following:
set_tris_b( 0x80 );
Secondly, be aware that fast I/O can be very fast. Consider the following code:
output_high( PIN_B0 ); output_low( PIN_B1 );
This will be implemented with two assembly instructions (BSF 6,0 and BCF 6,1). The microprocessor implements the BSF and BCF as a read of the entire port, a modify of the bit and a write back of the port. In this example, at the time that the BCF is executed, the B0 pin may not have yet stabilized. The previous state of pin B0 will be seen and written to the port with the B1 change. In effect, it will appear as if the high to B0 never happened. With standard and fixed I/O, this is not usually a problem since enough extra instructions are inserted to avoid a problem. The time it takes for a pin to stabilize depends on the load placed on the pin. The following is an example of a fix to the above problem:
output_high( PIN_B0 ); delay_cycles(1); //Delay one instruction time output_high( PIN_B1 );
The delay_cycles(1) will simply insert one NOP between the two I/O commands. At 20mhz a NOP is 0.2 us.