Embedded Programming Textbook PDF
Embedded Programming Textbook PDF
Embedded Programming Textbook PDF
Contents
Preface
Acknowledgements
ix
Bibliography
Chapter 1
1.0
1.1
1.2
1.3
1.4
1.5
Introduction
Overview of 8051 Micro-controller
On-Chip Memory Organization
Special Function Registers
Multiplexing Data and Address Bus
Tutorial Questions
Chapter 2
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
4.0
4.1
4.2
4.3
4.4
4.5
4.6
22
22
24
26
29
31
32
32
33
38
Instruction Set .. 39
Introduction
Addressing Modes
Instruction Types
Tutorial Questions
Chapter 4
2
2
5
11
17
19
Introduction
CIP-51
C8051F020 System Overview
Memory Organization
I/O Ports and Crossbar
12-Bit Analog to Digital Converter
8-Bit Analog to Digital Converter
Digital to Analog Converters and Comparators
Voltage Reference
Tutorial Questions
Chapter 3
3.0
3.1
3.2
3.3
40
40
43
69
Introduction
Address Control
Symbol Definition
Memory Initialization/Reservation
Segment Control
Example Program Template
Tutorial Questions
72
72
74
75
78
80
81
Chapter 5
5.0
5.1
5.2
5.3
5.4
5.5
5.6
5.7
Introduction
Oscillator Programming Registers
Watchdog Timer
Digital Crossbar
GPIO
Crossbar and GPIO SFRs
Ports 4 through 7
Tutorial Questions
Chapter 6
6.0
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
8.0
8.1
8.2
8.3
8.4
8.5
8.6
8.7
ii
110
110
111
115
122
123
127
127
129
130
Introduction
Starting a Project
Blinking Using Software Delays
Blinking Using a Timer
Programming the LCD
Reading Analog Signals
Expansion Board Pictures
Circuit Diagram of the Expansion Board
Expansion Board Physical Component Layout
Chapter 8
85
86
88
90
93
96
103
106
Introduction
Register Definitions, Initialization and Startup Code
Programming Memory Models
C Language Control Structures
Functions
Interrupt Functions
Re-entrant Functions
Pointers
Summary of Data Types
Tutorial Questions
Chapter 7
7.0
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
132
134
135
138
142
151
153
154
155
Introduction
Functional Description
Timer Programming
Timer SFRs
Timers 0 and 1 Operating Modes
Timers 2, 3 & 4 Operating Modes
CKCON Register
Timers 0 and 1 SFRs
158
159
160
161
161
164
170
171
8.8
8.9
8.10
8.11
8.12
Timer 2 SFRs
Timer 3 SFRs
Timer 4 SFRs
Timer 2 - C Programming Example
Tutorial Questions
Chapter 9
9.0
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11
9.12
9.13
173
175
176
178
181
Introduction
12-Bit ADC (ADC0)
Data Word Conversion Map (12-bit)
Programming ADC0
ADC0 SFRs
8-Bit ADC (ADC1)
Data Word Conversion Map (8-bit)
Programming ADC1
ADC1 SFRs
12-Bit DACs (DAC0 and DAC1)
Programming the DACs
DAC0 SFRs
DAC1 SFRs
Tutorial Questions
185
186
188
189
195
199
201
201
206
208
210
212
213
214
Introduction
UART0 and UART1
Programming the UARTs
Operation Modes
Interrupt Flags
UARTx SFRs
Blinking LED at Different Frequencies
C Programming Example
10.7 Tutorial Questions
216
217
219
220
225
227
229
233
Introduction
Interrupt Organization
Interrupt Process
Interrupt Vectors
External Interrupts
Interrupt Latency
Interrupt SFRs
Tutorial Questions
236
236
239
239
240
241
241
249
iv
Preface
The 8051 Family is one of the fasted growing microcontroller
architectures in the world of electronics today. Intel Corporation
introduced 8051 more than two decades ago; it is as popular today as it
was then. Several chip vendors have extended 8051 to incorporate
advanced features, numerous additional peripherals and extra memory,
and have implemented mixed-signal capabilities. There are over 400
device variants of 8051 from various silicon vendors like Silicon
Laboratories, Philips, Amtel, Dallas and Infineon to name a few. 8051
and its variants are immensely popular in the academic world too, just as
they are in the arena of industrial applications. Embedded microcontrollers have found their way into many everyday appliances like the
microwave oven, dish-washer and the fax machine. It is found even in
toys! Of course microprocessors and microcontrollers have been the
workhorses of embedded industrial applications for many years. It is for
these reasons that teaching of microcontrollers is integral to any degree
or diploma course in Electronics, Electrical, Mechatronics and
Information Science.
Why we
wrote this
book?
Who
should
read this
book?
What is
in this
book?
Assembly level programming has had its days, though they are by no
means over yet. It is still the preferred language choice for writing
applications where program execution speed is critical and of paramount
importance such as in real-time systems. However, with the advent of
robust high level programming language compilers, there is a definite
shift towards writing programs in high-level languages such as C. In this
vi
vii
viii
Acknowledgements
Several wonderful people have contributed to the successful completion
of this book and the authors wish to extend their sincere thanks to them
for their fabulous work.
Dr Chris Messom, Institute of Information and Mathematical Sciences,
Massey University, wrote Chapter 6, C8051F020 C Programming.
Ken Mercer, Institute of Information Sciences and Technology, Massey
University, wrote Chapter 7, Expansion Board for C8051F020 Target
Board. Ken also designed and built the expansion board and for this he
was ably supported by our student, Jonathan Seal. Together they have
done a wonderful job.
James Cheong, graduate of IIST, Massey University, contributed by way
of initial script typing and building the data tables.
Dr Subhas Mukhopadhyay, Institute of Information Sciences and
Technology, Massey University, made several valuable suggestions to
improve the book overall.
Dr Douglas R. Holberg, Director of Engineering, Silicon Laboratories,
for his immense enthusiasm and support for the project.
Last, but not the least, the authors gratefully acknowledge the efforts put
in by Prof. Serge Demidenko, Monash University, for pouring over the
manuscript and painstakingly editing it. His suggestions and inputs have
been invaluable.
ix
Bibliography
I. Scott MacKenzie, The 8051 Microcontroller, Upper Saddler River,
N.J.: Prentice Hall, 1999
Sencer Yeralan, Helen Emery, The 8051 cookbook for Assembly and
C : with experiments in mechatronics and robotics, Gainesville, Fla.:
Rigel Press, 2000
David
Calcutt,
Fred
Cowan,
Hassan
Parchizadeh,
8051
microcontrollers : an applications-based introduction, Boston, Mass.
: Newnes, 2003
Cx51 Compiler, Optimizing C Compiler and Library Reference for
Classic and Extended 8051 Microcontrollers Users Guide, Keil
Software, 11.2000
Macro Assembler and Utilities for 8051 and Variants, Macro
Assembler, Linker/Locator, Library Manager, Object Hex-Converter
for 8051, extended 8051 and 251 Microcontrollers Users Guide, Keil
Software , 07.2000
C8051F020 Data Sheets (C8051F020/1/2/3-DS14 Preliminary Rev. 1.4),
Silicon Laboratories, 12/03
1
8051 Architecture Overview
1.0
Introduction
1.1
1.2
1.3
11
1.4
17
1.5
Tutorial Questions
19
1.0
1.1
Introduction
In 1980, Intel introduced 8051, which is the first device in the MCS-51
family of microcontroller, to the market. There are other second source
suppliers of the ICs; these include Silicon Laboratories, Atmel, Philips,
Dallas Semiconductor and several others.
Though more than 20 years have passed since its introduction, the 8051
is still as relevant today as it was in those days. In recent years some
companies have incorporated many different features into the basic 8051
chip and one such company is Silicon Laboratories. In 2000, Silicon
Laboratories manufactured a field programmable, mixed signal chip
(C8051F020) based on the 8051 core CPU. The chip is offered in
different combinations of clock speed, FLASH and on-chip RAM size.
They also offer different digital and analog peripherals such as:
I/O ports
Timers/Counters
Temperature Sensor
3
T0
/INT0 /INT1
T1
Other
interrupts
128 bytes
Data Memory
(RAM)
4K byte Program
Memory (ROM)
Timer/Counter
(Timer 0 &
Timer 1)
8051 CPU
Oscillator
&Timing
From Crystal
Oscillator or RC
network
64 K Bus
Expansion Control
ALE
/PSEN
I/O ports
P3 P2 P1 P0
(Address/data)
Serial Port
TxD
RxD
Ports
Intel 8051 has
four multipurpose
flexible ports
which are bitand byteaddressable.
The 8051 consists of 4 standard Ports (0, 1, 2, and 3). The ports are
multi-purpose. In a minimum-component basic design without expansion,
they are used as general purpose I/O. For larger designs incorporating
external memory, the ports function as multiplexed address and data
buses. With careful hardware design and satisfying the timing
requirements of 8051, the external memory can be easily and
seamlessly made accessible to the programmer. In addition, the ports
may be controlled by a digital peripheral, UART or external interrupts.
The ports are both bit- and byte-addressable. For example, the pins of
Port 0 are designated as P0.0, P0.1, P0.2, etc. Figure 1.2 shows the pin
assignments of 8051.
the port lines are available for data input or output. When ALE falls,
signaling the beginning of the second phase, the address latch outputs
remain fixed and are no longer dependent on the latch input. Later in the
second phase, the Data Bus controls the state of the AD[0:7] at the time
/RD or /WR is asserted.
XTL2
18
XTL1
19
RXD P3.0
TXD P3.1
/INT0 P3.2
/INT1 P3.3
T0
P3.4
T1
P3.5
/WR P3.6
/RD P3.7
10
11
12
13
14
15
16
17
RST
/EA
31
ALE
30
/PSEN
29
8051
40
VCC
39
38
37
36
35
34
33
32
P0.0 AD0
P0.1 AD1
P0.2 AD2
P0.3 AD3
P0.4 AD4
P0.5 AD5
P0.6 AD6
P0.7 AD7
21
22
23
24
25
26
27
28
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
1
to
P1.0
to
8
20
P1.7
VSS
AD8
AD9
AD10
AD11
AD12
AD13
AD14
AD15
Reset (RST)
Reset circuitry allows the 8051 to be easily placed in a predefined default
condition. On entry to the reset state, the following occur:
XTAL2
The nominal crystal frequency is 12 MHz for most ICs in the MCS-51
family. The on-chip oscillator neednt be driven by a crystal; it can be
replaced by a TTL clock source connected to XTAL1 instead.
1.2
Code and data
memory may
be expanded
using external
components.
FFFFH
0FFFH
0FFFH
/EA=0
External
/EA=1
Internal
0000H
0000H
/PSEN
External
0FFFFH
Internal
FFH
80H
7FH
00H
Upper 128
Indirect
addressing
Special Function
Registers
Direct
Addressing
Lower 128
Direct/Indirect
Addressing
/RD
0000H
/WR
Figure 1.5 Internal & External Data memory (Random Access Memory) Organization
As shown in Figure 1.6a and 1.6b, the internal data memory space is
divided into register banks (00H-1FH), bit-addressable RAM (20H-2FH),
general purpose RAM (30H-7FH), and special function registers (80HFFH).
In the Lower 128 bytes of RAM, 4 banks of 8 registers each are available
to the user. The 8 registers are named R0 through R7. By programming
two bits in the Program Status Word (PSW), an appropriate register bank
can be selected.
In the Special Function Register (SFR) block (Figure 1.6b) registers
which have addresses ending with OH or 8H are byte- as well as bitaddressable. Some registers are not bit-addressable at all. For example,
the Stack Pointer Register (SP) and Data Pointer Register (DPTR) are
not bit-addressable.
Byte
Address
7F
Bit Address
General
Purpose
RAM
B
i
t
A
d
d
r
e
s
s
a
b
l
e
30
2F
2E
2D
2C
2B
2A
29
28
27
26
25
24
23
22
21
20
1F
18
17
10
0F
08
07
00
7F
77
6F
67
5F
57
4F
47
3F
37
2F
27
1F
17
0F
07
7E
76
6E
66
5E
56
4E
46
3E
36
2E
26
1E
16
0E
06
7D
75
6D
65
5D
55
4D
45
3D
35
2D
25
1D
15
0D
05
7C
74
6C
64
5C
54
4C
44
3C
34
2C
24
1C
14
0C
04
7B
73
6B
63
5B
53
4B
43
3B
33
2B
23
1B
13
0B
03
7A
72
6A
62
5A
52
4A
42
3A
32
2A
22
1A
12
0A
02
79
71
69
61
59
51
49
41
39
31
29
21
19
11
09
01
Bank 3
Bank 2
Bank 1
Default Register Bank for R0 R7
78
70
68
60
58
50
48
40
38
30
28
20
18
10
08
00
Byte
Address
Bit Address
FF
F0
F7
F6
F5
F4
F3
F2
F1
F0
E0
E7
E6
E5
E4
E3
E2
E1
E0
ACC
D0
D7
D6
D5
D4
D3
D2
D0
PSW
B8
BC
BB
BA
B9
B8
IP
B0
B7
B6
B5
B4
B3
B2
B1
B0
P3
A8
AF
AC
AB
AA
A9
A8
IE
A0
A7
A6
A5
A4
A3
A2
A1
A0
P2
99
98
9F
96
Not bit-addressable
95
94
93
92
91
90
SBUF
SCON
90
97
96
95
91
90
P1
94
93
92
8D
8C
8B
Not bit-addressable
Not bit-addressable
Not bit-addressable
8A
Not bit-addressable
TL0
89
88
87
Not bit-addressable
8D 8C 8B
8A
Not bit-addressable
TMOD
TCON
PCON
8F
8E
TH1
TH0
TL1
89
88
83
Not bit-addressable
DPH
82
81
80
Not bit-addressable
Not bit-addressable
85
84
83
82
DPL
SP
P0
87
86
81
80
10
Any location in
the general
purpose RAM
can be
accessed
freely using
the direct or
indirect
addressing
modes.
There are 80 bytes of general purpose RAM from address 30H to 7FH.
The bottom 32 bytes from 00H to 2FH can be used as general purpose
RAM too, although these locations have other specific use also.
Any location in the general purpose RAM can be accessed freely using
the direct or indirect addressing modes. For example, to read the
contents of internal RAM address 62H into the accumulator A, the
following instruction could be used:
MOV
A,62H
The above instruction uses direct addressing and transfers a byte of data
from location 62H (source) to the accumulator (destination). The
destination for the data is implicitly specified in the instruction op-code as
the accumulator (A). Addressing modes are discussed in details in
Chapter 3 (Instruction Set).
Internal RAM can also be accessed using indirect addressing through R0
or R1. The following two instructions perform the same operation as the
single instruction above:
MOV
MOV
R1,#62H
A,@R1
The first instruction uses immediate addressing to transfer the value 62H
into register R1. The second instruction uses indirect addressing to
transfer the data pointed to by R1 into the accumulator.
Bit-addressable RAM
There are 128 general purpose bit-addressable locations at byte
addresses 20H through 2FH. For example, to clear bit 78H, the following
instructions could be used:
CLR
78H
Referring to Figure 1.6a, note that bit address 78H is the least
significant bit (bit 0) at byte address 2FH. The instruction has no effect
on the other bits at this address. The same operation can also be
performed as follows:
MOV
ANL
MOV
11
A,2FH
A,#11111110B
2FH,A
2F.0H
Register Banks
The bottom 32 locations of the internal memory, from location 00H to
1FH, contain the register banks. The 8051 instruction set supports 8
registers, R0 through R7. After a system reset these registers are at
addresses 00H to 07H. The following instruction reads the contents of
address 04H into the accumulator A:
MOV
A,R4
1.3
SFRs are
generally
accessed
using direct
addressing.
Some SFRs
are both bitand byteaddressable.
0D3H
0D4H
12
set bit 3 and 4 in the Program Status Word (PSW.3 and PSW.4), leaving
the other bits unchanged. This will select Bank 3 for registers R0 to R7 at
address locations 18H to 1FH. Since the SETB instruction operates on
bits (not bytes), only the addressed bit is affected.
Symbol
Bit Address
Description
PSW.7
CY
D7H
Carry flag
PSW.6
AC
D6H
PSW.5
F0
D5H
User Flag 0
PSW.4
RS1
D4H
PSW.3
RS0
D3H
PSW.2
OV
D2H
PSW.1
D1H
Reserved
PSW.0
D0H
A,#15
leaves a value of 00H in the accumulator and sets the carry flag in PSW
(PSW.7).
The carry flag is extensively used as a 1-bit register in Boolean
operations on bit-valued data. For example, the following instruction
13
ANDs bit 38H with the carry flag and places the result back in the carry
flag:
ANL
C,038H
R0,#2
A,#8
A,R0
RS1
SETB
RS0
MOV
A,R0
14
test this bit to determine if the result is in the proper range. For 8-bit
signed numbers, the result should be in the range of +127 to 128).
Even Parity Flag (P)
The number of 1 bits in the accumulator plus the parity bit (P) is always
even. The parity bit is automatically set or cleared to establish even
parity with the accumulator. For example, if the accumulator contains
00101100B then P is set to 1. P is reset to 0 if the accumulator contains
even number of 1s.
The B Register
The B register, or accumulator B, is at address F0H and is used along
with the accumulator for multiplication and division operations. For
example:
MOV
MOV
MUL
A,#9
B,#5
AB
MOV
MOV
MUL
A,#99
B,#5
AB
;99 x 5 = 495 or 1EFH, B=1, A=EFH
MOV
MOV
DIV
A,#10
B,#5
AB
;10/5 = 2, Remainder=0 , B=0, A=2
MOV
MOV
DIV
A,#99
B,#5
AB
;99/5=19(13H),Remainder=4,B=4,A=13H
The B register can also be used as a general purpose register. It is bitaddressable through bit address F0H to F7H.
Stack Pointer
The stack pointer (SP) is an 8-bit register and is located at address 81H.
Stack operations include pushing data on the stack and popping data
off the stack. Each time data is pushed on to the stack, SP is
15
A,#20H
SP,#6FH
ACC
6FH
70H
XX
20H
.
.
.
Figure 1.7 Memory snap-shot of the Stack
Data Pointer
The data pointer register (DPTR) is used to access external code or data
memory. It is a 16-bit register located at addresses 82H (DPL, low byte)
and 83H (DPH, high byte). The following instructions load 5AH into the
external RAM location 1040H.
MOV
MOV
MOVX
A,#5AH
DPTR,#1040H
@DPTR,A
The first instruction uses immediate addressing to load the data constant
5AH into the accumulator. The second instruction also uses immediate
addressing to load the 16-bit address constant 1040H into the data
pointer. The third instruction uses indirect addressing to move the value
in A (i.e. 5AH) to the external RAM location whose address is in the
DPTR register (i.e. 1040H). The X in the mnemonic MOVX indicates
that the move instruction accesses external data memory.
16
Bitaddressable
ports of 8051
provide
powerful
interfacing
possibilities.
The 8051 I/O ports consist of Port 0 located at address 80H, Port 1 at
address 90H, Port 2 at address A0H and Port 3 is located at address
B0H. All the ports are bi-directional. Besides being used as general
input/output lines, Port 0 and 2 can be used to form the 16-bit address in
order to interface with the external memory, with Port 0 being the low
byte of the address and the Port 2 outputting the high byte of the
address. Similarly port 3 pins have alternate functions of serving as
interrupt and timer inputs, serial port inputs and outputs, as well as RD
and WR lines for external Data Memory.
All the ports are bit-addressable and thus provide powerful interfacing
possibilities. For example, if a Light Emitting Diode (LED) is connected
through an appropriate driver to Port 1 bit 5, it could be turned on and off
using the following instructionsSETB
P1.5
P1.5
95H
Timer Registers
The 8051
contains two
16-bit
timer/counters
for timing
intervals and
counting
events.
The basic 8051 contains two 16-bit timer/counters for timing intervals or
counting events. Timer 0 is located at addresses 8AH (TL0, low byte)
and 8CH (TH0, high byte) and Timer 1 is located at addresses 8BH (TL1,
low byte) and 8DH (TH1, high byte). The Timer Mode register (TMOD),
which is located at address 89H, and the Timer Control register (TCON),
which is located at address 88H, are used to program the timer
operations. Only TCON is bit-addressable. Timer operations and
programming details of C8051F020 are discussed in Chapter 8.
17
1.4
18
Figure 1.8a shows the normal write cycle in the execution of a 8051
instruction. Figure 1.8b shows the hardware connection to de-multiplex
the address and data lines to allow for external memory access.
Memory Cycle
A8-A15
Address
ALE
AD0-AD7
Address
Data
/WR
Figure 1.8a Write cycle of 8051 instruction
D0
D7
8051
P0.0
P0.7
ALE
AD0
AD7
74LS373
A0
Latch
EL
/OE
A7
P2.0
A8
P2.7
A15
/WR
Figure 1.8b Hardware connection to de-multiplex the address and data bus
1.5
19
Tutorial Questions
1.
2.
3.
Write an instruction to initialize the stack pointer to create a 32byte stack at the top of the memory of 8051.
4.
What is the bit address of bit 2 in the byte address 2BH in the
8051s internal data memory?
5.
What is the bit address of the most significant bit in the byte
address 2DH in the 8051s internal data memory?
6.
What is the state of the Parity Flag in the PSW after the execution
of each of the following instructions?
(a)
(b)
MOV A, #0F1H
MOV A, #0CH
2
Intro to Silicon Labs C8051F020
2.0
Introduction
22
2.1
CIP-51
22
2.2
24
2.3
Memory Organization
26
2.4
29
2.5
31
2.6
32
2.7
2.8
Voltage Reference
33
2.9
Tutorial Questions
38
22
2.0
Introduction
This chapter gives an overview of the Silicon Labs C8051F020 microcontroller. On-chip peripherals like ADC and DAC, and other features like
the cross-bar and the voltage reference generator are briefly introduced.
While programming using a high level language, such as C, makes it
less important to know the intricacies of the hardware architecture of the
micro-controller, it is still beneficial to have some knowledge of the
memory organization and special function registers. Thus, these are also
covered in this chapter.
2.1
CIP-51
Silicon Labs mixed-signal system chips utilize the CIP-51 microcontroller
core. The CIP-51 implements the standard 8051 organization, as well as
additional custom peripherals. The block diagram of the CIP-51 is shown
in Figure 2.1.
Silicon Labs
mixed-signal
system chips
utilise the CIP51 microcontroller core,
which is fully
compatible
with 8051
instruction
sets.
23
TMP2
B REGISTER
STACK POINTER
SRAM
ADDRESS
REGISTER
PSW
SRAM
(256 X 8)
D8
D8
D8
ALU
D8
DATA BUS
D8
D8
TMP1
ACCUMULATOR
D8
D8
D8
DATA BUS
DATA BUS
SFR_ADDRESS
BUFFER
D8
DATA POINTER
D8
D8
SFR
BUS
INTERFACE
SFR_CONTROL
SFR_WRITE_DATA
SFR_READ_DATA
DATA BUS
PC INCREMENTER
PROGRAM COUNTER (PC)
PRGM. ADDRESS REG.
PIPELINE
RESET
CLOCK
STOP
IDLE
MEM_ADDRESS
D8
A16
MEMORY
INTERFACE
MEM_CONTROL
MEM_WRITE_DATA
MEM_READ_DATA
D8
CONTROL
LOGIC
SYSTEM_IRQs
D8
POWER CONTROL
REGISTER
INTERRUPT
INTERFACE
EMULATION_IRQ
D8
Clock Cycles to
execute
Number of
Instructions
2/3
3/4
4/5
26
50
14
24
2.2
ADC
VREF
12-Bit
DAC
12-Bit
DAC
8-bit
500ksps
ADC
PGA
+
VOLTAGE
COMPARATORS
DIGITAL I/O
UART0
UART1
SMBus
SPI Bus
PCA
Timer 0
Timer 1
Timer 2
Timer 3
Timer 4
CROSSBAR
10/12-bit
100ksps
PGA
AMUX
Silicon Labs
C8051F020 is
a fully
integrated
mixed-signal
System-on-aChip microcontroller
AMUX
TEMP
SENSOR
Port 0
External Memory Interface
ANALOG PERIPHERALS
Port 1
Port 2
Port 3
Port 4
Port 5
Port 6
Port 7
64KB
ISP FLASH
DEBUG
CIRCUITRY
4352 B
JTAG
SRAM
CLOCK
SANITY
CIRCUIT
CONTROL
25
Peak Throughput
FLASH Program Memory
On-chip Data RAM
Full-duplex UARTS
16-bit Timers
Digital I/O Ports
12-bit 100ksps ADC
8-bit 500ksps ADC
DAC Resolution
DAC Outputs
Analog Comparators
Interrupts
Programmable Counter Arrays
(PCA)
25 MIPS
64K
4352 bytes
x2
x5
64 pins
8 channels
8 channels
12 bits
x2
x2
Two levels
VDD
VDD
VDD
DGND
DGND
DGND
Port I/O
Config.
Digital Power
AV+
AV+
AGND
AGND
Analog Power
TCK
TMS
TDI
TDO
JTAG
Logic
8
0
5
1
Boundary Scan
Debug HW
Reset
/RST
MONEN
VDD
Monitor
XTAL1
XTAL2
External
Oscillator
Circuit
C
o
r
e
WDT
System
Clock
Internal
Oscillator
UART0
UART1
SMBus
SPI Bus
PCA
SFR Bus
64kbyte
FLASH
256 byte
RAM
Timers 0,
1, 2, 4
Timer 3/
RTC
P0, P1,
P2, P3
Latches
VREFD
DAC1
DAC1
(12-Bit)
DAC0
DAC0
(12-Bit)
4kbyte
RAM
CP0+
CP0CP1+
CP1-
A
M
U
X
Prog
Gain
TEMP
SENSOR
CP0
Prog
Gain
P1
Drv
P1.0/AIN1.0
P2
Drv
P2.0
P3
Drv
P3.0
P0.7
P1.7/AIN1.7
P2.7
P3.7
A
M
U
X
8:1
P4.0
Bus Control
ADC
100ksps
(12-Bit)
P0.0
VREF1
ADC
500ksps
(8-Bit)
C
T
L
VREF0
AIN0.0
AIN0.1
AIN0.2
AIN0.3
AIN0.4
AIN0.5
AIN0.6
AIN0.7
P0
Drv
Crossbar
Config.
VREF
VREF
C
R
O
S
S
B
A
R
Address Bus
Data Bus
A
d
d
r
D
a
t
a
CP1
P4 Latch
P4
DRV
P5 Latch
P5
DRV
P5.0/A8
P6 Latch
P6
DRV
P6.0/A0
P7 Latch
P7
DRV
P7.0/D0
P4.4
P4.5/ALE
P4.6/RD
P4.7/WR
P5.7/A15
P6.7/A7
P7.7/D7
26
2.3
Memory Organization
The memory organization of the CIP-51 System Controller is similar to
that of a standard 8051 (Figure 1.2). There are two separate memory
spaces: program memory and data memory. The CIP-51 memory
organization is shown in Figure 2.4. Program and data memory share the
same address space but are accessed via different instruction types.
The memory
organisation of
the CIP-51
System
Controller is
similar to that
of a standard
8051
PROGRAM/DATA MEMORY
(FLASH)
0x1007F
0x10000
0xFFFF
0xFE00
0xFDFF
Scrachpad Memory
(DATA only)
0xFF
0x80
0x7F
RESERVED
FLASH
(In-System
Programmable in 512
Byte Sectors)
0x30
0x2F
0x20
0x1F
0x00
Bit Addressable
Special Function
Register's
(Direct Addressing Only)
General Purpose
Registers
0x0000
0xFFFF
0x1000
0x0FFF
0x0000
27
Program Memory
There are two
separate
memory
spaces:
program
memory and
data memory
Data Memory
The C8051F020 data memory has both internal and external address
spaces. The internal data memory consists of 256 bytes of RAM. The
Special Function Registers (SFR) are accessed anytime the direct
addressing mode is used to access the upper 128 bytes of memory
locations from 0x80 to 0xFF, while the general purpose RAM are
accessed when indirect addressing is used (refer to Chapter 3 for
addressing modes). The first 32 bytes of the internal data memory are
addressable as four banks of 8 general purpose registers, and the next
16 bytes are bit-addressable or byte-addressable.
The external data memory has a 64K address space, with an on-chip 4K
byte RAM block. An external memory interface (EMIF) is used to access
the external data memory. The EMIF is configured by programming the
EMI0CN and EMI0CF SFRs. The external data memory address space
can be mapped to on-chip memory only, off-chip memory only, or a
combination of the two (addresses up to 4K directed to on-chip, above
4K directed to EMIF). The EMIF is also capable of acting in multiplexed
mode or non-multiplexed mode, depending on the state of the EMD2
(EMI0CF.4) bit.
Stack
The programmer stack can be located anywhere in the 256 byte internal
data memory. A reset initializes the stack pointer (SP) to location 0x07;
therefore, the first value pushed on the stack is placed at location 0x08,
which is also the first register (R0) of register bank 1. Thus, if more than
one register bank is to be used, the stack should be initialized to a
location in the data memory not being used for data storage. The stack
depth can extend up to 256 bytes.
28
SPI0CN
PCA0H
F0
PCA0CPH0
PCA0CPH1
PCA0CPH2
PCA0CPH3
PCA0CPH4
WDTCN
SCON1
SBUF1
SADDR1
TL4
TH4
EIP1
EIP2
E8
ADC0CN
PCA0L
PCA0CPL0
PCA0CPL1
PCA0CPL2
PCA0CPL3
PCA0CPL4
RSTSRC
EIE2
E0
ACC
XBR0
XBR1
XBR2
RCAP4L
RCAP4H
EIE1
D8
PCA0CN
PCA0MD
PCA0M0
PCA0CPM1
PCA0CPM2
PCA0CPM3
PCA0CPM4
D0
PSW
REF0CN
DAC0L
DAC0H
DAC0CN
DAC1L
DAC1H
DAC1CN
C8
T2CON
T4CON
RCAP2L
RCAP2H
TL2
TH2
C0
SMB0CN
SMB0STA
SMB0DAT
SMB0ADR
ADC0GTL
ADC0GTH
ADC0LTL
ADC0LTH
B8
IP
SADEN0
AMX0CF
AMX0SL
ADC0CF
P1MDIN
ADC0L
ADC0H
B0
P3
OSCXCN
OSCICN
P74OUT
FLSCL
FLACL
A8
IE
SADDR0
ADC1CN
ADC1CF
AMX1SL
P3IF
SADEN1
EMI0CN
A0
P2
EMI0TC
EMI0CF
P0MDOUT
P1MDOUT
P2MDOUT
P3MDOUT
98
SCON0
SBUF0
SPI0CFG
SPIODAT
ADC1
SPI0CKR
CPT0CN
CPT1CN
90
P1
TMR3CN
TMR3RLL
TMR3RLH
TMR3L
TMR3H
P7
88
TCON
TMOD
TL0
TL1
TH0
TH1
CKCON
PSCTL
80
P0
SP
DPL
DPH
P4
P5
P6
PCON
1(9)
2(A)
3(B)
4(C)
5(D)
6(E)
7(F)
0(8)
Bit
addressable
SMB0CR
The SFRs provide control and data exchange with the C8051F020s
resources and peripherals. The C8051F020 duplicates the SFRs found
in a typical 8051 implementation as well as implements additional SFRs
which are used to configure and access the sub-systems unique to the
microcontroller. This allows the addition of new functionalities while
retaining compatibility with the MCS-51 instruction set. Table 2.3 lists
the SFRs implemented in the CIP-51 microcontroller.
C8051F020
duplicates the
SFRs of 8051
and
implements
additional
SFRs used to
configure and
access the
microcontroller
sub-systems
The SFR registers are accessed anytime the direct addressing mode is
used to access memory locations from 0x80 to 0xFF. The SFRs with
addresses ending in 0x0 or 0x8 (e.g. P0, TCON, P1, SCON, IE etc.) are
bit-addressable as well as byte-addressable. All other SFRs are byteaddressable only. Unoccupied addresses in the SFR space are reserved
for future use. Accessing these areas will have an indeterminate effect
and should be avoided.
2.4
29
C8051F020
has a total of
64 general
purpose port
I/O pins
/WEAK-PULLUP
VDD
PUSH-PULL
/PORT-OUTENABLE
(WEAK)
PORT
PAD
PORT-OUTPUT
ANALOG INPUT
VDD
Analog Select
(Port 1 Only)
DGND
PORT-INPUT
30
Highest
Priority
UART0
SPI
P0MDOUT, P1MDOUT,
P2MDOUT, P3MDOUT
Registers
External
Pins
SMBus
Lowest
Priority
XBR0, XBR1,
XBR2, P1MDIN
Registers
UART1
Priority
Decoder
8
PCA
P0
I/O
Cells
P0.0
Highest
Priority
P0.7
Comptr.
Outputs
Digital
Crossbar
T0, T1,
T2, T2EX,
T4,T4EX
/INT0,
/INT1
P1
I/O
Cells
P1.0
P1.7
8
8
/SYSCLK
P2
I/O
Cells
P2.0
P3
I/O
Cells
P3.0
P2.7
CNVSTR
8
8
P0
(P0.0-P0.7)
P3.7
Lowest
Priority
8
P1
Port
Latches
(P1.0-P1.7)
8
P2
To External
Memory
Interface
(EMIF)
To
ADC1
Input
(P2.0-P2.7)
8
P3
(P3.0-P3.7)
The digital
crossbar
allows the
designer the
flexibility to
select the
exact mix of
GPIO and
digital
resources for
an application.
2.5
C8051F020
has two onchip Analog to
Digital
converters.
31
AIN0.0
Window
Compare
Interrupt
Window Compare
Logic
AIN0.1
AIN0.2
AIN0.3
AIN0.4
Programmable Gain
Amplifier
AIN0.5
9-to-1
AMUX
(SE or
- DIFF)
AIN0.6
AIN0.7
AV+
+
-
12-Bit
SAR
12
ADC Data
Registers
ADC
Conversion
Complete
Interrupt
TEMP
SENSOR
External VREF
Pin
AGND
VREF
Start
Conversion
DAC0 Output
Write to AD0BUSY
Timer 3 Overflow
CNVSTR
Timer 2 Overflow
Software command,
2)
Overflow of Timer 2,
32
3)
Overflow of Timer 3, or
4)
2.6
2.7
33
Analog Multiplexer
Configuration, Control, and Data Registers
AIN1.0
AIN1.1
Programmable Gain
Amplifier
AIN1.2
AIN1.3
AIN1.4
AIN1.5
8-to-1
AMUX
AV+
+
-
8-Bit
SAR
ADC
AIN1.6
Conversion
Complete
Interrupt
ADC Data
Register
AIN1.7
Write to AD1BUSY
External VREF
Pin
VREF
Start Conversion
AV+
Timer 3 Overflow
CNVSTR Input
Timer 2 Overflow
Write to AD0BUSY
(synchronized with
ADC0)
The DAC output is updated each time when there is a software write
(DACxH), or a Timer 2, 3, or 4 overflow (Figure 2.10). The DACs are
especially useful as references for the comparators or offsets for the
differential inputs of the ADC.
The comparators have software programmable hysteresis and can
generate an interrupt on its rising edge, falling edge, or both. The
comparators' output state can also be polled in software and
programmed to appear on the lower port I/O pins via the Crossbar.
More information on programming applications using the ADCs and
DACs will be presented in Chapter 9.
2.8
Voltage Reference
A voltage reference has to be used when operating the ADC and DAC.
The C8051F020s three voltage reference input pins allow each ADC
and the two DACs to reference an external voltage reference or the onchip voltage reference output. ADC0 may also reference the DAC0
output internally, and ADC1 may reference the analog power supply
voltage (AV+), via the VREF multiplexers shown in Figure 2.11.
34
CP0
CP1
CROSSBAR
CP0+
CP0-
CP1+
CP0
CP1 SFR's
CP1-
CP0
CP1
(Data
and
Cntrl)
CIP-51
and
Interrupt
Handler
REF
DAC0
DAC0
REF
DAC1
DAC1
Timer 2
35
REF
Latch
Dig. MUX
12
DAC0
DAC0
AGND
Timer 2
Timer 4
Timer 3
DAC0L
Latch
AV+
DAC1H
DAC1EN
DAC1MD1
DAC1MD0
DAC1DF2
DAC1DF1
DAC1DF0
REF
Latch
DAC1L
Dig. MUX
Latch
AV+
DAC1H
DAC1CN
Timer 4
DAC0H
DAC0MD1
DAC0MD0
DAC0DF2
DAC0DF1
DAC0DF0
DAC0H
DAC0CN
DAC0EN
Timer 3
12
DAC1
DAC1
AGND
36
AD0VRS
AD1VRS
TEMPE
BIASE
REFBE
REF0CN
ADC1
AV+
Ref
1
VREF1
VDD
External
Voltage
Reference
Circuit
R1
ADC0
VREF0
DGND
Ref
0
1
DAC0
VREFD
Ref
DAC1
BIASE
EN
VREF
4.7F
x2
0.1F
1.2V
Band-Gap
Bias to
ADCs,
DACs
REFBE
Recommended Bypass
Capacitors
Example:
MOV
REF0CN,#00000011B
This enables the use of the ADC or DAC, and the internal voltage
reference. The appropriate jumpers have to be set on the development
board to connect the internal voltage reference to the ADC or DAC
voltage reference inputs.
In summary, the benefits of a highly integrated microcontroller include:
1)
2)
3)
Cost effective
Bit
7-5
Symbol
-
AD0VRS
AD1VRS
TEMPE
BIASE
REFBE
Description
Unused. Read=000b; Write=Dont care.
ADC0 Voltage Reference Select
0: ADC0 voltage reference from VREF0 pin.
1: ADC0 voltage reference from DAC0 output.
ADC1 Voltage Reference Select
0: ADC1 voltage reference from VREF1 pin.
1: ADC1 voltage reference from AV+
Temperature Sensor Enable Bit
0: Internal Temperature Sensor Off.
1: Internal Temperature Sensor On.
ADC/DAC Bias Generator Enable Bit
(Must be 1 if using ADC or DAC)
0: Internal Bias Generator Off.
1: Internal Bias Generator On.
Internal Reference Buffer Enable Bit
0: Internal Reference Buffer Off.
1: Internal Reference Buffer On. Internal voltage
reference is driven on the VREF pin.
37
38
2.9
Tutorial Questions
1.
2.
3.
4.
Which of the 4 ports in the Silicon Labs target board can be used
to generate the GPI/O or input/outputs for some digital peripheral?
5.
3
Instruction Set
3.0
Introduction
40
3.1
Addressing Modes
40
3.2
Instruction Types
43
3.3
Tutorial Questions
69
40
3.0
Introduction
A computer instruction is made up of an operation code (op-code)
followed by either zero, one or two bytes of operands information. The
op-code identifies the type of operation to be performed while the
operands identify the source and destination of the data. The operand
can be the data itself, a CPU register, a memory location or an I/O port.
If the instruction is associated with more than one operand, the format is
always:
Instruction
3.1
Destination, Source
Addressing Modes
Eight modes of addressing are available with C8051F020. The different
addressing modes, shown in Table 3.1, determine how the operand byte
is selected. Each addressing mode is discussed in detail below.
Addressing Modes
Register
Direct
Indirect
Immediate Constant
Relative*
Absolute*
Long*
Indexed
Instruction
MOV A, B
MOV 30H,A
ADD A,@R0
ADD A,#80H
SJMP AHEAD
AJMP BACK
LJMP FAR_AHEAD
MOVC A,@A+PC
Register Addressing
The register addressing instruction involves information transfer between
registers.
Example:
MOV
R0,A
41
Direct Addressing
The instruction allows you to specify the operand by giving its actual
memory address (in Hexadecimal) or by giving its abbreviated name
(e.g. P3).
Example:
MOV
A, P3
MOV
A, 20H
Indirect Addressing
This mode uses a pointer to hold the effective address of the operand.
However only registers R0, R1 and DPTR can be used as the pointer
registers. The R0 and R1 registers can hold an 8-bit address whereas
DPTR can hold a 16-bit address.
Example:
MOV
@R0,A
MOVX
A,@DPTR
42
MOV DPTR,#FE00H
Relative Addressing
This mode of addressing is used with some type of jump instructions like
SJMP (short jump) and conditional jumps like JNZ. This instruction
transfers control from one part of a program to another. The transfer
control must be within -128 and +127 bytes from the instruction address.
Example:
SJMP
LOC1
Absolute Addressing
Two instructions associated with this mode of addressing are ACALL and
AJMP instructions. This is a 2-byte instruction where the absolute
address is specified by a label. The branch address must be within the
current 2K byte page of program memory.
Example:
ACALL ARRAY
43
Long Addressing
This mode of addressing is used with the LCALL and LJMP instructions.
It is a 3-byte instruction and the last 2 bytes specify a 16-bit destination
location where the program branches to. It allows use of the full 64K
code space. The program will always branch to the same location
irrespective of where the program starts.
Example:
LCALL TABLE
Indexed Addressing
The Indexed addressing is useful when there is a need to retrieve data
from a look-up table (LUT). A 16-bit register (data pointer) holds the base
address and the accumulator holds an 8-bit displacement or index value.
The sum of these two registers forms the effective address for a JMP or
MOVC instruction.
Example:
MOV
MOV
MOVC
A,08H
DPTR,#1F00H
A,@A+DPTR
After the execution of the above instructions, the program will branch to
address 1F08H (1F00+08) and transfer into the accumulator a data byte
retrieved from that location (from the look-up table).
3.2
Instruction Types
The C8051F020 instructions are divided into five functional groups:
1)
Arithmetic Operations
2)
Logical Operations
3)
4)
5)
44
Arithmetic Operations
With arithmetic instructions, the C8051F020 CPU has no special
knowledge of the data format, e.g. signed binary, unsigned binary, binary
coded decimal, ASCII, etc. Therefore, the appropriate status bits in the
PSW are set when specific conditions are met to manage the different
data formats. Table 3.2 lists the arithmetic instructions associated with
the C8051F020 MCU.
Mnemonic
ADD A, Rn
ADD A, direct
ADD A,@Ri
ADD A,#data
ADDC A,Rn
ADDC A, direct
ADDC A,@Ri
ADDC A,#data
SUBB A,Rn
SUBB A, direct
SUBB A,@Ri
SUBB A,#data
INC A
INC Rn
INC direct
INC @Ri
DEC A
DEC Rn
DEC direct
DEC @Ri
MUL AB
DIV AB
DA A
Description
A = A + [Rn]
A = A + [direct memory]
A = A + [memory pointed to by Ri]
A = A + immediate data
A = A + [Rn] + CY
A = A + [direct memory] + CY
A = A + [memory pointed to by Ri] + CY
A = A + immediate data + CY
A = A - [Rn] - CY
A = A - [direct memory] - CY
A = A - [@Ri] - CY
A = A - immediate data - CY
A=A+1
[Rn] = [Rn] + 1
[direct] = [direct] + 1
[@Ri] = [@Ri] + 1
A=A-1
[Rn] = [Rn] - 1
[direct] = [direct] - 1
[@Ri] = [@Ri] - 1
Multiply A & B
Divide A by B
Decimal adjust A
Note:
ADD
45
ADD adds the data byte specified by the source operand to the
accumulator, leaving the result in the accumulator.
ADDC adds the data byte specified by the source operand, the carry flag
and the accumulator contents, leaving the result in the accumulator.
Operation of both the instructions, ADD and ADDC, can affect the carry
flag (CY), auxiliary carry flag (AC) and the overflow flag (OV).
CY=1
SUBB A,<source-byte>
SUBB subtracts the specified data byte and the carry flag together from
the accumulator, leaving the result in the accumulator.
CY=1
A, R1
46
borrow) flag being set before the start of operation. So if the state of the
carry bit is unknown before the execution of the SUBB instruction, it must
be explicitly cleared by using CLR C instruction.
INC
<byte>
INC
6FH
If the internal RAM location 6FH contains 30H, then the instruction
increments this value, leaving 31H in location 6FH.
Example:
MOV
INC
INC
R1, #5E
R1
@R1
If R1=5E (01011110) and internal RAM location 5FH contains 20H, the
instructions will result in R1=5FH and internal RAM location 5FH to
increment by one to 21H.
DEC
<byte>
DPTR
Increments the 16-bit data pointer by 1. DPTR is the only 16-bit register
that can be incremented.
Example:
INC DPTR
MUL
47
AB
MUL
AB
If ACC=85 (55H) and B=23 (17H), the instruction gives the product 1955
(07A3H), so B is now 07H and the accumulator is A3H. The overflow flag
is set and the carry flag is cleared.
DIV
AB
DIV AB
DA
48
If CY=1, because the high order bits A7-4 is now exceeding 9 (1010xxxx1111xxxx), then these high order bits will be increased by 6 to produce a
correct proper BCD in the high order nibble but not clear the carry.
Example:
MOV
MOV
ADDC
DA
R0,#38H
A,#80H
A
A
Before carrying out the above instruction, the accumulator value was
given as ACC=80H (10000000), which also represents BCD=80, R0=
38H (00111000) representing BCD=38, and the carry flag is cleared.
After the operation of ADDC, the result in the accumulator is ACC=B8H,
which is not a BCD value. In order to do a decimal adjustment to the
value, the DA instruction needs to be incorporated. Once DA operation is
carried out, the accumulator will result in ACC=18H, indicating
BCD=18.The carry flag is set, indicating that a decimal overflow occurred
(38+80=118).
Logical Operations
Logical instructions perform Boolean operations (AND, OR, XOR, and
NOT) on data bytes on a bit-by-bit basis. Table 3.3 lists the logical
instructions associated with the C8051F020.
ANL
<dest-byte>,<source-byte>
This instruction performs the logical AND operation on the source and
destination operands and stores the result in the destination variable. No
flags are affected.
Example:
ANL
A,R2
ANL
P1,#10111001B
49
Mnemonic
ANL A, Rn
ANL A, direct
ANL A,@Ri
ANL A,#data
ANL direct,A
ANL direct,#data
ORL A, Rn
ORL A, direct
ORL A,@Ri
ORL A,#data
ORL direct,A
ORL direct,#data
XRL A, Rn
XRL A, direct
XRL A,@Ri
XRL A,#data
XRL direct,A
XRL direct,#data
CLR A
CPL A
RL A
RLC A
RR A
RRC A
SWAP A
Description
A = A & [Rn]
A = A & [direct memory]
A = A & [memory pointed to by Ri]
A= A & immediate data
[direct] = [direct] & A
[direct] = [direct] & immediate data
A = A OR [Rn]
A = A OR [direct]
A = A OR [@RI]
A = A OR immediate data
[direct] = [direct] OR A
[direct] = [direct] OR immediate data
A = A XOR [Rn]
A = A XOR [direct memory]
A = A XOR [@Ri]
A = A XOR immediate data
[direct] = [direct] XOR A
[direct] = [direct] XOR immediate data
Clear A
Complement A
Rotate A left
Rotate A left (through C)
Rotate A right
Rotate A right (through C)
Swap nibbles
Table 3.3 List of Logical Instructions
50
ORL
<dest-byte>,<source-byte>
ORL
A,R2
ORL
P1,#11000010B
<dest-byte>,<source-byte>
XRL
A,R0
XRL
P1,#00110001
This instruction clears the accumulator (all bits set to 0). No flags are
affected.
Example:
CLR
CPL
51
CPL
The eight bits in the accumulator are rotated one bit to the left. Bit 7 is
rotated into the bit 0 position. No flags are affected.
Example:
RL
The instruction rotates the accumulator contents one bit to the left
through the carry flag. This means that the Bit 7 of the accumulator will
move into carry flag and the original value of the carry flag will move into
the Bit 0 position. No other flags are affected.
Example:
RLC
The eight bits in the accumulator are rotated one bit to the right. Bit 0 is
rotated into the bit 7 position. No flags are affected.
Example:
RR
52
The instruction rotates the accumulator contents one bit to the right
through the carry flag. This means that the original value of carry flag will
move into Bit 7 of the accumulator and Bit 0 rotated into carry flag. No
other flags are affected.
Example:
RRC
SWAP
53
Mnemonic
Description
[@Ri] = [direct]
[@Ri] = immediate data
[DPTR] = immediate data
A = Code byte from [@A+DPTR]
A = Code byte from [@A+PC]
A = Data byte from external ram [@Ri]
A = Data byte from external ram [@DPTR]
External[@Ri] = A
External[@DPTR] = A
Push into stack
Pop from stack
A = [Rn], [Rn] = A
A = [direct], [direct] = A
A = [@Rn], [@Rn] = A
Exchange low order digits
MOV
<dest-byte>,<source-byte>
This instruction moves the source byte into the destination location. The
source byte is not affected, neither are any other registers or flags.
Example:
MOV
MOV
MOV
ADD
MOV
R1,#60
A,@R1
R2,#61
A,@R2
R7,A
;R1=60H
;A=[60H]
;R2=61H
;A=A+[61H]
;R7=A
MOV
DPTR, #data 16
This instruction loads the data pointer with the 16-bit constant and no
flags are affected.
54
Example:
MOV
DPTR,#1032
This instruction loads the value 1032H into the data pointer, i.e.
DPH=10H and DPL=32H.
MOVC A,@A + <base-reg>
This instruction moves a code byte from program memory into ACC. The
effective address of the byte fetched is formed by adding the original 8bit accumulator contents and the contents of the base register, which is
either the data pointer (DPTR) or Program Counter (PC). 16-bit addition
is performed and no flags are affected.
The instruction is useful in reading the look-up tables in the program
memory. If the PC is used, it is incremented to the address of the
following instruction before being added to the ACC.
Example:
LOC1:
Look_up
CLR
INC
MOVC
RET
DB
DB
DB
DB
A
A
A,@A + PC
10H
20H
30H
40H
Example:
55
MOV
MOV
R0,#34H
MOVX
A,@R0
The 16-bit form of the MOVX instruction accesses the memory location
pointed to by the contents of the DPTR register.
Example:
MOV
MOVX
A,@DPTR
The above example uses the 16-bit immediate MOV DPTR instruction to
set the contents of DPTR. Alternately, the DPTR can be accessed
through the SFR registers DPH, which contains the upper 8 bits of
DPTR, and DPL, which contains the lower 8 bits of DPTR.
PUSH Direct
This instruction increments the stack pointer (SP) by 1. The contents of
Direct, which is an internal memory location or a SFR, are copied into the
internal RAM location addressed by the stack pointer. No flags are
affected.
Example:
PUSH
PUSH
22H
23H
Direct
56
POP
POP
DPH
DPL
If SP=51H originally and internal RAM locations 4FH, 50H and 51H
contain the values 30H, 11H and 12H respectively, the instructions
above leave SP=4FH and DPTR=1211H.
POP
SP
A,<byte>
This instruction swaps the contents of ACC with the contents of the
indicated data byte.
Example:
XCH
A,@R0
XCHD
A,@R0
57
Mnemonic
CLR
CLR
SETB
SETB
CPL
CPL
ANL
ANL
ORL
ORL
MOV
MOV
JC
JNC
JB
JNB
JBC
C
bit
C
bit
C
bit
C,bit
C,/bit
C,bit
C,/bit
C,bit
bit,C
rel
rel
bit,rel
bit,rel
bit,rel
Description
Clear C
Clear direct bit
Set C
Set direct bit
Complement c
Complement direct bit
AND bit with C
AND NOT bit with C
OR bit with C
OR NOT bit with C
MOV bit to C
MOV C to bit
Jump if C set
Jump if C not set
Jump if specified bit set
Jump if specified bit not set
if specified bit set then clear it and jump
CLR
<bit>
CLR
P2.7
58
If Port 2 has been previously written with DCH (11011100), then the
operation leaves the port set to 5CH (01011100).
SETB
<bit>
This operation sets the specified bit to 1. SETB instruction can operate
on the carry flag or any directly-addressable bit. No other flags are
affected.
Example:
SETB
SETB
C
P2.0
If the carry flag is cleared and the output Port 2 has the value of 24H
(00100100), then the result of the instructions sets the carry flag to 1 and
changes the Port 2 value to 25H (00100101).
CPL
<bit>
CPL
CPL
P2.1
P2.2
If Port 2 has the value of 53H (01010011) before the start of the
instructions, then after the execution of the instructions it leaves the port
set to 55H (01010101).
ANL
C,<source-bit>
This instruction ANDs the bit addressed with the Carry bit and stores the
result in the Carry bit itself. If the source bit is a logical 0, then the
instruction clears the carry flag; else the carry flag is left in its original
value. If a slash (/) is used in the source operand bit, it means that the
logical complement of the addressed source bit is used, but the source
bit itself is not affected. No other flags are affected.
Example:
59
MOV
C,P2.0
ANL
C,P2.7
MOV
P2.1,C
ANL
C,/OV
If P2.0=1, P2.7=0 and OV=0 initially, then after the above instructions,
P2.1=0, CY=0 and the OV remains unchanged, i.e. OV=0.
ORL
C,<source-bit>
This instruction ORs the bit addressed with the Carry bit and stores the
result in the Carry bit itself. It sets the carry flag if the source bit is a
logical 1; else the carry is left in its original value. If a slash (/) is used in
the source operand bit, it means that the logical complement of the
addressed source bit is used, but the source bit itself is not affected.
No other flags are affected.
Example:
MOV
MOV
C,P2.0
ORL
C,P2.7
MOV
P2.1,C
ORL
C,/OV
<dest-bit>,<source-bit>
The instruction loads the value of source operand bit into the destination
operand bit. One of the operands must be the carry flag; the other may
be any directly-addressable bit. No other register or flag is affected.
Example:
MOV
MOV
MOV
P2.3,C
C,P3.3
P2.0,C
60
If P2=C5H (11000101), P3.3=0 and CY=1 initially, then after the above
instructions, P2=CCH (11001100) and CY=0.
JC
rel
CLR
SUBB
JC
MOV
C
A,R0
ARRAY1
A,#20H
The carry flag is cleared initially. After the SUBB instruction, if the value
of A is smaller than R0, then the instruction sets the carry flag and
causes program execution to branch to ARRAY1 address, otherwise it
continues to the MOV instruction.
JNC
rel
CLR
SUBB
JNC
MOV
C
A,R0
ARRAY2
A,#20H
The above sequence of instructions will cause the jump to be taken if the
value of A is greater than or equal to R0. Otherwise the program will
continue to the MOV instruction.
JB
61
<bit>,rel
JB
JB
ACC.7,ARRAY1
P1.2,ARRAY2
<bit>,rel
JNB
JNB
ACC.6,ARRAY1
P1.3,ARRAY2
<bit>,rel
If the source bit is 1, this instruction clears it and branches to the address
indicated; else it proceeds with the next instruction. The bit is not
cleared if it is already a 0. No flags are affected.
Example:
JBC
JBC
P1.3,ARRAY1
P1.2,ARRAY2
62
Mnemonic
ACALL addr11
LCALL addr16
RET
RETI
AJMP addr11
LJMP addr16
SJMP rel
JMP @A+DPTR
JZ
rel
JNZ
rel
CJNE A,direct,rel
CJNE A,#data,rel
CJNE Rn,#data,rel
CJNE @Ri,#data,rel
DJNZ Rn,rel
DJNZ direct,rel
NOP
Description
Absolute subroutine call
Long subroutine call
Return from subroutine
Return from interrupt
Absolute jump
Long jump
Short jump
Jump indirect
Jump if A=0
Jump if A NOT=0
Compare and Jump if Not Equal
63
ACALL addr11
This instruction unconditionally calls a subroutine indicated by the
address. The operation will cause the PC to increase by 2, then it pushes
the 16-bit PC value onto the stack (low order byte first) and increments
the stack pointer twice. The PC is now loaded with the value addr11 and
the program execution continues from this new location. The subroutine
called must therefore start within the same 2K block of the program
memory. No flags are affected.
Example:
ACALL LOC_SUB
LCALL addr16
This instruction calls a subroutine located at the indicated address. The
operation will cause the PC to increase by 3, then it pushes the 16-bit PC
value onto the stack (low order byte first) and increments the stack
pointer twice. The PC is then loaded with the value addr16 and the
program execution continues from this new location. Since it is a Long
call, the subroutine may therefore begin anywhere in the full 64KB
program memory address space. No flags are affected.
Example:
LCALL LOC_SUB
64
RET
This instruction returns the program from a subroutine. RET pops the
high byte and low byte address of PC from the stack and decrements the
SP by 2. The execution of the instruction will result in the program to
resume from the location just after the call instruction. No flags are
affected.
Example:
RET
Suppose SP=0BH originally and internal RAM locations 0AH and 0BH
contain the values 30H and 02H respectively. The instruction leaves
SP=09H and program execution will continue at location 0230H.
RETI
This instruction returns the program from an interrupt subroutine. RETI
pops the high byte and low byte address of PC from the stack and
restores the interrupt logic to accept additional interrupts. SP decrements
by 2 and no other registers are affected. However the PSW is not
automatically restored to its pre-interrupt status. After the RETI, program
execution will resume immediately after the point at which the interrupt is
detected.
Example:
RETI
AJMP addr11
The AJMP instruction transfers program execution to the destination
address which is located at the absolute short range distance (short
range means 11-bit address). The destination must therefore be within
the same 2K block of program memory.
Example:
AJMP
NEAR
65
LJMP addr16
The LJMP instruction transfers program execution to the destination
address which is located at the absolute long range distance (long range
means 16-bit address).The destination may therefore be anywhere in the
full 64K program memory address space. No flags are affected.
Example:
LJMP
FAR_ADR
SJMP
RELSRT
If the label RELSRT is at program memory location 0120H and the SJMP
instruction is located at address 0100H, after executing the instruction,
PC=0120H.
JMP
@A + DPTR
This instruction adds the 8-bit unsigned value of the ACC to the 16-bit
data pointer and the resulting sum is returned to the PC. Neither ACC
nor DPTR is altered. No flags are affected.
Example:
LOOK_TBL:
MOV
JMP
AJMP
AJMP
AJMP
DPTR, #LOOK_TBL
@A + DPTR
LOC0
LOC1
LOC2
66
JZ
rel
SUBB A,#20H
JZ
LABEL1
DEC
A
If ACC originally holds 20H and CY=0, then the SUBB instruction
changes ACC to 00H and causes the program execution to continue at
the instruction identified by LABEL1; otherwise the program continues to
the DEC instruction.
JNZ
rel
DEC
JNZ
MOV
A
LABEL2
RO, A
If ACC originally holds 00H, then the instructions change ACC to FFH
and cause the program execution to continue at the instruction identified
by LABEL2; otherwise the program continues to MOV instruction.
CJNE <dest-byte>,<source-byte>,rel
This instruction compares the magnitude of the dest-byte and the
source-byte and branches if their values are not equal. The carry flag is
set if the unsigned dest-byte is less than the unsigned integer sourcebyte; otherwise, the carry flag is cleared. Neither operand is affected.
Example:
67
CJNE
R3,#50H,NEQU
NEQU: JC
LOC1
LOC1:
;R3
;If
;R7
;R3
= 50H
R3 < 50H
> 50H
< 50H
DJNZ <byte>,<rel-addr>
This instruction is decrement jump not zero. It decrements the contents
of the destination location and if the resulting value is not 0, branches to
the address indicated by the source operand. An original value of 00H
underflows to FFH. No flags are affected.
Example:
DJNZ
DJNZ
DJNZ
20H,LOC1
30H,LOC2
40H,LOC3
If internal RAM locations 20H, 30H and 40H contain the values 01H, 5FH
and 16H respectively, the above instruction sequence will cause a jump
to the instruction at LOC2, with the values 00H, 5EH, and 15H in the 3
RAM locations. Note, the first instruction will not branch to LOC1
because the [20H] = 00H, hence the program continues to the second
instruction. Only after the execution of the second instruction (where the
location [30H] = 5FH), then the branching takes place.
NOP
This is the no operation instruction. The instruction takes one machine
cycle operation time. Hence it is useful to time the ON/OFF bit of an
output port.
Example:
CLR
NOP
NOP
NOP
NOP
SETB
P1.2
P1.2
68
3.3
69
Tutorial Questions
1.
2.
3.
4.
and
A, @R5
ADD
A, R5
A,13H
R2,18H
A, R2
A
5.
6.
7.
Assuming that the CY=1 and ACC= F0H find the content of the
accumulator after the execution of the following instruction:
RLC
8.
4
ASM Directives
4.0
Introduction
72
4.1
Address Control
72
4.2
Symbol Definition
74
4.3
Memory Initialization/Reservation
75
4.4
Segment Control
78
4.5
80
4.6
Tutorial Questions
81
72
4.0
Introduction
Assembler directives are special codes placed in the assembly language
program to instruct the assembler to perform a particular task or function.
They can be used to define symbol values, reserve and initialize storage
space for variables and control the placement of the program code. They
are not assembly language instructions as they do not generate any
machine code.
The ASM directives are grouped into the following categories:
4.1
1)
2)
3)
4)
Address Control
ORG
The specified format for the ORG directive is:
ORG
expression
The ORG directive is used to set the location counter in the current
segment to an offset address specified by the expression. However, it
does not alter the segment address. The segment address can only be
changed by using the standard segment directives.
Example:
ORG
80H
The ORG directive need not only be used in the code segment but can
be used in other segments too like the data segment. For example, to
73
SECONDS:
MINUTES:
DSEG
ORG 30H
DS
1
DS
1
;data segment
USING
The specified format for the USING directive is:
USING expression
expression may have a value from 0 to 3. This USING directive merely
informs the assembler which register bank to use for coding the AR0
through AR7 (the value of AR0 through AR7 is calculated as the absolute
address of R0 through R7 in the register bank specified by the USING
directive). It does not generate any code to select the register bank. So
to ensure a correct register bank, one must program the PSW register
appropriately.
Example:
MOV
PSW,#00010000B
MOV
USING 2
PUSH AR7
PSW,#00001000B
USING 1
PUSH AR3
;select Register
;Bank 2
;using Bank 2
;select Register
;Bank 1
;using Bank 1
END
The specified format for the END directive is:
END
74
The END directive indicates the end of the source file. It informs the
assembler where to stop assembling the program. Hence any text that
appears after the END directive will be ignored by the assembler. The
END directive is a must in every source file. If it is not written at the end
of the program, the assembler will give an error message.
4.2
Symbol Definition
The symbol definition directive assigns a symbolic name to an
expression or a register. This expression can be a constant number, an
address reference or another symbolic name.
EQU, SET
The format of the EQU and SET directives are as follows:
Symbol
Symbol
Symbol
Symbol
Note:
EQU
EQU
SET
SET
expression
register
expression
register
Examples:
COUNT
TOTAL
AVERG
TABLE
VALUE
EQU
EQU
SET
EQU
SET
R3
200
TOTAL/5
10
TABLE*TABLE
75
BIT
CODE
bit_address
code_address
Symbol
Symbol
Symbol
DATA
IDATA
XDATA
data_address
idata_address
xdata_address
Note:
bit_address
4.3
Act_bit
BIT
2EH
Port2
DATA
A0H
;a special function
;register, P2
Memory Initialization/Reservation
The directives for memory initialization and reservation are DB, DW, DD
and DS. These directives will initialize or reserve memory storage in the
form of a byte, a word, or a double word in the code space.
76
DB (Define Byte)
The DB directive initializes code memory with a byte value. The directive
has the following format:
label:
DB
expression, expression
Note:
label
expression
Example:
MSG:
ARRAY:
CSEG AT
200H
DB Please enter your password, 0
DB 10H, 20H,30H,40H,50H
The above string of characters will be stored as ASCII bytes starting from
location 200H, which means location [200H]=50H, [201H]=6CH and so
on.
Notice that the DB directive is declared in a code segment. If it is defined
in a different segment, the assembler will generate an error.
DW (Define Word)
The DW directive initializes the code memory with a double byte or a 16bit word. The DW directive has the following format:
label:
DW
expression ,expression
77
Example:
;2 words allocated
CNTVAL
DW
1025H, 2340H
;10 values of 1234H starting from location XLOC
XLOC
DW
10 DUP (1234H)
The DUP operator can be used to duplicate a sequence of memory
contents.
Similarly, the DW directive can only be used in the code segment. If it is
defined in other segments, the assembler will give an error message.
label:
expression ,expression
Example:
ADDR
EMPT
DD
DD
820056EFH, 10203040H
3 DUP ( 0 )
DS (Define Storage)
The DS directive reserves a specified byte space in the memory. It can
only be used in the currently active segment like ISEG, DSEG or XSEG.
The DS directive has the following format:
label:
DS
expression
78
Example:
XSEG
AT 1000H
Input:
Wavetyp:
DS
DS
4.4
Segment Control
In x51 CPU structure, a block of code or data memory is usually referred
to as a segment. There are two types of segments: generic and absolute.
Generic Segment
Generic segments are created using the SEGMENT directive. The
format is as follows:
Symbol
SEGMENT
segment_type
SEGMENT
DATA
Example:
MYDATA
MYDATA
79
Absolute Segment
Absolute segment means a fixed memory segment. Absolute segments
are created by CSEG, DSEG and XSEG directives. The format of this
directive is as follows:
CSEG AT address
DSEG AT address
XSEG AT address
Example:
CSEG
AT
DSEG
AT
set
0300H
set
0400H
80
4.5
MYDATA
30h
Input:
DS
16
temp:
DS
1
;---------------------------------------------------------; CODE SEGMENT
;---------------------------------------------------------MYCODE
SEGMENT CODE
RSEG
MYCODE ; Switch to this code segment
USING
0
; Specify register bank
; for main code.
Main:
4.6
81
Tutorial Questions
1.
2.
3.
(b)
(c)
DSEG
VAL:
(b)
AT
DB
0300H
1
4.
5.
6.
10H
61H, 62H, 63H
0,1,2
5
System Clock, Crossbar and
GPIO
5.0
Introduction
85
5.1
86
5.2
Watchdog Timer
88
5.3
Digital Crossbar
90
5.4
GPIO
93
5.5
96
84
5.6
Ports 4 through 7
103
5.7
Tutorial Questions
106
Introduction
The C8051F020 micro-controller may be operated from an external
oscillator or an internal oscillator, both are included on the target board.
After any reset, the MCU operates from the internal oscillator at a typical
frequency of 2.0MHz by default but may be configured by software to
operate at other typical frequencies of 4.0Mhz, 8.0MHz or 16MHz.
Therefore, in many applications an external oscillator is not required.
However, an external 22.1184MHz crystal is installed on the target board
as shipped from the factory. It is especially useful in providing a system
clock frequency suitable for high baud rate generation for UART. Both
the oscillators are disabled when the /RST pin is held low. The oscillator
and its associated programming registers are shown in Figure 5.1.
IFRDY
CLKSL
IOSCEN
IFCN1
IFCN0
MSCLKE
OSCICN
VDD
EN
Internal Clock
Generator
opt. 2
SYSCLK
AV+
opt. 4
XTAL1
opt. 3
XTAL1
XTAL2
AV+
opt. 1
XTAL1
XTAL1
XTAL2
Input
Circuit
OSC
XFCN2
XFCN1
XFCN0
AGND
XTLVLD
XOSCMD2
XOSCMD1
XOSCMD0
5.0
85
OSCXCN
86
5.1
MSCLKE
6-5
Unused
IFRDY
CLKSL
IOSCEN
1-0
IFCN1IFCN 0
Description
Missing Clock Enable Bit
0: Missing Clock Detector Disabled
1: Missing Clock Detector Enabled. The reset is
triggered if clock is missing for more than 100s
Read = 00b, Write = dont care
Internal Oscillator Frequency Ready Flag
0: Internal Oscillator Frequency not running at
speed specified by the IFCN bits.
1: Internal Oscillator Frequency is running at
speed specified by the IFCN bits.
System Clock Source Select Bit
0: Uses Internal Oscillator as System Clock
1: Uses External Oscillator as System Clock
Internal Oscillator Enable Bit
0: Internal Oscillator Disabled
1: Internal Oscillator Enabled
Internal Oscillator Frequency Control Bit
00: Internal Oscillator typical frequency is 2 MHz
01: Internal Oscillator typical frequency is 4 MHz
10: Internal Oscillator typical frequency is 8 MHz
11: Internal Oscillator typical frequency is 16 MHz
Bits 0 and 1 are used to select the internal oscillator frequency. Bit 2
enables or disables the internal oscillator while bit 3 selects between the
internal and external oscillator. Once the internal oscillator is enabled, it
takes a while for it to settle down and generate the desired frequency set
by IFCN1-IFCN0 (Bits 0 and 1). Bit 4 shows the status of the internal
oscillator and is set to 1 when the oscillator is running at the specified
speed. Bit 7 is to be set to 1 if missing clock detector is to be enabled. A
reset is triggered if the clock is missing for a period greater than 100s.
87
The OSCICN SFR is at address 0xB2. Upon reset, the value in this
register is set to 0001 0100. This enables the internal oscillator to
operate at a frequency of 2 MHz.
Symbol
XTLVLD
6-4
XOSCMD2-0
3
2-0
Reserved
XFCN2-0
Description
Crystal Oscillator Valid Flag
0: Crystal Oscillator is unused or not yet stable
1: Crystal Oscillator is running and stable
External Oscillator Mode Bits
00x: Off. XTAL1 pin is grounded internally.
010: System Clock from External CMOS Clock
on XTAL1 pin.
011: System Clock from External CMOS Clock
on XTAL1 pin divided by 2.
10x: RC/C Oscillator Mode with divide by 2
stage.
110: Crystal Oscillator Mode
111: Crystal Oscillator Mode with divide by 2
stage
Read = undefined, Write = dont care
External Oscillator Frequency Control Bit
If the crystal frequency is greater than 6.7 MHz, which is indeed the case
for the on-board crystal of the target board, bits 2-0 (XFCN2-0) must be
set to 111. Bits 6-4 (XOSCMD2-0) are programmed based on the
external oscillator whether it is RC/C, crystal or CMOS clock on XTAL1.
After the external oscillator has been enabled, by setting CLKSL
(OSCICN.3) to 1, one must wait for the crystal oscillator to be stable.
This can be checked by polling bit 7 (XTLVLD) of OSCXCN.
The OSCXCN SFR is at address 0xB1. Upon reset, the value in this
register is set to 0000 0000. This turns off the crystal oscillator and the
XTAL1 pin is grounded internally.
88
Example:
MOV
OSCXCN, #67H
;enable external
;crystal oscillator at
;22.1184MHz
;wait at least 1ms
;wait ~512us
;wait ~512us
;poll for XTLVLD1
CLR
A
DJNZ ACC, $
DJNZ ACC, $
XTLVLD_wait:
MOV
A, OSCXCN
JNB
ACC.7, XTLVLD_wait
ORL
OSCICN, #08H
;select external
;oscillator as system
;clock source (CLKSL=1)
;disable Internal
;Oscillator (IOSCEN=0)
ORL
OSCICN, #80H
;enable missing clock
;detector (MSCLKE=1)
5.2
Watchdog Timer
The MCU has a programmable Watchdog Timer (WDT) which runs off
the system clock. An overflow of the WDT forces the MCU into the reset
state. Before the WDT overflows, the application program must restart it.
WDT is useful in preventing the system from running out of control,
especially in critical applications. If the system experiences a software or
hardware malfunction which prevents the software from restarting the
WDT, the WDT will overflow and cause a reset. After a reset, the WDT is
automatically enabled and starts running at the default maximum time
interval which is 524 ms for a 2 MHz system clock.
The WDT consists of a 21-bit timer running from the programmed system
clock. A WDT reset is generated when the period between specific writes
to its control register exceeds the programmed limit.
The WDT may be enabled or disabled by software. It may also be locked
to prevent accidental disabling. Once locked, the WDT cannot be
disabled until the next system reset. It may also be permanently
disabled. The watchdog features are controlled by programming the
Watchdog Timer Control Register (WDTCN). The details of WDTCN are
shown in Table 5.3.
89
2-0
Description
WDT Control
Writing 0xA5 both enables and reloads the WDT
Writing 0xDE followed within 4 system clocks by
0xAD disables the WDT
Writing 0xFF locks out the disable feature
Watchdog Status Bit (when Read)
Reading this bit indicates the Watchdog Timer
Status
0: WDT is inactive
1: WDT is active
Watchdog Timeout Interval Bits
These bits set the Watchdog Timer Interval.
When writing these bits, WDTCN.7 must be set
to 0.
Table 5.3 WDTCN (Watchdog Timer Control) Register
Enable/Reset WDT
To enable and reset the watchdog timer, write 0xA5 to the WDTCN
register. To prevent a watchdog timer overflow, the application must
periodically write 0xA5 to WDTCN.
Disable WDT
To disable the WDT, the application must write 0xDE followed by, within
4 clock cycles, 0xAD to the WDTCN register. If 0xAD is not written with 4
cycles of writing 0xDE, the disable operation is not effective. Interrupts
must be disabled during this procedure to avoid delay between the two
writes.
90
Example:
CLR
MOV
MOV
SETB
EA
WDTCN, #0DEH
WDTCN, #0ADH
EA
5.3
Digital Crossbar
The C8051F020 has a rich set of digital resources like UARTs, System
Management Bus (SMBus),Timer control inputs and interrupts. However,
these peripherals do not have dedicated pins through which they may be
accessed. Instead they are available through the four lower I/O ports
(P0, P1, P2 and P3). Each of the pins on P0, P1, P2 and P3 can be
defined as a General Purpose Input/Output (GPIO) pin or can be
controlled by a digital peripheral. Thus the lower ports have dual
functionalities. Based on the application, a system designer would have
to decide what capabilities are required and then allocate the necessary
digital functions to the port pins. This flexibility makes the MCU very
versatile. The resource allocation is controlled by programming the
Priority Crossbar Decoder, simply called the Crossbar. The port pins
are allocated and assigned to the digital peripherals using a priority
order. Figure 5.2 is the functional block diagram showing the priority
decoder, lower ports and the digital resources that may be controlled.
UART0
SPI
UART1
(Internal Digital Signals)
XBR0, XBR1,
XBR2, P1MDIN
Registers
P0MDOUT, P1MDOUT,
P2MDOUT, P3MDOUT
Registers
External
Pins
SMBus
Lowest
Priority
91
Priority
Decoder
8
PCA
P0
I/O
Cells
P0.0
P1
I/O
Cells
P1.0
Comptr.
Outputs
Digital
Crossbar
T0, T1,
T2, T2EX,
T4,T4EX
/INT0,
/INT1
P1.7
P2
I/O
Cells
P2.0
P3
I/O
Cells
P3.0
/SYSCLK
CNVSTR
8
8
P0
Highest
Priority
P0.7
(P0.0-P0.7)
P2.7
P3.7
Lowest
Priority
8
P1
Port
Latches
(P1.0-P1.7)
8
P2
To External
Memory
Interface
(EMIF)
To
ADC1
Input
(P2.0-P2.7)
8
P3
(P3.0-P3.7)
92
P1
P2
P3
Crossbar Register Bits
5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
UART0EN: XBR0.2
SPI0EN: XBR0.1
SMB0EN: XBR0.0
UART1EN: XBR2.2
PCA0ME: XBR0.[5:3]
ECI0E: XBR0.6
CP0E: XBR0.7
CP1E: XBR1.0
T0E: XBR1.1
INT0E: XBR1.2
T1E: XBR1.3
INT1E: XBR1.4
T2E: XBR1.5
T2EXE: XBR1.6
T4E: XBR2.3
T4EXE: XBR2.4
SYSCKE: XBR1.7
CNVSTE: XBR2.0
ALE
/RD
/WR
AIN1.0/A8
AIN1.1/A9
AIN1.2/A10
AIN1.3/A11
AIN1.4/A12
AIN1.5/A13
AIN1.6/A14
AIN1.7/A15
A8m/A0
A9m/A1
A10m/A2
A11m/A3
A12m/A4
A13m/A5
A14m/A6
A15m/A7
AD0/D0
AD1/D1
AD2/D2
AD3/D3
AD4/D4
AD5/D5
AD6/D6
AD7/D7
Pin I/O
Tx0
Rx0
SCK
MISO
MOSI
NSS
SDA
SCL
TX1
RX1
CEX0
CEX1
CEX2
CEX3
CEX4
ECI
CP0
CP1
T0
/INT0
T1
/INT1
T2
T2EX
T4
T4EX
/SYSCLK
CNVSTR
AIN1 Inputs /
Muxed Addr H /
Mon-Muxed Addr H Non-Muxed Addr L
Muxed Data /
Non-Muxed Data
The output states of port pins that are allocated by the crossbar are
controlled by the digital peripheral that is mapped to those pins and
hence Writes to the Port Data registers (or associated Port bits) will have
no effect on the states of the pins. The Port pins on Port 0 to 3 that are
not allocated by the Crossbar may be accessed as General-Purpose I/O
pins by reading and writing the associated Port Data registers.
93
A Read of a Port Data Register (or Port bit) will always return the logic
state present at the pin itself, regardless of whether the Crossbar has
allocated the pin for peripheral use or not.
Although the crossbar can be configured dynamically at runtime, the
crossbar registers are typically configured in the initialization code of the
application software and thereafter left alone. The peripherals are then
configured individually.
5.4
GPIO
The block diagram of the Port I/O cell is shown in Figure 5.4.
/WEAK-PULLUP
VDD
PUSH-PULL
/PORT-OUTENABLE
(WEAK)
PORT
PAD
PORT-OUTPUT
ANALOG INPUT
VDD
Analog Select
(Port 1 Only)
DGND
PORT-INPUT
94
95
Disables the digital input path from the pin. This prevents
additional power supply current from being drawn when the
voltage at the pin is near VDD / 2. A read of the Port Data bit will
return a logic 0 regardless of the voltage at the Port pin.
2.
3.
Causes the Crossbar to skip over the pin when allocating Port
pins for digital peripherals.
It is important to note that the output drivers on a pin, which has been
configured as an Analog Input, are not explicitly disabled. Therefore, the
associated P1MDOUT bits of pins configured as Analog Inputs should
explicitly be set to logic 0 (Open-Drain output mode) and the associated
Port Data bits should be set to logic 1 (high-impedance).
96
5.5
Bit
Symbol
CP0E
ECI0E
5-3
PCA0ME
UART0EN
SPI0EN
SMB0EN
Description
Comparator 0 Output Enable Bit.
0: CP0 unavailable at Port pin.
1: CP0 routed to Port pin.
PCA0 External Counter Input Enable Bit.
0: PCA0 External Counter Input unavailable at Port
pin.
1: PCA0 External Counter Input (ECI0) routed to
Port pin.
PCA0 Module I/O Enable Bits.
000: All PCA0 I/O unavailable at Port pins.
001: CEX0 routed to Port pin.
010: CEX0, CEX1 routed to 2 Port pins.
011: CEX0, CEX1, and CEX2 routed to 3 Port pins.
100: CEX0, CEX1, CEX2, and CEX3 routed to 4
Port pins.
101: CEX0, CEX1, CEX2, CEX3, and CEX4 routed
to 5 Port pins.
110: RESERVED
111: RESERVED
UART0 I/O Enable Bit.
0: UART0 I/O unavailable at Port pins.
1: UART0 TX routed to P0.0, and RX routed to
P0.1
SPI0 Bus I/O Enable Bit.
0: SPI0 I/O unavailable at Port pins.
1: SPI0 SCK, MISO, MOSI, and NSS routed to 4
Port pins.
SMBus0 Bus I/O Enable Bit.
0: SMBus0 I/O unavailable at Port pins.
1: SMBus0 SDA and SCL routed to 2 Port pins.
Table 5.4 XBR0 (Crossbar Register 0)
97
Bit Symbol
7
SYSCKE
T2EXE
T2E
INT1E
T1E
INT0E
T0E
CP1E
Description
/SYSCLK Output Enable Bit.
0: /SYSCLK unavailable at Port pin.
1: /SYSCLK routed to Port pin.
T2EX Input Enable Bit.
0: T2EX unavailable at Port pin.
1: T2EX routed to Port pin.
T2 Input Enable Bit.
0: T2 unavailable at Port pin.
1: T2 routed to Port pin.
/INT1 Input Enable Bit.
0: /INT1 unavailable at Port pin.
1: /INT1 routed to Port pin.
T1 Input Enable Bit.
0: T1 unavailable at Port pin.
1: T1 routed to Port pin.
/INT0 Input Enable Bit.
0: /INT0 unavailable at Port pin.
1: /INT0 routed to Port pin.
T0 Input Enable Bit.
0: T0 unavailable at Port pin.
1: T0 routed to Port pin.
CP1 Output Enable Bit.
0: CP1 unavailable at Port pin.
1: CP1 routed to Port pin.
Table 5.5 XBR1 (Crossbar Register 1)
98
Bit
Symbol
WEAKPUD
XBARE
T4EXE
T4E
UART1E
EMIFLE
CNVSTE
Description
Weak Pull-Up Disable Bit.
0: Weak pull-ups globally enabled.
1: Weak pull-ups globally disabled.
Crossbar Enable Bit.
0: Crossbar disabled. All pins on Ports 0, 1, 2, and
3, are forced to Input mode.
1: Crossbar enabled.
UNUSED. Read = 0, Write = don't care.
T4EX Input Enable Bit.
0: T4EX unavailable at Port pin.
1: T4EX routed to Port pin.
T4 Input Enable Bit.
0: T4 unavailable at Port pin.
1: T4 routed to Port pin.
UART1 I/O Enable Bit.
0: UART1 I/O unavailable at Port pins.
1: UART1 TX and RX routed to 2 Port pins.
External Memory Interface Low-Port Enable
Bit.
0: P0.7, P0.6, and P0.5 functions are determined
by the Crossbar or the Port latches.
1: If EMI0CF.4 = 0 (External Memory Interface is
in Multiplexed mode) P0.7 (/WR), P0.6 (/RD),
and P0.5 (ALE) are skipped by the Crossbar
and their output states are determined by the
Port latches and the External Memory Interface.
1: If EMI0CF.4 = 1 (External Memory Interface is
in Non-multiplexed mode) P0.7 (/WR) and P0.6
(/RD) are skipped by the Crossbar and their
output states are determined by the Port
latches and the External Memory Interface.
External Convert Start Input Enable Bit.
0: CNVSTR unavailable at Port pin.
1: CNVSTR routed to Port pin.
Table 5.6 XBR2 (Crossbar Register 2)
99
Bit Symbol
Description
Port0 Output Latch Bits.
7-0
P0.[7:0]
Bit
7-0
Symbol
P0MDOUT.[7:0]
Description
Port0 Output Mode Bits.
0: Port Pin output mode is configured as
Open-Drain.
1: Port Pin output mode is configured as
Push-Pull.
100
Bit Symbol
7-0
P1.[7:0]
Description
Port1 Output Latch Bits.
(Write - Output appears on I/O pins per XBR0, XBR1,
XBR2, and XBR3 Registers)
0: Logic Low Output.
1: Logic High Output (open if corresponding
P1MDOUT.n bit = 0).
(Read - Regardless of XBR0, XBR1, XBR2, and XBR3
Register settings).
0: P1.n pin is logic low.
1: P1.n pin is logic high.
Table 5.9 P1 (Port1 Data Register)
Bit
7-0
Symbol
P1MDOUT.[7:0]
Description
Port1 Output Mode Bits.
0: Port Pin output mode is configured as
Open-Drain.
1: Port Pin output mode is configured as
Push-Pull.
Bit
7-0
Symbol
Description
P1MDIN.[7:0]
101
Bit Symbol
Description
Port2 Output Latch Bits.
7-0
P0.[7:0]
Bit
7-0
Symbol
P2MDOUT.[7:0]
Description
Port2 Output Mode Bits.
0: Port Pin output mode is configured as
Open-Drain.
1: Port Pin output mode is configured as
Push-Pull.
102
Bit Symbol
Description
Port3 Output Latch Bits.
7-0
P3.[7:0]
Bit
7-0
Symbol
P3MDOUT.[7:0]
Description
Port3 Output Mode Bits.
0: Port Pin output mode is configured as
Open-Drain.
1: Port Pin output mode is configured as
Push-Pull.
103
Bit Symbol
7
IE7
IE6
5-4
IE7CF
IE6CF
1-0
Description
External Interrupt 7 Pending Flag
0: No falling edge has been detected on P3.7 since
this bit was last cleared.
1: This flag is set by hardware when a falling edge
on P3.7 is detected.
External Interrupt 6 Pending Flag
0: No falling edge has been detected on P3.6 since
this bit was last cleared.
1: This flag is set by hardware when a falling edge
on P3.6 is detected.
UNUSED. Read = 00b, Write = dont care.
External Interrupt 7 Edge Configuration
0: External Interrupt 7 triggered by a falling edge on
the IE7 input.
1: External Interrupt 7 triggered by a rising edge on
the IE7 input.
External Interrupt 6 Edge Configuration
0: External Interrupt 6 triggered by a falling edge on
the IE6 input.
1: External Interrupt 6 triggered by a rising edge on
the IE6 input.
UNUSED. Read = 00b, Write = dont care.
5.6
Ports 4 through 7
All Port pins on Ports 4 through 7 can be accessed as General-Purpose
I/O (GPIO) pins by reading and writing the associated Port Data
registers, a set of SFRs which are byte-addressable.
A Read of a Port Data register (or Port bit) will always return the logic
state present at the pin itself, regardless of whether the Crossbar has
allocated the pin for peripheral use or not.
104
The SFRs associated with Ports 7 to 4 are P74OUT and the individual
Port Data registers, P4, P5, P6 and P7. These SFRs are described next.
Bit Symbol
7
P7H
P7L
P6H
P6L
P5H
P5L
P4H
P4L
Description
Port7 Output Mode High Nibble Bit.
0: P7.[7:4] configured as Open-Drain.
1: P7.[7:4] configured as Push-Pull.
Port7 Output Mode Low Nibble Bit.
0: P7.[3:0] configured as Open-Drain.
1: P7.[3:0] configured as Push-Pull.
Port6 Output Mode High Nibble Bit.
0: P6.[7:4] configured as Open-Drain.
1: P6.[7:4] configured as Push-Pull.
Port6 Output Mode Low Nibble Bit.
0: P6.[3:0] configured as Open-Drain.
1: P6.[3:0] configured as Push-Pull.
Port5 Output Mode High Nibble Bit.
0: P5.[7:4] configured as Open-Drain.
1: P5.[7:4] configured as Push-Pull.
Port5 Output Mode Low Nibble Bit.
0: P5.[3:0] configured as Open-Drain.
1: P5.[3:0] configured as Push-Pull.
Port4 Output Mode High Nibble Bit.
0: P4.[7:4] configured as Open-Drain.
1: P4.[7:4] configured as Push-Pull.
Port4 Output Mode Low Nibble Bit.
0: P4.[3:0] configured as Open-Drain.
1: P4.[3:0] configured as Push-Pull.
105
Bit Symbol
7-0
Px.[7:0]
Description
Portx Output Latch Bits.
Write - Output appears on I/O pins.
0: Logic Low Output.
1: Logic High Output (Open-Drain if corresponding
P74OUT bit = 0).
Read - Returns states of I/O pins.
0: Px.n pin is logic low.
1: Px.n pin is logic high.
Table 5.18 Px (Port x Data Register)
106
5.7
Tutorial Questions
1.
Four toggle switches (SW3, SW2, SW1 and SW0) are connected
to Port 2 [7:4]. Four LEDs (LED3, LED2, LED1, and LED0) are
connected to Port 3 [3:0]. Write the code to initialize the ports
accordingly. The output port pins for LED must be in push-pull
mode. Disable global weak pull-ups. If only a part of the port is
used, make sure that the configuration and mode of the unused
pins are not disturbed.
2.
3.
4.
5.
107
(b) Write a function to set up the timer and UART1. The function
prototype is
void Init_UART1_T4(void);
UART1 interrupts have to be enabled and set to high
priority.
(You can assume that the system clock has been properly
setup by another function)
(c) Show the working of how you have calculated the Timer 4
Capture Register reload value for 9600 baud rate.
6
C8051F020 C Programming
6.0
Introduction
110
6.1
6.2
111
6.3
115
6.4
Functions
122
6.5
Interrupt Functions
123
6.6
Reentrant Functions
127
6.7
Pointers
127
6.8
129
6.9
Tutorial Questions
130
110
6.0
Introduction
This chapter introduces the KeilTM C compiler for the Silicon Labs
C8051F020 board. We assume some familiarity with the C programming
language to the level covered by most introductory courses in the C
language.
Experienced C programmers, who have little experience with the
C8051F020 architecture, should become familiar with the system. The
differences in programming the C8051F020 in C, compared to a
standard C program, are almost all related to architectural issues. These
explanations will have little meaning to those without an understanding of
the C8051F020 chip.
The KeilTM C compiler provided with the C8051F020 board does not
come with a floating point library and so the floating point variables and
functions should not be used. However if you require floating point
variables, a full license for the KeilTM C compiler can be purchased.
6.1
Or
#include < c8051f020.h >
These files contain all the definitions of the C8051F020 registers. The
standard initialization and startup procedures for the C8051F020 are
contained in startup.a51. This file is included in your project and will be
assembled together with the compiled output of your C program. For
custom applications, this startup file might need modification.
111
}
//--------------------------------------------------------------
Note: All variables must be declared at the start of a code block. You
cannot declare variables amongst the program statements.
You can test this program in the Silicon Labs IDE (Integrated
Development Environment). You wont see anything happening on the
C8051F020 development board, but you can step through the program
using the debugger.
6.2
112
Any variable declared in this file (such as the variable X above) will be
stored in the internal memory of the C8051F020.
The choice of which memory model to use depends on the program, the
anticipated stack size and the size of data. If the stack and the data
cannot fit in the 128 Bytes of internal memory then the default memory
model should be LARGE, otherwise SMALL should be used.
Yet another memory model is the COMPACT memory model. This
memory model is not discussed in this chapter. More information on the
compact model can be found in the document Cx51 Compiler Users
Guide for KeilTM Software.
You can test the different memory models with the Silicon Labs IDE
connected to the C8051F020-TB development board. Look at the symbol
view after downloading your program and see in which memory
addresses the compiler has stored your variables.
The integer variable X and character variable Initial are stored in the first
128 bytes of internal memory while the integer variable Y and character
variable SInitial are stored in the external memory overriding any default
memory model.
113
Bit-valued Data
Bit-valued data and bit-addressable data must be stored in the bitaddressable memory space on the C8051F020 (0x20 to 0x2F). This
means that bit- valued data and bit-addressable data must be labeled as
such using the bit, sbit and bdata.
Bit-addressable data must be identified with the bdata language
extension:
int bdata X;
114
bdata X;
sbit X7flag = X^7;
/* bit 7 of X */
*/
Extensions of the 8051 often have the low byte of a 16 bit register
preceding the high byte. In this scenario it is possible to declare a 16 bit
special function register, sfr16, giving the address of the low byte:
115
0x40;
The above statement locates the integer X at the memory location 0x40.
The _at_ language extension can not be used to locate bit addressable
data.
6.3
Relational Operators
Relational operators compare data and the outcome is either True or
False. The if statements, for loops and while loops can make use of C
relational operators. These are summarized in Table 6.1.
Operator
==
!=
<
>
<=
>=
Description
Equal to
Not Equal to
Less than
Greater than
Less than or equal to
Greater than or equal to
116
Logical Operators
Logical operators operate on Boolean data (True and False) and the
outcome is also Boolean. The logical operators are summarized in Table
6.2.
Operator
&&
||
!
Description
Logical AND
Logical OR
Logical NOT
Operator
&
|
~
^
Description
Bitwise AND
Bitwise OR
Bitwise NOT
Bitwise XOR
The above statement will assign the value 0x61 to the variable X.
0x40
0x21
0100 0000
0010 0001 bitwise logical OR
0x61
0110 0001
117
Compound Operators
C language provides short cut bitwise operators acting on a single
variable similar to the +=, -=, /= and *= operators. These are summarized
in Tables 6.4 and 6.5.
Operator
Description
Example
Equivalent
+=
Add to variable
X += 2
X=X + 2
-=
Subtract from
variable
X -= 1
X=X - 1
/=
Divide variable
X /= 2
X=X / 2
*=
Multiply variable
X *= 4
X=X * 4
Operator
Description
Example
Equivalent
&=
X &= 0x00FF
|=
Bitwise Or with
variable
X |= 0x0080
X=X | 0x0080
^=
X ^= 0x07A0
X=X | 0x07A0
118
Making Choices
No
Execute
Statement Block2
Is the
Condition
True?
Yes
Execute
Statement Block 1
When the Condition is evaluated as True the first block is executed and if
the Condition evaluates as being False the second block is executed.
More conditions can be created using a sequence of if and else if
statements.
if (x > 10)
{ y=y+1; }
else if (x > 0)
{ y=y-1; }
else
{ y=y-2; }
switch (x)
{
case 5:
y=y+2;
case 4: case
y=y+1;
case 2: case
y=y-1;
default:
y=y-2;
}
119
break;
3:
break;
1:
break;
break;
When the variable x in the switch statement matches one of the case
statements, that block is executed. Only when the break statement is
reached does the flow of control break out of the switch statement. The
default block is executed when there are no matches with any of the
case statements.
If the break statements are missing from the switch-case statement
then the flow will continue within the switch-case block until a break
statement or the end of the switch-case block is reached.
Repetition
Numeric repetition of a code block for a fixed set of times is achieved
using a for loop construct.
int i;
int sum=0;
for( i = 0; i<10; i++)
{
sum = sum + i;
}
120
Execute
statement(s) within
the loop
Yes
Completed
the required
number of
times?
No
Execute statement that
follows the loop
Execute
statement(s)
within the loop
Yes
Is the
condition
true?
No
Execute statement that
follows the loop
Figure 6.3 Flow chart for a while loop
The while loop repeats the loop while the condition specified is true.
121
Early Exits
When executing a code block or a loop, sometimes it is necessary to exit
the current code block. The C language provides several mechanisms to
do this.
The break statement will move the flow of control outside the end of the
current loop.
int i;
int sum=0;
for( i = 0; i<10; i++)
{
sum = sum + i;
if (sum > 25) break;
}
The continue statement skips the remaining code in the current loop,
but continues from the start of the code block of the loop (after
incrementing and checking that the loop should not terminate)
int i;
int sum=0;
for( i = 0; i<10; i++)
{
if (i == 5) continue;
sum = sum + i;
}
122
6.4
Functions
Functions in C are declared using the return data type, the data type of
the parameters and the body of the function.
Unsigned long square (int x)
{
return x*x;
}
123
void Init_Clock(void)
{
OSCXCN = 0x67;
//-- 0110 0111b
//-- External Osc Freq Control Bits (XFCN2-0) set
//
to 111 because crystal frequency > 6.7 MHz
//-- Crystal Oscillator Mode (XOSCMD2-0) set to 110
//-- wait till XTLVLD pin is set
while ( !(OSCXCN & 0x80) );
OSCICN =
//-- Bit
//-- Bit
//
//-- Bit
0x88;
//-- 1000 1000b
2 : Internal Osc. disabled (IOSCEN = 0)
3 : Uses External Oscillator as System
Clock (CLKSL = 1)
7 : Missing Clock Detector Enabled (MSCLKE = 1)
6.5
Interrupt Functions
The basic 8051 has 5 possible interrupts which are listed in Table 6.6.
124
Interrupt No.
Description
Address
External INT 0
0x0003
Timer/ Counter 0
0x000B
External INT 1
0x0013
Timer/ Counter 1
0x001B
Serial Port
0x0023
Interrupt functions must not take any parameters and not return any
parameters. Interrupt functions will be called automatically when the
interrupt is generated; they should not be called in normal program code,
this will generate a compiler error.
125
126
//-//-//-//-//
Register Banks
Normally a function uses the default set of registers. However there are 4
sets of registers available in the C8051F020. The register bank that is
currently in use can be changed for a particular function via the using
KeilTM C language extension.
int count;
void timer1 (void) interrupt 3 using 1
{
count++;
}
6.6
127
Reentrant Functions
Normal KeilTM C functions are not re-entrant. A function must be declared
as re-entrant to be able to be called recursively or to be called
simultaneously by two or more processes. This capability is often
required in real-time applications or in situations when interrupt code and
non-interrupt code need to share a function.
int fact (int X) reentrant
{
if ( X==1) { return 1; }
else { return X*fact(X-1); }
}
6.7
Pointers
Pointers in C are a data type that stores the memory addresses. In
standard C the data type of the variable stored at that memory address
must also be declared:
int * X;
128
but are less efficient as the compiler needs to store what memory model
is being pointed to. This means that a generic pointer takes 3 bytes of
storage - 1 byte to store the type of memory model that is pointed to and
two bytes to store the address.
int * Y;
char * ls;
long * ptr;
You may also explicitly specify the memory location that the generic
pointer is stored in, to override the default memory model.
int * xdata Y;
char * idata ls;
long * data ptr;
You may also specify the memory location that the memory-specific
pointer is stored in, to override the default memory model.
int data * xdata Y;
char xdata * idata ls;
long idata * data ptr;
6.8
129
Data Type
Bits
Bytes
bit
0 to 1
signed char
-128 to +127
unsigned char
0 to 255
8/16
1 or 2
-128 to +127 or
-32768 to +32767
signed short
16
-32768 to +32767
unsigned
short
16
0 to 65535
signed int
16
-32768 to +32767
unsigned int
16
0 to 65535
signed long
32
-2147483648 to 2147483647
unsigned long
32
0 to 4294967295
float
32
1.175494E-38 to
3.402823E+38
sbit
0 to 1
sfr
0 to 255
sfr16
16
0 to 65535
enum
Value Range
130
6.9
Tutorial Questions
1.
2.
3.
How do you override the default memory model for the storage of
a variable in your program?
4.
5.
6.
7.
Why cant normal KeilTM C functions be used for recursive or reentrant calls?
8.
9.
10.
11.
12.
How does the use of different register banks make interrupt calls
more efficient?
13.
14.
15.
16.
7
Expansion Board for C8051F020
Target Board
7.0
Introduction
132
7.1
Starting a Project
134
7.2
135
7.3
138
7.4
142
7.5
151
7.6
153
7.7
154
7.8
132
7.0
Introduction
The Silicon Labs C8051F020 evaluation board has pin header
connections for all 8 ports so it is not difficult to attach additional devices.
Nevertheless it is much more convenient and robust to mount switches
and displays on a printed circuit board, which can then connect using the
DIN96 connector which also has all the ports available. The board we
have developed uses the upper four ports which leave the lower, more
versatile, ports available for other uses.
This chapter will explain the expansion board and provide a number of
example programs which are designed to help learning to program the
Silicon Labs C8051F020. Some of these will require the expansion board
(or similar hardware) while others only use the Silicon Labs C8051F020
Evaluation board itself.
96 pin
DIN
connector
to Silicon
Labs
MCU
Board
P5 b7-4
P5 b3-0
P4
AIN0.2
AIN0.3
DAC1
DAC0
Control
3
8
Data
16 Character x 2 Lines
Liquid Crystal Display
Four LEDs
DIP 8 Switches
Potentiometer
Temperature Sensor
133
134
Temperature Sensor
Either a thermistor or a three-terminal temperature sensor (e.g. LM335)
can be used. The pull up resistor should be changed to suit the device.
LCD Contrast
JP1 selects the source of the contrast voltage for the LCD, which is
either the 10k trim pot (VR2) or the DAC1 output. Using the latter allows
software control of the LCD contrast.
Test Points
The four analog signals on the board are all available on test points
which allow an oscilloscope to be easily connected. Analog and digital
grounds, and the 3.3 V supply, are also provided.
Power Supply
A very small current is drawn from the 3.3 V supply of the development
board for biasing the potentiometer and temperature sensor. The LCD
operates from +5 V generated using a 5 V regulator chip which runs off
the unregulated supply on the Silicon Labs C8051F020 board. This can
be grounded with a push button switch (via a current limiting resistor) so
that the LCD can be reset. The LCD requires about 1.5 mA which
increases to 8 mA when the switch is pressed.
7.1
Starting A Project
In common with many Windows based software development
environments, the Silicon Labs Integrated Development Environment
(IDE) uses a project file to specify the actions to be performed on the set
of files it is currently working with. With a simple project there will only be
a single file involved but larger projects quickly expand to a number of .c
source files and perhaps assembly files too. The project file stores other
information too, including the state of the IDE desktop.
A sensible approach is to start with an empty directory for a new project.
Using
the
menus:
Project/New Project
followed
by
Project/Save Project As and navigating to your new directory results in a
workspace file (.wsp). Similarly, create a new .c file and save it as well.
At this point you have a .wsp and a .c file in your directory, but the .c file
is not actually part of the project. Project/Add Files to Project will allow
you to add the file to the project.
135
You can now proceed to write your program. It can be compiled with F7,
downloaded with Alt-D, and run with F5. To combine the compile and
download steps go to Project/Target Build Configuration and check the
Enable automatic download/connect after build check box.
7.2
The Silicon Labs C8051F020 has many internal registers which control
how it operates. These must be configured at run time by a program as
it executes. Although this could be performed by a block of code at the
start of main() it is clearer to use a separate function here, in Figure
7.3, called init().
Watchdog Timer
The watchdog timer (WDT) is enabled by default at system start-up. Its
purpose is to reset the microcontroller, should the running program lose
control perhaps by entering an infinite loop. The first two lines of init()
136
for P0
for P1,
for P2
for P3
for P4-7
}
Figure 7.3 Initializing Internal Registers
To disable the WDT, two writes are needed to the watch dog timer
control register (WDTCN). These must occur within 4 clock cycles of
each other so interrupts should not be enabled at the time.
Port Configuration
With the crossbar enabled, the port pins will pull high via the internal
resistors, allowing the pin to source a few A, when a logic 1 is written to
the pin. Internal transistors will pull the pin low in response to a logic 0
allowing it to sink up to 50 mA. The LED on P1.6 and the four LEDs on
the expansion board must be driven high to illuminate and so the port
pins that drive them need to be configured in push-pull output mode.
They can then easily source the required 10 mA for the LED. The output
137
mode of each of the pins of the lower four ports (P0 to P3) is individually
configurable while with the upper ports (P4 to P7) they are set in groups
of four pins.
A good programming practice is to configure only those pins that are
actually needed is.
Software Delays
Because the MCU operates very fast it is necessary to slow it down if a
blinking LED is to be observed. The program in Figure 7.4 shows three
functions which provide different amount of delays when called in a
program. huge_delay() calls large_delay() which in turn calls
small_delay(). Each has a loop which counts down. Calling
small_delay(10) will give a delay of about 40 s while huge_delay(10)
will take about 2.3 s. Of course, if the clock is different from 2.0 MHz the
delays will be different too.
While software delays can be quite accurate, if calibrated, it is difficult to
do so and they lose any time taken by interrupts. They also tie up the
processor while running, so other tasks such as reading the keyboard
are ignored.
void small_delay(char d)
{
while (d--);
}
void large_delay(char d)
{
while (d--)
small_delay(255);
}
void huge_delay(char d)
{
while (d--)
large_delay(255);
}
Figure 7.4 The software delay routines
138
//
//
//
//
//
Another point to note is that while the delay is about 180 ms, the LED
flashes at about 2.8 Hz. This is because the loop must run twice for the
LED to go through one cycle.
7.3
139
examines the high byte of Timer 3 and uses a bit mask to determine
when the count has passed halfway.
void main(void)
{
init();
Timer3_Init(0x0000);
while (1)
{
LED_16 = ((TMR3H & 0x80) == 0x80);
}
}
void Timer3_Init(int counts)
{
TMR3CN = 0x00;
TMR3RL = -counts;
TMR3 = 0xffff;
TMR3CN |= 0x04;
//
//
//
//
//
}
Figure 7.6 Polling Timer 3
Timer 3 Interrupt
An interrupt can be generated when Timer 3 overflows. This causes
execution to jump to its interrupt service routine (ISR) which has priority
14, and is identified to the compiler by interrupt 14 (in Figure 7.7). The
actual ISR code is only two lines. The timer flag bit is what actually
generates the interrupt it is set when the timer overflows and it is the
programmers responsibility to clear it. It is part of the Timer 3 control
register and is not bit addressable so an AND operation must be used
with a bit mask which has a value 0111 1111b.
Interrupts in General
Interrupts can be tricky to use. It is necessary to be very careful when
accessing data which is shared between an ISR and another function,
say, main(). This is because main() doesnt know when it will be
interrupted; it could be partway through reading a variable when an ISR
is called that changes the same variable. Even though this sequence of
events might be very unlikely, after many thousands of interrupts, it WILL
140
happen. This results in a program which works well almost all the time,
but occasionally does something strange. Globally disabling interrupts
(by setting the EA bit to zero) before accessing shared data from outside
an interrupt will prevent the above corruption.
void main(void)
{
init();
Timer3_Init(SYSCLK / 12 / 10);
EA = 1;
while (1);
//
//
//
//
}
void Timer3_Init(int counts)
{
TMR3CN = 0x00;
TMR3RL = -counts;
TMR3 = 0xffff;
EIE2 |= 0x01;
TMR3CN |= 0x04;
//
//
//
//
//
//
}
void Timer3_ISR(void) interrupt 14
{
TMR3CN &= ~(0x80);
// clear TF3
LED_16 = ~LED_16;
// change state of LED
}
141
indicates the crystal has stabilized. Only at that point does the operation
switch to the external oscillator.
OSCXCN = 0x67;
//
//
for (n = 0; n != 255; n++);
//
while ((OSCXCN & 0x80) == 0); //
OSCICN = 0x0C;
EXTERNAL Oscillator
Control Register
wait for osc to start
wait for xtal to stabilize
// INTERNAL Oscillator
// Control Register
There are two things to note in the new Timer 3 ISR shown in Figure 7.9.
The first is that with the faster oscillator the LED will flash too fast and
appear to be continuously illuminated. To make it flash slow enough to
be visible, an additional byte is used within the ISR to divide the blinking
rate, in this case, by five. Four out of five times the ISR is exited
prematurely; on the fifth the switch statement is executed.
The second feature shows a simple finite state machine. State machines
are useful when a sequence of actions or events must be detected or
created. In this case the four states loop in numerical order but they can
become much more complex in some scenarios.
void Timer3_ISR(void) interrupt 14
{
static uchar state = 0;
static uchar ctr = 0;
TMR3CN &= ~(0x80);
if (ctr--)
return;
ctr = 4;
// clear TF3
switch (state)
{
case 0:
state = 1;
P5 = 0x1F; break;
case 1:
state = 2;
P5 = 0x2F; break;
case 2:
state = 3;
P5 = 0x4F; break;
case 3:
state = 0;
P5 = 0x8F; break;
default: state = 0; break;
}
// state machine
// divide by 5
// LED 1 on
// LED 2 on
// LED 3 on
// LED 4 on
// for safety
142
MPU
Interface
7.4
Display Data
(DD) RAM
128 Bytes
Character Generator
(CG) RAM
64 Bytes
DDRAM
Address
Counter
CGRAM
Address
Counter
143
39h
Line 1
40h
79h
Line 2
2 rows of 16
characters visible
Figure 7.11 2 Line x 16 Character LCD Window
The characters in the first row start at address 00h and the second row
starts at 40h. The value in the DDRAM Address Counter is where the
next character will be written. It can be set to any address. Normally the
module is configured so that subsequent characters go into the next
highest location (DDRAM is incremented). This results in writing from left
to right on the display. The simplest approach is to leave the display
window at 00h.
The Character Generator RAM (CGRAM) holds user defined characters.
Programming these is not covered in this text.
Pin Definitions
Figure 7.12 shows the pin out of the LCD module. Three pins are used
for the power supply, ground and contrast adjustment. HD22780S
modules require a +5V supply, however the HD44780U controller can
operate on a range of supplies, down to 2.7V. The expansion board
includes a 5V regulator so the S variant may be used too.
The C8051F020
port pins are
also tolerant of
5V!
There are 11 signal lines altogether. These include the 8-bit bidirectional
data bus and three control lines which are write-only. They require clean
digital signals which would normally be 0 or 5V. However they are
tolerant of 3.3V systems.
Be aware that the data bus is bidirectional, so at times the LCD controller
will want to drive the pins high or low. A connected microcontroller must
not attempt to drive the bus at the same time, except through pull-up
144
Signal
Pin
Number
Full
Name
VSS
Ground
0V common connection
VDD
Supply
Voltage
+5V supply
VO
Contrast
Function
Enable
RS
Register
Select
0: Instruction Register
1: Data Register
RW
Read or
Write
0: Write to LCD
1: Read from LCD
D[0..7]
7 - 14
8-bit Data
Bus
15,16
Instructions
Instructions are used for configuring the LCD controller, to pass it data
which will be displayed, and to read status information back from it.
Figure 7.13 shows the instructions in numerical order.
Control
Data Bits
R R
E
S W
Clear Display 0 0
Return Home 0 0
0 0
1 I/D S
Display On/Off
0 0
Control
0 0
Function Set 0 0
1 DL N
Set CGRAM
Address
0 0
Set DDRAM
Address
0 0
Read Busy
Flag &
Address
1 0 1 BF
Instruction
Entry Mode
SET
Cursor or
Display Shift
Write to
RAM
Read from
RAM
145
S/C R/L -
b[5..0] Address
b[6..0] Address
Address Counter
Description
Exec
Time
(s)
1 0
37
1 1
37
Initialization Requirements
When the power is first applied to a HD44780 based module it will self
initialize provided the power supply rises at the correct rate. Interestingly
the default wakeup state has the display off! With a normal power supply,
146
LCD Software
Communication with the LCD can generally be divided into either data
which is to be displayed, or commands which affect how it operates. The
difference is caused by the state of the two control lines, RS and RW.
Figure 7.14 shows the two functions for writing either data or a command
byte.
char lcd_dat(char dat)
{
lcd_busy_wait();
LCD_CTRL_PORT = LCD_CTRL_PORT | RS_MASK; // RS = 1
LCD_CTRL_PORT = LCD_CTRL_PORT & ~RW_MASK; // RW = 0
LCD_DAT_PORT = dat;
pulse_E();
return 1;
}
void lcd_cmd(char cmd)
{
lcd_busy_wait();
LCD_CTRL_PORT = LCD_CTRL_PORT & ~RS_MASK; // RS = 0
LCD_CTRL_PORT = LCD_CTRL_PORT & ~RW_MASK; // RW = 0
LCD_DAT_PORT = cmd;
pulse_E();
}
Figure 7.14 Communicating with the LCD
147
LCD_DAT_PORT P6
// LCD is in 8 bit mode
LCD_CTRL_PORT P7
// 3 control pins on P7
RS_MASK
0x01
// for assessing LCD_CTRL_PORT
RW_MASK
0x02
E_MASK
0x04
pulse_E();\
small_delay(1);\
LCD_CTRL_PORT = LCD_CTRL_PORT | E_MASK;\
small_delay(1);\
LCD_CTRL_PORT = LCD_CTRL_PORT & ~E_MASK;\
Figure 7.15 Preprocessor macros
These functions rely on several #defines and a macro for pulsing the
enable line which are shown in Figure 7.15. The MASK values reflect the
bits the control lines are connected to.
void lcd_busy_wait(void)
{
LCD_DAT_PORT = 0xFF;
// allow port pins to float
LCD_CTRL_PORT = LCD_CTRL_PORT & ~RS_MASK; // RS = 0
LCD_CTRL_PORT = LCD_CTRL_PORT | RW_MASK; // RW = 1
small_delay(1);
LCD_CTRL_PORT = LCD_CTRL_PORT | E_MASK;
// E = 1
do {
// wait for busy flag to drop
small_delay(1);
} while ((LCD_DAT_PORT & 0x80) != 0);
}
Figure 7.16 Waiting for the busy bit
148
~RS_MASK; // RS = 0
~RW_MASK; // RW = 0
~E_MASK; // E = 0
16ms delay
LCD_DAT_PORT = 0x38;
pulse_E();
large_delay(50);
LCD_DAT_PORT = 0x38;
pulse_E();
large_delay(2);
LCD_DAT_PORT = 0x38;
pulse_E();
large_delay(2);
lcd_cmd(0x06);
lcd_cmd(0x01);
lcd_cmd(0x0E);
// 4.1ms delay
// 1.5ms delay
// 1.6ms delay
}
#pragma OPTIMIZE (9)
Figure 7.17 Initializing the LCD
149
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
38
Description
Function Set
8 bits, 2 lines,
5x8 font
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
38
Description
Function Set
8 bits, 2 lines,
5x8 font
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
38
Description
Function Set
8 bits, 2 lines,
5x8 font
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
06
Description
Entry mode set
Display on,
Increment curser,
Dont shift display.
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
01
Description
Clear display
Data
Data
E RS RW B7 B6 B5 B4 B3 B2 B1 B0
Hex
0E
Description
Display on/off
ctrl. Display on,
curser on, No
blink.
150
LCD Integration
While it is nice to be able to write a character to the display, it is much
more convenient to write entire strings especially if they can be
formatted. Astute readers may have noticed that the function
lcd_dat() in Figure 7.14 returned char which appeared unnecessary.
It does so to take advantage of a feature of the KeilTM compiler which has
the ability to redefine built-in library functions.
This can be done by renaming lcd_dat() as putchar(), which is an
ANSI C function in the stdio library. KeilTM have written putchar() to
send characters to the serial port. By replacing it with lcd_dat() they
now go to the LCD.
Replacing one function with another doesnt appear very helpful. The
beauty of this approach is that other standard functions call putchar()
to achieve their low level output. One of the more useful I/O functions is
printf() which can now send formatted output to the LCD! This can
be seen in Figure 7.18.
void main(void)
{
int ctr;
init();
P5 = 0x0F;
lcd_init();
while (1)
{
printf("Hello World %4d ", ctr++);
huge_delay(3);
}
}
Figure 7.18 Writing a string to the LCD
This will quickly write past the end of the display and, after 64 characters,
wrap around to the second line, eventually coming back to overwrite the
first line. A function called lcd_goto() is also very useful and is shown in
Figure 7.19. To write to the start of the first line use lcd_goto(0), the
second line would use lcd_goto(0x40);
void lcd_goto(char addr)
{
lcd_cmd(addr | 0x80);
}
Figure 7.19 Moving the text entry point
7.5
151
Initialization
Normally analog readings are required at regular intervals. In the next
example Timer 3 will be used to initiate conversions at a SAMPLE_RATE
times each second. To allow capturing fast changing signals, the system
clock is used directly, rather than dividing it by 12.
void Timer3_Init(int counts)
{
TMR3CN = 0x02;
// Stop Timer3; Clear TF3
// use SYSCLK as timebase
TMR3RL = -counts;
// Init reload values
TMR3 = 0xFFFF;
// set to reload immediately
EIE2 |= 0x01;
// enable Timer3 interrupts
TMR3CN |= 0x04;
// start Timer3
}
void ADC0_Init(void)
{
ADC0CN = 0x05;
REF0CN = 0x07;
AMX0SL = 0x02;
ADC0CF = 0x86;
EIE2 |= 0x02;
//
//
//
//
//
//
//
//
//
//
}
Figure 7.20 Timer and ADC Initialization.
152
adc_result;
adc_ready = 0;
void main(void)
{
init();
lcd_init();
Timer3_Init(SYSCLK / SAMPLE_RATE); // initialize Timer3 to
// overflow at SAMPLE_RATE
ADC0_Init();
// init ADC
AD0EN = 1;
// enable ADC
EA = 1;
// Enable global interrupts
while (1)
{
if (adc_ready)
{
EA = 0;
// Disable interrupts
adc_ready = 0;
printf("ADC value %4u ", adc_result);
EA = 1;
// Enable interrupts
lcd_goto(0x00);
}
}
}
Figure 7.22 The main() code
The key points to observe in main() are calling the various initialization
routines, activating the interrupts, and the way the flag adc_ready is
used to determine that a conversion is ready for display. In particular,
note that interrupts are switched off while accessing adc_result. This is
necessary to prevent the ADC ISR from changing the value half way
through printing it. All the same it is a poor practice to switch interrupts
off for a significant time (remember the LCD is quite slow). A better
153
approach is to use a second buffer to copy the result into, before printing
it. This is left as an exercise for the reader.
7.6
154
7.7
B
+3VD2 (+3.3VDC)
MONEN
P1.5
P1.2
P2.7
P2.4
P2.1
P3.6
P3.3
P3.0
P0.5
P0.2
P7.7
NC2
P7.4
R/W
P7.1
DB6
P6.6
DB3
P6.3
DB0
P6.0
P5.5
P5.2
P4.7
P4.4
P4.1
TCK
/RST
AGND (Analog Gnd)
CP1CP0+
VREF0
AIN0.6
AIN0.3
AIN0.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
CON32
C
DGND (Digital Gnd)
P1.7
P1.4
P1.1
P2.6
P2.3
P2.0
P3.5
P3.2
P0.7
P0.4
P0.1
P7.6
NC1
P7.3
RS
P7.0
DB5
P6.5
DB2
P6.2
P5.7
P5.4
P5.1
P4.6
P4.3
P4.0
TDI
DGND (Digital Gnd)
DAC1
CP1+
VREF
VREF1
AIN0.5
AIN0.2
AGND (Analog Gnd)
CON32
CON32
RS 406-385
RS 173-675
AIN0.2
P5.0
VCC
DAC1
C1
100nF
DAC0
Test Point 2
AIN0.2
Test Point 3
AIN0.3
Test Point 4
VCC
Modified to
Pot1
0.5
100k
R17
R18
pull up to +5V
1k
1k
AGND
Test Point 5
AGND
P5.1
AIN0.3
SW1
DGND
R20
SW2
100
TR1
Res Thermal
DGND
AGND (Analog Gnd)
R19
100
Test Point 1
VCC
+3VD2 (+3.3VDC)
XTAL1
P1.6
P1.3
P1.0
P2.5
P2.2
P3.7
P3.4
P3.1
P0.6
P0.3
P0.0
P7.5
EN
P7.2
DB7
P6.7
DB4
P6.4
DB1
P6.1
P5.6
P5.3
P5.0
P4.5
P4.2
TMS
TDO
VUNREG
DAC0
CP0VREFD
AIN0.7
AIN0.4
AIN0.1
+3VD2 (+3.3VDC)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
P5.2
DGND
R21
SW3
100
AGND
Test Point 6
DGND
Power Coupling
SW5
LCD Reset Sw
C2
100nF
Test Points
Vin
Vout
GND
DGND
Power Reg
P5.3
DGND
C3
100nF
+5V
VSS
VDD
VO
RS
R/W
EN
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
NC1
NC2
SW4
KPT-1105D
S1
LCD1
DGND
C4
4.7uF
DGND
R22
100
Pot2
0.5
10k
+5V
U1
VUNREG R23
100
VO
AGND
VCC
VSS
VDD
VO
RS
R/W
E
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
NC
NC
P4.0
P4.1
P4.2
P4.3
P4.4
P4.5
P4.6
P4.7
16
15
14
13
12
11
10
9
SW DIP-8
DGND
R13
LED1
330
R14
P5.6
LED2
330
R15
P5.5
LED3
Components
1
2
3
4
5
6
7
8
100
P5.7
LCD, 16-PIN
Z4170
R1
R2
R3
R4
R5
100
R6
R7
R8
330
R16
P5.4
LED4
1k
DGND
DGND
7.8
155
8
Timer Operations and
Programming
8.0
Introduction
158
8.1
Functional Description
159
8.2
Timer Programming
160
8.3
Timer SFRs
161
8.4
161
8.5
164
8.6
CKCON Register
170
8.7
171
TMOD, TCON
8.8
Timer 2 SFRs
173
T2CON
8.9
Timer 3 SFRs
175
TMR3CN
8.10
Timer 4 SFRs
176
T4CON
8.11
178
8.12
Tutorial Questions
181
158
8.0
Introduction
One of the many tasks that are normally performed by a CPU is to time
internal and external events. The microprocessor usually provides the
timing functions such as timeout delays and event counting by means of
software. Many a times, these functions are implemented using loops
within the software. This effectively takes a lot of CPUs processing time.
A timer/counter is useful in the sense that it is able to handle various
timing applications independently; hence it frees the CPU of such tasks,
allowing it to do other urgent functions.
A timer/counter consists of a series of divide-by-two flip-flops (FF). Each
FF is clocked by the Q output of the previous FF, as shown in Figure 8.1.
Each stage, starting from the LSB F/F to the MSB F/F, generates an
output signal which is half the frequency of its own clock input. Thus a 3bit timer in Figure 8.1 will divide the input clock frequency by 8 (23).
Normally a timer/counter has added features like producing an interrupt
at the end of a count. By adding the timer overflow F/F to the counter, it
will aid to generate such an interrupt signal. Figure 8.1 shows that the
output of the last stage (MSB) clocks a timer overflow flag. The counter
will count from 000b to 111b and set the overflow flag on the transition
from 111b to 000b.The timing diagram is shown in Figure 8.2.
Timer
Overflow
F/F or
Flag
VCC
J
K
Clock
CLK
Q
QN
JKFF
LSB
J
K
CLK
JKFF
Q
QN
J
K
CLK
Q
QN
JKFF
MSB
Figure 8.1 Schematic of a 3-Bit Timer
J
K
CLK
JKFF
Q
QN
159
Clock
LSB
M SB
Count
Flag
Figure 8.2 Timing Diagram
8.1
Functional Description
Timers are used for:
Interval timing
Event counting
Baud rate generation
Timer 0 & 1
Timer 2
13 bit counter/timer
16 bit
counter/timer
with capture
16 bit counter/timer
16 bit
counter/timer
with autoreload
Timer 3
16 bit
timer with
autoreload
Baud rate
generator for
UART0
Timer 4
16 bit
counter/timer
with capture
16 bit
counter/timer
with autoreload
Baud rate
generator for
UART1
160
Timer 0 and Timer 1 are nearly identical and have four primary modes of
operation. Timer 2 offers additional capabilities not available in Timers 0
and 1. Timer 3 is similar to Timer 2, but without the capture or Baud rate
generation modes. Timer 4 is identical to Timer 2, except it supplies
baud rate generation capabilities to UART1 instead. The main Timer
operation modes are discussed in the following sections.
The timers are fully independent, and each may operate in a different
mode. Timers 1, 2 and 4 may be used for UART baud rate generation in
mode 2. Chapter 10 has more information on baud rate generation.
8.2
Timer Programming
The timers can be programmed through the following sequence:
A.
B.
2.
3.
4.
mode
(TMOD.0/TMOD.1
or
For Timer 3:
1.
2.
3.
C.
8.3
161
2.
3.
4.
Timer SFRs
The 22 special function registers used to access and control the timers
are summarized in Table 8.2. Refer to Sections 8.7 to 8.10 for more
detail on each bit of the six timer control SFRs (CKCON, TCON, TMOD,
T2CON, TMR3CN and T4CON).
8.4
162
Affected
Timers
0, 1, 2
and 4
0 and 1
Timer
SFR
Purpose
CKCON
TCON
TMOD
TL0
TL1
TH0
TH1
T2CON
RCAP2L
RCAP2H
TL2
TH2
TMR3CN
TMR3RLL
TMR3RLH
TMR3L
TMR3H
T4CON
RCAP4L
RCAP4H
TL4
TH4
Address
Bit
Addressable
Clock Control
8EH
No
Timer Control
Timer Mode
Timer 0 Low Byte
Timer 1 Low Byte
Timer 0 High Byte
Timer 1 High Byte
Timer 2 Control
Timer 2 Low Byte
Capture
Timer 2 High Byte
Capture
Timer 2 Low Byte
Timer 2 High Byte
Timer 3 Control
Timer 3 Low Byte
Reload
Timer 3 High Byte
Reload
Timer 3 Low Byte
Timer 3 High Byte
Timer 4 Control
Timer 4 Low Byte
Capture
Timer 4 High Byte
Capture
Timer 4 Low Byte
Timer 4 High Byte
88H
89H
8AH
8BH
8CH
8DH
C8H
Yes
No
No
No
No
No
Yes
CAH
No
CBH
No
CCH
CDH
91H
No
No
No
92H
No
93H
No
94H
95H
C9H
No
No
No
E4H
No
E5H
No
F4H
F5H
No
No
Starting the timer by setting TRx (TCON.4 and TCON.6 refer to Figure
8.3 and Table 8.6) does not reset the count register. The count register
should be initialized to the desired value before enabling the timer. If an
interrupt service routine is required, the interrupt flag is enabled and an
interrupt is generated whenever the timer overflows. This applies to all
timer modes, for Timers 0 to 4.
163
12
TMOD
G
A
T
E
1
C
/
T
1
T T
1 1
MM
1 0
G
A
T
E
0
C
/
T
0
T T
0 0
MM
1 0
SYSCLK
0
1
1
T0
TL0
(8 bits)
TCON
TCLK
Crossbar
TR0
/INT0
GATE0
TH0
(8 bits)
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
Interrupt
Reload
The timer low byte (TLx) operates as an 8-bit timer while the timer high
byte (THx) holds a reload value. When the count in TLx overflows from
FFH to 00H, the timer flag is set and the value in THx is loaded into TLx.
Counting continues from the reload value up to the next FFH overflow,
and so on.
This mode is convenient as the timer overflows at specific periodic
intervals once TMOD and THx are initialized. TLx must be initialized to
the desired value before enabling the timer for the first count to be
correct.
Timer 1 can be used as an 8-bit baud rate generator for UART0 and/or
UART1 in mode 2.
164
TR1
SYSCLK
1
C
/
T
1
T T
1 1
MM
1 0
G
A
T
E
0
C
/
T
0
T T
0 0
MM
1 0
TH0
(8 bits)
TCON
12
TMOD
G
A
T
E
1
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
Interrupt
Interrupt
1
T0
TL0
(8 bits)
Crossbar
TR0
/INT0
GATE0
8.5
165
Timers 2 & 4
As mentioned before, Timer 4 is identical to Timer 2. This mode is
selected by clearing the CP/RLz bit (Refer to Figure 8.5, Table 8.7 for
T2CON and Table 8.10 for T4CON).
NOTE: z = 2 or 4
The count register reload occurs on an FFFFH to 0000H transition and
sets the TFz timer overflow flag. An interrupt will occur if enabled. On
overflow, the 16 bit value held in the two capture registers (RCAPzH and
RCAPzL) is automatically loaded into the count registers (TLz and THz)
and the timer is restarted.
Setting TRz to 1 enables and starts the timer. The timers can use either
the system clock or transitions on an external input pin (Tz) as the clock
source (counter mode), specified by the C/Tz bit. If EXENz is set to 1, a
high-to-low transition on TzEX will also cause a Timer z reload, and a
Timer z interrupt if enabled. If EXENz is 0, transitions on TzEX will be
ignored.
The appropriate crossbars have to be configured to enable the input
pins. TLz and THz must be initialized to the desired value before
enabling the timer for the first count to be correct.
CKCON
TTTT
4 2 1 0
MMMM
12
SYSCLK
0
T2
Crossbar
T2EX
TCLK
TL2
TH2
RCAP2L
RCAP2H
T2CON
TR2
Reload
CP/RL2
C/T2
TR2
EXEN2
TCLK0
RCLK0
EXF2
TF2
EXEN2
Interrupt
166
Example:
; Uses Timer 4 in 16-bit Auto-Reload Mode (Mode 1)
; ------------------------------------------------ORL
CKCON, #01000000b ; Timer 4 uses system clock
MOV
RCAP4L, #33h
; Set reload time to 0x2233
MOV
RCAP4H, #22h
MOV
TL4, RCAP4L
; Initialize TL4 & TH4 before
MOV
TH4, RCAP4H
; counting
MOV
T4CON, #00000100b ; Start Timer 4 in Mode 1
Timer 3
Timer 3 is always configured as an auto-reload timer, with the reload
value held in TMR3RLL and TMR3RLH. The operation is essentially the
same as for Timers 2 and 4, except for slight differences in register
names and clock sources (refer to Figure 8.6). TMR3CN is the only SFR
required to configure Timer 3.
T3XCLK
(to ADC)
8
1
12
TCLK
0
0
SYSCLK
1
SCL
TMR3H
TR3
TMR3L
Crossbar
TF3
TMR3CN
External
Oscillator Source
Reload
TMR3RLL TMR3RLH
Interrupt
TR3
T3M
T3XCLK
167
2)
3)
12
SYSCLK
0
T2
Crossbar
T2EX
TCLK
TL2
TH2
RCAP2L
RCAP2H
TR2
T2CON
Capture
CP/RL2
C/T2
TR2
EXEN2
TCLK0
RCLK0
EXF2
TF2
Interrupt
EXEN2
The timers can use either SYSCLK, SYSCLK divided by 12, or high-tolow transitions on the Tz input pin as the clock source when operating in
Capture mode. Clearing the C/Tz bit selects the system clock as the
input for the timer (divided by 1 or 12 as specified by TzM). When C/Tz is
set to 1, a high-to-low transition at the Tz input pin increments the
counter register.
168
T2
Crossbar
RCLK0
TCLK
TL2
Timer 2
Overflow
TH2
TR2
SS
MS
OT
DA
0 T
0
2
Timer 1
Overflow
PCON
SS
MS
OT
DA
1 T
1
Reload
S I
TD
OL
PE
RCAP2L
16
TX0 Clock
RCAP2H
T2CON
EXEN2
Crossbar
RX0 Clock
T2EX
16
1
CP/RL2
C/T2
TR2
EXEN2
TCLK0
RCLK0
EXF2
TF2
TCLK0
Interrupt
Figure 8.8 Block Diagram of Timer 2 in Baud Rate Generation Mode (Mode 2)
169
In Baud Rate Generator mode, the Timer z time base is the system clock
divided by two. When selected as the UARTx baud clock source, Timer z
defines the UARTx baud rate as follows:
BaudRate =
SYSCLK
(65536 [ RCAPzH , RCAPzL ]) 32
If a different time base is required, setting the C/Tz bit to 1 will allow the
time base to be derived from the external input pin Tz. In this case, the
baud rate for the UART is calculated as:
BaudRate =
FCLK
(65536 [ RCAPzH , RCAPzL ]) 16
where FCLK is the frequency of the signal (TCLK) supplied to Timer z and
[RCAPzH, RCAPzL] is the 16 bit value held in the capture registers.
Figure 8.9 Block Diagram of Timer 4 in Baud Rate Generation Mode (Mode 2)
170
Example:
;---------------------------------------------------; Using Timer 4 in Mode 2 to generate a baud rate of
; 2400 for UART1. System clock = 22.1184 MHz external
; oscillator
;---------------------------------------------------ORL
CKCON, #40h
; Timer 4 uses system clock
MOV
RCAP4L, #0E0h
; Auto-reload value=FEE0
MOV
RCAP4H, #0FEh
MOV
TL4, RCAP4L
; Initialize count registers
MOV
TH4, RCAP4H
MOV
T4CON, #00110100b ; Set Timer 4 in mode 2 and
; start timer
8.6
CKCON Register
For interval timing, the count registers are incremented on each clock
tick. Clock ticks are derived from the system clock divided by either 1 or
12 as specified by the Timer Clock Select bits (T0M T2M and T4M) in
CKCON register (Table 8.3). The twelve-clocks-per-tick option provides
compatibility with the older generation of the 8051 family. Applications
that require a faster timer should use the one-clock-per-tick option.
For Timer 3, the clock is selected using T3M bit in TMR3CN register.
Bit
Symbol
T4M
T2M
T1M
T0M
2-0
Reserved
Description
8.7
171
Bit Symbol
7
GATE1
C/T1
5-4
T1M1T1M0
GATE0
C/T0
1-0
T0M1T0M0
Description
Timer 1 Gate Control
0: Timer 1 enabled when TR1(TCON.6)=1
irrespective of /INT logic level
1: Timer 1 enabled only when TR1=1 AND
/INT=logic 1
Counter/Timer 1 Select
0: Timer Function: Timer 1 incremented by clock
defined by T1M bit (CKCON.4).
1: Counter Function: Timer 1 incremented by highto-low transition on external input pin (T1).
Timer 1 Mode Select (Table 8.5)
Timer 0 Gate Control
0: Timer 0 enabled when TR0(TCON.4)=1
irrespective of /INT logic level
1: Timer 0 enabled only when TR0=1 AND
/INT=logic 1
Counter/Timer 0 Select
0: Timer Function: Timer 0 incremented by clock
defined by T0M bit (CKCON.3).
1: Counter Function; Timer 0 incremented by highto-low transition on external input pin (T0).
Timer 0 Mode Select (Table 8.5)
Table 8.4 TMOD: Timer Mode Register
172
M1
M0
Mode
0
0
0
1
0
1
Description
13 bit Counter/Timer
16 bit Counter/Timer
8 bit Counter/Timer with Autoreload
Timer 1: Inactive
Timer 0: Two 8 bit Counter/Timers
TCON
Bit Symbol
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
Description
Timer 1 Overflow Flag
Set by hardware when Timer 1 overflows. This flag can
be cleared by software but is automatically cleared when
the CPU vectors to the Timer 1 interrupt service routine
(ISR).
0: No Timer 1 overflow detected
1: Timer 1 has overflowed
Timer 1 Run Control
0: Timer 1 disabled
1: Timer 1 enabled
Timer 0 Overflow Flag
Same as TF1 but applies to Timer 0 instead.
0: No Timer 0 overflow detected
1: Timer 0 has overflowed
Timer 0 Run Control
0: Timer 0 disabled
1: Timer 0 enabled
External Interrupt 1
This flag is set by hardware when an edge/level of type
defined by IT1 is detected. It can be cleared by software
but is automatically cleared when the CPU vectors to the
External Interrupt 1 ISR if IT1=1. This flag is the inverse of
the /INT1 input signals logic level when IT1=0
Interrupt 1 Type Select
0: /INT1 is level triggered
1: /INT1 is edge triggered
External Interrupt 0
Same as IE1 but applies to IT0 instead.
Interrupt 0 Type Select
0: /INT0 is level triggered
1: /INT0 is edge triggered
Table 8.6 TCON: Timer Control Register
173
The TCON register contains status and control bits for Timer 0 and 1
(Table 8.6). The upper four bits in TCON are used to turn the timers on
and off (TR0, TR1) or to signal a timer overflow (TF0, TF1). The lower
four bits in TCON have nothing to do with the timers as such. They are
used to detect and initiate external interrupts. Please refer to Chapter 11
for more details on interrupts.
The other 4 registers associated with Timers 0 and 1 are TL0, TL1, TH0
and TH1. These enable access to the timers as two separate bytes.
8.8
Timer 2 SFRs
T2CON
The operating mode of Timer 2 is selected by setting the configuration
bits in T2CON (Table 8.7 and Table 8.8).
The RCAP2L and RCAP2H registers capture the low and high byte of
Timer 2 respectively when Timer 2 is configured in capture mode. If
Timer 2 is configured in auto-reload mode, these registers hold the low
and high byte of the reload value. TL2 and TH2 are used to access
Timer 2 as two separate bytes.
Mode
174
Bit Symbol
TF2
EXF2
RCLK0
TCLK0
EXEN2
TR2
C/T2
CP/RL2
Description
Timer 2 Overflow Flag
Set by hardware when Timer 2 overflows. When the Timer 2
interrupt is enabled (IE.5, see Chapter 8), setting this bit
causes the CPU vectors to the Timer 2 ISR. This bit is not
automatically cleared by hardware and must be cleared by
software. TF2 will not be set when RCLK0 and/orTCLK0 are
logic 1.
Timer 2 External Flag
Set by hardware when either a capture or reload is caused by
a high-to-low transition on the T2EX input pin and EXEN2 is
logic 1. When the Timer 2 interrupt is enabled, setting this bit
causes the CPU to vector to the Timer 2 ISR. This bit is not
automatically cleared by hardware and must be cleared by
software.
Receive Clock Flag for UART0
0: Timer 1 overflows used for receive clock
1: Timer 2 overflows used for receive clock
Transmit Clock Flag for UART0
0: Timer 1 overflows used for transmit clock
1: Timer 2 overflows used for transmit clock
Timer 2 External Enable
Enables high-to-low transitions on T2EX to trigger captures
or reloads when Timer 2 is not operating in Baud Rate
Generator mode.
0: High-to-low transitions on T2EX ignored.
1: High-to-low transitions on T2EX cause a capture or reload.
Timer 2 Run Control
0: Timer 2 disabled 1: Timer 2 enabled
Counter/Timer Select
0: Timer Function: Timer 2 incremented by clock defined by
T2M bit (CKCON.5).
1: Counter Function: Timer 2 incremented by high-to-low
transition on external input pin (T2).
Capture/Reload Select
EXEN2 must be logic 1 for high-to-low transitions on T2EX to
be recognized and used to trigger captures or reloads. If
RCLK0 or TCLK0 is set, this bit is ignored and Timer 2 will
function in auto-reload mode.
0: Auto-reload on Timer 2 overflow or high-to-low transition at
T2EX (EXEN2=1)
1: Capture on high-to-low transition at T2EX (EXEN2=1)
Table 8.8 T2CON: Timer 2 Control Register
8.9
175
Timer 3 SFRs
TMR3CN
Timer 3 is always configured as an auto-reload timer, with the 16-bit
reload value held in the TMR3RLL (low byte) and TMR3RLH (high byte)
registers.
TMR3L and TMR3H are the low and high bytes of Timer 3. TMR3CN is
used to select the clock source and is the only SFR used to configure
Timer 3 (Table 8.9).
Bit Symbol
TF3
6-3
UNUSED
TR3
T3M
T3XCLK
Description
Timer 3 Overflow Flag
Set by hardware when Timer 3 overflows from
FFFFH to 0000H. When the Timer 3 interrupt is
enabled (EIE2.0, see Chapter 8), setting this bit
causes the CPU vectors to the Timer 3 ISR. This bit
is not automatically cleared by hardware and must
be cleared by software.
Read=0000b, Write=dont care
Timer 3 Run Control
0: Timer 3 disabled
1: Timer 3 enabled
Timer 3 Clock Select
0: Counter/Timer 3 uses the system clock divided
by 12.
1: Counter/Timer 3 uses the system clock.
Timer 3 External Clock Select
0: Timer 3 clock source defined by bit T3M
(TMR3CN.1)
1: Timer 3 clock source is the external oscillator
input divided by 8. T3M is ignored.
Table 8.9 TMR3CN: Timer 3 Control Register
176
RCLK1
TCLK1
CP/RL4
TR4
Mode
16 Bit Counter/Timer with
Capture
16 Bit Counter/Timer with
Auto-reload
Baud Rate Generator for
UART1
Baud Rate Generator for
UART1
Baud Rate Generator for
UART1
Off
Bit Symbol
TF4
EXF4
RCLK1
TCLK1
EXEN4
TR4
C/T4
CP/RL4
177
Description
Timer 4 Overflow Flag
Set by hardware when Timer 4 overflows. When the Timer
4 interrupt is enabled (EIE2.2, see Chapter 8), setting this
bit causes the CPU vectors to the Timer 4 ISR. This bit is
not automatically cleared by hardware and must be cleared
by software. TF4 will not be set when RCLK1 and/orTCLK1
are logic 1.
Timer 4 External Flag
Set by hardware when either a capture or reload is caused
by a high-to-low transition on the T4EX input pin and
EXEN4 is logic 1. When the Timer 4 interrupt is enabled,
setting this bit causes the CPU to vector to the Timer 4 ISR.
This bit is not automatically cleared by hardware and must
be cleared by software.
Receive Clock Flag for UART1
0: Timer 1 overflows used for receive clock
1: Timer 4 overflows used for receive clock
Transmit Clock Flag for UART1
0: Timer 1 overflows used for transmit clock
1: Timer 4 overflows used for transmit clock
Timer 4 External Enable
Enables high-to-low transitions on T4EX to trigger captures
or reloads when Timer 4 is not operating in Baud Rate
Generator mode.
0: High-to-low transitions on T4EX ignored.
1: High-to-low transitions on T4EX cause a capture or
reload.
Timer 4 Run Control
0: Timer 4 disabled
1: Timer 4 enabled
Counter/Timer Select
0: Timer Function: Timer 4 incremented by clock defined by
T4M bit (CKCON.6).
1: Counter Function: Timer 4 incremented by high-to-low
transition on external input pin (T4).
Capture/Reload Select
EXEN4 must be logic 1 for high-to-low transitions on T4EX
to be recognized and used to trigger captures or reloads. If
RCLK1 or TCLK1 is set, this bit is ignored and Timer 4 will
function in auto-reload mode.
0: Auto-reload on Timer 4 overflow or high-to-low transition
at T4EX (EXEN4=1)
1: Capture on high-to-low transition at T4EX (EXEN4=1)
Table 8.11 T4CON: Timer 4 Control Register
178
void main(void)
{
unsigned char blink_speed = 10;
EA = 0;
WDTCN = 0xDE;
WDTCN = 0xAD;
Init_Port();
LED = 0;
Init_Timer2(SYSCLK/12/blink_speed);
//-- Initialize Timer3 to generate interrupts
EA = 1;
while(1)
{
}
//-- go on forever
In the main() function, the Watchdog timer is disabled, the Crossbar and
I/O ports are configured and the Timer 2 is initialized. The endless while
loop makes the program go on forever.
179
IE |= 0x20;
T2CON |= 0x04;
The above code segment initializes the Timer 2 in the desired mode of
operation.
180
The above code segment will be executed each time the Timer 2
overflows.
The definition of LED is given below. The LED is connected to pin 6 of
Port 0.
sbit LED = P1^6;
181
2.
3.
4.
5.
6.
7.
8.
9
ADC and DAC
9.0
Introduction
185
9.1
186
9.2
188
9.3
Programming ADC0
189
9.4
ADC0 SFRs
195
9.5
199
9.6
201
9.7
Programming ADC1
201
9.8
ADC1 SFRs
206
9.9
208
9.10
210
9.11
DAC0 SFRs
212
184
9.12
DAC1 SFRs
213
9.13
Tutorial Questions
214
9.0
185
Introduction
A fully integrated control system with both analog and digital capabilities
is the emerging trend in the technological industry. There is currently a
demand for sophisticated control systems with high-speed precision
digital and analog performance. Signals in the real world are analog and
have to be digitized for processing.
This chapter will introduce the analog and digital peripherals of the
C8051F020. The chip contains one 8 bit and one 12 bit analog-to-digital
converters (ADCs) and two 12 bit digital-to-analog converters (DACs).
The C8051F020 also contains programmable gain amplifiers (PGAs),
analog multiplexer (8 channel and 9 channel), two comparators, a
precision voltage reference, and a temperature sensor to support analog
applications. These are shown in Figure 9.1. The following sections will
discuss the operation of these peripherals, as well as the SFRs used to
configure them. A voltage reference has to be used when operating the
ADC and DAC. Please refer to Section 2.8 for more details on setting the
Voltage Reference.
VDD
VDD
VDD
DGND
DGND
DGND
Digital Power
AV+
AV+
AGND
AGND
Analog Power
TCK
TMS
TDI
TDO
JTAG
Logic
Port I/O
Config.
8
0
5
1
Boundary Scan
Debug HW
Reset
/RST
MONEN
VDD
Monitor
XTAL1
XTAL2
External
Oscillator
Circuit
C
o
r
e
WDT
System
Clock
Internal
Oscillator
UART0
UART1
SPI Bus
PCA
SFR Bus
64kbyte
FLASH
256 byte
RAM
Timers 0,
1, 2, 4
Timer 3/
RTC
P0, P1,
P2, P3
Latches
DAC1
DAC1
(12-Bit)
DAC0
DAC0
(12-Bit)
4kbyte
RAM
CP0+
CP0CP1+
CP1-
A
M
U
X
Prog
Gain
TEMP
SENSOR
CP0
Prog
Gain
P1.0/AIN1.0
P2
Drv
P2.0
P3
Drv
P3.0
P1.7/AIN1.7
P2.7
P3.7
A
M
U
X
8:1
P4.0
Bus Control
ADC
100ksps
(12-Bit)
P1
Drv
P0.7
VREF1
ADC
500ksps
(8-Bit)
C
T
L
VREF0
AIN0.0
AIN0.1
AIN0.2
AIN0.3
AIN0.4
AIN0.5
AIN0.6
AIN0.7
P0.0
Crossbar
Config.
VREF
VREF
VREFD
C
R
O
S
S
B
A
R
SMBus
P0
Drv
A
d
d
r
Address Bus
Data Bus
D
a
t
a
CP1
P4 Latch
P4
DRV
P5 Latch
P5
DRV
P5.0/A8
P6 Latch
P6
DRV
P6.0/A0
P7
DRV
P7.0/D0
P7 Latch
P4.4
P4.5/ALE
P4.6/RD
P4.7/WR
P5.7/A15
P6.7/A7
P7.7/D7
186
ADC0LTH
ADC0LTL
24
AIN2
AIN3
AIN5
9-to-1
AMUX
+
(SE or
- DIFF)
AIN6
AIN7
AD0EN
AV+
12-Bit
SAR
+
AGND
ADC
AD0CM
TEMP
SENSOR
AD0EN
AD0TM
AD0INT
AD0BUSY
AD0CM1
AD0CM0
AD0WINT
AD0LJST
AMX0SL
AD0SC4
AD0SC3
AD0SC2
AD0SC1
AD0SC0
AMP0GN2
AMP0GN1
AMP0GN0
AMX0AD3
AMX0AD2
AMX0AD1
AMX0AD0
AGND
AMX0CF
12
ADC0CF
ADC0CN
ADC0L
AIN4
ADC0H
AD0WINT
12
REF
AIN1
SYSCLK
AV+
AIN0
Comb.
Logic
00
Start Conversion 01
AD0BUSY (W)
Timer 3 Overflow
10
CNVSTR
11
Timer 2 Overflow
AD0CM
ADC0GTH
AIN67IC
AIN45IC
AIN23IC
AIN01IC
9.1
187
2)
Overflow of Timer 2
3)
Overflow of Timer 3
4)
188
9.2
Gain n
2
VREF
AIN0 AGND
(Volts)
4095
VREF
4096
VREF
2
2047
VREF
4096
0
ADC0H:ADC0L
(AD0LJST=0)
ADC0H:ADC0L
(AD0LJST=1)
0FFFH
FFF0H
0800H
8000H
07FFH
7FF0H
0000H
0000H
Example:
AIN0 and AIN1 are used as the inputs in differential mode
(AMX0CF=01H and AMXSL=00H). Gain is set to 1.
AIN0 AGND
(Volts)
2047
VREF
2048
VREF
2
1
VREF
2048
189
ADC0H:ADC0L
(AD0LJST=0)
ADC0H:ADC0L
(AD0LJST=1)
07FFH
7FF0H
0400H
4000H
0001H
0010H
0000H
0000H
FFFFH (-1d)
FFF0H
FC00H (-1024d)
C000H
F800H (-2048d)
8000H
1
2048
VREF
2
VREF
VREF
9.3
Programming ADC0
ADC0 can be programmed through the following sequence:
1)
2)
3)
4)
5)
The next multiplexer input channel (Step 3) can be selected in the ADC0
ISR after the current channel has been converted and the AD0INT bit
cleared. The newly selected channel will then be converted in the next
conversion cycle. When initiating conversions by setting AD0BUSY to 1,
the AD0INT bit (ADC0CN.5) may be polled to determine when a
conversion has completed. The recommended polling procedure is:
190
1.
Clear AD0INT to 0
2.
Set AD0BUSY to 1
3.
4.
Example:
;---------------------------------------------------; Vref setup:
; Enable internal bias generator and internal
; reference buffer, and select ADC0 reference from
; VREF0 pin.
;---------------------------------------------------MOV
REF0CN, #00000011b
MOV
MOV
MOV
ADC0CF, #10000000b;
;
;
AMX0CF, #00H
;
AMX0SL, #00H
;
ADC0CN, #10001101b;
;
;
;
;
;
SAR0 Conversion
clock=941 kHz approx,
Gain=1
8 single-ended inputs
Select AIN0 input
Enable ADC0,
Continuous Tracking
Mode, conversion
initiated on
Timer 2 overflow and
left justify data
191
#include <c8051f020.h>
//-------------------------------------------------------------// 16-bit SFR Definitions for C8051F020
//-------------------------------------------------------------sfr16 TMR3RL
= 0x92;
// Timer3 reload value
sfr16 TMR3
= 0x94;
// Timer3 counter
sfr16 ADC0
= 0xbe;
// ADC0 data
//-------------------------------------------------------------// Global CONSTANTS
//-------------------------------------------------------------#define SYSCLK 22118400 //-- External Crystal Oscillator @22MHz
sbit LED = P1^6;
unsigned int ADC0_reading;
//-- function prototypes --------------------------------------void Init_Clock(void); //-- initialize the clock to use external
//
crystal oscillator
void Init_Port(void); //-- Configures the Crossbar & GPIO ports
void Init_ADC0(void); //-- Initialize the ADC1
void Init_Timer3(unsigned int counts);
void Timer3_ISR(void); //-- ISR for Timer 3
//-------------------------------------------------------------void main(void)
{
EA = 0;
EA = 1;
while(1)
{
}
//-- go on forever
192
//-------------------------------------------------------------void Init_Clock(void)
{
OSCXCN = 0x67;
//-- 0110 0111b
//-- External Osc Freq Control Bits (XFCN2-0) set to 111
//
because crystal frequency > 6.7 MHz
//
Crystal Oscillator Mode (XOSCMD2-0) set to 110
//-- wait till XTLVLD pin is set
while ( !(OSCXCN & 0x80) );
OSCICN = 0x88;
//-//-- Bit 2 : Internal Osc.
//-- Bit 3 : Uses External
// (CLKSL = 1)
//-- Bit 7 : Missing Clock
1000 1000b
disabled (IOSCEN = 0)
Oscillator as System Clock
Detector Enabled (MSCLKE = 1)
}
//-------------------------------------------------------------void Init_Port(void) //-- Configures the Crossbar and GPIO ports
{
XBR1 = 0x00;
XBR2 = 0x40;
//-- Enable Crossbar and weak pull-ups
//
(globally)
P1MDOUT |= 0x40;
//-- Enable P1.6 (LED) as push//
pull output
}
//-------------------------------------------------------------//-- Configure Timer3 to auto-reload and generate an interrupt
//
at interval specified by <counts> using SYSCLK/12 as its
//
time base.
void Init_Timer3 (unsigned int counts)
{
TMR3CN = 0x00;
//-- Stop Timer3; Clear TF3;
//-- use SYSCLK/12 as timebase
TMR3RL = -counts;
TMR3
= 0xffff;
EIE2
|= 0x01;
TMR3CN |= 0x04;
}
//-//-//-//-//
193
//-------------------------------------------------------------void Init_ADC0(void)
{
REF0CN = 0x07; //--Enable internal bias generator and
// internal reference buffer
// Select ADC0 reference from VREF0 pin
// Internal Temperature Sensor ON
ADC0CF = 0x81; //--SAR0 conversion clock=1.3MHz
// approx., Gain=2
AMX0SL = 0x08; //-- Select Temp Sensor
ADC0CN = 0x84; //-- enable ADC0, Continuous Tracking
//
Mode Conversion initiated on Timer 3
//
overflow, ADC0 data is right
//
justified
}
//-------------------------------------------------------------//-- Interrupt Service Routine
void Timer3_ISR (void) interrupt 14
{
TMR3CN &= ~(0x80);
//-- clear TF3 flag
//-- wait for ADC0 conversion to be over
while ( (ADC0CN & 0x20) == 0); //-- poll for AD0INT-->1
ADC0_reading = ADC0;
//-- read ADC0 data
ADC0CN &= 0xDF;
//-- clear AD0INT
}
194
}
//-------------------------------------------------------------void ADC0_ISR(void) interrupt 15
{
AD0INT = 0;
//-- clear ADC0 conversion complete
//
interrupt flag
ADC0_reading = ADC0;
}
ADC0 SFRs
AMX0SL: AMUX0 Channel Selection Register
Bit
Symbol
3-0
AMX0AD3-0
7-4
Description
9.4
195
0000
0001
0010
0011
0100
0101
0110
0111
0000
AIN0
AIN1
AIN2
AIN3
AIN4
AIN5
AIN6
AIN7
0001
+(AIN0)
-(AIN1)
AIN2
AIN3
AIN4
AIN5
AIN6
AIN7
0010
AIN0
AIN4
AIN5
AIN6
AIN7
0011
+(AIN0)
-(AIN1)
AIN4
AIN5
AIN6
AIN7
0100
AIN0
AIN6
AIN7
0101
+(AIN0)
-(AIN1)
AIN6
AIN7
AIN6
AIN7
AIN6
AIN7
0110
AIN0
0111
+(AIN0)
-(AIN1)
1000
AIN0
1001
+(AIN0)
-(AIN1)
1010
AIN0
1011
+(AIN0)
-(AIN1)
1100
AIN0
1101
+(AIN0)
-(AIN1)
1110
AIN0
1111
+(AIN0)
-(AIN1)
AIN1
AIN1
AIN1
AIN1
AIN1
AIN1
AIN1
+(AIN2)
-(AIN3)
+(AIN2)
-(AIN3)
AIN2
AIN3
AIN2
AIN3
+(AIN2)
-(AIN3)
+(AIN2)
-(AIN3)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
AIN2
AIN3
AIN4
AIN5
AIN2
AIN3
AIN4
AIN5
AIN4
AIN5
AIN4
AIN5
+(AIN2)
-(AIN3)
+(AIN2)
-(AIN3)
AIN2
AIN3
AIN2
AIN3
+(AIN2)
-(AIN3)
+(AIN2)
-(AIN3)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
+(AIN4)
-(AIN5)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
+(AIN6)
-(AIN7)
1xxx
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
Temp
Sensor
196
AIN67IC
AIN45IC
AIN23IC
AIN01IC
Description
197
7-3
Symbol
AD0SC4-0
Description
ADC0 SAR0 Conversion Clock frequency Bits
SAR0 Conversion clock is derived from system
clock by the following equation, where AD0SC
refers to the 5 bit value in AD0SC4-0 and CLKSAR0
refers to the desired ADC0 SAR0 conversion
clock frequency.
AD0SC =
2-0
SYSCLK
1
CLK SAR 0
CLK SAR 0 =
SYSCLK
AD0SC + 1
198
Description
3-2
AD0CM1-0
AD0WINT
AD0LJST
If AD0TM=1:
00: Tracking starts with the write of 1 to AD0BUSY and
lasts for 3 SAR clocks, followed by conversion.
01: Tracking started by overflow of Timer 3 and last for 3
SAR clocks, followed by conversion.
10: ADC0 tracks only when CNVSTR input is 0, conversion
starts on rising CNVSTR edge.
11: Tracking started by overflow of Timer 2 and last for 3
SAR clocks, followed by conversion.
ADC0 Window Compare Interrupt Flag
NOTE: This bit must be cleared by software.
ADC0 Left Justify Select
0: Data in ADC0H:ADC0L registers are right justified.
1: Data in ADC0H:ADC0L registers are left justified.
199
The ADC0H and ADC0L registers are used to store the MSB and LSB of
the ADC0 data word respectively.
The ADC0GTH, ADC0GTL, ADC0LTH and ADC0LTL registers are the
ADC0 Greater-Than and Less-Than registers respectively. These
registers are used in the ADC0 Programmable Window Detector mode to
store the 16 bit limits for the ADC0 output. The Programmable Window
Detector mode is used to save code space and CPU bandwidth to
deliver faster system response times. An interrupt is generated when an
out-of-bound condition is detected.
AV+
AD1EN
AIN1.0 (P1.0)
AIN1.1 (P1.1)
8-Bit
SAR
+
-
AIN1.5 (P1.5)
ADC
AGND
AIN1.6 (P1.6)
AMX1SL
ADC1CF
Start Conversion
AD1EN
AD1TM
AD1INT
AD1BUSY
AD1CM2
AD1CM1
AD1CM0
AMP1GN1
AMP1GN0
AD1SC4
AD1SC3
AD1SC2
AD1SC1
AD1SC0
AD1CM
AIN1.7 (P1.7)
ADC1CN
000
Write to AD1BUSY
001
Timer 3 Overflow
010
CNVSTR
011
Timer 2 Overflow
1xx
Write to AD0BUSY
(synchronized with
ADC0)
AD1CM
AIN1.4 (P1.4)
8-to-1
AMUX
ADC1
AV+
AIN1.2 (P1.2)
AIN1.3 (P1.3)
REF
SYSCLK
AMX1AD2
AMX1AD1
AMX1AD0
9.5
200
2)
Overflow of Timer 2
3)
Overflow of Timer 3
4)
5)
9.6
201
ADC1Code = Vin
Gain
256
VREF
Example:
Suppose AIN1.0 is used as the analog input (AMX1SL=00H):
9.7
ADC1
FFH
80H
7FH
00H
Programming ADC1
ADC1 can be programmed through the following sequence:
1)
2)
3)
4)
5)
202
1.
Clear AD1INT to 0
2.
Set AD1BUSY to 1
3.
4.
Example:
;---------------------------------------------------; Vref setup:
; Enable internal bias generator and internal
; reference buffer, and select ADC1 reference from
; VREF1 pin.
;---------------------------------------------------MOV
REF0CN, #00000011b
MOV
MOV
P1MDIN, #11111110b;
;
ADC1CF, #10000001b;
;
;
AMX1SL, #00H
;
ADC1CN, #10000010b;
;
;
;
;
P1.0 configured as
analog input
SAR1 Conversion
clock=941 kHz approx,
Gain=1
Select AIN1.0 input
Enable ADC1,
Continuous Tracking
Mode and conversion
initiated on Timer 3
overflow.
203
EA = 1;
while(1)
{
}
//-- go on forever
204
10000111b
Enable 16 MHz Internal Oscillator
and use it as System Clock
Missing Clock Detector Enabled
}
//--------------------------------------------------------------
//-------------------------------------------------------------//
Configure Timer3 to auto-reload and generate an interrupt
//
at interval specified by <counts> using SYSCLK/12 as its
//
time base.
void Init_Timer3 (unsigned int counts)
{
TMR3CN = 0x00;
//-- Stop Timer3; Clear TF3;
//-- use SYSCLK/12 as timebase
TMR3RL = -counts;
TMR3
= 0xffff;
EIE2
|= 0x01;
TMR3CN |= 0x04;
//-//-//-//-//
}
//--------------------------------------------------------------
205
//-------------------------------------------------------------void Init_ADC1(void)
{
REF0CN = 0x03; //-- Enable internal bias generator and
//
internal reference buffer
//
Select ADC1 reference from VREF1 pin
ADC1CF = 0x81; //-- SAR1 conversion clock=941KHz
//
approx., Gain=1
AMX1SL = 0x00; //-- Select AIN1.0 input
ADC1CN = 0x82; //-- enable ADC1, Continuous Tracking
//
Mode, Conversion initiated on Timer
//
3 overflow
}
//--------------------------------------------------------------
206
9.8
ADC1 SFRs
AMX1SL: AMUX1 Channel Select Register
Bit
Symbol
3-0
AMX1AD2-0
7-3
Description
7-3
Symbol
AD1SC4-0
Description
ADC1 SAR Conversion Clock frequency Bits
SAR Conversion clock is derived from system
clock by the following equation, where AD1SC
refers to the 5 bit value in AD1SC4-0, and
CLKSAR1 refers to the desired ADC1 SAR
conversion clock frequency.
AD1SC =
2
1-0
SYSCLK
1
CLK SAR1
207
Symbol
Description
AD1EN
AD1TM
AD1INT
AD1BUSY
3-1
AD1CM2-0
208
Timer 2
Timer 4
DAC0H
DAC0MD1
DAC0MD0
DAC0DF2
DAC0DF1
DAC0DF0
REF
Dig. MUX
12
DAC0
DAC0
AGND
Timer 2
Latch
Timer 4
Timer 3
DAC0L
DAC1H
DAC1EN
Latch
AV+
DAC0H
DAC1MD1
DAC1MD0
DAC1DF2
DAC1DF1
DAC1DF0
REF
DAC1L
Dig. MUX
Latch
DAC1H
AV+
8
Latch
DAC0CN
DAC0EN
Timer 3
DAC1CN
9.9
12
DAC1
DAC1
AGND
209
Output Scheduling
The DACs have four modes of output scheduling:
1)
2)
Timer 2 Overflow
3)
Timer 3 Overflow
4)
Timer 4 Overflow
NOTE: x = 0 or 1
The Output on Demand mode is the default mode. In this mode, the DAC
output is updated when DACxH is written to.
Writes to DACxL are held and have no effect on the DACx output until
DACxH is written to. Therefore, to write a 12 bit data word at full
resolution to DACx, the write sequence should be DACxL, followed by
DACxH.
The DACs can be used in 8 bit mode by initializing DACxL to the desired
value (typically 00H) and writing data to only DACxH. See section on
Output Scaling.
In the Timer Overflow modes, the DAC outputs are updated by a timer
overflow independently of the processor. Writes to both DAC data
registers (DACxL and DACxH) are held until an associated timer
overflow event occurs. The DACxH:DACxL contents are then copied to
the DAC input latches, allowing the DAC output to change to the new
value.
Timer Overflow Modes are useful for scheduling outputs at periodic
intervals, e.g. waveform generation at a defined output frequency.
210
Output Scaling
The format of the 12 bit data word in the DACxH and DACxL registers
can be configured by setting the appropriate DACxDF bits
(DACxCN.[2:0]). The five data word orientations are shown in Figure 9.5.
DACxDF2-0 = 000:
DACxH
MSB
DACxL
LSB
001:
DACxH
MSB
DACxL
DACxH
MSB
DACxL
DACxH
DACxL
LSB
DACxH
DACxL
LSB
LSB
010:
LSB
011:
MSB
1xx:
MSB
Figure 9.5 DAC Data Format
2)
Load the data word registers with the desired 12 bit digital value
(DACxH and DACxL).
3)
4)
211
Example:
;---------------------------------------------------; Vref setup:
; Enable internal bias generator and internal
; reference buffer. Pins 1-2 of J22 on the C8051F020
; development board must be connected to use the
; internal voltage reference generated as input to
; VREFD
;---------------------------------------------------MOV
REF0CN, #00000011b
DAC0H, #0FFh
DAC0L, #0h
DAC0CN, #10010100b; Enable DAC0 in left
; justified mode and
; update on Timer4 overflow
MOV
MOV
MOV
212
6-5
4-3
2-0
Symbol
Description
DAC0H and DAC0L are used to store the most significant and least
significant DAC0 data word respectively.
213
6-5
4-3
2-0
Symbol
Description
DAC1H and DAC1L are used to store the most significant and least
significant DAC1 data word respectively.
214
2.
Which are the four extra SFRs that have to be configured when
using ADC0 in the Programmable Window Detector mode?
3.
What are the 4 possible events that can trigger ADC0 to start
conversion?
4.
What are the largest and smallest input voltage values recognized
before the output is clipped at the full scale range if ADC0 is
configured in single-ended input mode?
5.
What are the largest and smallest input voltage values recognized
before the output is clipped at the full scale range if ADC0 is
configured in differential input mode?
6.
Suppose ADC0 is configured such that all 8 inputs are singleended. Show the contents of AMX0CF and AMX0SL if we want to
convert channel 5.
7.
8.
Which are the 5 possible events that can trigger ADC0 to start
conversion?
9.
What is ADC1s operational mode and what are the largest and
smallest input voltage values recognized before the output is
clipped at the full scale range?
10.
11.
What are the 4 possible events that can be used to schedule the
DACs output?
12.
What is the DAC0 and DAC1 full scale output voltage swing?
13.
What are the 5 data word formats that can be used with the
DACs?
10
Serial Communication
10.0
Introduction
216
10.1
217
10.2
219
10.3
Operation Modes
220
10.4
Interrupt Flags
225
10.5
UARTx SFRs
227
10.6
10.7
229
Tutorial Questions
233
216
10.0 Introduction
With serial communication, data is transferred one bit at a time. An
interface device converts the CPUs parallel data and transmits it across
a single link to another device. This data has to be reconstructed again
before it can be understood by the device.
There are 2 types of serial communication asynchronous and
synchronous.
With asynchronous communication, the transmitter and receiver do not
share a common clock. The transmitter shifts the data onto the serial line
using its own clock. The transmitter also adds the start, stop and parity
check bits as shown in Figure 10.1. The receiver will extract the data
using its own clock and convert the serial data back to the parallel form
after stripping off the start, stop and parity bits.
Start Bit
D0
Parity Bit
D1
D2
D3
D4
D5
D6
1 or 2 Stop Bits
D7
1 Asynchronous Byte
Sync
Char
Sync
Char
Header
Byte
Header
Byte
Data
Data
Char 1 Char 2
217
Data
Check
Char n Byte
Check
Byte
Sync
Char
Sync
Char
218
CPU
Tx
TTL to
RS-232C
Level Shifter
UART
Rx
Parallel
Data
Start bit
Stop bits
(b)
Start bit
Parity bit
Stop bits
+12V
5V
0V
Rx
(a)
Parity bit
External
Service
Device
LSB
MSB
Data = 49H
-12V
LSB
MSB
Data = 49H
buffer while a second is being received. If the CPU reads the first
character before the second has been fully received, data are not lost. A
Receive Overrun bit indicates when new received data is latched into the
receiver buffer before the previous received byte is read.
NOTE: x = 0 or 1. The two UARTs are functionally identical.
The UART block diagram is shown in Figure 10.4. Each UART is
accessed by two SFRs, SBUFx and SCONx. The Serial Port Buffer
(SBUFx) is essentially two buffers - writing loads data to be transmitted
and reading accesses received data. These are two separate and
distinct buffers (registers): the transmit write-only buffer and the receive
read-only register.
The Serial Port Control register (SCONx) contains status and control
bits. The control bits set the operating mode for the serial port, and
status bits indicate the end of the character transmission or reception.
The status bits are tested in software (polling) or programmed to cause
an interrupt.
The serial port frequency of operation, or baud rate, can be fixed
(derived from the on-chip oscillator) or variable. If a variable baud rate is
used, Timer 1 supplies the baud rate clock and must be programmed
accordingly.
In the following sections we will discuss the operational modes of the
UARTs and the steps required to configure them for use.
219
SFR Bus
Write to
SBUF
TB8
SBUF
(Transmit Shift)
SET
Q
CLR
TX
Crossbar
Zero Detector
Stop Bit
Gen.
Shift
Data
Tx Control
Start
Tx Clock
Send
Tx IRQ
SCON
UART
Baud Rate
Generation
Logic
TI
S S S R T R T R
M M M E B B I I
0 1 2 N 8 8
EN
Rx Clock
RI
Rx IRQ
Rx Control
Start
Shift
Frame Error
Detection
Serial Port
(UART0/1)
Interrupt
0x1FF
Load
SBUF
Address
Match
Port I/O
Load SBUF
RB8
SBUF
(Receive Latch)
Match Detect
SADDR
SADEN
Read
SBUF
SFR Bus
RX
Crossbar
2)
3)
4)
220
XBR0,#00000100b
XBR2,#01000000b
CKCON,#00010000b
MOV
TMOD,#20h
MOV
SETB
ORL
TH1, #F4h
TR1
PCON,#80h
MOV
SCON0,#01010000b
TXx
Shift Register
C8051F020
RXx
Data
8 Extra Outputs
221
Eight data bits are transmitted or received on the RXx pin with the LSB
first, and the TIx, Transmit Interrupt Flag (SCONx.1) is set at the end of
the 8th bit time. The baud rate is fixed at
SYSCLK
12
Data transmission begins when an instruction writes a data byte to the
SBUFx register. Data are shifted out on RXx with clock pulses sent out
TXx (Figure 10.6). Each transmitted bit is valid on the RXx pin for 1
machine cycle.
Data reception begins when the RENx Receive Enable bit (SCONx.4) is
set to 1 and the RIx Receive Interrupt Flag (SCONx.0) is cleared. One
cycle after the eighth bit is shifted in, the RIx flag is set and reception
stops until software clears the RIx bit. An interrupt will occur if enabled
when either TIx or RIx are set.
The general rule is to set RENx at the beginning of a program to initialize
the UART and then clear RIx to begin a data input operation. When RIx
is cleared, clock pulses are written out to TXx, beginning the next
machine cycle and data are clocked in from RXx.
RXx is used for both data input and output and TXx serves as the clock.
The clocking of data into the UART occurs on the rising edge of TXx.
Mode 0 Transmit
RXx (Data out)
D0
D1
D2
D3
D4
D5
D6
D7
D4
D5
D6
D7
Mode 0 Receive
RXx (Data in)
D0
D1
D2
D3
222
32
256
TH
1
BaudRate =
223
SYSCLK
for Timer 2 or 4
32 (65536 [RCAPzH , RCAPzL ])
Start
Bit
D0
D1
D2
D3
D4
D5
D6
D7
Stop
Bit
Bit Times
Bit Sampling
If a valid start bit was detected, character reception continues. The start
bit is skipped, the 8 data bits are stored in SBUFx, the stop bit is stored
in RB8x and the RIx flag is set. However, these only occur if the following
conditions exist:
1. RIx = 0
2. SM2x = 0 (stop bit ignored) or SM2x = 1 and the received stop bit = 1
If these conditions are not met, SBUFx and RB8x will not be loaded and
the RIx flag will not be set. An interrupt will occur if enabled when either
TIx or RIx is set. The requirement that RIx = 0 ensures that software has
read the previous character (and cleared RIx).
224
C,P
TB80, C
SBUF0,A
SYSCLK
64
BaudRate = 2SMODx
Mark
Space
Start
Bit
D0
D1
D2
D3
D4
D5
D6
D7
Bit Times
Bit Sampling
D8
Stop
Bit
225
RI0,WAIT
RI0
A,SBUF0
226
TI0,WAIT
TI0
SBUF0,A
The receive and transmit instruction sequences above are usually part of
standard input character and output character subroutines. The following
example illustrates a subroutine called OUTCHR which transmits the 7bit ASCII code in the accumulator out UART0, with odd parity as the 8th
bit.
Example:
OUTCHR:
AGAIN:
again
MOV
CPL
MOV
JNB
C,P
; Put parity bit in C flag
C
; Change to odd parity
ACC.7,C
; Add to character code
TI0,AGAIN ;TX empty? No: check
CLR
MOV
CLR
TI0
SBUF0,A
ACC.7
RET
;
Yes: clear flag
; and send
; Strip off parity bit
A,#Z
OUTCHR
227
SM0xSM1x
SM2x
RENx
TB8x
RB8x
TIx
RIx
Description
Serial Port Operation Mode
00: Mode 0: Shift Register Mode
01: Mode 1: 8 Bit UART, Variable Baud Rate
10: Mode 2: 9 Bit UART, Fixed Baud Rate
11: Mode 3: 9 Bit UART, Variable Baud Rate
Multiprocessor Communication Enable
The function of this bit depends on the Serial Port Operation
Mode.
Mode 0: No effect.
Mode 1: Checks for valid stop bit.
0: Logic level of stop bit is ignored.
1: RIx will only be activated if stop bit is 1
Mode 2 & 3: Multiprocessor Communications Enable.
0: Logic level of 9th bit is ignored.
1: RIx is set and an interrupt is generated only when
the 9th bit is 1 and the received address matches
the UARTx address or broadcast address.
Receive Enable
0: UARTx reception disabled
1: UARTx reception enabled
9th Transmission Bit
The logic level of this bit will be assigned to the 9th transmission
bit in Modes 2 & 3. It is not used in Modes 0 & 1.
Set or cleared by software as required.
9th Receive Bit
This bit is assigned the logic level of the 9th bit received in
Modes 2 & 3. In Mode 1, if SM2x is 0, RB8x is assigned the
logic level of the received stop bit. RB8 is not used in Mode 0.
Transmit Interrupt Flag
Set by hardware when a byte of data has been transmitted by
UARTx (after the 8th bit in Mode 0, or at the beginning of the
stop bits in other modes). When the UARTx interrupt is enabled,
setting this bit causes the CPU to vector to the UARTx ISR. This
bit must be cleared manually by software.
Receive Interrupt Flag
Set by hardware when a byte of data has been received by
UARTx (as selected by the SM2x bit). When the UARTx
interrupt is enabled, setting this bit causes the CPU to vector to
the UARTx ISR. This bit must be cleared manually by software.
Table 10.1 SCONx: UARTx Control Register
228
The other registers associated with the UARTs are SBUFx, SADDRx and
SADENx. SBUFx accesses 2 registers, a transmit shift register and a
receive latch register. When data is written to SBUFx, it goes to the
transmit shift register and is held for serial transmission. Writing a byte to
SBUFx initiates transmission. A read of SBUFx returns the contents of
the receive latch. SADDRx and SADENx deal with slave addresses and
will not be discussed here.
Symbol
SMOD0
6
5
SSTAT0
Reserved
SMOD1
3
2
SSTAT1
Reserved
STOP
IDLE
Description
UART0 Baud Rate Doubler Enable
0: UART0 baud rate divide-by-two enabled.
1: UART0 baud rate divide-by-two disabled.
UART0 Enhanced Status Mode Select
Read is undefined. Must write 0.
UART1 Baud Rate Doubler Enable
0: UART1 baud rate divide-by-two enabled.
1: UART1 baud rate divide-by-two disabled.
UART1 Enhanced Status Mode Select
Read is undefined. Must write 0.
STOP Mode Select
This bit will always read 0. Writing a 1 will place
the microcontroller into STOP mode. (Turns off
oscillator).
IDLE Mode Select
This bit will always read 0. Writing a 1 will place
the microcontroller into IDLE mode. (Shuts off
clock to CPU, but clock to Timers, Interrupts, and
all peripherals remain active).
Table 10.2 PCON: Power Control Register
229
The user may click on the desired radio button and click the Send button
to send the command to the target board. The command sent is a one
byte data 0x01 for slow, 0x02 for medium and 0x03 for fast blinking
speed respectively. The serial communication is at a baud rate of
115200. The baud rate is generated using Timer 1. Each time a new
command is received by the program running on C8051F020, the
blinking speed of the LED is altered accordingly. The program code is
given below:
230
//-//
//-//-//-//--
#include <c8051f020.h>
//-------------------------------------------------------------// 16-bit SFR Definitions for 'F02x
//-------------------------------------------------------------sfr16 TMR3RL
= 0x92;
// Timer3 reload value
sfr16 TMR3
= 0x94;
// Timer3 counter
//-------------------------------------------------------------// Global CONSTANTS
//-------------------------------------------------------------#define BLINKCLK 2000000
sbit LED = P1^6;
unsigned char LED_count;
unsigned char blink_speed;
char received_byte;
unsigned short new_cmd_received;
//-- function prototypes --------------------------------------void Init_Clock(void); //-- initialize the clock to use external
//
crystal oscillator
void Init_Port(void); //-- Configures the Crossbar and GPIO
//
ports
void Init_UART0(void); //-- configure and initialize the UART0
//
serial comm
void Init_Timer3(unsigned int counts);
void Timer3_ISR(void);
//-- ISR for Timer 3
void UART0_ISR(void);
//-- ISR for UART0
//-------------------------------------------------------------void Init_Clock(void)
{
OSCXCN = 0x67;
//-- 0110 0111b
//-- External Osc Freq Control Bits (XFCN2-0) set to 111
//
because crystal frequency > 6.7 MHz
//-- Crystal Oscillator Mode (XOSCMD2-0) set to 110
//-- wait till XTLVLD pin is set
while ( !(OSCXCN & 0x80) );
OSCICN = 0x88;
//-//-- Bit 2 : Internal Osc.
//-- Bit 3 : Uses External
//
(CLKSL = 1)
//-- Bit 7 : Missing Clock
}
1000 1000b
disabled (IOSCEN = 0)
Oscillator as System Clock
Detector Enabled (MSCLKE = 1)
void Init_Port(void)
{
XBR0 = 0x04;
XBR1 = 0x00;
XBR2 = 0x40;
231
}
//-------------------------------------------------------------void Init_UART0(void)
{
//-- set up Timer 1 to generate the baude rate (115200)
//
for UART0
CKCON |= 0x10; //-- T1M=1; Timer 1 uses the system clock
//
22.11845 MHz
TMOD = 0x20;
//-- Timer 1 in Mode 2 (8-bit auto//
reload)
TH1 = 0xF4;
//-- Baud rate = 115200
TR1 = 1;
//-- start Timer 1 (TCON.6 = 1)
T2CON &= 0xCF; //-- Timer 1 overflows used for receive &
//
transmit clock (RCLK0=0, TCLK0=0)
//-- Set up the UART0
PCON |= 0x80; //-- SMOD0=1 (UART0 baud rate divide-by-2
//
disabled)
SCON0 = 0x50; //-- UART0 Mode 1, Logic level of stop
//
bit ignored and Receive enabled
//-- enable UART0 interrupt
IE |= 0x10;
IP |= 0x10;
//-- set to high priority level
RI0= 0;
}
//-------------------------------------------------------------//-- Configure Timer3 to auto-reload and generate an interrupt
//
at interval specified by <counts> using SYSCLK/12 as its
// time base.
void Init_Timer3 (unsigned int counts)
{
TMR3CN = 0x00;
//-- Stop Timer3; Clear TF3;
//-- use SYSCLK/12 as time base
TMR3RL = -counts;
TMR3
= 0xffff;
EIE2
|= 0x01;
TMR3CN |= 0x04;
}
//-//-//-//-//
232
//-- This routine changes the state of the LED whenever Timer3
//
overflows.
void Timer3_ISR (void) interrupt 14
{
TMR3CN &= ~(0x80);
//-- clear TF3
LED_count++;
if ( (LED_count % 10) == 0)
//-- do every 10th count
{
LED = ~LED;
//-- change state of LED
LED_count = 0;
}
}
//-------------------------------------------------------------void UART0_ISR(void) interrupt 4
{
//-- pending flags RI0 (SCON0.0) and TI0(SCON0.1)
if ( RI0 == 1) //-- interrupt caused by received byte
{
received_byte = SBUF0; //-- read the input buffer
RI0 = 0;
//-- clear the flag
new_cmd_received=1;
}
}
//-------------------------------------------------------------void main(void)
{
blink_speed = 10;
received_byte = 2;
new_cmd_received = 0; LED_count = 0;
LED = 0;
EA = 0;
//-- disable global interrupts
WDTCN = 0xDE; //-- disable watchdog timer
WDTCN = 0xAD;
Init_Clock(); Init_Port();
Init_Timer3(BLINKCLK/12/blink_speed); Init_UART0();
EA = 1;
//-- enable global interrupts
while(1)
//-- go on forever
{
if (new_cmd_received == 1)
{
switch (received_byte)
{
case 1
: blink_speed = 1; break; // slow
case 2
: blink_speed = 10; break; // medium
case 3
: blink_speed = 50; break; // fast
default : blink_speed = 10; break;
}
EA = 0;
Init_Timer3(BLINKCLK/12/blink_speed);
EA = 1; //-- enable interrupts
new_cmd_received = 0;
}
}
}
233
2.
What are the different modes of operation for UART0 and UART1?
3.
4.
5.
Sketch the timing diagram for the 7 bit ASCII coded character Z
with even parity and 1 stop bit as it is transmitted out of UART0.
a)
b)
11
Interrupts
11.0
Introduction
236
11.1
Interrupt Organization
236
11.2
Interrupt Process
239
11.3
Interrupt Vectors
239
11.4
External Interrupts
240
11.5
Interrupt Latency
241
11.6
Interrupt SFRs
241
11.7
Tutorial Questions
249
236
Chapter 11 Interrupts
11.0 Introduction
An interrupt is an event or an occurrence of a condition which causes a
temporary suspension of the current program. Control is then passed to
another special software sub-routine, called the Interrupt Service Routine
(ISR). The ISR handles and executes the operations that must be
undertaken in response to the interrupt. Once it finishes its task, it will
return control back to the original program, which resumes from where it
was left off.
Sometimes multiple interrupts may happen at the same time. In
situations like this, the CPU needs to decide which interrupt should be
serviced first. This is usually done according to a pre-determined
sequence and importance of the interrupt. This is termed as interrupt
service priority.
Interrupt Handler
Each interrupt source can be individually enabled or disabled through the
use of associated interrupt enable bit in the SFRs IE, EIE1 and EIE2
(Tables 11.2, 11.4, and 11.5). However one must set the global
enable/disable bit, EA (IE.7), to logic 1 before any individual interrupt is
enabled. If the EA bit is 0, none of the interrupt sources will be
recognized by the CPU regardless of their interrupt enable settings.
Chapter 11 Interrupts
237
Example:
SETB EA
ORL EIE2,#4H
Programmable
Counter Array
Comparator 0
Falling Edge
Comparator 0
Rising Edge
Comparator 1
Falling Edge
Comparator 1
Rising Edge
Timer 3
Overflow
ADC0 End of
Conversion
Timer 4
Overflow
ADC1 End of
Conversion
External
Interrupt 6
External
Interrupt 7
Priority
Control
Timer 2
Overflow
Serial
Peripheral
Interface
SMBus
Interface
ADC0 Window
Comparator
Enable
Flag
UART0
Pending
Flag
External
Interrupt 0
(/INT0)
Timer 0
Overflow
External
Interrupt 1
(/INT1)
Timer 1
Overflow
Priority
Order
Reset
Interrupt
Vector
Chapter 11 Interrupts
Interrupt
Source
238
0000
Top
None
Always Enabled
Always
Highest
0003
IE0 (TCON.1)
EX0 (IE.0)
PX0 (IP.0)
000B
TF0 (TCON.5)
ET0 (IE.1)
PT0 (IP.1)
0013
IE1 (TCON.3)
EX1 (IE.2)
PX1 (IP.2)
001B
TF1 (TCON.7)
ET1 (IE.3)
PT1 (IP.3)
0023
RI0 (SCON0.0)
TI0 (SCON0.1)
ES0 (IE.4)
PS0 (IP.4)
002B
TF2 (T2CON.7)
ET2 (IE.5)
PT2 (IP.5)
0033
SPIF (SPI0CN.7)
ESPI0 (EIE1.0)
PSPI0
(EIP1.0)
003B
SI (SMB0CN.3)
0043
ESMB0
(EIE1.1)
EWADC0
(EIE1.2)
PSMB0
(EIP1.1)
PWADC0
(EIP1.2)
004B
EPCA0 (EIE1.3)
PPCA0
(EIP1.3)
0053
10
005B
11
0063
12
006B
13
0073
14
007B
0083
AD0WINT
(ADC0CN.2)
CF (PCA0CN.7)
CCFn
(PCA0CN.n)
CP0FIF
(CPT0CN.4)
CP0RIF
(CPT0CN.5)
CP1FIF
(CPT1CN.4)
CP1RIF
(CPT1CN.5)
ECP1R
(EIE1.7)
PCP0F
(EIP1.2)
PCP0R
(EIP1.5)
PCP1F
(EIP1.6)
PCP1F
(EIP1.7)
TF3 (TMR3CN.7)
ET3 (EIE2.0)
PT3 (EIP2.0)
15
AD0INT
(ADC0CN.5)
EADC0
(EIE2.1)
PADC0
(EIP2.1)
16
TF4 (T4CON.7)
ET4 (EIE2.2)
PT4 (EIP2.2)
008B
17
AD1INT
(ADC1CN.5)
EADC1
(EIE2.3)
PADC1
(EIP2.3)
0093
18
IE6 (PRT3IF.5)
EX6 (EIE2.4)
PX6 (EIP2.4)
009B
19
IE7 (PRT3IF.6)
EX7 (EIE2.5)
PX7 (EIP2.5)
ES1 (EIE2.6)
PS1 (EIP2.6)
EXVLD (EIE2.7)
PXVLD
(EIP2.7)
UART1
00A3
20
External
Crystal OSC
Ready
RI1 (SCON1.0)
TI1 (SCON1.1)
00AB
21
XTLVLD
(OSCXCN.7)
ECP0F (EIE1.4)
ECP0R
(EIE1.5)
ECP1F (EIE1.6)
Chapter 11 Interrupts
239
b)
c)
d)
The execution of ISR proceeds until the RETI (Return from Interrupt)
instruction is encountered. The RETI instruction informs the CPU that the
interrupt subroutine has finished. The top two bytes from the stack are
then popped and loaded into the Program Counter (PC). The CPU will
continue executing the instruction from this PC address, which is the
place where the execution of the main program was left off in order to
invoke the ISR. Some interrupt pending flags are automatically cleared
by the hardware when the CPU vectors to the ISR. This has an
advantage of preventing a further interrupt within an interrupt. However,
if the interrupt flag is not cleared by the hardware, then it is the
programmers responsibility to clear it, using some software means, upon
entering the ISR. If an interrupt pending flag remains set after the CPU
completes the RETI instruction, a new interrupt request will be generated
immediately and the CPU will re-enter the ISR after the completion of the
next instruction.
240
Chapter 11 Interrupts
Example:
CSEG AT 0
LJMP MAIN
MYCODE
ORG
0083H
LJMP
TIMER4INT
SEGMENT
RSEG
USING
MAIN:
.
.
.
TIMER4INT:
.
.
.
RETI
; vector address of
; timer 4
CODE
MYCODE ; switch to this code
; segment
0
; use register bank 0
; main program entry point
Chapter 11 Interrupts
241
must then deactivate the interrupt request before execution of the ISR
completes otherwise another interrupt request will be generated.
The other 2 external interrupts (External Interrupts 6 & 7) are edgesensitive inputs and can be configured to trigger on a positive or negative
edge. The interrupt-pending flags and configuration bits for these
interrupts are in the Port 3 Interrupt Flag Register (Table 11.8).
242
Chapter 11 Interrupts
Symbol
EA
IEGF0
ET2
ES0
ET1
EX1
ET0
EX0
Description
Enable All Interrupts
0: Disable all interrupt sources.
1: Enable each interrupt according to its
individual mask setting.
General Purpose Flag 0
This is a general purpose flag for use under
software control.
Enable Timer 2 Interrupt
0: Disable Timer 2 Interrupt.
1: Enable interrupt requests generated by TF2
(T2CON.7).
Enable UART0 Interrupt
0: Disable UART0 Interrupt.
1: Enable UART0 Interrupt.
Enable Timer 1 Interrupt
0: Disable Timer 1 Interrupt.
1: Enable interrupt requests generated by TF1
(TCON.7).
Enable External Interrupt 1
0: Disable external interrupt 1.
1: Enable interrupt request generated by the
/INT1 pin.
Enable Timer 0 Interrupt
0: Disable Timer 0 Interrupt.
1: Enable interrupt requests generated by TF0
(TCON.5).
Enable External Interrupt 0
0: Disable external interrupt 0.
1: Enable interrupt request generated by the
/INT0 pin.
Table 11.2 IE (Interrupt Enable)
Chapter 11 Interrupts
243
Symbol
-
PT2
PS0
PT1
PX1
PT0
PX0
Description
244
Chapter 11 Interrupts
Symbol
Description
Chapter 11 Interrupts
245
Symbol
EXVLD
ES1
EX7
EX6
EADC1
ET4
EADC0
ET3
Description
Enable External Clock Source Valid (XTLVLD)
Interrupt
0: Disable XTLVLD interrupt.
1: Enable interrupt requests generated by XTLVLD
(OXCXCN.7)
Enable UART1 Interrupt
0: Disable UART1 Interrupt.
1: Enable UART1 Interrupt.
Enable External Interrupt 7
0: Disable external interrupt 7.
1: Enable interrupt request generated by the
External Interrupt 7 input pin.
Enable External Interrupt 6
0: Disable external interrupt 6.
1: Enable interrupt request generated by the
External Interrupt 6 input pin.
Enable ADC1 End of Conversion Interrupt
0: Disable ADC1 End of Conversion interrupt.
1: Enable interrupt requests generated by the
ADC1 End of Conversion Interrupt.
Enable Timer 4 Interrupt
0: Disable Timer 4 Interrupt.
1: Enable interrupt requests generated by TF4
(T4CON.7).
Enable ADC0 End of Conversion Interrupt
0: Disable ADC0 End of Conversion interrupt.
1: Enable interrupt requests generated by the
ADC0 End of Conversion Interrupt.
Enable Timer 3 Interrupt
0: Disable Timer 3 Interrupt.
1: Enable interrupt requests generated by TF3
(TMR3CN.7).
246
Chapter 11 Interrupts
Symbol
PCP1R
PCP1F
PCP0R
PCP0F
PPCA0
PWADC0
PSMB0
PSPI0
Description
Comparator1 (CP1) Rising Interrupt Priority
Control
0: CP1 Rising interrupt set to low priority level.
1: CP1 Rising interrupt set to high priority level.
Comparator1 (CP1) Falling Interrupt Priority
Control
0: CP1 Falling interrupt set to low priority level.
1: CP1 Falling interrupt set to high priority level.
Comparator0 (CP0) Rising Interrupt Priority
Control
0: CP0 Rising interrupt set to low priority level.
1: CP0 Rising interrupt set to high priority level.
Comparator0 (CP0) Falling Interrupt Priority
Control
0: CP0 Falling interrupt set to low priority level.
1: CP0 Falling interrupt set to high priority level.
Programmable Counter Array (PCA0) Interrupt
Priority Control
0: PCA0 interrupt set to low priority level.
1: PCA0 interrupt set to high priority level.
ADC0 Window Comparator Interrupt Priority
Control
0: ADC0 Window interrupt set to low priority level.
1: ADC0 Window interrupt set to high priority level.
System Management Bus (SMBus0) Interrupt
Priority Control
0: SMBus interrupt set to low priority level.
1: SMBus interrupt set to high priority level.
Serial Peripheral Interface (SPI0) Interrupt
Priority Control
0: SPI0 interrupt set to low priority level.
1: SPI0 interrupt set to high priority level.
Chapter 11 Interrupts
247
Symbol
PXVLD
EP1
PX7
PX6
PADC1
PT4
PADC0
PT3
Description
External Clock Source Valid (XTLVLD) Interrupt
Priority Control
0: XTLVLD interrupt set to low priority level.
1: XTLVLD interrupt set to high priority level.
UART1 Interrupt Priority Control
0: UART1 interrupt set to low priority level.
1: UART1 interrupt set to high priority level.
External Interrupt 7 Priority Control
0: External Interrupt 7 set to low priority level.
1: External Interrupt 7 set to high priority level.
External Interrupt 6 Priority Control
0: External Interrupt 6 set to low priority level.
1: External Interrupt 6 set to high priority level.
ADC1 End of Conversion Interrupt Priority
Control
0: ADC1 End of Conversion interrupt set to low
priority level.
1: ADC1 End of Conversion interrupt set to high
priority level.
Timer 4 Interrupt Priority Control
0: Timer 4 interrupt set to low priority level.
1: Timer 4 interrupt set to high priority level.
ADC0 End of Conversion Interrupt Priority
Control
0: ADC0 End of Conversion interrupt set to low
priority level.
1: ADC0 End of Conversion interrupt set to high
priority level.
Timer 3 Interrupt Priority Control
0: Timer 3 interrupt set to low priority level.
1: Timer 3 interrupt set to high priority level.
248
Chapter 11 Interrupts
Symbol
IE7
IE6
5-4
IE7CF
IE6CF
1-0
Description
External Interrupt 7 Pending Flag
0: No falling edge has been detected on P3.7
since this bit was last cleared.
1: This flag is set by hardware when a falling edge
on P3.7 is detected.
External Interrupt 6 Pending Flag
0: No falling edge has been detected on P3.6
since this bit was last cleared.
1: This flag is set by hardware when a falling edge
on P3.6 is detected.
UNUSED. Read = 00, Write = dont care
External Interrupt 7 Edge Configuration
0: External Interrupt 7 triggered by a falling edge
on the IE7 input.
1: External Interrupt 7 triggered by a rising edge
on the IE7 input.
External Interrupt 6 Edge Configuration
0: External Interrupt 6 triggered by a falling edge
on the IE6 input.
1: External Interrupt 6 triggered by a rising edge
on the IE6 input.
UNUSED. Read = 00, Write = dont care
Chapter 11 Interrupts
249
2.
3.
What is the next available memory where the user can write his or
her program without interfering with the interrupt vector? Give an
example of program code.
4.
5.
6.
Index
Absolute Addressing, 42
Absolute Segment, 79
AD0BUSY bit, 187
AD0INT, 193
ADC0 SFRs, 195
ADC0CF:
ADC0 Configuration Register, 197
ADC0CN:
ADC0 Control Register, 198
ADC1 SFRs, 206
ADC1CF:
ADC1 Configuration Register, 206
ADC1CN:
ADC1 Control Register, 207
Address Control, 72
Address Latch Enable (ALE), 3
Addressing Modes, 40
AMUX0 Channel Selection, 195
AMUX1, 200
AMX0CF:
AMUX0 Configuration Register, 196
AMX0SL:
AMUX0 Channel Selection Register, 195
AMX1SL:
AMUX1 Channel Select Register, 206
Analog Measurement using Interrupts, 194
Analog Multiplexer 0 (AMUX0), 186
Arithmetic Operations, 44
Asynchronous Serial Data Format, 216
Auxiliary Carry Flag (AC), 13
B register, 14
Baud Rate Generation, 168
Baud Rate, 169, 222
Bit-addressable RAM, 10
Bit-valued Data, 113
Bitwise Logical Operators, 116
Block Diagram:
ADC0, 186
ADC1, 199
DAC0 and DAC1, 208
Timer 3, 166
UART, 219
Boolean Variable Instructions, 57
Functions, 122
252
Interrupt: (Contd.)
Vectors, 239
IP (Interrupt Priority), 243
Logical Operations, 48
Logical Operators, 116
Long Addressing, 43
Memory Initialization, 75
ORG directive, 72
OSCICN:
Internal Oscillator Control Register, 86
Oscillator Programming Registers, 86
OSCXCN:
External Oscillator Control Register, 87
Overflow Flag (OV), 13
P0 (Port0 Data Register), 99
P0MDOUT (Port0 Output Mode Register),
99
P1: Port1 Data Register, 100
P1MDIN:
Port1 Input Mode Register, 100
P1MDOUT:
Port1 Output Mode Register, 100
P2: Port2 Data Register, 101
P2MDOUT:
Port2 Output Mode Register, 101
P3: Port3 Data Register, 102
P3IF:
Port 3 Interrupt Flag Register, 103
P3IF:
Port 3 Interrupt Flag Register, 248
P3MDOUT:
Port3 Output Mode Register, 102
P74OUT:
Port 7-4 Output Mode Register, 104
PCON: Power Control Register, 228
PGA0, 186
PGA1, 200
Pointers, 127
Port Configuration, 136
Program Branching Instructions, 62
Program Status Word, 12
Programming:
ADC0, 189
ADC1, 201
Memory Models, 111
DACs, 210
Timer, 160
UARTs, 219
Random Access Memory, 7
Reading Analog Signals, 151
Register Addressing, 40
Index
Register Bank Select Bits, 13
Register Banks, 11, 126
Relational Operators, 115
Relative Addressing, 42
Reset (RST), 4
SCON0: UART0 Control Register, 227
SCON1: UART1 Control Register, 227
SEGMENT directive, 78
Software Delays, 135, 137
Special Function Registers, 9, 11, 114, 162
Stack Pointer, 14
Starting:
A Project, 134
ADC0 Conversions, 187
ADC1 Conversions, 200
Symbol Definition, 74
Synchronous Serial Data Format, 217
System Clock Oscillator, 5
T2CON:
Timer 2 Control Register, 174
T4CON:
Timer 4 Control Register, 177
TCON:
Timer Control Register, 172
Timer 0:
8 Bit Auto-Reload Mode, 163
Two 8-bit Timers Mode, 164
Timer 2, 161
Timer 2:
C Programming Example, 178
16 Bit Capture Mode, 167
16-Bit Auto-Reload Mode (Mode 1), 165
Baud Rate Generation Mode, 168
Mode Configuration, 173
SFRs, 173
Timer 3, 160, 166
Timer 3:
SFRs, 175
Timer 4, 161, 165
Timer 4:
Baud Rate Generation Mode, 169
SFRs, 176
Mode Configuration, 176
Timer Modes, 172
Timer SFRs, 161
Timers 0 and 1 SFRs, 171
Timers 0 and 1, 160
Timers and Operating Modes, 159
TMOD: Timer Mode Register, 171
TMR3CN: Timer 3 Control Register, 175
UART:
Interrupt Flags, 225
Operation Modes, 220
Index
UART: (Contd.)
SFRs, 227
Fixed Baud Rate (Mode 2), 224
Variable Baud Rate (Mode 1), 222
Variable Baud Rate (Mode 3), 225
UART0, 217
UART1, 217
USING directive, 73
Watchdog Timer, 135
253
WDTCN:
Watchdog Timer Control Register, 89
XBR0:
Crossbar Register 0, 96
XBR1:
Crossbar Register 1, 97
XBR2:
Crossbar Register 2, 98