
- •Table of Contents
- •Preface
- •Additional Material
- •Basic Electronics
- •1.0 The Atom
- •1.1 Isotopes and Ions
- •1.2 Static Electricity
- •1.3 Electrical Charge
- •1.4 Electrical Circuits
- •1.5 Circuit Elements
- •1.6 Semiconductors
- •Number Systems
- •2.0 Counting
- •2.1 The Origins of the Decimal System
- •2.2 Types of Numbers
- •2.3 Radix Representations
- •2.4 Number System Conversions
- •Data Types and Data Storage
- •3.0 Electronic-Digital Machines
- •3.1 Character Representations
- •3.2 Storage and Encoding of Integers
- •3.3 Encoding of Fractional Numbers
- •3.4 Binary-Coded Decimals (BCD)
- •Digital Logic, Arithmetic, and Conversions
- •4.0 Microcontroller Logic and Arithmetic
- •4.1 Logical Instructions
- •4.2 Microcontroller Arithmetic
- •4.3 Bit Manipulations and Auxiliary Operations
- •4.4 Unsigned Binary Arithmetic
- •4.5 Signed Binary Arithmetic
- •4.6 Data Format Conversions
- •Circuits and Logic Gates
- •5.0 Digital Circuits
- •5.1 The Diode Revisited
- •5.2 The Transistor
- •5.3 Logic Gates
- •5.4 Transistor-Transistor Logic
- •5.5 Other TTL Logic Families
- •5.6 CMOS Logic Gates
- •Circuit Components
- •6.0 Power Supplies
- •6.1 Clocked Logic and Flip-flops
- •6.2 Clocks
- •6.3 Frequency Dividers and Counters
- •6.4 Multiplexers and Demultiplexers
- •6.5 Input Devices
- •The Microchip PIC
- •7.0 The PICMicro Microcontroller
- •7.1 PIC Architecture
- •Mid-range PIC Architecture
- •8.0 Processor Architecture and Design
- •8.1 The Mid-range Core Features
- •8.2 Mid-Range CPU and Instruction Set
- •8.3 EEPROM Data Storage
- •8.4 Data Memory Organization
- •8.5 Mid-range I/O and Peripheral Modules
- •PIC Programming: Tools and Techniques
- •9.0 Microchip’s MPLAB
- •9.1 Integrated Development Environment
- •9.2 Simulators and Debuggers
- •9.3 Programmers
- •9.4 Engineering PIC Software
- •9.5 Pseudo Instructions
- •Programming Essentials: Input and Output
- •10.0 16F84A Programming Template
- •10.1 Introducing the 16F84A
- •10.2 Simple Circuits and Programs
- •10.3 Programming the Seven-segment LED
- •10.4 A Demonstration Board
- •Interrupts
- •11.0 Interrupts on the 16F84
- •11.1 Interrupt Sources
- •11.2 Interrupt Handlers
- •11.3 Interrupt Programming
- •11.4 Sample Programs
- •Timers and Counters
- •12.0 The 16F84 Timer0 Module
- •12.1 Delays Using Timer0
- •12.2 Timer0 as a Counter
- •12.3 Timer0 Programming
- •12.4 The Watchdog Timer
- •12.5 Sample Programs
- •LCD Interfacing and Programming
- •13.0 LCD Features and Architecture
- •13.1 Interfacing with the HD44780
- •13.2 HD44780 Instruction Set
- •13.3 LCD Programming
- •13.4 Sample Programs
- •Communications
- •14.0 PIC Communications Overview
- •14.1 Serial Data Transmission
- •14.2 Parallel Data Transmission
- •14.4 PIC Protocol-based Serial Programming
- •14.5 Sample Programs
- •Data EEPROM Programming
- •15.0 PIC Internal EEPROM Memory
- •15.1 EEPROM Devices and Interfaces
- •15.2 Sample Programs
- •Analog to Digital and Realtime Clocks
- •16.0 A/D Converters
- •16.1 A/D Integrated Circuits
- •16.2 PIC On-Board A/D Hardware
- •16.3 Realtime Clocks
- •16.4 Sample Programs
- •Index

