Dynamic Input/Output Changing

Has anyone made a bi-direction bus using the I/Os on the Robot Controller? That is, has anybody dynamically changed I/Os from input to output using software control?

If you mean so that at one point in the code we read in, and then later switch it and put something out, then maybe even later again read in… Nope, I haven’t. :smiley:

It’s possible (I think, it’s been a while since I’ve done anything with the FRC, but…).

Was that just a question in general, or are you don’t something with it?

I have done this and there are no problems doing it. All you have to do is set the TRIS bit(digital_io_xx flag) to either an input or an output. This presents no special problems and your direction change will take effect immediately.

----------- A short Explanation of the IO pins on a PIC --------
The PIC18F8520 inside the robot controller has 8 groups of I/O lines. The groups are lettered A,B,C,D,E,F,G, and H. Each one of these groups consists of 8 pins. On the robot controller we have access to only 18 of these general purpose I/O pins. They correspond to the digital IO pins on the RC as follow:

[digital input on RC: group and pin number (processor pin name)]
digital I/O 01: group B pin 2 (RB2)
digital I/O 02: group B pin 3 (RB3)
digital I/O 03: group B pin 4 (RB4)
digital I/O 04: group B pin 5 (RB5)
digital I/O 05: group B pin 6 (RB6)
digital I/O 06: group B pin 7 (RB7)

digital I/O 07: group H pin 0 (RH0)
digital I/O 08: group H pin 1 (RH1)
digital I/O 09: group H pin 2 (RH2)
digital I/O 10: group H pin 3 (RH3)

digital I/O 11: group J pin 1 (RJ1)
digital I/O 12: group J pin 2 (RJ2)
digital I/O 13: group J pin 3 (RJ3)

digital I/O 14: group C pin 0 (RC0)

digital I/O 15: group J pin 4 (RJ4)
digital I/O 16: group J pin 5 (RJ5)
digital I/O 17: group J pin 6 (RJ6)
digital I/O 18: group J pin 7 (RJ7)

Each group is controlled by 3 registers, TRISx LATx and PORTx, where x is the group letter. Each bit of these registers modifies one pin in the group. These registers are what are called special function registers(SFRs). There are lots of SFRs that control various aspects of the the processor’s operation.

The TRISx register of each group can be used to individually set pins as inputs or outputs. 0 corresponds to output, 1 corresponds to input. IE: if you set bit 0 of the TRISG register to 0, you make digital I/O 1 on the robot controller an output. NOTE: on power up all pins are set as inputs. IFI wanted to make things easier for you so they created the digital_io_xx variables in the default code. If you look at their declaration in ifi_aliases.h you will see that they are actaully just direct aliases for various bits in various TRIS registers.

Ok so now we have a pin that is configured as an output and we want to set its state. This is the job of the LATx registers. They control the output of a pin when it is set as a output. They have no effect when the pin is set as an input. Example: We set bit 0 of LATB to a 1. This sets Digital I/O 1 on the RC to high(+5v). Again to make things easier to remember IFI has provided a set of logically named aliases for you (rc_dig_out01 - rc_dig_out18). Again these are direct aliases and map directly to different bits of different LAT registers.

Lets say we have changed our mind now and we now want to use pin RB2 (Digital I/O 01) as an input. This is where the PORTx registers are used. they are read to determine the state of a pin. The pin must first be set as an input using it’s TRIS bit. Yet again IFI has made things easier and provided you with at set of aliases for these registers(rc_dig_in01 - rc_dig_in18). I don’t remember what a PORT bit will read if its corresponding pin is set as an output.

Yes. We did some experiments with a robot position sensing system using an optical tracking chip watching the floor, and the communication had bidirectional data on one pin (with a clock signal on another).

Yes, this is pretty common function. You might look into using a state-machine to keep track of what direction the pins should be in at any given time.

-Kevin

We already knew that it was possible on the PIC. We knew how to do it in assembly. Can anyone tell me how to do it in C?

I already did:

Just write to these variables to change the direction.