Dev Gualtieri
When designing with low pin-count microcontrollers, you sometimes need just one or two more digital input pins than you have available. Fortunately, many digital inputs on such microcontrollers also double as low-resolution analog converter inputs.
This analog capability provides an easy way to increase the number of logic states a microcontroller can read on its input pins, especially with relatively static states such as panel switch positions and jumper configurations. Simply use ternary (three-state) logic instead of binary.
Binary logic needs three input pins to sense up to eight encoded states, from (000)b to (111)b (23 – 1). With ternary logic, however, just two inputs will sense nine states from (00)t to (22)t (32 – 1).
The example in the figure shows an implementation with two ternary inputs, using two of four available analog inputs among the configurable I/O pins of a PIC12F675 microcontroller. The switches are single-pole double-throw (SPDT) type with a center off position and tap simple voltage dividers, presenting the input pins with 0, 2.5, or 5 V depending on switch position.
Software in the microcontroller reads and interprets these voltages as ternary values 0, 1, and 2. By combining the values from two inputs, the system obtains nine states for a configuration setup such as baud rate (see the table).
The technique could also be used with only with a single such panel switch and input, serving, for example, in a motor controller as an Up – Stop – Down control. Similarly, three switches and input pins could sense 27 states instead of needing four pins for 16 states in binary logic.
With ternary logic, then, you can increase the number of input states the microcontroller can sense while reducing the number of input pins needed.
Using the analog converter input function of a compact microcontroller’s input pins to read ternary logic encoding allows two pins to sense as many as nine configuration states.
Ternary logic configurations
S1
|
0
|
1
|
2
|
0
|
1
|
2
|
0
|
1
|
2
|
S2
|
0
|
0
|
0
|
1
|
1
|
1
|
2
|
2
|
2
|
State
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|