Circuit Components |
107 |
The selection depends on the specific microcontroller, the circuit requirements, and the cost of each available option. The least expensive option is the resistor/capacitor oscillator circuit (RC network). The disadvantages are its slow speed and inherent inaccuracies. Some of the newer generations of microcontrollers come equipped with an internal RC oscillator that operate as a programmable timer. Typical speeds are 4 MHz with a 1.5 percent error. The actual use and implementation of microcontroller clocks is discussed in relation to each specific device.
6.3 Frequency Dividers and Counters
Frequency dividers and counters are actually the same circuitry used in different ways. Counters are one of the most useful and versatile digital devices. Counters can be used to count the number of clock cycles and as an instrument for measuring time and therefore period or frequency. The two different types of counters are synchronous and asynchronous.
6.3.1 Frequency Dividers
Circuit designers often needed to reduce the frequency of a wave clock signal. One easy way of doing it is to divide the frequency by two, which is done by feeding back the not-Q output of a D-type flip-flop to its data line. Figure 6-14 shows a divide-by-2 circuit and its effect on the resulting wave.
|
D |
Q |
f/2 |
f |
|
|
output |
||
|
|
|
|
|
f |
|
Q |
|
|
input |
|
|
|
|
|
|
|
|
|
|
|
|
|
f/2 |
|
|
|
|
time |
Figure 6-14 A Divide-by-two Circuit
In the circuit of Figure 6-14 the frequency division occurs because each input clock rising edge toggles the flip-flop’s output. When the Q output goes low, the not-Q line goes high and the high feedback signal is fed back to the data line, thus canceling out the next high wave of the f signal.
6.3.2 The JK Flip-flop Counter
One type of specialized flip-flop that we did not cover in Section 6.2 is the JK flip-flop. The JK flip-flop is an ideal component to build a circuit that keeps track of the number of positive or negative clock edges on the input clock. The name of this flip-flop relates to the two variables, J and K, that are used as inputs to the circuit. Figure 6-15 shows one possible circuit implementation for the JK flip-flop.

108 |
Chapter 6 |
J |
S |
Q |
CLK
K |
R |
Q |
CLK |
J |
K |
Q |
X |
L |
L |
last state |
H |
L |
H |
L |
H |
H |
L |
H |
H |
H |
H |
toggle |
|
|
|
|
Figure 6-15 A JK Flip-Flop Circuit
In Figure 6-15 the RC circuit converts the rectangular wave clock pulse into a narrow spike. The three-input AND gates make the circuit positive-edge-triggered. When J and K are low, both AND gates are disabled; therefore, clock pulses have no effect. This corresponds to the first entry in the truth table. When J is low and K is high (second entry in the truth table) the upper gate is disabled, so the flip-flop cannot be set; it must be reset. When Q is high, the lower gate passes a Reset trigger as soon as the next positive clock edge arrives. This forces Q to become low (the same second entry in the truth table). Therefore, J low and K high means that the next positive clock edge resets the flip-flop.
When J is high and K is low (third entry in the truth table) the lower gate is disabled, so it is impossible to reset the flip-flop. However, the flip-flop can be reset when Q is low because not-Q is high; therefore, the upper gate passes a Set trigger on the next positive clock edge. This drives Q into the high state (the third entry in the truth table). As you can see, J = 1 and K = 0 means that the next positive clock edge sets the flip-flop (unless Q is already high). When J and K are both high it is possible to set or reset the flip-flop. If Q is high, the lower gate passes a RESET trigger on the next positive clock edge. On the other hand, when Q is low, the upper gate passes a SET trigger on the next positive clock edge. Either way, Q changes to the complement of the last state (see last entry in the truth table). Therefore, when J = 1 and K = 1 the flip-flop will toggle on the next positive clock edge.
6.3.3 Ripple Counters
The simplest of all counters is called a ripple counter. A two-bit ripple counter can be constructed by wiring together two divide-by-two circuits, as in Figure 6-16.

Circuit Components
Q0
D |
Q |
|
|
||
wave |
Q |
|
input |
||
|
109
Q1
D |
Q |
|
|
|
Q |
clock
Q0
Q1
0/0 0/1 1/0 1/1 0/0 ...
Figure 6-16 Two-Bit Ripple Counter
Stringing together two divide-by-two circuits, as in Figure 6-16, produces a di- vide-by-four circuit. Stringing together three flip-flops produces a divide-by-eight circuit, four flip-flops create a divide-by-sixteen circuit, and so on. The counting action of the connected flip-flops is based on the fact that each flip-flop changes state before triggering the next one in line. Thus, each stage performs as a bit in a binary counter, the first stage being the LSB and the last stage the MSB. Since the preceding flip-flop acts as a clock for the next one in line, the flip-flop to the right toggles each time its neighbor to the left goes low. In Figure 6-15 the signal labeled Q0 is the LSB of a two-bit counter, while the signal labeled Q1 is the most significant bit.
In this design each flip-flop is triggered by the previous one; thus the count is said to “ripple” down the device. One objection to the ripple counter is that the change in each output is determined by the previous output in the flip-flop chain; this produces a few nanoseconds of time lag from output line to output line. This cumulative settling time is why these counters are called serial or asynchronous.
Note that the ripple counter of Figure 6-16 uses the not-Q line to drive the following flip-flop. If a ripple counter is wired so that the Q line drives each next stage, then the transitions take place not when the previous waveform goes low, but when it goes high. The result is that the counter counts down instead of up. In other words, in the down counter, the count is reduced by one during each clock transition. Commercial counters, such as the 74193, can be made to operate as up-counters or down-counters by selecting the corresponding input line.

110 |
Chapter 6 |
6.3.4 Decoding Gates
A decoding gate is a way of connecting the output of a counter so that it signals a given state. For example, if four D-type flip-flops are wired so as to produce a four-bit ripple counter similar to the one in Figure 6-15, the counter represents binary digits 0000 to 1111. If we wanted to detect the value 1101 (16 decimal) the resulting circuit could be designed as in Figure 6-17.
1101
|
|
Q0 |
|
Q1 |
|
Q2 |
|
Q3 |
|
D |
Q |
D |
Q |
D |
Q |
D |
Q |
|
|
|
|
|
||||
wave |
|
Q |
|
Q |
|
Q |
|
Q |
input |
|
|
|
|
||||
|
|
|
|
|
|
|
|
Figure 6-17 Decoding Gate
The circuit of Figure 6-17 uses a NOR gate to invert the value of bit number 1. Then the AND gate serves to trigger the output when bits 0, 2, and 3 are high and bit 1 is low. This corresponds to the binary value 1011.
6.3.5 Synchronous Counters
Although the ripple counter is the simplest one, it has the previously mentioned disadvantage that each flip-flop has to wait for its neighbor to switch states. This means that in a ripple counter the delay times are additive, and also that the total “settling” time for the counter is approximately the delay multiplied by the total number of flip-flops. In addition, with ripple counters the resulting delay creates the possibility of glitches occurring at the output of decoding gates. These problems can be overcome by the use of a synchronous or parallel counter.
By observing how counting takes place in binary numbers, a counter in which each flip-flop is triggered at every clock beat can be built. Binary counting has the property that when a bit changes from high to low (1 to 0) it sends a toggle command to its neighbor to the left. So assuming that the low-order bit changes consecutively from one state to its complement, and starting from all bits initialized to 0, binary counting can be visualized as in Figure 6-18.

Circuit Components |
111 |
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Figure 6-18 The Binary Counting Mechanism
Note in Figure 6-18 that the arrows indicate the transition from high to low, which is the command for the column to the left to change to its complement (toggle). Using this property of binary counting, it is possible to wire four JK flip-flops so that every high-to-low transition of a flip-flop triggers its higher-order neighbor to toggle its state. Figure 6-19 shows such a system.
|
Q0 |
|
Q1 |
|
Q2 |
|
Q3 |
+5V |
|
|
|
|
|
|
|
J |
Q |
J |
Q |
J |
Q |
J |
Q |
wave |
|
|
|
|
|
|
|
input |
|
|
|
|
|
|
|
K |
Q |
K |
Q |
K |
Q |
K |
Q |
wrap-around point
Q0
Q1
Q2
Q3
Figure 6-19 Synchronous Four-Bit Up Counter

112 |
Chapter 6 |
In Figure 6-19 note that the first flip-flop (the one with the Q0 output) has a posi- tive-edge triggered clock input. The lowest-order flip-flop toggles with each rising edge of the clock signal (not shown in the illustration). The second flip-flop to the right toggles with every falling edge of the signal from its neighbor to the left. And so on to the last flip-flop in the chain. The arrows in the waveform portion of Figure
6-19 show that each succeeding output bit is toggled by the transition from high to low of its lower-ordered neighbor. Also note the dashed line that marks the point where all found counters are transitioning from high-to-low. At this point all four counters wrap around to zero and a new count begins.
Observe that the not-Q output line transitions opposite to the Q output. That is, when the Q output line goes high, not-Q goes low, and viceversa. So if the pulse into each successive flip-flop originated in the not-Q line, instead of the Q line, then the resulting circuit would be a synchronous counter that transitions on the positive edge (low-to-high) instead of in the negative edge, as is the case with the counter in
Figure 6-19. Furthermore, the not-Q line provides a set of negated outputs in reference to the Q lines; therefore, it is possible to come up with a circuit that serves both as an upand down-counter according to the selected set of outputs. Such a circuit is shown in Figure 6-20.
Q0 |
Q1 |
Q |
2 |
Q |
3 |
|
|
|
|
+5V
J |
Q |
J |
Q |
J |
Q |
J |
Q |
wave input
K |
Q |
K |
Q |
K |
Q |
K |
Q |
|
|
|||||||||||
|
||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
Q0 |
|
|
|
Q1 |
|
|
Q2 |
|
|
|
Q3 |
Figure 6-20 Synchronous 4-bit Upand Down-counter
In the counter of Figure 6-20 the Q outputs generate the up-count series while the not-Q outputs produce the down-count series.
6.3.6 Counter ICs
Counters are available as standard TTL components. The 7493 is an asynchronous 4-bit ripple counter that counts from 0 to 15. The 7490 is another version of the ripple counter, called a decade counter, since the count output is in the range 0 to 9. The 74193 is a 4-bit synchronous up/down counter in the range 0 to 15. Figure 6-21 is a pin diagram of the 74193.

