08 Chapter 2
08 Chapter 2
2.1 INTRODUCTION
New science called for new scientists. White collars took the place of blue
collars and there came the people who could "understand the electronics". And
while the world stared in awe, the conspirators split up in two factions: those
who specialized in software and those who specialized in hardware. Younger
and more enthusiastic than their teachers, both groups carried on their efforts,
but following the different paths. While the first advanced steadily to this day,
those dedicated to hardware got carried away by their success and eventually
discovered a transistor.
While this drama took place, nameless hobbyists and professionals across
the world kept working on their projects, although still divided into two large
factions. And then somebody came up with another great idea: why not create an
all-purpose component? A cheap and universal integrated circuit that could be
16
programmed and used in any device, anywhere. Technology was up to the task;
there was an interested market... why not? Soul and body were united into one
powerful MICROCONTROLLER.
17
are performed. The architecture of the PIC 16F873 Microcontroller is shown in
Figure 2.1.
RAQAMO
RA1<AN1
RiCAtCVRS*.
RA3AIJ3-VHE--*-
RA+TOCKI
RA5'AM4)S5
RBOiNF
RBI
RB2
R83PGM
RB4
RB5
RflftPSC
RSTiPGD
RCOTIOSOfTICKi
HCVT10SKCP2
RCS’CCPt
RC3SCK/SCL
RC*SO>SDA
RCS'SOS
ReerocK
RC'PXCT
!8
Microcontroller Core Features:
19
- < 2 mA typical @ 5V, 4 MHz
- 20 mA typical @ 3V, 32 kHz
-< 1 mA typical standby current
❖ Peripheral Features
The PIC 16F873 Microcontroller is a 28 pin DIP. The Pin diagram [12]
contains the abbreviated names of the signals for each pin. It is important to note
that many of the pins are used for more than one function. Programming
functions or physical pin connections determine the use of any multifunction
pins. The system designer decides which of these functions is to be used and
designs the hardware and software affecting that pin accordingly.
20
The Pm diagram of PIC 16F873 Microcontroller is shown m Figure 2.2.
The pinout description of PIC 16F873 Microcontroller is given in table 2.1.
! - i n::
-E 3 — R67-GC
raCD?Vp=;THV-
q> rj
3 REt.'-GC
j
RAD'ANO * -t:
o> r
fj (j
RA1AN1 •> -E 3 *— RK
P IC 1 6 F 8 7 6 /8 7 3
*«•
tj fr»j oi
* -t 3 —* =.E4
a> c*
■E 3 — *.E»'-Gf,'
I
RAS/AM®* -t RBI
*-*•
co
Vss- •E 3 —~ REC.IKT
C'SCTwLK'M- -E «—Vcc
o »
OSC2.CLKOiT • -E 3 .»_Vss
n>
3 —* RC7-RXO-
RCaTtOSOTlCK - •C to rS
3 —*■ RC6'TX.'CK
RCinos.'CC?2",-*C 3 RCS'SOO
RC1'CC=1—*-E
o oi
RCS'SCK'SCl *— C 3 — RC4:SD1,'£3A
^ j
21
Pin out Description
22
2.3 MEMORY ORGANIZATION
There are three memory blocks in PIC 16F873 Microcontroller [13]. The
Program Memory and Data Memory have separate buses so that concurrent
The PIC 16F873 Program memory Map and stack is shown in Figure 2.3.
23
2.3.2 Data Memory Organization
The data memory is partitioned into multiple banks, which contain the
General Purpose Registers and the Special Function Registers. Bits RP1
(STATUS<6>) and RPO (STATUS<5>) are the bank select bits. These bits are
represented in the Table 2.2.
RP1:RP0 Bank
CO 0
01 1
10 2
11 3
Each bank extends up to 7Fh (128 bytes). The lower locations of each
bank are reserved for the Special Function Registers. Above the Special
Function Registers are General Purpose Registers, implemented as Static RAM.
All implemented banks contain Special Function Registers. Some “high use”
Special Function Registers from one bank may be mirrored in another.
The Special Function Registers are registers used by the CPU and
peripheral modules for controlling the desired operation of the device. These
registers are implemented as static RAM. The Special Function Registers can be
classified into two sets; core (CPU) and peripheral.
24
Status Register
The STATUS register contains the arithmetic status of the ALU, the
RESET status and the bank select bits for data memory. The STATUS register
can be the destination for any instruction, as with any other register. If the
STATUS register is the destination for an instruction that affects the Z, DC or C
bits, then the write to these three bits is disabled. These bits are set or cleared
according to the device logic. Furthermore, the TO and PD bits are not writable,
therefore, the result of an instruction with the STATUS register as destination
may be different than intended. It is recommended, that only BCF, BSF, SWAPF
and MOVWF instructions are used to alter the STATUS register, because these
instructions do not affect the Z, C or DC bits from the STATUS register.
❖ OptionReg Register
❖ PIE1 Register
The PIE1 register contains the individual enable bits for the peripheral
interrupts.
❖ PIR1 Register
The PIR1 register contains the individual flag bits for the peripheral
interrupts.
❖ PIE2 Register
25
The PIE2 register contains the individual enable bits for the CCP2
peripheral interrupt, the SSP bus collision interrupt, and the EEPROM write
operation interrupt.
❖ PIR2 Register
The PIR2 register contains the flag bits for the CCP2 interrupt, the SSP
bus collision interrupt and the EEPROM write operation interrupt.
❖ PCON Register
Some pins for these I/O ports are multiplexed with an alternate function
for the peripheral features on the device. In general, when a peripheral is
enabled, that pin may not be used as a general purpose I/O pin.
There are three ports in PIC 16F873 [14] Microcontroller. They are
Port A, Port B and Port C.
26
□ PORTB and the TRISB Register
2.5 TIMERS
27
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to OOh
• Edge select for external clock
• As a timer
* As a counter
28
The operating mode is determined by the clock select bit, TMR1CS
(T1C0N<1>). In timer mode, Timer 1 increments every instruction cycle. In
counter mode, it increments on every rising edge of the external clock input.
Timer 1 can be enabled/disabled by setting/clearing control bit TMRION
(T1CON<0>). Timer 1 also has an internal “reset input”. This reset can be
generated by either of the two CCP modules counter mode is selected by setting
bit TMR1CS. In this mode, the timer increments on every rising edge of clock
input on pin RC1/T10SI/CCP2, when bit T10SCEN is set, or on pin
RCO/TIOSO/TICKI, when bit T10SCEN is cleared. Timerl may operate in
asynchronous or synchronous mode depending on the setting of the TMR1CS
bit.
29
O USART Asynchronous Mode
The USART transmitter block diagram is shown in Figure 2.4. The heart
of the transmitter is to transmit (serial) shift register (TSR). The shift register
obtains its data from the read/write transmit buffer, TXREG. The TXREG
register is loaded with data in software. The TSR register is not loaded until the
STOP bit has been transmitted from the previous load. As soon as the STOP bit
is transmitted, the TSR is loaded with new data from the TXREG register (if
available). Once the TXREG register transfers the data to the TSR register
(occurs in one TCY), the TXREG register is empty and flag bit TXIF (PIR1<4>)
is set. This interrupt can be enabled/disabled by setting/clearing enable bit TXIE
(PIE1<4>). Flag bit TXIF will be set, regardless of the state of enable bit TXIE
30
and cannot be cleared in software. It will reset only when new data is loaded into
the TXREG register. While flag bit TXIF indicates the status of the TXREG
register, another bit TRMT (TXSTA<1>) shows the status of the TSR register.
Status bit TRMT is a read only bit, which is set when the TSR register is empty.
No interrupt logic is tied to this bit, so the user has to poll this bit in order to
determine if the TSR register is empty. Transmission is enabled by setting
enable bit TXEN (TXSTA<5>).
• Initialize the SPBRG register for the appropriate baud rate. If a high
speed baud rate is desired, set bit BRGH.
• Enable the asynchronous serial port by clearing bit SYNC and setting bit
SPEN.
• If interrupts are desired, then get enable bit TXIE.
• If 9-bit transmission is desired, then set transmit bit TX9.
• Enable the transmission by setting bit TXEN, which will also set bit
TXIF.
• If 9-bit transmission is selected, the ninth bit should be loaded in bit
TX9D.
• Load data to the TXREG register (starts transmission).
31
O USART ASYNCHRONOUS RECEIVER
32
;<34 Baud Rate C_K
CREN
SP3RG 1
50'*
Baud Rate Generate' n •
»ie
RC7.-RX.-DT
SPEM
Interrupt
• Initialize the SPBRG register for the appropriate baud rate. If a high
• Enable the asynchronous serial port by clearing bit SYNC and setting bit
SPEN.
• Flag bit RCIF will be set when reception is complete and an interrupt
• Read the RCSTA register to get the ninth bit (if enabled) and determine
• If any error occurred, clear the error by clearing enable bit CREN.
The Analog-to-Digital (A/D) Converter module [14] has five inputs for
the 28-pin devices and eight for the other devices. The analog input charges a
sample and hold capacitor. The output of the sample and hold capacitor is the
input into the converter. The converter then generates a digital result of this
33
analog level via successive approximation. The A/D conversion of the analog
input signal results in a corresponding 10-bit digital number. The A/D module
has high and low voltage reference input that is software selectable to some
combination of VDD, VSS, RA2 or RA3.The A/D converter has a unique
feature of being able to operate while the device is in SLEEP mode. To operate
in sleep, the A/D clock must be derived from the A/D’s internal RC oscillator.
The ADCONO register controls the operation of the A/D module. The
ADCON1 register configures the functions of the port pins. The port pins can be
configured as analog inputs (RA3 can also be the voltage reference) or as digital
I/O.The ADRESH: ADRESL registers contain the 10-bit result of the A/D
conversion. When the A/D conversion is complete, the result is loaded into this
A/D result register pair, the GO/DONE bit (ADCONO<2>) is cleared and the
A/D interrupt flag bit ADIF is set. The block diagram of the A/D module is
shown in Figure 2.6.After the A/D module has been configured as desired, the
selected channel must be acquired before the conversion is started. The analog
input channels must have their corresponding TRIS bits selected as inputs.
34
> Configure A/D interrupt (if desired):
• Clear ADIF bit
• Set ADIE bit
• Set GIE bit
> Wait the required acquisition time.
> Start conversion:
• Set GO/DONE bit (ADCONO)
> Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
OR
• Waiting for the A/D interrupt
> Read A/D Result register pair (ADRESH: ADRESL), clear bit
ADIF if required.
> For next conversion, go to step 1 or step 2 as required. The A/D
conversion time per bit is defined as TAD. A minimum wait of
2TAD is required before next acquisition starts.
35
CH$2:ChSC
36
Timer Wake-up). The TO bit in the STATUS register will be cleared upon a
Watchdog Timer time-out. The WDT can be permanently disabled by clearing
configuration bit WDTE. Values for the WDT prescaler may be assigned using
the OPTION REG register.
«"*— P32P3C
To TMRC (Figure M)
PSA
WOT
Tire-ou?
37
from STATUS register .Any access to SFR registers can be an example of direct
addressing.
• Indirect Addressing Mode
Indirect addressing mode does not take an address from an instruction but
makes it with the help of IRP bit of STATUS and FSR registers. Addressed
location is accessed via INDF register which infact holds the address indicated
by a FSR.
For example, one general purpose register at address OFh contains a value
of 20.By writing a value of OFh in FSR register we will get a register indicator at
address OFh and by reading from INDF register, we will get a value of 20, which
means that we have read from the first register its value without accessing it
directly.
The instruction set is highly orthogonal [17] and is grouped into three
basic categories:
• Byte-oriented operations
• Bit-oriented operations
• Literal and control operations
All instructions are executed within one single instruction cycle, unless a
conditional test is true or the program counter is changed as a result of an
instruction. In this case, the execution takes two instruction cycles with the
second cycle executed as a NOP. One instruction cycle consists of four oscillator
periods. Thus, for an oscillator frequency of 4 MHz, the normal instruction
38
execution time is 1 ms. If a conditional test is true or the program counter is
changed as a result of an instruction, the instruction execution time is 2 ms.
• Byte-oriented operations
• Bit-oriented operations
For literal and control operations, ’k’ represents an eight or eleven bit
constant or literal value.
39
Jiersi and centra ape-atons
Gere-a
IS 8 C
OPCODE h (kerall
The PIC 16F873 Microcontroller instruction set [18] is shown in Table 2.3
Some of the important instructions used in the present study are explained
below in detail.
40
Instruction Descriptions
■ CLRF Clear f
■ COMF Complement f
■ DECF Decrement f
■ INCF Increment f
42
Operands: 0£f£127d I [0,1]
* NOP No Operation
Syntax: [label] NOP
Operands: None
Operation: No operation
Status Affected: None
Description: No operation.
43