Monday, December 4, 2006

Strange blue LED problem!




Blue LED-PIC connection diagram

The first problem we faced is in the blue LED of sensor 5 (sensor numbers start from 0, being the front sensor, sensors numbered in CW direction and 4 being the rear sensor). Blue LED of sensor 5, is connected to the RA4 (bit 4 of port A) of the host microcontroller of the short-range sensing system as indicated in the above figure. The LED will be lit when RA4 is set to HIGH, and LED will be turned off when RA4 is set to LOW, but this is not the case! The LED behaves erratically.

Open-drain output diagram of RA4


When we look at the datasheet of 16F877A, we have noticed that the RA4, has open-drain output characteristics, as shown in the above figure. It is noted in http://www.brouhaha.com/~eric/pic/open_drain.html that:

Open-drain outputs are outputs which at any given time are either actively sinking current (i.e., low voltage, typically considered logic 0) or are high impedance, but which never source current (high voltage, logic 1). Open-drain refers to the drain terminal of a MOS FET transistor. The equivalent concept on a bipolar device is called open-collector.

Most microcontrollers don't have dedicated open-drain outputs. On some PICs the PA4 pin (also known as RTCC) may be used as an open-drain output. Most of the other port pins are bidirectional, and have separate output data registers and direction registers. On the PIC, the direction registers are called TRIS registers, an abbreviation for Tri-state, which is actually a trademark of National Semiconductor. The TRIS register bits should be set to logic 0 for lines that should be active outputs, and logic 1 for lines that should be high-impedance inputs. Note that this is the reverse of the conventions used by almost all other microcontrollers and peripheral devices.

In order to obtain the effect of open-drain output, it is necessary to use the TRIS register bit to control the output rather than the more usual use of the data output register. This is most easily accomplished by leaving the appropriate bit of the data output register set to 0, and setting the corresponding TRIS bit to 0 when the output should sink current, and 1 when the output should be in the open-drain (high-impedance) state.

The important point to note is that open-drain ports can not source current, they can only sink current. In our configuration, shown in Blue LED-PIC connection diagram, we are sourcing current to the blue LED's to lit them. This connection type is appropriate for I/O pins having TTL characteristic only, not suitable for open-drain output pins. Hence a small modification is performed on the LED of sensor 5. The ground connection of the sensor is cut using a sharp knife and 5V is connected to this trace instead. The polarity of the LED is also reversed since current will flow from 5V to RA4 port of the PIC (the port will sink current).

The following C-code(CCS PIC C compiler) is used to turn on and turn off the LED:

To turn on LED:

output_low (SENSOR5_LED); //changes the state of RA4 to LOW

bit_clear(trisa,4); // makes RA4 as output and RA4 sinks current

To turn off LED:

output_low (SENSOR5_LED); //changes the state of RA4 to LOW

bit_set(trisa,4); // makes RA4 as input which means it is in high-impedance state and no current flows

No comments: