
- •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
204 |
Chapter 10 |
org 0x04 ;=============================
;main program ;============================= main:
;Initialize all lines in Port-B for output
|
movlw |
B’00000000’ |
; w = 00000000 binary |
|
|
tris |
PORTB |
|
; Set up Port-B for output |
; Set Port-B bit 0 ON |
|
|
||
|
movlw |
B’00000000’ |
; w := 0 binary |
|
|
movwf |
PORTB |
|
; Port-B itself := w |
; Clear the carry bit |
|
|
||
|
bcf |
STATUS,C |
|
|
mloop: |
|
|
|
|
|
incf |
PORTB,f |
|
; Add 1 to register value |
|
call |
delay |
|
|
|
goto |
mloop |
|
|
;================================ |
|
|||
; |
delay sub-routine |
|
||
;================================ |
|
|||
delay: |
|
|
|
|
|
movlw |
.200 |
; w = 200 decimal |
|
|
movwf |
j |
; j = w |
|
jloop: |
|
|
|
|
|
movwf |
k |
; k = w |
|
kloop: |
|
|
|
|
|
decfsz |
k,f |
; k = k-1, skip next if zero |
|
|
goto |
kloop |
|
|
|
decfsz |
j,f |
; j = j-1, skip next if zero |
|
|
goto |
jloop |
|
|
|
return |
|
|
|
end
10.3 Programming the Seven-segment LED
A 7-segment display can be connected to output ports on the PIC and used to display numbers and some digits. The circuit in Figure 10-6 shows one possible wiring scheme.
As the name indicates, the seven-segment display has seven linear LEDs that allow forming all the decimal and hex digits and some symbols and letters. Once the mapping of the individual bars of the display to the PIC ports has been established, digits and letters are shown by selecting which port lines are set and which are not. For example, in the seven-segment LED of Figure 10-5, the digit 2 is displayed by setting segments a, b, g, e, and d. In this particular wiring, these segments correspond to Port-B lines 0, 1, 6, 4, and 5.

Programming Essentials: Input and Output |
205 |
R=10K
+5v
1 |
16F84 |
18 |
|
|
|
|
|
|
|
RA2 |
RA1 |
|
|
|
|
|
|
2 |
|
|
17 |
Osc |
|
|
|
|
|
|
|
|
|
|
|
||
|
RA3 |
RA0 |
|
|
|
|
|
|
3 |
|
|
16 |
|
|
|
|
|
|
RA4/TOCKI |
OSC1 |
|
|
|
|
|
|
4 |
|
|
15 |
|
|
|
|
|
|
MCLR |
OSC2 |
|
|
|
|
|
|
5 |
|
|
14 |
+5v |
|
|
|
+5v |
|
Vss |
Vdd |
|
|
|
|
|
|
6 |
|
|
13 |
|
|
7-segment |
|
|
|
RB0/INT |
RB7 |
|
|
|
|
||
7 |
|
|
12 |
|
|
|
LED |
|
|
RB1 |
RB6 |
|
|
f |
|
|
a |
|
|
|
|
|
|
|
||
8 |
|
|
11 |
|
|
|
|
|
|
RB2 |
RB5 |
|
|
|
|
|
|
9 |
|
|
|
|
g |
|
a |
b |
|
|
10 |
|
|
|
|
||
|
RB3 |
RB4 |
|
|
|
|
|
|
|
|
|
|
|
|
f |
b |
|
|
|
|
|
|
|
|
g |
|
|
|
|
|
220 R |
|
|
|
PWR |
|
|
|
|
X 7 |
e |
e |
c |
|
|
|
|
|
ON |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d |
|
d |
c |
Figure 10-6 Seven-segment LED Circuit
As the name indicates, the seven-segment display has seven linear LEDs that allow forming all the decimal and hex digits and some symbols and letters. Once the mapping of the individual bars of the display to the PIC ports has been established, digits and letters are shown by selecting which port lines are set and which are not. For example, in the seven-segment LED of Figure 10-6, the digit 2 is displayed by setting segments a, b, g, e, and d. In this particular wiring, these segments correspond to Port-B lines 0, 1, 6, 4, and 5.
Conversion of the individual digits to port display codes is easily accomplished by means of a lookup table. The processing depends on three special features of PIC assembly language:
•The program counter file register (labeled PC and located at offset 0x02) holds the address in memory of the current instruction. Since each PIC instruction takes up a single byte (except for those that modify the PC), one can jump to consecutive entries in a table by adding an integer value to the program counter.
•The addwf instruction is used to add a value in the w register to the program counter.
•The retlw instruction returns to the caller a literal value stored in the w register. In the case of retlw the literal value is the instruction operand.
If the lookup table is located at a subroutine called getcode, then the processing can be implemented as follows:
206 |
|
Chapter 10 |
getcode: |
|
|
addwf |
PC,f |
; Add value in w register to program |
counter |
|
|
retlw |
0x3f |
; code for number 0 |
retlw |
0x06 |
; code for number 1 |
retlw |
0x5b |
; code for number 2 |
... |
|
|
retlw |
0x6f |
; code for number 9 |
The calling routine places in the w register the numeric value whose code is desired, and then calls the table lookup, as follows:
movlw |
0x03 |
; |
Code for number 3 desired |
call |
getcode |
|
|
movwf |
PORTB |
; |
Display 3 in 7-segment display |
10.4 A Demonstration Board
A demonstration board, also known as a demo board, is a useful tool in mastering PIC programming. Many are available commercially; like programmers, there is a cottage industry of PIC demo boards on the internet. Constructing your own demo boards and circuits is not difficult. The components can be placed on a breadboard, or wire-wrapped onto a special circuit board, or a printed circuit board can be homemade, or ordered through the internet. These options have been previously discussed and Appendix B contains instructions on how to build your own PCBs.
Figure 10-7 shows a simple 16F84A-based demo board with a seven-segment LED, buzzer, pushbutton switch, and a bank of four toggle switches.
10.4.1 PCB Images for Demo Board
Some PCBs contain circuit etchings on both sides. In this case two circuit board images are required. In addition, most boards contain a top-side image of the components, company logos, model numbers, and other information. Commercially, this image is silk-screened onto the board.
The homemade board (see Appendix B) usually contains a single etched image and a top-side image with informational text and graphics. Both images can be created with a conventional drawing program, such as Corel Draw, Adobe Illustrator, or Windows Paint, or with a specialized application, several of which are available free and for purchase on the Web. Figure 10-7 shows the images used for making the PCB for the circuit in Figure 10-8.
Note that the top-side (text) image has been mirrored on the horizontal plane. This is necessary so that the text and graphics coincide with the circuit etchings once the images are transferred to the board. The process for making your own PCBs is described in Appendix B.

Programming Essentials: Input and Output |
207 |
PB SW
R=10K
R=10K
1
2
3
4
5
6
7
8
9
+5v
10k R
X 4
DIP SW
|
|
|
|
|
|
Piezo |
|
|
|
|
|
|
|
Buzzer |
|
|
|
|
|
|
18 |
|
|
|
|
|
RA2 |
|
RA1 |
|
|
|
|
|
|
RA3 |
|
RA0 |
17 |
Osc |
|
|
|
|
|
|
|
16 |
|
|
|
|
|
RA4/TOCKI |
OSC1 |
|
|
|
|
|
|
|
|
|
|
15 |
|
|
|
|
|
MCLR |
16F84A |
OSC2 |
|
|
|
|
|
|
|
|
14 |
+5v |
|
|
|
|
|
Vss |
|
Vdd |
|
|
|
|
|
|
|
|
|
13 |
|
|
7-segment |
|
|
RB0/INT |
|
RB7 |
|
|
|
|
||
|
|
|
12 |
|
|
|
LED |
|
RB1 |
|
RB6 |
|
|
f |
|
|
a |
|
|
|
|
|
|
|
||
|
|
|
11 |
|
|
|
|
|
RB2 |
|
RB5 |
|
|
|
|
|
|
|
|
|
|
|
g |
|
a |
b |
|
|
|
10 |
|
|
|
|
|
RB3 |
|
RB4 |
|
|
|
|
|
|
|
|
|
|
|
|
f |
b |
|
|
|
|
|
|
|
|
g |
|
|
|
|
|
220 R |
|
|
|
PWR |
|
|
|
|
X 7 |
e |
e |
c |
|
|
|
|
|
ON |
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d |
|
d |
c |
Figure 10-7 PIC 16F87A Demo Board
Figure 10-8 Bottomand Top-side images of a PCB.
208 |
Chapter 10 |
10.4.2 TestDemo1 Program
The following program exercises some of the experiments that can be implemented on the demo boards in Figure 10-7:
;File: TestDemo1.asm
;Date: June 2, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Program to exercise the demonstration circuit and board
;number 1
;===========================
;switches ;===========================
;Switches used in __config directive:
; |
|
_CP_ON |
Code protection ON/OFF |
; * |
_CP_OFF |
|
|
; |
* |
_PWRTE_ON |
Power-up timer ON/OFF |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog timer ON/OFF |
|
|
; * _WDT_OFF |
|
|
|
|
; |
_LP_OSC |
Low power crystal |
oscillator |
|
; * _XT_OSC |
External parallel |
resonator/crystal |
|
|
; |
|
oscillator |
|
|
; |
_HS_OSC |
High speed crystal resonator (8 to |
10 MHz) |
|
; |
|
Resonator: Murate |
Erie CSA8.00MG = |
8 MHz |
; |
_RC_OSC |
Resistor/capacitor oscillator |
|
;|
;|_____ * indicates setup values
|
processor 16f84A |
|
|
|
include |
<p16f84A.inc> |
|
|
__config |
_XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF |
|
;===================================================== |
|||
; |
variables in PIC RAM |
||
;===================================================== |
|||
|
cblock |
0x0c |
; Start of block |
|
count1 |
|
; Counter # 1 |
|
j |
|
; counter J |
|
k |
|
; counter K |
|
endc |
|
|
;=======================================================
; P R O G R A M
;=======================================================
org |
0 |
; start at address 0 |
goto |
main |
|
; |
|
|
Programming Essentials: Input and Output |
|
209 |
|
; Space for interrupt handlers |
|
|
|
org |
0x08 |
|
|
main: |
|
|
|
; Port A (5 lob) for input |
|
|
|
movlw |
B’00011111’ |
; w := 00001111 |
binary |
tris |
PORTA |
; Port-A (lines |
0 to 4) to |
input |
|
|
|
; Port-Bit (8 lines) for output |
|
|
|
movlw |
B’00000000’ |
; w := 00000000 |
binary |
tris |
PORTB |
; Port-B to output |
;==============================
;Pushbutton switch processing ;============================== pbutton:
;Push button switch on demo board is wired to RA4
;Switch logic is active low
btfss |
PORTA,4 |
; Test and skip if bit is set |
goto |
buzzit |
; Buz if switch ON |
; At this point Port-A bit 4 is set (switch is off) |
||
call |
buzoff |
; Buzzer off |
goto |
readdip |
; Read DIP switches |
buzzit: |
|
|
call |
buzon |
; Turn on buzzer |
goto |
pbutton |
|
;==============================
;DIP switch processing ;==============================
;Read all bits of Port-A readdip:
movf |
PORTA,w |
; Port A bits to w |
;If board uses active low then all switch bits must be negated
;This is done by XORing with 1-bits
xorlw |
b’11111111’ |
; Invert all bits in w |
; Eliminate all 4 high order bits |
|
|
andlw |
b’00001111’ |
; And with mask |
; Get digit into w |
|
|
call |
segment |
; get digit code |
movwf |
PORTB |
; Display digit |
call |
delay |
; Give time |
; Update digit and loop counter |
|
|
goto |
pbutton |
|
;******************************* ; 7-segment table of hex codes ;******************************* segment:
addwf |
PCL,f |
; |
PCL is |
program counter latch |
retlw |
0x3f |
; |
0 code |
|
210 |
|
Chapter 10 |
retlw |
0x06 |
; 1 |
retlw |
0x5b |
; 2 |
retlw |
0x4f |
; 3 |
retlw |
0x66 |
; 4 |
retlw |
0x6d |
; 5 |
retlw |
0x7d |
; 6 |
retlw |
0x07 |
; 7 |
retlw |
0x7f |
; 8 |
retlw |
0x6f |
; 9 |
retlw |
0x77 |
; A |
retlw |
0x7c |
; B |
retlw |
0x39 |
; C |
retlw |
0x5b |
; D |
retlw |
0x79 |
; E |
retlw |
0x71 |
; F |
retlw |
0x7f |
; Just in case all on |
;****************************
;piezo buzzer ON ;****************************
;Routine to turn on piezo buzzer on Port-B bit 7 buzon:
bsf |
PORTB,7 |
; Tune on bit 7, Port-B |
return
;****************************
;piezo buzzer OFF ;****************************
;Routine to turn off piezo buzzer on Port-B bit 7 buzoff:
|
bcf |
PORTB,7 |
; Bit 7 Port-B clear |
|
return |
|
|
;================================ |
|||
; |
delay subroutine |
|
|
;================================ |
|||
delay: |
|
|
|
|
movlw |
.200 |
; w = 200 decimal |
|
movwf |
j |
; j = w |
jloop: |
|
|
|
|
movwf |
k |
; k = w |
kloop: |
|
|
|
|
decfsz |
k,f |
; k = k-1, skip next if zero |
|
goto |
kloop |
|
|
decfsz |
j,f |
; j = j-1, skip next if zero |
|
goto |
jloop |
|
|
return |
|
|
|
end |
|
|