Circuit Components |
113 |
|
|
|
|
|
|
|
input B |
1 |
|
16 |
+5V |
||
output QB |
2 |
|
15 |
input A |
||
output QA |
3 |
|
14 |
reset |
||
down clock |
4 |
|
13 |
borrow out |
||
up clock |
|
74193 |
|
carry out |
||
5 |
|
12 |
||||
output QC |
|
|
|
|
|
|
6 |
|
11 |
|
preset (active low) |
||
output QD |
7 |
|
10 |
input C |
||
0V |
8 |
|
9 |
input D |
||
|
|
|
|
|
|
|
Figure 6-21 74193 Asynchronous Up/Down Counter Pin Diagram
The 74193 is a synchronous counter, so its output changes precisely at each clock pulse. This is convenient since it allows connecting its output to other logic gates and avoids the glitches associated with ripple counters. Note from Figure 6-21 that the 74194 has separate clock inputs for counting up and counting down. The count increases as the up clock input becomes high (on the rising-edge). The count decreases as the down clock input becomes high (on the rising-edge). In both cases the other clock input should be high. For normal operation the Preset input should be high and the Reset input low. When the Reset input is high it resets the count to zero, that is, lines QA to QD are low. The counter can be preset by placing any desired binary number on inputs A to D and making the Preset input low. These inputs may be left unconnected if not required.
Several 74193 counters can be chained by wiring a common Reset line, connecting the carry to the up clock line of the next counter and the borrow to the down line.
6.3.7 Shift Registers
In chapter 4 we discussed logical operations that shift and rotate the operand bits. These manipulations are useful in inspecting individual bits, in performing fast multiplications, in implementing time delays, and in converting parallel input to serial output, and vice versa. The hardware implementation of shift-and-rotate operations are called shift counters or shift registers.
Shift counters are often based on the D-type flip-flop. Actually, several D-type flip-flops can be chained together so that the D output of one goes into the D input of the next one. If all the flip-flops are driven by the same clock signal, then the effect would be to shift the bits from one flip-flop into the next one at each rising clock pulse.
A common implementation of a shift counter is called a parallel-in/serial-out shift register, as the one in Figure 6-22.

114 |
|
|
Chapter 6 |
A |
B |
C |
D |
|
|
|
load
serial |
|
SET |
|
SET |
|
SET |
|
SET |
serial |
D |
Q |
D |
Q |
D |
Q |
D |
|
||
in |
|
Q |
|||||||
|
|
|
|
|
|
|
|
out |
|
|
|
CLR |
|
CLR |
|
CLR |
|
CLR |
|
CLOCK
Figure 6-22 Four-Bit Parallel-In/Serial-Out Shift Counter
The circuit in Figure 6-22 shows four flip-flops connected so that the output of one feeds into the input of the next one. Also, a set of NAND gates allow parallel data input. When the load signal is set high the flip-flops in the shift register are loaded simultaneously with the logic values at the inputs A, B, C, and D. The 74165 IC is an 8-bit parallel-in/serial-out shift register with asynchronous parallel load and two OR-gated clock inputs. Figure 6-23 is a pin diagram of the 74165 IC.
shift/load |
|
|
|
|
+5V |
||
1 |
|
16 |
|||||
clock |
2 |
|
15 |
clock inhibit |
|||
input E |
3 |
|
14 |
input D |
|||
input F |
4 |
|
13 |
input C |
|||
input G |
|
74165 |
|
|
input B |
||
5 |
|
12 |
|||||
input H |
6 |
|
11 |
input D |
|||
|
|
|
7 |
|
|
serial input |
|
output Q |
|
10 |
|||||
GND |
8 |
|
9 |
output Q |
|||
|
|
|
|
|
|
|
|
Figure 6-23 Pin Diagram of IC 74165
The serial input line in the diagram of Figure 6-22 and in the 74165 IC in Figure 6-23 allows cascading multiple chips.
Parallel-in/serial-out shift registers find common use in the implementation of serial ports. In serial communications data is sent one bit at a time over a single wire.