EE6008 Notes Rejinpaul
EE6008 Notes Rejinpaul
EE6008 Notes Rejinpaul
com
UNIT I
INTRODUCTION TO PIC MICROCONTROLLER
Introduction to PIC Microcontroller–PIC 16C6x and PIC16C7x Architecture–
PIC16cxx–- Pipelining -Program Memory considerations – Register File Structure
- Instruction Set - Addressing modes –Simple Operations.
1. PIC Microcontrollers
PIC stands for Peripheral Interface Controller coined by Microchip Technology to
identify its single chip microcontrollers. These devices have been phenomenally
successful in 8-bit microcontroller market.The main reason is that Microchip
Technology has constantly upgraded the device architecture and added needed
peripherals to the microcontroller to ’suit customers’ requirements. The development
tools such as assembler and simulator are freely available on the internet at
www.microchip.com
1.1Low-end Architectures
Microchip PIC microcontrollers are available in various types. When PIC −
MicroMCU first became available from General Instruments in early 1980’s, the
microcontroller consisted of a very simple processor executing 12-bit wide
instructions with basic I/O functions. These devices are known as low-end
architectures.
Some of the low-end device past numbers
are 12C5XX, 16C5X, and 16C505
1.2Mid-range Architectures
Mid-range Architectures are built by upgrading low-end architecture with more
number of peripherals,more numbers of register and more data memory. Some of the
mid-range devices are16C6X16C7X, 16F87X ↑Program memory type
C = EPROM
F = Flash
RC = Mask ROM
Popularity of PIC microcontrollers is due to the following factors-
1. Speed: Harvard Architecture, RISC Architecture
1 instruction Cycle = 4 clock cycles.
For 20 MHz clock, most of the instructions are executed in 0.2μs or five instructions
per microsecond.
2. Instruction Set Simplicity:
The instruction set consists of just 35 instructions (as opposed to 111 instructions for
8051)
3. Power on reset
Power-out reset
Watch-dog timer
Oscillator Options
• low-power Crystal
• Mid-range Crystal
• High-range Crystal
• RC Oscillator
4. Programmable timer options on chip ADC
5.Up to 12 independent interrupt sources
6. Powerful output pin control25mA (max.) current sourcing capability.
7. EPROM/OTP/ROM/Flash memory options.
Memory
The memory in a chip used to store the data and program temporary or permanently
.As like normal microcontrollers, the PIC chip also has certain amount of RAM,
ROM, EEPROM, other flash memory, etc. A PIC chip normally contains a certain
amount of Flash memory
ALU
Perform Operations on temporary working register and register file. It is 8 bit wide
ALU. One of the operand is normally in W register and the other one is either a fine
register or an immediate constant. Effect various flags, namely, carry, digit carry,
zero in status register
BUS
Bus is the communication or data transmission /reception path in a microcontroller
unit. In a normal microcontroller chip, two types of buses are normally available
Data Bus
Data bus is used for memory addressing. The function of data bus is interfacing all
the circuitry components inside the PIC chip
Address bus
Address bus mostly used for memory addressing. The function of address bus is to
transmit the address from the CPU to memory locations.
Power On Reset task
The task of POR is ensuring that the processor starts at known address when power is
first on. The reset function will set the program counter to the starting address.
Power Up timer
A special timer that delays the start of program execution after the PIC has been rest
on Power. This time delay allows Vdd to rise to the required level.
Brown Out delay
When the power supply drops below a certain voltage (4V in case of PIC), it causes
PIC to reset
WDT
A Watch dog timer is a simple timer circuit that performs a specific operation after a
certain period of time if something goes wrong. Suppose we have written a program
which is compiled successfully and when we simulate if every time seems to work
fine. Then we program the PIC. However after a long period of time the program gets
stuck somewhere. What needs it this case is some kind of reset if the program is gets
stuck. This is a purpose of a watchdog timer circuit. When the WDT is enable counter
starts at 00 and increment by 1 unit until it reaches FF.When it goes from FF to 00 the
PIC will be reset, irrespective of what it is doing. The only way we can stop the
WDT, from resetting the WDT back to 00 throughout the program which is done by
the processor. Which indicates that the processor functioning is going on. Watch dog
timer is thus increase the system reliability.
Fig1.2 : W register
STATUS Register
The STATUS register is a 8-bit register that contains the arithmetic status of the
(ALU) , the reset status and the bank select bits for the data memory.
Bit 0 (c): Carry /borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)
1= A carry-out from the Most Significant bit of the result occurred
0= No carry-out from the Most Significant bit of the result occurred
Digit Carry (DC)
This bit indicates a carry from the lower 4 bits during 8 bit addition. If set, it means
rd th
there is a carry from the 3 bit to 4 bit position. Bit 1 DC: Digit carry/borrow bit
(AADWF, ADDLW, SUBLW, SUBWF instructions) for borrow, the polarity is
reversed.
th
1= A carry-out from the 4 low order bit of the result occurred
th
0= No carry-out from the 4 low order bit of the result
Zero (Z)
Many arithmetic and logic instruction affects the zero flag.
Decf Instruction can be used to decrement a variable in RAM, and if result is zero, Z
bit is set, otherwise cleared. Bit 2,(z) : Zero bit
1=the result of an arithmetic or logic operation is zero
0= the result of an arithmetic or logic operation is not Zero.
TO and PD (active low)
Used along with SLEEP mode of PIC.
During the sleep mode the Microcontroller save lots of power
After coming out of this mode, the CPU can check these two status bit to
determine which kind of event is responsible to bring it out of the SLEEP
Mode.
Bit 4 , (TO): This is a time –out bit used for timing and counting , Sleep and
reset functions.
1= after power-up , CLRWDT instruction or SLEEP
instruction 0=A WDT time-out Occurred
Bit 3, (PD) : power – down bit
1=after power-up or by the CLRWDT instruction
0= by execution of the SLEEP instruction
PIC 16C74A has 5 I/O Ports. Each port is a bidirectional I/O port In addition, they
have the following alternate functions.
Most of the PIC16cx/7x family controllers have 33 I/O lines and five I/O ports They are PORT A,
PORT B, PORT C , PORT D and PORT E.
PORT A:
Port A is a 6-bit wide bi-directional port. Its data direction register is TRISA
setting TRISA bit to 1 will make the corresponding PORT A Pin an input. Clearing a
TRIS a bit will make the corresponding pin as an output.
PORT B:
Port B is an 8-bit wide, bi-directional port. Four of the PORT B pins RB7 –
RB4 have an interrupt-on- change feature. Only the pins configured as inputs can
cause this interrupt to occur.
PORT C:
PORT D:
Port D is an 8-bit wide bi-directional port. In addition to I/O port, Port D also
works as 8-bit parallel slave port or microprocessor port. When control bit
PSPMODE (TRISE:4) is set.
PORT E:
It is a 3-bit bi-directional port. Port E bits are multiplexed with analog inputs of
ADC and they serve as control signals (RD , WR, CS) for parallel slave port mode of
operation.
Instructions are fetched from program memory using buses that are distinct from the
buses used for accessing variables in data memory, I/O ports etc. Every instruction is
coded as a single 14-bit word and fetched over a 14-bit wide bus.
Separating program and data buses further allows instructions to be sized differently
than 8-bit wide data words. Instruction op-codes are 14-bits wide making it possible
to have all single word instructions. A 14-bit wide program memory access bus
fetches a 14-bit instruction in a single cycle
1.6Pipelining
The CPU executes each instruction during the cycle following its fetch, pipelining
instruction fetches and instruction executions to achieve the execution of one
instruction every cycle. This is illustrated in Figure 1.6 . It can be seen that while
each instruction requires two cycles (a fetch cycle followed by an execute cycle), the
overlapping of the execute cycle of one instruction with the fetch cycle of the next
instruction leads to the execution of a new instruction every cycle.
This lockstep progression is broken whenever an instruction includes a branch
operation, as illustrated on Figure In this example, an instruction is fetched during the
second cycle, goto New Address, whose job it is to change the normal flow of
instruction fetches from one address to the next address. During the third cycle, the CPU
carries out the sequential fetch from address n+2. At the end of that third cycle, the CPU
executes the goto New Address instruction by changing the program counter to New
Address instead of simply incrementing it to n+3. On the fourth cycle while it is fetching
the instruction at New Address , ilt ignores the instruction automatically fetched from
th
address n+2. While this (n+2) instruction is located in the program immediately after
th
the (n+1) goto New Address instruction, it is never executed
th
immediately after the execution of that (n+1) goto New Address instruction.
Two addresses in the program memory address space are treated in a special way by
the CPU. When the CPU starts up from its reset state, its program counter is
automatically cleared to zero. This is illustrated in Figure . with the content of
address H'000'1 being a go to Mainline instruction. The second special address H'004'
, is automatically loaded into the program counter when an interrupt occurs. As
shown in Figure , a goto IntService instruction can be assigned to this address, to
cause the CPU to jump to the beginning of the interrupt service routine, located
elsewhere in the memory space.
The program memory map of PIC16C74A is shown in Fig. On reset, the program
counter is cleared and the program starts at 00H. Here a 'goto' instruction is required
that takes the processor to the mainline program. When a peripheral interrupt, that is
enabled, is received, the processor goes to 004H. A suitable branching to the interrupt
service routine (ISR) is written at 004H.
The mainline program begins execution when the PIC comes out of reset. It continues
’
running until one of the PIC s interrupt sources requests service. At that point the
execution of the mainline code is temporarily suspended. The CPU begins the
execution of the interrupt service routine by automatically loading the program
counter with H'004'. At the completion of the interrupt service routine, the CPU
returns to where it left off in the mainline program. Program writing is somewhat
simplified if all the program code for the tables, the mainline program and its
subroutines, and the interrupt service routine and its subroutines take up less than 2K
words of instruction
As shown in Figure, bits 10…0 of the call instruction are loaded into the program
counter. At the same time, bits 4 and 3 of a special register called PCLATH
(“program counter latch ”) are loaded into bits 12 and 11 of the program counter. As
long as the program memory is less than 2048(i.e.,2K) words, bits 4 and 3 of
' ' ' '
PCLATH can be left initialized to H 00 , and then the 11 address H 7FF .
For the programs larger than this, it is necessary to ensure that bit 3 of PCLATH is
set or cleared appropriately each time a subroutine is called. The goto instruction,
which also has an 11-bit address field, requires an identical treatment
The general purpose register file is another name for the microcontroller’s
RAM . Data can be written to each 8-bit location updated and retrieved any number
of times.
The special function register file consists of input, output ports and control
registers used to configure each 8-bit port either as input or output. It contains
registers that provide the data input and data output to a chip resources like Timers,
Serial Ports and Analog to Digital converter and also the registers that contains
control bits for selecting the mode of operation and also enabling or disabling its
operation.
The instruction set of PIC is divided into Three basic categories. They are
(a) Byte oriented Instructions
(b) Bit oriented Instructions
(c) Literal and Control Instructions
Byte Oriented Instructions
In a byte oriented Instructions f represents a file register and d represents destination
register.The destination specifies where the result of operation is to be placed. If D=
0 the result is placed in W register(Accumulator) and if d = 1 , the
result is placed in the file register specified in the instruction.
ADDWF f, d ; Add W and f
CLRF f ; Clear f
MOVWF f ,d ; Move f
NOP ; No operation
SUBWF f ,d ; Subtract W from f
COMF f, d ; Complement f .
(iii).Increment/Decrement Instructions
INCF f ,d ; Increment contents of f register by 1
DECF f , d ; Decrement f by 1
(iv).Data Transfer instructions
MOVF f,d ; Move f to W i.e The contents of register f is moved to a destination
depending on d
MOVLW k ; Move literal k to W
MOVWF f ; Move W to f
(v) Clear Instructions
CLRF ;Clear file f
CLR W ; Clear the contents of W register and zero bit is set
CLRWDT ; Clear Watch dog timer
BCF ; Clear bit b of register f.
(vi)Rotate Instructions
RLF ; Rotate Left f through carry
RRF ; Rotate Right f through carry
If bit B in register f is zero ,then the next instruction is executed, otherwise next
instruction is discarded and a NOP is executed.
DECFSZ f,d ; Decrement f ,skip if zero.
INCFSZ f,d ; Increment f ,skip if zero
1.9ADDRESSING MODES.
The PIC microcontrollers support only TWO addressing modes .They are
2. Movlw 5
3. Movwf TEMP
It's easy to understand, that direct addressing method means working directly with
the variables. In the second line we put the number 5 into the working register W,
and in the line 3, the content of the W passes to the TEMP variable
Movlw 0x030
Movwf FSR
Movlw 5
Movwf INDF
In the second line, we put a value into the W register. In the third line, the value
passes to the FSR register, and from this moment FSR points to the address of the
TEMP variable. In the fourth line, the number 5 passes to the W register, and in the
fifth line, we move the contents of W register (which is 5) to the INDF. In fact INDF
performs the following: it takes the number 5 and puts it in the address indicated by
FSR register
Simple Operations
Assume that an instruction that affects the Z bit has just been executed. Then
depending on the result one instruction sequence or another is to be executed,
continuing on after either case.
Assume that the upper byte of the counter is called COUNTH and the lower byte is
called COUNTL
Note how the movf instruction is first used to test COUNTL for zero without
changing it, and even without having to move it into W
UNIT II
INTERRUPTS AND TIMER
PIC micro controller Interrupts- External Interrupts-Interrupt Programming–
Loop time subroutine – Timers-T7imer Programming– Front panel I/O-Soft
Keys– State machines and key switches– Display of Constant and Variable
strings.
Introduction:
POLLING
INTERRUPTS
POLLING
Using this method, the microcontroller accesses at the exact time interval
the external device, and gets the required information. The user is the one
who determines the time intervals in which micro “contacts” the device.
In the Polling method, the PIC microcontroller must "access by himself" the
device and “ask” for the information it needs for processing. In fact we see
that in the Polling method the external devices are not independent systems;
they depend on the microcontroller, and only the micro is entitled to obtain
access to the information it needs. The main drawback of this method when
writing program is waste of time of microcontroller, which needs to wait and
check whether the new information has arrived.
INTERRUPTS
Interrupt is the signal sent to the micro to mark the event that requires
immediate attention. Interrupt is “requesting" the processor to stop to
perform the current program and to “make time” to execute a special code.
In fact, the method of interrupt defines the option to transfer the
information generated by internal or external systems inside the micro by
them self! Once the system has finished the task imposed on it, the
processor will be notified that it can access and receive the information and
use it.
The “request” for the microcontroller to “free itself” to execute the interrupt
could come from several sources:
1
www.rejinpaul.com
Software interrupts - come from a program that runs by the processor and
“request” the processor to stop running the program, go to make an
interrupt and then to return to continue to execute the program.
An example: Procedure - when there is a procedure call, the processor stops
the execution of the program, jumps to the place in memory that reserved
for a procedure – executes the procedure and only then returns back to the
program and continues to execute.
Interrupts in PIC16C6X:
• USART Interrupts
• Receive Interrupt
• Transmit Interrupt
• LCD Interrupt.
• CCP Interrupt
• SSP Interrupt
There is a minimum of one register used in the control and status of the
interrupts. This register is: INTCON
Global interrupt enable bit, GIE (INTCON) enables (if set) all un-masked
interrupts or disables (if cleared) all interrupts. When bit GIE is enabled,
and an interrupt flag bit and mask bit are set, the interrupt will vector
immediately. Individual interrupts can be disabled through their
corresponding enable bits in the INTCON register. GIE is cleared on reset.
The “return from interrupt” instruction, RETFIE, exits the interrupt routine
as well as sets the GIE bit, which re-enable interrupts. The RB0/INT pin
interrupt, the RB port change interrupt and the TMR0 overflow interrupt
flag bits are contained in the INTCON register. The peripheral interrupt flag
bits are contained in special function registers PIR1 and PIR2. The
corresponding interrupt enable bits are contained in special function
registers PIE1 and PIE2 and the peripheral interrupt enable bit is contained
3
www.rejinpaul.com
4
www.rejinpaul.com
INT INTERRUPT:
TMR0 INTERRUPT:
An overflow in the TMR0 register will set flag bit T0IF. The interrupt can
be enabled/disabled by setting/clearing enable bit T0IE.
PORTB INTERRUPT ON CHANGE:
An input change on PORTB sets flag bit RBIF. The interrupt can be
enabled/disabled by setting/clearing enable bit RBIE.
WATCH DOG TIMER (WDT):
The Watchdog Timer is a free running on-chip RC oscillator which does
not require any external components. This RC oscillator is separate from the RC
5
www.rejinpaul.com
oscillator of the OSC1/CLKIN pin. That means that the WDT will run, even if
the clock on the OSC1/CLKIN and OSC2/CLKOUT pins of the device has been
stopped, for example, by execution of a SLEEP instruction. During normal
operation, a WDT time-out generates a device reset. If the device is in SLEEP
mode, a WDT time-out causes the device to wake-up and continue with normal
operation. The WDT can be permanently disabled by clearing configuration bit
WDTE.
WDT PERIOD:
The WDT has a nominal time-out period of 18 ms, (with no prescaler).
The time-out periods vary with temperature, VDD and process variations from
part to part (see DC specs). If longer time-out periods are desired, a prescaler
with a division ratio of up to can be assigned to the WDT under software
control by writing to the OPTION register. Thus, time-out periods up to seconds
can be realized.
The CLRWDT and SLEEP instructions clear the WDT and the postscaler, if
assigned to the WDT, and prevent it from timing out and generating a device
RESET condition.
The TO bit in the STATUS register will be cleared upon a WDT time-out.
WDT PROGRAMMING CONSIDERATIONS:
It should also be taken in account that under worst case conditions (VDD =
Min., Temperature = Max., max WDT prescaler) it may take several seconds
before a WDT time-out occurs.
ADC Interrupt
This interrupt is generated when the analog-to-digital conversion ends. This is
only to 16 C7X parts in general. ADIE is the bit that enables this interrupt in
INTCON register. Apart from this the master control bit GIE must be enabled.
The end of the conversion interrupt flag (ADIF) bit is in ADCON0 register.
i. On reset all interrupts are disabled
6
www.rejinpaul.com
OPTION_REG Register
The OPTION_REG Register is a readable and writable register, which contains
various control bits to configure the TMR0 prescaler/WDT postscaler (single
8
www.rejinpaul.com
assignable register known also as the prescaler), the external INT interrupt,
TMR0 and the weak pull-ups on PORTB.
PIE Register(s)
Although, the PIE register bits have a general bit location with each register,
future devices may not have consistent placement. Bit location
inconsistencies will not be a problem if you use the supplied Microchip
Include files for the symbolic use of these bits. This will allow the
Assembler/ Compiler to automatically take care of the placement of these
bits by specifying the correct register and bit name.
Interrupt Latency
9
www.rejinpaul.com
Interrupt latency is defined as the time from the interrupt event (the
interrupt flag bit gets set) to the time that the instruction at address 0004h
starts execution (when that interrupt is enabled). For synchronous
interrupts (typically internal), the latency is 3TCY. For asynchronous
interrupts (typically external), such as the INT or Port RB Change Interrupt,
the interrupt latency will be 3 - 3.75TCY (instruction cycles). The exact
latency depends upon when the interrupt event occurs (Figure 8-2) in
relation to the instruction cycle. The latency is the same for both one and
two cycle instructions.
The external interrupt on the INT pin is edge triggered: either rising if the
INTEDG bit (OPTION<6>) is set, or falling, if the INTEDG bit is clear. When a
valid edge appears on the INT pin, the INTF flag bit (INTCON<1>) is set. This
interrupt can be enabled/disabled by setting/clearing the INTE enable bit
(INTCON<4>). The INTF bit must be cleared in software in the interrupt
service routine before re-enabling this interrupt. The INT interrupt can
wake-up the processor from SLEEP, if the INTE bit was set prior to going
into SLEEP. The status of the GIE bit decides whether or not the processor
branches to the interrupt vector following wake-up. See the “Watchdog
Timer and Sleep Mode” section for details on SLEEP and for timing of
wake-up from SLEEP through INT interrupt.
10
www.rejinpaul.com
Interrupt Constrain:
11
www.rejinpaul.com
example a PIC chip controlling the operation of a device may use it UART
serial port as an optional port from a personal computer so the PC can
control an arrray of such devices. Alternatively that device may be used in
sand alone mode, with nothing connected to its UART serial port.Give these
two modes of operation , the PIC must be able to rspond to each chracter
recives via the UART and its two-byte FIFO before another two character
arive to overrun the first character. On the other hand, the PIC cannot
dedicate itself solely to waiting for characters to arrive via UART interrupts
because it has ongoing control to do.
12
www.rejinpaul.com
With other interrupt sources enable, the story changes, as illustrate in the
secound figure now the UARTcan recive a character and send an interrupt
signal to the CPU, only to have its service postponed temporarily while the
CPU finishes servicing another interrupt. As can be seen in the figure , an
error may be occasionally occur if care is not taken to keep interrupt service
routines short
For the more general case with multiple interrupt sources, the worst case
sequencing of interrupts must be examined to determine whether a problem
will ever arise. Each interrupt source I, must be characterized by two
parameters
The minimum time interval between interrupts for a given interrupt source
is determine by the application for example in the case of the 9600Bd UART,
each 8-bit character is farmed between a start bit and a stop bit with a bit
time of 1/9600 secound, each 10-bit frame can arive 10/9600 secound
apart
When ever an interrupt occurs the CPU automatically pushed the return
address in the program counter onto the stack and clear the GIE(global
interrupt enable) bit, disabling further interrupts. No other register or W are
automatically set aside. Consequently the first job of Intservice is to set
aside the content of W and the SATAUS. Then they can be restored at the
end of the interrupt service routine to exactly the same state they were in
when the interrupt occurred, as required for the proper execution of the
mainline code
This setting aside of W and the SATAUS is illustrated in the first three
instruction of below figure. The assumption is made that the mainline
program will not be switched for direct access of Bank 1 register of RAM
variables at the moment that the interrupts occurs. Once the interrupts
have been enable Bank 1 register and RAM should only be accessed by
indirect addressing.
13
www.rejinpaul.com
The three instructions for setting aside W and the SATAUS use
Swapf SATATUS, W
Movf SATATUS,W
Note that swapf instruction doesnot ever affect the Z bit in the SATATUS
register when it makes this move (even though it copies the upper 4 bits of
SATATUS to the lower 4 bits of W and the lower 4 bits of SATATUS to the
upper 4 bits of W)in contrast the movf instruction corrupt the Z bit restoring
SATATUS and W again the instruction
Swapf W_TEMP,W
Swapf W_TEMP,F
14
www.rejinpaul.com
Which swaps the two halves of W_TEMP so that the following swapf
instruction will swap them again, ending up with every bit of W just where it
was when the interrupt occurred.
The loop time subroutine is called from the mainline loop,in the case of LED
blink program actually the loop time subroutine to work correctly,the worst
case execution of the remainder of the code in the mainline loop plus the
worst case execution time for all the interrupt service routine that should
request the service. As a consequence successive execution of some task
may occur less than the predefine time like in the Les blinking program. On
the other hand even if this mainline overrun condition dose occur the long
range timing provided by the loop time subroutine will still be accurate
aslong as no counts of scalar are ever lost.
Example:
15
www.rejinpaul.com
Looptime
Btfsc SCLAER ,7
Goto looptime
Movlw 5
Addwf SCALER,F
return
External interrupts:
The PIC microcontroller has one pin, RBO/INT that serves as its primary
external interrupt input. This pin is bit 0 of PORTB before initializing the
interrupt circuitry PORTB itself should be initialized. The bit of bank 1
register TRISB set up the corresponding bits of PORTB as either inputs or
outputs. All of the pins that are setup as input pinas optional a weak pullup
resistor is added by clearing the NOT_RBPU bit of OPTION_REG as show in
the figure 6.1b this provides a useful input for a pushbutton switch or for a
array of keyswitches such as keypad show in 6.2a the internal pull-ups of
the circuit in figure 6.2b hold each input pin high until any of the key
switches are pressed, if any of the keyswitch are pressed then the
corresponding column driver drive the output to low.
16
www.rejinpaul.com
For example bit 7 of PORTB will be driven low if the key switch labeled “1”
is pressed and if the bit 3 output from PORTB is driven low otherwise the
internal pullup resistor pulls the bit 7 input high
Figure 6.2b also illustrate the use of bit 0 of PORTB as an interrupt input
that can be used independently of the manner in which the other pins of
PORTB are used. the setup for this independent interrupt input show in the
figure 6.3. the presence or absence of the weak pullup resistor on allPORTB
input is irrelevant to this bit0 input since the device that derives this
interrupt pin will override the weak pullup. The INTEDG bit of
OPTION_REG permits us to setup this input to generate an interrupt on
either a rising edge or falling edge. In addition , when used as an interrupt
input, this PB0/INT pin is automatically configured as a Schmitt-trigger
input . Triggering on the input edge regardless of its rise(or fall) time
17
www.rejinpaul.com
bcf INTCON,INTF
then service the interrupt and go back to INtservice’s polling routine to look
for any other pending interrupts
18
www.rejinpaul.com
TIMER MODULES:
Timer 0
Timer1
19
www.rejinpaul.com
Timer2
Timer 0:
Timer 1
Timer1 is the most versatile and can be used to monitor the time
between signal transition occuringon an input pin or control the precise
time of transitions on an output pin. It is a 16-bit timer/counter. The clock
source can be either the internal system clock (Fosc/4), an external clock, or
an external crystal. Timer1 can operate as either a timer or a counter. When
operating as a counter (external clock source), the counter can either
operate synchronized to the device or asynchronously to the device.
Asynchronous operation allows Timer1 to operate during sleep, which is
useful for applications that require a real-time clock as well as the power
savings of SLEEP mode.
Timer 2
20
www.rejinpaul.com
Generator for the Synchronous Serial Port (SSP). The prescaler option allows
Timer2 to increment at the following rates: 1:1, 1:4 and 1:16.
The post scaler allows TMR2 register to match the period register
(PR2) a programmable number of times before generating an interrupt. The
postscaler can be programmed from 1:1 to 1:16 (inclusive).
PIC 16C74A has three modules, viz., Timer-0, Timer-1 and Timer-2. Timer-0
and Timer-2 are 8-bit timers. Timer-1 is a 16-bit timer. Each timer module
can generate an interrupt on timer overflow.
Timer-0 Overview:.
The timer-0 module is a simple 8-bit counter TMR0. Which can be written
to or read from. The counter sets a flag,T0IF, when it overflows and can
causes an interrupt at that time if that interrupt source has been
enable(T0IF=1) Timer0 can be assigned an 8 bit prescaler that can divide
the counter’s input by 2,4,8,16,….256 writing to TMR0 resets the prescaler
assigned to it.
If the prescaler is bypassed and the internal clock used, the circuit is show
in figure6.5 results. The two cycles delay is a result of the need to
synchronize the external clock, TCK1. The internal clock. is write to TMR0
will reset the delay circuit, causing the two cycles that follow the write not to
be counte. Because of these two cycle delay if 256-10 = 246is written to the
TMR0, the T0IF flag will bw set in 12 cycles, no the 10 cycles expected
21
www.rejinpaul.com
Option Register (Addr: 81H) Controls the prescaler and Timer -0 clock
source. The following OPTION register configuration is for clock source =
fosc /4 and no Watchdog timer.
22
www.rejinpaul.com
Timer - 1 Module
23
www.rejinpaul.com
1 - Do not synchronize
0 – Synchronize
1 1 1:8
1 0 1:4
0 1 1:2
0 0 1:1
24
www.rejinpaul.com
25
www.rejinpaul.com
Timer 1 and one of the CCP module can be used as a scaler. This takes
advantage of thr CCP module’s tiggers special events mode. When the
comparetakes place between TMR1 and the compare register (CCPR1 or
CCPR2). TMR1 is reset to zero giving a period of one greater than the value
loaded into CCPR1 (or CCPR2) the register ans circuit are shown in the
figure 6.11 using CCP1 module
Example:
goto next;
movwf TMPH
movwf TMPL
net:nop
Timer 1 like timer0, can be used to count external events. When used with
one of the CCP modules, it can generate a CCP interrupt after every Nth
input edge , for any integer value of N upto 65536 with timer1 prescaler,
this can be extended to every multiple of 8 up to 524288.
This basic counter is show in the figure 6.12 this is all that is need to count
N events with T1CON setr to H’02 the prescaler is bypassed and the input
from the pin0 of PORTC, the external clock input to timer1 (T1CK1) is
blocked by TMR1ON = 0 to count N input rising edges TMR1 is preset to
65536-N, the TMR1IF flag in the P1R! register is cleared , and the TMR1IE
interrupt enable bit in the PIE1 register is set . finally counting is begun by
setting TMR1ON bit in the T1CON register ,after N raising edges on the
input pin RC0/T1CK1, Timer 1 will generate an interrupt, permitting the
desired action to be taken at that time.
The synchronizer in the figure 6.12 synchronizes the input to the internal
clock. It is an optional feature, control by the NOT_T1SYNC (bit 2 ) of the
T1CON register. Synchronizing the external input clock. It is an optional
feature, controlled by thr NOT_T1SYNC bit of T1CON register.
Synchronizing the external input to the internal clock permits TMR1 to be
read from and write to. Even as the counter is counting , synchronization is
27
www.rejinpaul.com
also vital to proper operation when timer 1 is used with one of the CCP
modules while counting external events. The state of the NOT_T1SYNC bit is
ignored, and the synchronizer bypassed, when TMR1CS=0 select the
internal clock OSC/4
One option that PIC chips make available to uses is the ability to stop the
internal clock(OSC/4) reducing power consumption significantly, and yet
have an accurate internal time base. Timer1 includes the pins and the
oscillator circuit to allow 32768 Hz crystal to save as its external clock
source. Since the synchronizer of figure6.12 will not produce output pluses
with the OSC/$ internal clock stopped, the synchronizer must be bypassed
TMR1 will overflow at 2-secound , 4-secound, 8-secpund, or 16-secound
intervals, depending on which prescaler value is used. The circuit is show in
the figure6.13
Each time tahat a TMR1 overflow occurs, the CPU initiates the startup of
the internal clock which may take as long as 1000 internal clock cycle
before the next instruction executed. If GIE bit had been cleared before the
Sleep instruction had been executed, then about a millisecound after TMR1
overflows, the CPU will countine execution with the code that follows the
Sleep instruction . if GIE=1 then about a millisecound after TMR1
overflows, the CPU will execute the one instruction that follows the sleep
instruction and then vector to the interrupt service routine at address
H’004’
28
www.rejinpaul.com
Timer 2 Overview
The output of TMR2 goes through a 4bit post-scaler (1:1, 1:2, to 1:16)
to generate a TMR2 interrupt by setting TMR2IF.
The Timer2 module has one source of input clock, the device clock
(FOSC/4). A prescale option of 1:1, 1:4 or 1:16 is software selected by
control bits T2CKPS1:T2CKPS0 (T2CON<1:0>).
29
www.rejinpaul.com
The TMR2 register is readable and writable, and is cleared on all device
resets. Timer2 increments from 00h until it matches PR2 and then resets
to 00h on the next increment cycle. PR2 is a readable and writable
register. TMR2 is cleared when a WDT, POR, MCLR, or a BOR reset
occurs, while the PR2 register is set.
1. Timer2 Postscaler
There are four bits which select the postscaler. This allows the postscaler
a 1:1 to 1:16 scaling(inclusive). After the postscaler overflows, the TMR2
interrupt flag bit (TMR2IF) is set to indicate the Timer2 overflow. This is
useful in reducing the software overhead of the Timer2 interrupt service
routine, since it will only execute once every postscaler # of matches.
The match output of TMR2 is also routed to the Synchronous Serial Port
module, which may software select this as the clock source for the shift
clock
The prescaler and postscaler counters are cleared when any of the
following occurs:
• any device reset (Power-on Reset, MCLR reset, Watchdog Timer Reset,
Brown-out Reset,or Parity Error Reset)
Sleep Operation
During sleep, TMR2 will not increment. The prescaler will retain the last
prescale count, ready for operation to resume after the device wakes from
sleep.
30
www.rejinpaul.com
PIC16C74A has two CCP Modules. Each CCP module contains a 16 bit
register (two 8-bit registers) and can operate in one of the three modes, viz.,
16-bit capture, 16-bit compare, or up to 10-bit Pulse Width Modulation
(PWM). The details of the two modules (CCP1 and CCp2) are given as
follows.
CCP1 Module:
31
www.rejinpaul.com
CCP1 Module consists of two 8-bit registers, viz., CCPR1L (low byte) and
CCPR1H (high byte). The CCP1CON register controls the operation of CCP1
Module.
CCP2 Module:
CCP2 Module consists of two 8 bit registers, viz., CCPR2L (Low byte) and
CCPR2H (high byte). The CCP1CON register controls the operation of CCP2
Module.
Both CCP1 and CCP2 modules are identical in operation with the exception
of the operation of special event trigger.
The following table shows the timer resources for the CCP Mode.
Bit 5-4:
CCP1X CCP1Y: PWM least significant bits. These bits are of no use in
Capture mode. In PWM Mode, these bits are the two Lsbs of the PWM duty
cycle. The eight Msbs are found in CCPR1L. Thus the PWM mode operates
in 10-bit mode.
Bit 3-0:
32
www.rejinpaul.com
11XX=PWM mode.
The combination of Timer1 and either the CCP1 or the CCP2 module
permits a PIC chip tot be used to determine the time of occurance of an
input edge. Timer1 can be used with its prescaler to let its 16-bit count
range measure longer intervals directly. How ever the finiest resolution in
the measurement result will occure if the prescaler is bypassed. Thus with
OSC=4MHz and bypassing the prescaler, the time of occurance of an input
edge will be ascertained to within 1µs
33
www.rejinpaul.com
Figure 6.9 shows the registers and circuity involved with the use of CCP1
module. The time between two input edges is determine by making two
captures and subtracting the one time from the other.
34
www.rejinpaul.com
Compare mode for CCP2 is similar to that of CCP1, except that in special
event trigger mode, CCP1 resets TMR1 only, whereas CCP2 resets TMR1
and starts A/D conversion if A/D module is enabled.
35
www.rejinpaul.com
Timer1 is 16 bit counter that together with a CCP module can derive a pin
high or low at a precisely controlled time,independent of what the CPU is
doing at that time. Of the seven PIC family members considered all have two
CCP modules except PIC16C62A, PIC16C64A, and the PIC16C72, ehich
has only one. Consequently, they can all control the RC1/CCP2 pin. These
pin disgnation indicate the alternative role played by these pins as general
purpose I/O pins of PORTC.
For the PIC chips having two CCP modules, if both modules are being used
for either a compare funcation or for a capture funcation, they will share
TMR1. In this TMR1 should never be changed by writing to it. How ever if
TMR1 is being in one role only, its use is simplified by being able to stop its
clocking by clearing the TMR1ON bit in the T1CON register, clearing TMR1
setting up CCPR1, and then starting the clocking of TMR1 again
In software interrupt mode, CCP1IF bit is set but CCP1 pin in unaffected.
As shown in the figure, in special event trigger mode, both CCP1 and CCP2
initiates an A/D conversion.PWM mode (CCP1)Both CCP1 and CCP2 have
similar operation in PWM mode. Here we will discuss PWM with respect to
CCP1.
36
www.rejinpaul.com
37
www.rejinpaul.com
The heart of the PIC’s PWM circuit is a 10bit counter formed from timer2’s
8-bit TMR2 counter for its upper bits and whatever 2 bit counter drives it.
As show in the figure 6.16a, these latter 2 bits depend on the prescaler
setting. If the prescaler is bypassed(ie set to divide by 1) as in the figure
6.16b, then the PWM circuity actually reaches into the 2 bit Q counter
which divides the crystal clock frequency by four to obtain the internal clock
frequency. This is the choice that leads to shortest period of the PWM
waveform.
The period of the waveform is controlled by the circuit of figure 6.17. this
figure shows that the period is controlled by two things: (i) the value
initialized for the Timer2 prescaler and (ii) the value initialized by PR2.
In addition to controlling the PWM frequency the value loaded to PR2
controls the duty cycle resoluation. Thus the circuity in figure 6.17a turns
on the PWM output , other circuitry will turn it off at any desired count of
the 10-bit counter in this figure. Consequently ,if PR2 is initialized to 63, the
full 10-bit counter will count with a scale of 64*4=256, or 8 bit resoluation
The circuitry that controls the ontime of the PWM waveform is contained
entirely in the CCP circuitry. For the RC2/CCP1 output, this CCP1 circuitry
is show in the figure 6.19a. this circuit illustrates how the 10bit value is
formed that turns off the PWM output, thus controlling the duty cycle whose
value is given by figure 6.19b.
The upper 8 bits of the 10bits value arre loaded under program control into
CCPR1L.the PWM circuitry automatically transfers this value to CCPR1H as
38
www.rejinpaul.com
TMR2 is reset to start each PWM period. This double buffering of the value
that isactually used in the comparison is designed to help prevent the glitch
that would occure in CCPR1H were changed from H’50’ to H’40’. In this
event, the PWM output would not go low at all until the next period
The lower 2 bits of the 10bit value are loaded under program control into
bits 5 and 4 of CCP1CON. If a 10 bit valueis loaded into a 2 byte RAM
variable PWM (made upof PWMH and PWML) then the code will transfer it
to CCPR1L and CCP1CON appropriately to vary the output over the full
duty cycle range from zero to one.
39
www.rejinpaul.com
PWMupdate
Rrf PWMH,F ;rotate bit8
Rrf PWML,F ;into PWML[7]
Rrf PWMH,F ;rotate bit 0 into PWML[7]
Rrf PWML,F ;and bit 9 into PWML[7]
Rrf PWMH,F ;and bits 1,0 into PWMH[7:6]
;upper 8 bits are now in PWML
;lower 2 bits are in PWML[7:6]
Rrf PWMH,F ;move bits 1,0 to align with CCP1CON
Rrf PWMH,W ;and move to W
Xorwf CCP1CON,W ;toggle if CCP1X;CCP1Y differ
Andlw B’00110000’ ;force other bits to zero
Xorwf CCP1CON,F ;chane bits that differ
Movf PWML,W ;move upper 8 bit
Movwf CCPR1l
Return ;fourteen cycles
The schematic block diagram of CCP1 module in PWM mode is shown in the
figure.
40
www.rejinpaul.com
It can be noted that PR2 (Period Register, 8 bit) decides the PWM period
where CCPR1L (8-bits) and CCP1CON <5:4> (2-bits) decide the PWM duty
cycle. When TMR2 equals PR2, the SR latch is set and RC2/CCP1 pin is
pulled high. In the same time, TMR2 is cleared and the duty cycle value
available in CCPR1L is latched to CCPR1H. CCPR1H, CCP1CON <5:4>
decide the duty cycle and when this 10-bit equals the TMR2+2 prescaler or
Q-bits, the SR latch is set and RC2/CCP1 pin is driven low.
A PWM output as shown has a time period. The time for which the output
stays high is called duty cycle.
PWM Period
The PWM period is specified by writing to PR2 register. The PWM period can
be calculated using the following formula:
41
www.rejinpaul.com
When TMR2 is equal to PR2, the following events occur on the next
increment cycle.
TMR2 is cleared
The PWM duty cycle is specified by writing to the CCPR1L register and to
CCP1CON < 5 : 4 >bits. Up to 10-bit resolution is available where CCPR1L
contains the eight MSBs and CCP1CON < 5 : 4 > contains the two LSB's.
The 10-bit value is represented by CCPR1L : CCP1CON < 5 : 4 >.
42
www.rejinpaul.com
Although CCPR1L and CCP1CON < 5 : 4 > can be written to at anytime, the
duty cycle value is not latched into CCPR1H until a match between PR2 and
TMR2 occurs. In PWM mode, CCPR1H is a read-only register.
The CCPR1H register and a 2-bit internal latch are used to double buffer the
PWM duty cycle. This double buffering is essential for glitch less PWM
operation. When the CCPR1H and 2-bit latch match TMR2 concatenated
with an internal 2-bit Q clock or 2-bits of prescaler, the CCP1 pin is cleared.
Maximum PWM resolution (bits) for a given PWM frequency can be
calculated as
43
www.rejinpaul.com
Front-Panel I/O :
Soft Keys:
A multiple line LCD display presents the opportunity, show in the figure 8.1
of being used with miniature pushbutton switches that are aligned to the
right of each line of the display. The label for each switch can be displayed,
and change at the right edge of row corresponding to that switch.
The circuit in the figure 8.2 illustrate how the four soft keys figure 8.1 can
be treated in the same manner as the 12keys of keypad. Infact , keyswitchs
are generally grouped into array such as this whether or not they are
physically grouped together in a keypad . figure 8.2 shows a pin from a
seprate port (bit 7 of PORTD) being used to derive the coloum of softkeys. If
bit 0 of PORTB is not used as an interrupt input. Then it actually makes
sense to use that pin to drive the column of softkeys.
44
www.rejinpaul.com
45
www.rejinpaul.com
Keyswitches are not changed very fast, they can be checked once each time
around the mainline loop in a keySwitch subroutine. Recall that a looptime
of 10ms was selected because the maximum keybounce time of most of the
mechanical keyswitches is less than 10ms. Consequently , if keyswitch
detects that a key is newly pressed, it can be assured that the next time is
called, 10ms later, any erratic bouncing of the key contacts will have settled
out, with the contacts firmly closed.
The press and relese of a keyswitch occurs over an interval of many tens of
millisecounds. For example, if a keyswitch is pressed and released at the
relatively fast rate of four timesa second. The switch may be closed for 12
looptimes, open for 12 looptimes, closed for 12 looptimes, etc. the
keyswitch subroutine will use a state variable call KEYSTATE to keep track,
from one call to the next , of the sequencing of the following task
46
www.rejinpaul.com
A flow chart of the keyswitch subroutine algorithm is show in the figure 8.3
each time keyswitch is called, if no key has been pressed during the last
several calls, then KEYSTATE will equal zero. The job of the keyswitch
subroutine in this case is to determine whether any key us newly pressed
The keyswitch algorithm of figure 8.3 test the STATUS register Z bit upon
returning from the AnyKey subroutine if Z=1, a return from the keyswitch
subroutine occurs. On the other hand if Z=0, a key is newly pressed , so
KEYSTATE is incremented to H’01’ before returning from the subroutine
The 220Ω resistors in figure 8.2 are there to product the PIC,s output
drivers during the execution of scankeys if two key switchs in the same row
are pressed simultaneously. In this abnormal case, two output drivers will
be shorted together. A high output will be shorted to a low output during the
testing of half of the keys, the 220Ωresistors limit the current to less than
10mA when this happens.
Upon the return from the scankeys subroutine, the keyswitch subroutine
tests the STATUS register Z bit if Z=1 it increments KEYSTATE and
returns, prepard to act on the pressed key in 10ms. If Z=0 then somehow
scankeys failed to identify a pressed key. This might occur if , for example,
two keys in the same column are pressed simultaneously. In that case, there
will be no entry in scankey_table that matches what is read from PORTB,
however this failure occurs, KEYSTATE is cleared to zero, starting over
again in the hunt for pressed key
47
www.rejinpaul.com
The last two states of the KeySwitch algorithm require the pressed key to be
released during two successive passes around the mainline loop. This
overcomes any potential problem with keybounce time during the relese key
;;;;;;;;;;;;;ScanKeys subroutine;;;;;;;;;;;;;;;;;;
ScanKeys
clrf KEYCODE ;Startby checking the “0” key
ScanKeys_1
call ScanKeys_Table ;get next table entry
movwf TEMP
bcf PORTD,7 ;PD7<- bit 0 of table entry
btfsc TEMP,0
bsf PORTD,7
movwf PORTB ;PB3,2,1 fromtable entry
xorfwf PORTB,W ;compare upper 4 bitsor PORTB with table entry
andlw B’11110000’ ;z=1 if a match
btfsc STATUS,Z
goto ScanKeys_done
incf KEYCODE,F ;try next key
btfsc KEYCODE,4 ;stop with Z=0 when KEYCODE =B’XXX1XXXX’
goto ScanKeys_1
ScanKeys_done
Return
;;;;;;;; ScanKeys_Table subroutine;;;;;;;;;;;;;;;;;;;
ScanKeys_Table
Movf KEYCODE,W
Addwf PCL,F ;Change PC with PCLATH=H’00’
retlw B’11101011’ ; Test “0” Key
retlw B’11101011’ ; Test “1” Key
retlw B’11101011’ ; Test “2” Key
retlw B’11101011’ ; Test “3” Key
retlw B’11101011’ ; Test “4” Key
retlw B’11101011’ ; Test “5” Key
retlw B’11101011’ ; Test “6” Key
retlw B’11101011’ ; Test “7” Key
retlw B’11101011’ ; Test “8” Key
retlw B’11101011’ ; Test “9” Key
retlw B’11101011’ ; Test “*” Key
retlw B’11101011’ ; Test “#” Key
retlw B’11101011’ ; Test SK1
retlw B’11101011’ ; Test SK2
retlw B’11101011’ ; Test SK3
retlw B’11101011’ ; Test SK4
48
www.rejinpaul.com
A popular display plus input device combination is show in the figure 8.5.
the grayhill rotary pulse generator its feature is 24 detented position per
revoluation so it clicks from one position to the next, giving the feel of rotary
switch . it is also features an integral momentary action push button switch.
When used with an alphanumeric display such as show in the figure 8.5
the RPG’s knob can be pushed and released to cycle the display among an
The PIC interface for grayhill RPG show in the figure 8.6b. The momentary
action push button switch can be treated in the same way as one of the soft
keys figure 8.2. then it will be checked, debounce, and handled at regular
intervals of time.
The twoRPG output must be treated differently from keyswitches since trhe
information they convey is represented by two output levels and their
changes. They must also treated differently from RPG interface. Any one
RPG output as in PB0/INT intrreput input. In the case of grayhilll RPG,
action is needed whenever either output change, not just when one of the
output riseing edge. The two RPG output can be checked in an RPG
49
www.rejinpaul.com
subroutine, called from the mainline loop[. The two inputs are compared
with their values 10ms ago. The selected parameter be inceremented or
decremented if the input have change
Display of variable
string:
When entering setup paramenter with either a keypad or an RPG and when
displaying the results of an instrument measurement, it is necessary to
write a string of ASCII-code characters to display. Since the character vary,
they are taken from the RAM
50
www.rejinpaul.com
51
www.rejinpaul.com
Constant string arise in seven ways. The labels associated with softkeys
represent one application. The units(kHz) associate with the variable
represent another. In this section a DisplayC subroutine that makes use of
display string stored in program memory will be developed. Each byte of
eachstring is accessed via retlw instruction in the process of returning from
52
www.rejinpaul.com
The source code form of display string store in program memory can be
illustrate by the following example used to display the string
ROW4Coll
Being in the first character position of the fourth row of the LCD display
_ROW4Coll
retlw H’D4’ ;cursor postioning code (figure 7.8)
dt “ROW4Coll” ;Character to be displayed
retlw 0
retlw A’R’
retlw A’O’
retlw A’W’
retlw A’4’
retlw A’C’
retlw A’o’
retlw A’l’
retlw A’l’
where A’R’ represented the ASCII code for the letter R the label for this
sequence of retlw instruction
54
www.rejinpaul.com
55
www.rejinpaul.com
I2C Bus for Peripherals Chip Access– Bus operation-Bus subroutines– Serial EEPROM—
Analog to Digital Converter–UART-Baud rate selection–Data handling circuit–Initialization –
LCD and keyboard Interfacing -ADC, DAC, and Sensor Interfacing.
Introduction
Microprocessor based system design involves interfacing of the processor
with one or more peripheral device for the purpose of communication with
various input and output devices connected to it. During the early days of the
microprocessor revolution, these techniques required complex hardware
consisting of Medium scale integration devices, making the design highly
complex and time consuming. So, the manufacturers (INTEL) have developed a
large number of general and special purpose peripheral devices most of them
being single chip circuits. They are also programmable devices. Hence, these
peripheral devices are found to be of tremendous use to a system designer.
Peripheral devices, can broadly be classified into two categories.
(a) General purpose peripherals and
(b) Special purpose peripherals (Dedicated function peripherals)
General purpose peripherals are devices that perform a task, but may be
used for interfacing a variety of I/O devices to the microprocessor.
Simple I/O -- (Non-programmable)
Programmable peripheral Interface (PPI) – (8255)
Programmable Interrupt Controller – (8259)
Programmable DMA Controller – (8237/8257)
Programmable Communication Interface – (8251)
Programmable Interval Timer – (8253/8254)
Special function peripherals or devices that may be used for interfacing a
microprocessor to a specific type of I/O device. These peripherals are more
complex and therefore, relatively more expensive than general purpose
peripherals. The special function peripherals (Dedicated function peripherals)
are
Programmable CRT Controller
Programmable Floppy Disc Controller
Programmable Hard Disc Controller
Programmable Keyboard and display interface.
The functioning of these devices varies depending on the type of I/O
device they are controlling.
I2C Bus for Peripherals:
In the early 1980's, NXP Semiconductors developed a simple bi-
directional 2-wire bus for efficient inter-IC control. This bus is called the Inter-
IC or I2C-bus. At present, NXP's IC range includes more than 150 CMOS and
bipolar I2C-bus compatible types of performing communication functions
between intelligent control devices (e.g. Microcontrollers), general-purpose
circuits (e.g. LCD drivers, remote I/O ports, memories) and application-
oriented circuits (e.g. Digital tuning and signal processing circuits for radio and
First, the MCU will issue a START condition. This acts as an 'Attention'
signal to all of the connected devices. All ICs on the bus will listen to the bus
for incoming data. Then the MCU sends the ADDRESS of the device, it wants
to access, along with an indication whether the access is a Read or Write
operation had received the address, all IC's will compare it with their own
address. If it doesn't match, they simply wait until the bus is released by the
stop condition If the address matches, however, the chip will produce a
response called the ACKNOWLEDGE signal. Once the MCU receives the
acknowledge, it can start transmitting or receiving DATA. In our case, the MCU
will transmit data. When all is done, the MCU will issue the STOP condition.
This is a signal that the bus has been released and that the connected ICs may
expect another transmission to start any moment.
We have had several states on the bus in our example: START, ADDRESS,
ACKNOWLEDGE, DATA, STOP. These are all unique conditions on the bus.
Before we take a closer look at these bus conditions we need to understand a
bit about the physical structure and hardware of the bus.
As explained earlier, the bus physically consists of 2 active wires called SDA
(data) and SCL (clock), and a ground connection.
Both SDA and SCL are initially bi-directional. This means that in a particular
device, these lines can be driven by the IC itself or from an external device. In
order to achieve this functionality, these signals use open collector or open
drain outputs (depending on the technology).
The bus interface is built around an input buffer and an open drain or open
collector transistor. When the bus is IDLE, the bus lines are in the logic HIGH
state (note that external pull-up resistors are necessary for this which is easily
Forgotten). To put a signal on the bus, the chip drives its output transistor,
thus pulling the bus to a LOW level. The "pull-up resistor" in the devices as
seen in the figure is actually a small current source or even non-existent.
The nice thing about this concept is that it has a "built-in" bus mastering
technique. If the bus is "occupied" by a chip that is sending a 0, then all other
chips lose their right to access the bus. More will be explained about this in the
section about bus arbitration.
However, the open-collector technique has a drawback, too. If you have a long
bus, this will have a serious effect on the speed you can obtain. Long lines
present a capacitive load for the output drivers. Since the pull-up is passive,
you are facing an RC constant which will reflect on the shapes of the signals.
The higher this RC constant, the slower you can go. This is due to the effect
that it influences the slew rate of the edges on the I2C bus. At a certain point,
the ICs will not be able to distinguish clearly between a logic 1 and 0.
SDA (above) and SCL (below) with Rp = 10 kΩ and Cp = 300 pF. The SCL clock
runs with 100 kHz (nominal).
Bus Arbitration:
So far we have seen the operation of the bus from the master's point of view
and using only one master on the bus. The I2C bus was originally developed as
a multi-master bus. This means that more than one device initiating transfers
can be active in the system. When using only one master on the bus there is no
real risk of corrupted data, except if a slave device is malfunctioning or if there
is a fault condition involving the SDA / SCL bus lines. This situation changes
with 2 MCU's:
When MCU 1 issues a start condition and sends an address, all slaves will
listen (including MCU 2 which at that time is considered a slave as well). If the
address does not match the address of CPU 2, this device has to hold back any
activity until the bus becomes idle again after a stop condition.
As long as the two MCU's monitors what is going on on the bus (start and stop)
and as long as they are aware that a transaction is going on because the last
issued command was not a STOP, there is no problem.
Let's assume one of the MCU's missed the START condition and still thinks the
bus is idle, or it just came out of reset and wants to start talking on the bus
which could very well happen in a real-life scenario. This could lead to
problems. Since the bus structure is a wired AND (if one device pulls a line low
it stays low), you can test if the bus is idle or occupied.
When a master changes the state of a line to HIGH, it MUST always check that
the line really has gone to HIGH. If it stays low then this is an indication that
the bus is occupied and some other device is pulling the line low.
Therefore the general rule of thumb is: If a master can't get a certain line to go
high, it lost the arbitration and needs to back off and wait until a stop
condition is seen before making another attempt to start transmitting. The
rule says that a master loses arbitration when it cannot get either SCL or SDA
to go high when needed, this problem does not exist. It is the device that is
sending the '0' that rules the bus. One master cannot disturb the other
master's transmission because if it can't detect one of the lines to go higher, it
backs off, and if it is the other master that can't do so, it will behave the same.
This kind of back-off condition will only occur if the two levels transmitted by
the two masters are not the same. Therefore, let's have a look at the following
figure, where two MCUs start transmitting at the same time:
The two MCU's are accessing a slave in writing mode at address 1111001. The
slave acknowledges this. So far, both masters are under the impression that
they "own" the bus. Now MCU1 wants to transmit 01010101 to the slave,
while MCU 2 wants to transmit 01100110 to the slave. The moment the data
bits do not match anymore (because what the MCU sends is different than
what is present on the bus) one of them loses arbitration and backs off.
Obviously, this is the MCU which did not get its data on the bus. For as long as
there has been no STOP present on the bus, it won't touch the bus and leave
the SDA and SCL lines alone (yellow zone). The moment a STOP was detected,
MCU2 can attempt to transmit again.
Clock Synchronization:
All masters generate their own clock on the SCL line to transfer messages to
the I2C-bus. Data is only valid during the HIGH period of the clock. A defined
clock is therefore needed for the bit-by-bit arbitration procedure to take place.
this clock may not change the state of the SCL line if another clock is still
within its LOW period. The SCL line will therefore be held LOW by the device
with the longest LOW period. Devices with shorter LOW periods enter a HIGH
wait-state during this time.
Special Addresses and Exceptions:
In the I2C address, map there are so-called "reserved addresses". This section
contains some more details on these addresses and what they do. For
information about the Extended Addressing Mode, please refer to the
corresponding chapter.
Address R/W Designation
The same as above, but without the reset . This can be useful if the
0000-
state of the address select pins of a device is configurable. This way
0100
the device address will change.
If the LSB is set to 1:
A chip that conforms to the new standard receives two address bytes. The first
consists of the extended addressing reserved address, including the 2 MSB's of
the device address and the Read/Write bit. The second byte contains the 8
LSB's of the address. This scheme ensures that the 10 bit addressing mode
stays completely transparent for the other devices on the bus. Any new design
should implement this new addressing scheme.
Enhanced I2C (FAST Mode)
In the FAST mode, the physical bus parameters are not altered. The protocol,
bus levels, capacitive load etc. remain unchanged. However, the data rate has
been increased to 400 Kbit/s and a constraint has been set on the level of
noise that can be present in the system. To accomplish this task, a number of
changes have been made to the I2C bus timing.
High-speed I2C (HS-Mode)
High-speed mode (Hs-mode) devices offer a quantum leap in I2C-bus transfer
speeds. Hs-mode devices can transfer information at bit rates of up to 3.4
Mbit/s, yet they remain fully downward compatible with Fast- or Standard-
mode (F/S-mode) devices for bi-directional communication in a mixed-speed
bus system. With the exception that arbitration and clock synchronization is
not performed during the Hs-mode transfer, the same serial bus protocol and
data format is maintained as with the F/S-mode system. Depending on the
application, new devices may have a Fast or Hs-mode I2C-bus interface,
This first byte after a start condition will identify the slave on the bus (address)
and will select the mode of operation. The meaning of all following bytes
depends on the slave.
A number of addresses have been reserved for special purposes. One of these
addresses is reserved for the "Extended Addressing Mode". As the I2C bus
gained popularity, it was soon discovered that the number of available
addresses was too small. Therefore, one of the reserved addresses has been
allocated to a new task to switch to 10-bit addressing mode. If a standard slave
(not able to resolve extended addressing) receives this address, it won't do
anything (since it's not its address).
If there are slaves on the bus that can operate in the extended 10-bit
addressing mode, they will ALL respond to the ACK cycle issued by the master.
The second byte that gets transmitted by the master will then be taken in and
evaluated against their address.
Note: Even in 10-bit extended addressing mode, Bit 0 of the first byte after the
Start condition determines the slave access mode ('1' = read / '0' = write).
The protocol syntax is the same as transmitting a byte to a slave, except that
now the master is not allowed to touch the SDA line. Prior to sending the 8
clock pulses needed to clock in a byte on the SCL line, the master releases the
SDA line. The slave will now take control of this line. The The line will then go
high if it wants to transmit a '1' or, if the slave wants to send a '0', remain low.
(1)All the master has to do is generate a rising edge on the SCL line
(2) read the level on SDA
(3) and generate a falling edge on the SCL line
(4). The slave will not change the data during the time that SCL is high.
(Otherwise a Start or Stop condition might inadvertently be generated.)
During (1) and (5), the slave may change the state of the SDA line.
In total, this sequence has to be performed 8 times to complete the data byte.
Bytes are always transmitted MSB first.
The meaning of all bytes being read depends on the slave. There is no such
thing as a "universal status register". You need to consult the data sheet of the
slave being addressed to know the meaning of each bit in any byte transmitted.
Getting Acknowledge from a Slave Device:
When an address or data byte has been transmitted onto the bus, then this
must be ACKNOWLEDGED by the slave(s). In case of an address: If the address
matches its own, then that slave and only that slave will respond to the
address with an ACK. In case of a byte transmitted to an already addressed
slave then that slave will respond with an ACK as well.
The slave that is going to give an ACK pulls the SDA line low immediately after
reception of the 8th bit transmitted, or, in case of an address byte, immediately
after evaluation of its address. In practical applications this will not be
noticeable.
This means that as soon as the master pulls SCL low to complete the
transmission of the bit
(1), SDA will be pulled low by the slave
(2). The master has now issued a clock pulse on the SCL line
(3). The slave will release the SDA line upon completion of this clock pulse
(4). The bus is now available again for the master to continue sending data or
to generate a stop condition.
In case of data being written to a slave, this cycle must be completed before
a stop condition can be generated. The slave will be blocking the bus (SDA kept
low by slave) until the master has generated a clock pulse on the SCL line.
After transmission of the last bit of the master (1) the slave will release the SDA
line.
ACKnowledge:
Condition Can Only Occur...
Not acknowledge (NACK) After a master has read a byte from a slave
No Acknowledge Condition:
This is not exactly a condition. It is merely a state in the data flow between
master and slave. If, after transmission of the 8th bit from the master to the
slave, the slave does not pull the SDA line low, then this is considered a No
ACKcondition.
A test for a "stuck bus" can be performed in the stop condition cycle.
The SCL pin must have an open drain output while the SDA pin must be either
an input or have an open drain output, the I2C bus subroutine will repeatedly
access TRISC, the data direction register for PORTC. However, TRISC is located
at the bank 1 address, H‘87‘, which cannot be accessed by direct addressing
without first executing the instruction
Then changing the required bit of TRISC, and finally reverting back to Bank 0
with
Bcf STATUS,RP0
Instead of doing this, load the indirect pointer FSR, with the address of TRISC
and then do the required bit setting and bit clearing of TRISC bits indirectly.
The equates and variables needed for the I2C subroutines are listed in Figuer
9.8 DEVADD is the selected peripheral chip‘s 7-bit address on the I2C bus
shifted left one place to align it for use as a control byte. INTADD is a selected
registered memory address inside the selected peripheral chip by an I2C output
subroutine, I2Cout.DATAIN is the repository for the byte of data retrived by an
I2C input subroutine, I2Cin, from the selected register in the selkected
peripheral chip.
The I2Cin subroutine of figure 9.9 is similar to the I2Cout subroutine. It calls
the Start subroutine and then the TX subroutine twice to send DEVADD (Plus
R/W = 0) and INTADD. Then it calls the Start subroutine to send (plus R/W =
1), the RX subroutine to read back a byte(with NOACK), and finally the Stop
subroutine.
noexpand
delay macro freq4, ferq 10, freq20
if freq==4
fill (nop), freq4
endif
if freq==10
fill (nop), freq10
endif
if freq==20
fill (nop), freq20
endif
endm
Figuer 9.7 (a) macro definition
Cblock
.
.
.
DEVADD ;device I2C address X2
INTADD ;Internal address
DATAOUT ;Data to be written into INTADD during a writ
DATAIN ;Data to be read from INTADD during a read
TXBUF ;Buffer for each byte sent by TX
RXBUF ;Buffer for each byte received by RX
.
.
.
endc
Figuer: 9.8 I2C variables
;;;;;;;;;;I2C Subroutine;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The I2Cout subroutine transfer out three bytes: DEVADD, INTADD and DATAOUT
I2Cout
Call start ; generate START condition
Movf DEVADD,W ;send peripheral address with R/W=0(write)
Call TX
Movf INTADD,W ;send the peripheral’s internal address
Call TX
Movf DATAOUT,W ;send data to write to peripheral
Call TX
Call STOP ; generate STOP condition
Return
I2Cin
call START ;generat the START condition
movf DEVADD,W ;send peripheral address with R/W = 0
call TX
movf INTADD,W ;send peripherals internal address
call TX
call Restart ;Restart
movf DEVADD,W ;send peripheral address
iorlw B’00000001’ ; with R/W=1 (Read)
call TX
bsf TXBUFF,7 ;NOACK the following read of one byte
call RX ;Read byte
movf DATAIN ;into DATAIN
call STOP ;generate STOP condition
return
; the START subroutine initializes the I2C bus and then generate the START condition on the
;I2C bus
; the Restart entry point bypasses the initialization of the I2C bus
Start
; the stop subroutine generate the stop condition on the I2C bus
Stop
Bcf INDF,SDA ;return SDA low
Bsf INDF,SCL ;drive SCL high
Delay 0,1,2 ;t : STOP
Bsf INDF,SDA ; and then drive SDA high
Return
; The TX subroutine sends the byte passed to it in W and returns Z=1 if ACK occurs & Z=0 if
NOACK occurs
TX
Movwf TXBUFF ;save parameter inTXBUFF
Bsf STATUS,C ;rotate a one through TXBUFF to count bits
TX_1
Rlf TXBUFF,F ;rotate TXBUFF left,through carry
Movf TXBUFF,F ;set Z bit when all eight bits have been transferred’
Btfss STATUS,Z ;until Z=1
Call Bitout ;send carry bit, then clear carry bit
Goto TX_1 ; then do it again
Call Bitin ; read acknowledge bit into bit 0 of RXBUFF
Movlw B’0000001 ; read acknowledge bit
Andwf RXBUFF,W ;Z=1 if ACK; Z=0 if NOACK
Return
; the RX subroutine recives a byte from the I2C bus into W, using RXBUFF buffer
; call RX with bit 7 of TXBUFF clear for ACK
;Call RX with bit 7 of RXBUFF set for NOACK
RX
Movlw B’00000001’ ; rotate a one through RXBUFF to the carry bit to count bits
Movwf RXBUFF
RX_1
Rlf RXBUFF,F ;shift previous bits left
Call Bitin ; read a bit fromSDA in to bit 0 of RXBUFF
Btfss STATUS,C ;C=1 yet
Got to RX_1 ; no do it again
Rlf TXBUFF,F ;move bit 7 of TXBUFF to carry bit
Call Bitout ; and from there to SDA as acknowledgement
Movf RXBUFF,W ;put received byte in to W
Return
Bitout
Bdf INDF,SDA ;copy carry bit to SDA
Btfsc STATUS,C
Bsf INDF,SDA
Bsf INDF,SCL ;puls clock line
Delay 0,1,2
Bcf INDF,SCl
Bcf STATUS,C ;Clear carry bit
Return
bitin
bsf INDF,SDA ;release SDA line
Bsf INDF,SCL ;drive clock line high
Bcf RXBUFF,0 ;copy SDA to bit 0 of RXBUFF
Btfsc PORTC,SDA
Bsf INDF,SCl ;drive clock line low again
Return
;;;;;;;;;;;;;;;;;;; end of I2C subroutine;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Serial EEPROM:
EEPROM technology supplies nonvolatile storage of variables to a PIC
controlled device or instruments. That is variables stored in an EEPROM will
remain there even after power has been turned off and then on again. Some
instruments use an EEPROM to store calibration data during manufacture. In
this way, each instrument is actually custom built, with customization that can
easily automate. Other instruments use an EEPROM to allow a user to store
several sets for setup information. For an instrument requiring a complicated
setup procedure, this permits a user to retrieve the setup for any one of several
very different measurements. Still other devices use an EEPROM in a way that
is transparent to a user, providing backup of setup parameters and thereby
bridging over power outages.
The device with its interface circuit is illustrate in figure 9.15 the WP (write
protect) pin permits a manufacturer to program a part with calibration
constants ( with WP low) and then to permit only reads thereafter ( with WP
tied high).
The EEPROM makes use of an internal address pointer that is set during the
second byte of a ―write‖ message string, as show in the figure 9.16a and 9.16b.
if further bytes are transmitted before the STOP condition, as in figure 9.16b,
they will accept as the data to be write into the selected internal address. The
reception of the STOP condition triggers the programming of these bytes into
the select addresses.
While the EEPROM is doing its autonomous programming operation, it will
acknowledge another write command. Because of this, the acknowledge bit can
be used as a flag to determine when the programming operation has been
completed. Simply send out the slave address with the write bit low and check
whether the ACK bit is pulled low by the EEPROM. Until it does get pulled low
in acknowledgment, the START condition followed by the same byte can be
sent repeatedly and the ACK bit tested. With a typical programming time of
2ms,, programming of many bytes can take place as rapidly as possible, faster
than simply allowing the 10ms worst-case write time to expire.
This EEPROM includes a page write buffer for writing upto 8bytes
simultaneously with the single write message string show in the figure 9.16b.
within 10ms after the STOP condition is received by the EEPROM, all of the
transmitted bytes will be programmed. However, all eight addresses are
constrained to have the same upper 5 bits. That is, only the lower 3 bits of
EEPROM internal address counter are incremented when more than one data
byte is included in a write command sequence. For example, if the EEPROM
address sent in the second byte of the write command is B‘00010110‘ and if
that address byte is followed by three data bytes and the STOP condition, then
the first of the three data bytes will be written into address B‘00011000‘. The
second into B‘00010111‘, and the third into B‘00010000‘
Reading any number of bytes of data from selected EEPROM address requires
that a starting address first be sent to EEPROM with the write message string
of figure 9.16a. this string is followed by the message string of figure 9.16c,
consisting of the START condition, a read command, and than a read of data
from consecutive address sent back by the EEPROM. The PIC signals the
EEPROM to send no further bytes by not pulling the SDA line low during the
last acknowledge but time. The sending of the STOP condition by the PIC
completes the message string.
• Data transfer may be initiated only when the bus is not busy.
• During data transfer, the data line must remain stable whenever the clock
line is high. Changes in the data line while the clock line is high will be
interpreted as a Start or Stop condition.
When the bus is not busy both the data and the clock line remains high
For data transfers high-to-low transition of the SDA line while the clock
(SCL) is high determines a Start condition. All commands must be preceded
by a Start condition
To stop the data transfer at low-to-high transition of the SDA line while the
clock(SCL) is high determines a Stop condition. All operations must be
ended with a Stop condition.
Each data transfer is initiated with a Start condition and terminated with a
Stop condition. The number of data bytes transferred between the Start and
Stop conditions is determined by the master device and is, theoretically,
unlimited (although only the last sixteen will be stored when doing a write
Device Addressing
A control byte is the first byte received following the Start condition from
the master device. The control byte consists of a four-bit control code. For
the 24XX01, this is set as ‗1010‘ binary for read and write operations. The
next three bits of the control byte are ‗don‘t care for the 24XX01. The last bit
of the control byte defines the operation to
be performed. When set to ‗1‘, a read operation is selected. When set to ‗0‘,
a write operation is selected. Following the Start condition, the 24XX01
monitors the SDA bus checking the device type identifier being transmitted.
Upon receiving a ‗1010‘ code, the slave device outputs an Acknowledge
signal on the SDA line. Depending on the state of the R/W bit, the 24XX01
will select a read or write operation.
WRITE OPERATION
Byte Write
Following the Start condition of the master, the device code (4 bits), the
block address (3 bits, don‘t care) and the R/W bit, which is a logic low, is
placed onto the bus by the master transmitter. This indicates to the
addressed slave receiver that a byte with a word address will follow after it
has generated an Acknowledge bit during the ninth clock cycle. Therefore,
the next byte transmitted by the master is the word address and will be
written into the address pointer of the 24XX01.
Page Write
The write control byte, word address and first data byte are transmitted
to the 24XX01 in the same way as a byte write. However, instead of
generating a Stop condition, the master transmits up to 8 data bytes to the
24XX01, which are temporarily stored in the on- chip page buffer and will
be written into the memory once the master has transmitted a Stop
condition.
ACKNOWLEDGE
ADDRESSING
The 7-bit address of an I2C device and the direction of the following data is
coded in the first byte after the start condition:
A ―0‖ on the least significant bit indicates that the master will write
information to the selected Slave address device; a ―1‖ indicates that the
master will read data from the slave.
The typical functions that serial EEPROMs are utilized for being:
• Memory storage of channel selectors or analog controls (volume, tone, etc.) in
consumer electronics products
• Power down storage and retrieval of events such as fault detection or error
diagnostics in automotive products
• Electronic real time event or maintenance logs such as page counting in office
automation products. Also, configuration or DIP switch storage in office
automation products
• Last number redial storage and speed dial number storage in telecom
products
• User in-circuit reprogrammable look up tables such as bar code readers,
point-of-sale terminals, environmental controls and other industrial products
Temperature Sensor
The LM75 chip also include thermal watchdog that can be set up to
interrupt the PIC on its RB0/INT edge triggered interrupt input when the
temperature rises past a programmable setpoint. TOS ( os stands fot over
temperature shut down) . it includes programmable hysteresis so that the
temperature must dip down below the setpoint TOS threshold to a lower
THYST threshold before rising again past the TOS setpoint to generate another
output edge.
The chip includes a power on reset circuit that defaults to the operation
show in the figure 9.12b. at the power on time the PIC may come out of the
reset first; therefore, it is necessary to insert a delay before initializing the
LM75‘s thermal watchdog circuitry. Otherwise the PIC‘s commands to the
LM&% may go unnoticed
The register structure of the LM75 is show in the figure 9.13. when a ―write‖
message string is transmitted to the chip, the first byte select the chip for a
write and then the second byte loads the pointer register. The write message
string can stop there( illustrate in figure 9.14a), or it can continue with a 2-
byt write of 1000F = 75.50C to the TOS register (illustrate infiguer9.14b). once
the pointer has been set, any of these register can be read, reading 2 bytes
for temperature. TOS or THYST or reading just 1 byte for the configuration
registers.
If the thermal watchdog function of the LM75 is not use , then advantage
can be taken of the power-on default clearing of the poiinter and
configuration register. In this case the interaction with the chip need be no
more than successive reads of the temperature. As in the figure 9.14C
The OS output operates in either of two modes, comparator or interrupt.
The host controls the temperature at which the alarm is asserted (TOS) and
the hysteresis temperature below which the alarm condition is not valid
(THYST). Also, the LM75‘s TOS and THYST registers can be read by the
host. The address of the LM75 is set with three pins to allow multiple
devices to work on the same bus. Power-up is in comparator mode, with
defaults of TOS = +80°C and THYST = +75°C. The 3.0V to 5.5V supply
voltage range, low supply current, and I2C interface make the LM75 ideal
for many applications in thermal management and protection.
Applications
Thermal System Management
Thermal Protection
Test Equipment, Computers and Office Electronics
8-bit conversion
8 analog input channels
An analog multiplexer
A sample and hold circuit for signal on the selected input channel
Alternative clock sources for carrying out conversion
Adjustable sampling rate
Choice of an internal or external reference voltage
ADC Characteristic:
The PIC analog to digital converter has the idealized transfer funcation show in
the figure 10.1. it converts an input voltage to an 8 bit number. The input
voltage is scaled against a reference voltage,VRef
The reference voltage that is best suited for many application is the PIC supply
voltage VDD select as one option and connected to the ADC internally, there by
avoiding the need to dedicated a pin to this role. For a transducer whose
output is proportional to its own supply voltage and that uses the PIC supply
voltage as its own supply voltage, making VRef equal to VDD ia an ideal choice.
For other applications, using VDDas the refrence voltage offers the largest
possible analog voltage input range, since proper ADC operation requires
Given this choice of VRef = VDD the PIc parts ca assign up to eight pins to serve
as analog inputs to the ADC, using the pins and the register initializations
show in the figure 10.2 any pin that is assigned to serve as an analog and
digital input to the ADC can be read as a digital input by reading the
appropriate port pin or used as an analog input by selecting it as the input
channel to the ADC
The default power-on state of ADCON!(H‘00‘) powers down the digital I/O
circuitry for the five pins labeled ―analog input or digital I/O‖, there by making
them ―analog input only‖ pins
For some applications, the use of an external voltage reference of 3.0V provides
the greatest possible resolution in the output.. this is particularly useful for
half of one of the voltage steps of figure 10.1. The larger resistance of the
source being measured, the longer this charging time will become. The
relationship is shown in figure 10.5b. A high source resistance can be
converted to a low source resistance with the help of the op amp ―follower‖
circuit of figure 10.5c.
After waiting out the sample time, a conversion can be initiated. The ADC
circuit will open the sampling switch and carry out the conversion of the input
voltage as it was at the moment the switch was opened. Upon completion of the
conversion, the sampling switch is closed and Vhold again tracks Vsource.
If the ADC is used to sample a single channel at equally spaced intervals, this
can be done automatically under interrupt control. The timer‘s CCp module
can be used with Timer1 to initiate periodic ADC conversion of the select
channel. In addition, the ADC is set up to generate an interrupt when the
conversion has been completed. After this process has been setup and begun,
the CPU simply deals with each sample as it becomes available.
ADC USE:
Register ADCON1, TRISA, and TRISE must be initialized to select the refrence
voltage and the input channel desired, as described by figure 10.2 and 10.3.
then ADCON0 is initialized with the step listed in figure 10.6. the first step is
select the ADC clock source from among the four choices. The choice show in
the figure 10.6a provide the highest rate consistent with the constrain that the
ADC clock period must be 1.6µs or greater. The RC choice (made by setting
ADCON0[7:6] to 11) is designed for use with PIC being clocked by a relatively
slow clock. It lets the ADC run at a nominal 250kHz rate.
If just one analog input is to be used, it can be selected once and for all by
combining the channel selection of figuer10.6b with the ADC clock period
selection of figure 10.6a. if several analog channels are to be used, it is
important to remember to wait for the sample time discussed in conjunction
with figure 10.5. that is, select the channel, wait out the required sample time ,
and the initiate the conversion by setting the GO_DONE bit of ADCON0. When
GO_DONE =0 again, the result from ADRES(H‘1E‘), the ADC result register
It is a serial communication interface which uses two lines for sending (TX) and
receiving (RX) data. As its name indicates it is an
asynchronous communication interface, which means it doesn‘t need to send
clock along with it as in synchronous communications. UART is the
communication standard of our old computer‘s RS-232 serial port. Most of the
Microchip‘s PIC Microcontrollers have built in USART Module. USART stands
for Universal Synchronous Asynchronous Receiver Transmitter. It can be
configured in the following Modes:
Bit 7 CSRC : Clock Source Select Bit, this bit has no application in the
Asynchronous mode operation of USART module. It is used to select
master or slave mode in Synchronous mode operation.
Bit 6 TX9 : When this bit is set it enables the 9 bit transmission
otherwise 8 bit transmission is used. 9th bit in the 9 bit transmission
mode is commonly used as parity bit.
Bit 4 SYNC : This is the USART Mode select bit. Setting this bit selects
Synchronous mode while clearing this bit selects Asynchronous mode.
Bit 2 BRGH : This is the High Baud Rate Select bit for Asynchronous
mode operation and is unused in Synchronous mode. Setting this bit
selects High Speed and clearing this bit selects Low Speed baud rates.
You will can see the baud rate calculation later in this article.
Bit 1 TRMT : This is the Transmit Shift Register (TSR) status bit. This
can be used to check whether the data written to transmit register is
transmitted or not. When the TRS is empty this bit is set and when the
TSR is full this bit will be 0.
Bit 0 TX9D : This is the 9th bit of data in the 9 bit transmission mode.
This is commonly used as parity bit.
Bit 7 SPEN : Serial Port Enable bit. Setting this bit enables serial port
and configures RC7, RC6 as serial port pins.
Bit 6 RX9 : Setting this bit enables 9 bit reception otherwise it will be in
8 bit reception mode.
Bit 5 SREN : Single Receive Enable bit. This bit has no effect on
Asynchronous mode and Synchronous Slave mode. Setting this bit will
enables Single Receive. This bit will cleared after the reception is
complete.
Bit 4 CREN : Continuous Receive Enable bit. Setting this bit will enable
Continuous Receive. In the Synchronous Mode CREN overrides SREN.
Bit 3 ADDEN : Address Detect Enable bit. This bit is applicable only in
Asynchronous 9 bit mode. Setting this bit enables Address Detect.
Bit 2 FERR : Framing Error bit. 1 at this bit stands for Framing Error
while 0 stands for No Framing Error.
Bit 1 OERR : Overrun Error bit. A high at this bit indicates that Overrun
error has occured.
Bit 0 RX9D : This is the 9th bit of Received Data and is commonly used
as Parity Bit.
Baud Rate Generator provides the required clock for the data transmission and
reception. USART module has a dedicated 8 bit baud rate generator which
supports both Synchronous and Asynchronous modes. The 8-
bit SPBRG register controls the time period of this free running timer. In
Asynchronous mode BRGH, 2nd bit of TXSTA register also controls the
generated baud rate but in Synchronous mode it is ignored. Baud Rate can be
calculated from the following equations, where FOSCis the clock frequency of the
microcontroller.
An example of the transmission of 4 bytes is show in the figure 11.1. each 8 bit
is framed by a START bit and STOP bit. For transmission at 9,600Bd, each of
these bits lasts for a bit time(BT) of 1/9,600 second. Before the first frame is
transmitted, the line from the transmitter‘s TX output to the recivers‘S RX
input idles high. The receiver monitors its RX input, waiting for the line to drop
low because of the transmission of the (low) START bit. The receiver
synchronizes on this high to low transition. Then the receiver reads the 8 bit of
serial data by sampling the RX input at 1.5BT,2.5BT, 3.5BT, 4.5BT, 6.5BT,
7.5BT, AND 8.5BT as show in the figure 1101. It checks that the framing of
The byte has been interpreted correctly by reading the high STOP bit at 9.5BT.
if the RX line is actually low at this time , for whatever the reason, the receiver
sets a flag to indicate a framing error. Regardless of whether or not a framing
error occurs, the receiver then begins again, resynchronizing upon the next
high to low transition of the RX line. Because of this resynchronization, the
receiver can generate its own baud-rate clock that only approximate the
transmitter‘s baud-rate clock and yet the reciver can recover the serial data
perfectly.
The PIC‘s baud-rate clock operates at either of two ranges, called high
speedbaud rate and low speed baud rate. Using the low speed baud rate, the
reciver looks for STOP to START transition by sampling its RX input every
1/16th of one of its bit times, as show in the figure 11.2, then it counts six
more of these sample times to point where it reads a cluster of three closely
spaced samples of RX and votes among them to ensure that it is seeing the low
START bit. There after, it reads successive clusters of three samples spaced 16
sample time apart.in effect, the reciver is reading its input every 16 periods of
its sample clock.
BAUD-RATE Selection:
A desired baud rate can be approximated by the UART‘s baud rate generator. If
the crystal clock rate were selected to be carefully chosen multiple pf the
desired baud rate, then the baud rate generator would produce the desired
baud rate exactly. The clock rates used by microchip to characterized the three
speed grades of their parts
These speed grades do not provide exact multiples of the popular 9,600Bd and
19,200Bd rate commonly used by personal computer serial port. However, the
flexibility of the baud rate generator circuitry permits close approximation to
both 9,600 Bd and 19,200Bd with any of the standard clock rates. The baud
rate is derived from the crystal rate using an 8 bit presentable divider and a
fixed diver of either 16 or 64, as show in the figure 11.4b. the results are
tabulated in the figure 11.4a. even in the worst case , the percent error of the
approximate baud rate is only one-third of the percent error that cannot be
tolerated by the UART.
The transmitted data circuit show in the figure 11.5a. to transmit a byte of
data serially from TX pin, the byte written to TXREG register. Assuming there
is not already data in TSR (transmit shift register), the content of TXREG will
be automatically transferred to the TSR, making TXREG available for a second
byte even as the first byte is being shifted out of the TX pin, framed by START
and STOP bits
The receiver data circuit is similar, with received data shifted into RSR(receiver
shift register) when it is in place, the STOP bit is checked and an error flag is
set if the STOP bit does not equal one. In any case, the receiver byte is
automatically transferred into a 2 byte FIFO. If the FIFO was initially empty,
the receiver byte will fall through to the RCREG (receiver register) virtually
immediately, where it is ready to be read by the CPU. If the CPU is slow in
reading the RCREG, a second byte can be received at the RX pin. When it is in
place in the RSR, it will follow the first byte into the 2 byte FIFO. At that point,
the FIFO is full. If the third byte enters the RX pin and is shifted all the way
across the RSR before at least one of the two bytes in the FIFO has been read,
then the new byte will be lost. An overflow error flag will be set, alerting the
receiver software of the loss of a byte of data
UART Initialization:
The register involved with UART is show in the figure 11.6. the data direction
bit associated with the RC6/TX pin and the RC7/RX pin must both be set up
as inputs, with ones in bits 6 and 7 of the TRISC register, the setting of these
two bits disable the general I/O port output circuitry associated with these two
pins( the handling of these bits of TRISC stands in contrast to the clearing of
bits 3 and 5 of TRISC in support of the serial peripheral interface output pins)
The UART‘s baud rate and its transmit and receive funcations are initialized by
writes to SPBRG, TXSTA, and RCSTA, as show in figures 11.4 and 11.6 at 9600
Bd, each transfer takes about a millisecond, so sending or receiving a string of
characters is best carried out under interrupt control. The flag and interrupt
enable bits of the PIR1,PIE1 and INTCOON register control the timing of CPU
interactions with the UART.
UART Use:
Given this setup, the PIC will respond to PCIF interrupts by reading each byte
from the RCREG register sent by the PC. The RCIF flag will clear itself when the
byte read from RCREG leaves the receive circuit‘s FIFO empty.
The PIC sends out a string of bytes by writing them, one by one under
interrupt control, to TXREG , the TXIF flag takes care of itself, clearing
automatically when TXREG is written to, and setting again as the data written
TXREG are automatically transferred to the transmit shift register, at the
completion of sending the string of bytes to the PC, the TXIE bit in the PIE1
register is cleared to disable further ‖transmit‖ interrupts until another string
needs to be sent to the PC
Another application of the PIC UART is to couple two PIC‘s together. In this way
some of the work that would be done by one PIC is off loaded to a second PIC,
figure 11.8 show s this connection of two PIC‘s, using the maximum possible
baud rate to obtain fast coupling between the two PICs with in 40 internal
clock cycles, what is written into one PIC‘s TXREG register appears in the
other PIC‘s RCREG register.
Carrying out transfers at this fast rate calls for some precautions is need if
overrun error are to avoided, given PICs that are try to carry out the task in
additional to monitoring the UART‘s RCREG register. A given PIC can only
receive 2 bytes into its FIFO without reading them immediately. Any further
bytes received will be discard until the earlier bytes are read out of the FIFO,
making room for new bytes
Keypad:
A keypad is a matrix of push-buttons aligned in rows and columns to provide
user input, a 3x4 keypad is basically formed like the following schematic:
it consists of 3 columns and 4 rows, hence the name, whenever the user taps
one button, the column and the row corresponding to that button are shorted,
for example, if a user chooses to tap the "5" button, col2 and row2 are shorted,
if a users picks "0" button, col2 and row4 are shorted, and so on. the method
used to read such input is called Scanning, you connect the rows and the
columns to the microcontroller, you set the columns as outputs, rows as
inputs (or vice versa) and you start feeding a pattern of inputs to the columns
while checking the readings of the rows.
For example, if you tied all rows through pull-up resistors to Vcc, all row
inputs are typically 1s (1111), now if you start feeding columns with the
following pattern, 011, 101, 110, you can check which row conducts the 0
signal to the controller, thus, you'll detect the shorted row and column.
example: if a user presses button "6", when I feed the columns by 011, the
rows output would be 1111, when I feed it by 101, the rows output would be
also 1111, but when I feed it by 110, button "6" will conduct the 0 signal, thus
my rows output would be 1011, Knowing that I used col3 to conduct the 0
signal, and I got the 0 signal at row2, I'll know the pressed button is "6".
kbd.c library uses either port D or port B to interface the keypad, you'll find the
schematic for the interface further below, but if you need to use other ports to
interface your keypad, you'll have to use a user defined library, you can find
those on ccsinfo.com forum, or you can write your own library (which is pretty
simple).
Note: the kbd_getc(); function returns the ASCII code of the pressed button
(returns a character).
K : Cathode (0V)
used to turn ON/OFF the backlight LED of your LCD.
After including the flex_lcd.c library in your code, you should initialize the LCD
at the beginning of your main code using the function lcd_init();
Then you can use the function lcd_putc("Your String"); to display your
strings.
The Schematic:
/*
PIC16F877A interface to 3x4 Keypad and 16x2 LCD
Badr Ghatasheh
[email protected]
*/
#include <16f877a.h>
#fuses XT,NOLVP,NOWDT,NOPROTECT
#use delay(clock=4000000)
#include "flex_lcd.c"
#include <kbd.c>
void main()
{
char k=0; // Character k to store keypad input
delay_ms(1);
/* Peripherals Configurations */
lcd_init(); // Turn LCD ON, along with other initialization
commands
kbd_init(); // Initialize Keypad
UNIT IV
INTRODUCTION TO ARM PROCESSOR
ARM Architecture –ARM programmer‟s model –ARM Development tools-
Memory Hierarchy –ARM Assembly Language Programming–Simple
Examples–Architectural Support for Operating systems
ARM introduction:
The first ARM processor was developed at Acorn Computers Limited, of
Cambridge, England, between October 1983 and April 1985. At that time,
and until the formation of Advanced RISC Machines Limited (which later was
renamed simply ARM Limited) in 1990, ARM stood for Acorn RISC Machine.
Acorn had developed a strong position in the UK personal computer market
due to the success of the BBC (British Broadcasting Corporation)
microcomputer. The BBC micro was a machine powered by the 8-bit
6502microprocessor and rapidly became established as the dominant
machine in UKschools following its introduction in January 1982 in support
of a series of television programmers broadcast by the BBC. It also enjoyed
enthusiastic support in the hobbyist market and found its way into a number
of research laboratories and higher education establishments. Following the
success of the BBC micro, Acorn's engineers looked at various
microprocessors to build a successor machine around, but found all the
commercial offerings lacking. The 16-bit CISC microprocessors that were
available in 1983 were slower than standard memory parts. They also had
instructions that took many clock cycles to complete (in some cases, many
hundreds of clock cycles), giving them very long interrupt latencies. The BBC
micro benefited greatly from the 6502's rapid interrupt response, so Acorn's
Designers were unwilling to accept a retrograde step in this aspect of the
processor's performance. As a result of these frustrations with the
commercial micro processor offerings, the design of a proprietary
microprocessor was considered. The major stumbling block was that the
Acorn team knew that commercial microprocessor projects had absorbed
hundreds of man-years of design effort. Acorn could not contemplate an
investment on that scale since it was a company of only just over 400
employees in total. It had to produce a better design with a fraction of the
design effort, and with no experience in custom chip design beyond a few
small gate arrays designed for the BBC micro. Into this apparently impossible
scenario, the papers on the Berkeley RISCI fell like a bolt from the blue. Here
was a processor which had been designed by a few postgraduate students in
under a year, yet was competitive with the leading commercial offerings. It
was inherently simple, so there were no complex instructions to ruin the
interrupt latency. It also came with supporting arguments that suggested it
could point the way to the future, though technical merit, however well
supported by academic argument, is no guarantee of commercial success.
The ARM, then, was born through a serendipitous combination of factors,
and became the core component in Acorn's product line. Later, after a
judicious modification of the acronym expansion to Advanced RISC Machine,
it lent its name to the company formed to broaden its market beyond Acorn‟s
product range. Despite the change of name, the architecture still remains
close to the original Acorn design
ARM Architecture:
Architectural overview
The ARM is a general purpose 32-bit microprocessor, which offers high
performance and very low power consumption. The ARM architecture is
based on Reduced Instruction Set Computer (RISC) principles, and the
instruction set and related decode mechanism are much simpler than those
of micro programmed Complex Instruction Set Computers (CISC). This
simplicity results in a high instruction throughput and impressive real-time
2
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
3
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
4
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
5
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
6
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
ARM Buses:
Register Bank is connected to ALU via two data paths.
A bus
B bus
B bus goes via Barrel Shifter. It preprocesses data from source register by
shifting left or right or even rotating. The Program Counter is that part of
register Bank , that generate address. Registers in register bank are
symmetric i.e., they can have both data and address. Program counter
generates address for next function.
Address Incrementer block, increments or decrements register value
independent of ALU. There is an Instruction Decode and control block that
provides control signals.
Pipeline
Pipeline is the mechanism used by the RISC processor to execute
instructions at an increased speed. This pipeline speeds up execution by
fetching the next instruction while other instructions are being decoded and
executed. During the execution of an instruction, the processor fetches the
instruction .It means loads an instruction from memory. And decodes the
instruction i.e. identifies the instruction to be executed and finally Executes
the instruction and writes the result back to a register.
Fetch: the instruction is fetched from memory and placed in the instruction
pipeline.
Decode:the instruction is decoded and the datapath control signals prepared
for the next cycle. In this stage the instruction 'owns' the decode logic but not
the datapath.
Execute: the instruction 'owns' the datapath; the register bank is read, an
operand shifted,the ALU result generated and written back into a destination
register.
7
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
The ARM processors are based on RISC architectures and this architecture
has provided small implementations, and very low power consumption.
Implementation size, performance, and very low power consumption remain
the key features in the development of the ARM devices.
Introduction
Our ARM has a 32-bit data bus and a 32-bit address bus. The data types the
Processor supports are Words (32 bits), where words must be aligned to four
Byte boundaries. Instructions are exactly one word, and data operations (e.g.
ADD) are only performed on word quantities. Load and store operations
can transfer words.
8
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
The ARM 7 architecture has a total of six different operating modes, as shown
below. These modes are protected or exception modes which have associated
interrupt sources and their own register sets
User: This mode is used to run the application code. Once in user mode the
CPSR cannot be written to and modes can only be changed when an
exception is generated.
FIQ: (Fast Interrupt reQuest) This supports high speed interrupt handling.
Generally it is used for a single critical interrupt source in a system
IRQ: (Interrupt ReQuest) This supports all other interrupt sources in a
system
Supervisor: A “protected” mode for running system level code to access
hardware or run OS calls. The ARM enters this mode after reset
Abort: If an instruction or data is fetched from an invalid memory region, an
abort exception will be generated
Undefined Instruction: If a FETCHED opcode is not an ARM instruction, an
undefined instruction exception will be generated
The User registers R0-R7 are common to all operating modes. However FIQ
mode has its own R8 –R14 that replace the user registers when FIQ is
entered. Similarly, each of the other modes have their own R13 and R14 so
that each operating mode has its own unique Stack pointer and Link register.
The CPSR is also common to all modes. However in each of the exception
modes, an additional register - the saved program status register (SPSR), is
added. When the processor changes the current value of the CPSR stored in
the SPSR, this can be restored on exiting the exception mode.
9
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
The format of the Program Status Registers is shown in Figure (3-2). The N,
Z, C and V bits are the condition code flags. The condition code flags in the
CPSR may be changed as a result of arithmetic and logical operations in the
processor and may be tested by all instructions to determine if the
Instruction is to be executed The I and F bits are the interrupt disable bits.
The I bit disables IRQ interrupts when it is set and the F bit disables FIQ
interrupts when it is set. The M0, M1, M2, M3 and M4 bits (M [4:0]) are the
mode bits, and these determine the mode in which the processor operates.
The interpretation of the mode bits is shown in Table (3-1). Not all
combinations of the mode bits define a valid processor mode. Only those
explicitly described shall be used.
Exceptions
ARM supports a range of interrupts, traps, and supervisor
calls – all are
grouped under the general heading of exceptions
Exceptions arise whenever there is a need for the normal flow of
program execution to be broken, the processor can be diverted to
handle the interrupt from the peripherals
So the processor move to handle the exceptions exception routine has
completed so that the original program can be resumed. Many
exceptions may arise at the same time
FIQ Exceptions
IRQ Exceptions
10
Abort Exceptions
(1) If the abort occurred during an instruction prefetch (a Prefetch Abort), the
prefetched instruction is marked as invalid but the abort exception does not
occur immediately
(2) If the abort occurred during a data access (a Data Abort), the action
depends on the instruction type.
(a) Single data transfer instructions (LDR, STR) will write back modified
base registers and the Abort handler must be aware of this.
(b) Block data transfer instructions (LDM, STM) complete. All register
overwriting is prevented after the Abort is indicated, which means in
particular that R15 (which is always last to be transferred) is preserved in an
aborted LDM instruction.
Software interrupt
The software interrupt instruction (SWI) is used for getting into Supervisor
mode, usually to request a particular supervisor function. When a SWI is
executed, ARM performs the following:
(1) Saves the address of the SWI instruction plus 4 in R14_svc; saves CPSR in
SPSR_svc
(2) Forces M[4:0]=10011 (Supervisor mode) and sets the I bit in the CPSR
(3) Forces the PC to fetch the next instruction from address 0x08
When the ARM comes across an instruction that it cannot handle, it offers it
to any coprocessors that may be present. If a coprocessor can perform this
instruction but is busy at that time, ARM will wait until the coprocessor is
ready or until an interrupt occurs. If no coprocessor can handle the
instruction then ARM will take the undefined instruction trap when ARM
takes the undefined instruction trap it performs the following:
(1) Saves the address of the Undefined or coprocessor instruction plus 4 in
R14_und; saves CPSR in SPSR_und.
(2) Forces M[4:0]=11011 (Undefined mode) and sets the I bit in the CPSR
(3) Forces the PC to fetch the next instruction from address 0x04
11
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
Vector Summary
Exception Priorities
When multiple exceptions arise at the same time, a fixed priority system
determines the order in which they will be handled:
(1) Reset (highest priority)
(2) Data abort
(3) FIQ
(4) IRQ
(5) Prefetch abort
(6) Undefined Instruction, Software interrupt (lowest priority)
12
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
13
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
The semi colon indicatres every thing right side to the semi colon is command
and it should be ignored by the assembler
The data processing instruction explain the data value in r1 and r2 is added
and the result is store in r0 register.
While eriting the assembly language source codecare must be need to write
the operands in the correct order that is result register in first followed by
first and second operands.
This instruction is executed and change the system state at the distination
register, optinally the N,Z,C 7 V flags in CPSR also change.
Arithmetic operations.
These instructions perform binary arithmetic (addition, subtraction and
reverse
subtraction, which is subtraction with the operand order reversed) on two
32-bit operands. The operands may be unsigned or 2's-complement signed
integers; the carry-in, when used, is the current value of the C bit in the
CPSR.
14
Comparison operations.
These instructions do not produce a result but just set the condition code
bits (N, Z, C and V) in the CPSR according to the selected operation.
Immediate operands:
Instead of adding two registers, we simply wish to add a constant to a register
we can replace the second source operand with an immediate value, which is
a literal constant, preceded by '#':
15
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
A right shift is in the opposite direction, the bits moving from the more
significant end to the lower end, or from left to right on the page. Again the
bits shifted out are lost, except for the last one which is put into the carry. If
the right shift is logical then zeros are shifted into the left end. In arithmetic
shifts, a copy of bit 31 (i.e. the sign bit) is shifted in.
n is the number of bit positions by which the value is shifted. It has the value
0..31. An LSL by one bit may be pictured as below:
After n shifts, n zero bits have been shifted in on the right and the carry is
set to bit 32-n of the original word.
LSR #n Logical shift right immediate
n is the number of bit positions by which the value is shifted. It has the value
1..32. An LSR by one bit is shown below:
After n of these, n zero bits have been shifted in on the left, and the carry flag
is set to bit n-1 of the original word.
ASR #n Arithmetic shift right immediate
n is the number of bit positions by which the value is shifted. It has the value
1..32. An ASR by one bit is shown below
16
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
If ' sign' is the original value of bit 31 then after n shifts, n 'sign' bits have
been shifted in on the left, and the carry flag is set to bit n-1 of the original
word.
ROR #n Rotate right immediate
n is the number of bit positions to rotate in the range 1..31. A rotate right by
one bit is shown below:
after n of these rotates, the old bit n is in the bit 0 position; the old bit (n-1) is
in bit 31 and in the carry flag.
RRX Rotate right one bit with extend
This special case of rotate right has a slightly different effect from the usual
rotates. There is no count; it always rotates by one bit only. The pictorial
representation of RRX is:
he old bit 0 is shifted into the carry. The old content of the carry is shifted
into bit 31.
Setting the condition codes:
Any data processing instruction can set the condition codes. The instruction
is executed conditionally, according to the status flags in the APSR, in exactly
the same way as the comparison operations only set the condition codes, An
17
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
Flag Explanation
We lost some data because the result did not fit into 32 bits, so
C=1
the processor indicates this by setting C (carry) to 1.
The result is 0, which is considered positive, and so
N=0
the N (negative) bit is set to 0.
From a two's complement signed-arithmetic
viewpoint, 0xffffffff really means -1, so the operation we did was
V=0
really (-1) + 1 = 0. That operation clearly does not overflow,
so V (overflow) is set to 0.
Z=1 The result is 0, so the Z (zero) bit is set to 1.
Multiplies :
•A special form of the data processing instruction supports multiplication
•Some important differences
– Immediate second operands are not supported
–The result register must not be the same as the first source register
– If the „S‟bit is set, the C flag is meaningless
18
19
The ARM and Thumb instruction sets include instructions that load and
store multiple registers. These instructions can provide a more efficient way
of transferring the contents of several registers to and from memory than
using single register loads and stores.
Multiple register transfer instructions are most often used for block copy and
for stack operations at subroutine entry and exit. The advantages of using a
multiple register transfer instruction instead of a series of single data transfer
instructions include:
Smaller code size.
A single instruction fetch overhead, rather than many instruction
fetches.
On uncached ARM processors, the first word of data transferred by a
load or store multiple is always a nonsequential memory cycle, but all
subsequent words transferred can be sequential memory cycles.
Sequential memory cycles are faster in most systems.
Stack Operations
•ARM use multiple load-store instructions to
operate stack
–POP: multiple load instructions
–PUSH: multiple store instructions
•Stack grows up or grows down
–Ascending, „A‟
–Descending, „D‟
•Full stack, „F‟: sp points to the last used address in the stack
•Empty stack, „E‟: sp points to the first unused address in the stack
20
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
21
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
•Problem
–If a subroutine wants to call another subroutine, the original return
address, r14, will be overwritten by the second BL instruction
•Solution
–Push r14 into a stack
–The subroutine will often also require some work registers, the old values in
these registers can be saved at the same time using a store multiple
instruction
22
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
Supervisor Calls
•SWI: Software Interrupt
•The supervisor calls are implemented in system software They
are probably different from one ARM system to another
Most ARM systems implement a common subset of calls in addition to any
specific calls required by the particular application
Jump tables
Call one
of a set of subroutines depending on a value computed by the
program
23
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
24
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
Memory Cost
Fast memory is more expensive per bit than slow memory, so a memory
hierarchy also aims to give a performance close to the fastest memory with an
average cost per bit approaching that of the slowest memory.
4.6.2 On-chip memory
On-chip RAM
25
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
26
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
27
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
28
29
Get useful study materials from www.rejinpaul.com
www.rejinpaul.com
UNIT -4 INTRODUCTION TO ARM PROCESSOR
30
address
mux
hit data
At the other extreme of associativity, it is possible to design a fully associative
cache in VLSI technology. Rather than continuing to divide the direct-mapped
cache ever smaller components, the tag store is designed differently using
content addressed memory (CAM). A CAM cell is a RAM cell with an inbuilt
comparator, so a CAM based tag store can perform a parallel search to locate
an address in any location. The organization of a fully associative cache is
illustrated in Figure Since there are no address bits implicit in the position of
data in the cache, the tag must store all the address bits apart from those used
to address bytes within the line.
Write strategies
There are more choices to make when the processor executes a write cycle. In
increasing order of complexity, the commonly used write strategies are: Write –
through
All write operations are passed to main memory
Write – through with buffered write
Write operations are passed to main memory through the write buffer
Copy – back (write – back)
Write operations update only the cache.
31
UNIT V
ARM ORGANIZATION
Register Bank which stores the processor state.it has 2 read ports, 1 write
ports, access any register ,1
additional read port, 1 additional write port for r15
,the program counter(PC)
The barrel shifter, which can shift or rotate one operand by any number of bits.
The ALU, which performs the arithmetic and logic functions required by
the instruction set.
The address register and incrementer, which select and hold all
memory addresses and generate sequential addresses when required.
The data registers, which hold data passing to and from memory.
The instruction decoder and associated control logic.
1
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
A[31:0] control
address register
P
C incrementer
PC
register
bank
instruction
decode
A multiply &
L register
U control
A B
b
u b b
s u u
s barrel s
shifter
ALU
D[31:0]
When the processor is executing simple data processing instructions the pipeline
enables one instruction to be completed every clock cycle. An individual instruction
takes three clock cycles to complete, so it has a three-cycle latency, but the through-
2
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
put is one instruction per cycle. The 3-stage pipeline operation for single-cycle
instructions is shown in Figure .
At any time slice, 3 different instructions may occupy each of these stages, so the
hardware in each stage has to be capable of independent operations
When the processor is executing data processing instructions , the latency = 3
cycles and the throughput = 1 instruction/cycle
3
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
The simplest way to view breaks in the ARM pipeline is to observe that:
• All instructions occupy the datapath for one or more adjacent cycles.
• For each cycle that an instruction occupies the datapath, it occupies the decode
logic in the immediately preceding cycle.
• During the first datapath cycle each instruction issues a fetch for the next instruction
but one.
• Branch instructions flush and refill the instruction pipeline.
• Decode
The instruction is decoded and register operands read from the register file. There are
three operand readports in the register file, so most ARM instructions can source all
their operands in one cycle.
• Execute
An operand is shifted and the ALU result generated. If the instruction is a load or
store the memoryaddress is computed in the ALU.
• Buffer/data
Data memory is accessed if required. Otherwise the ALU result is simply buf- fered
for one clock cycle to give the same pipeline flow for all instructions.
• Write-back
The results generated by the instruction are written back to the register file, including
any data loaded from memory.
next
pc
+4
I-cache fetch
pc + 4
pc + 8
I decode
r15 instruction
decode
register read
immediate
fields
mul
LDM/
STM post -
+4 index reg
shift shift
pre-index
execute
ALU forwarding
paths
mux
B, BL
MOV pc
SUBS pc
byte repl.
D-cache buffer/
load/store
address data
rot/sgn ex
LDR pc
5
This 5-stage pipeline has been used for many RISC processors and considered to be
the classic way to design such a processor.
There is a case where, even with forwarding, it is not possible to avoid a pipeline
stall. Consider the following code sequence:
The processor cannot avoid a one-cycle stall as the value loaded into rN only enters
6
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
the processor at the end of the buffer/data stage, and it (rN) is needed by the
following instruction at the start of the execute stage.
The only way to avoid this stall is to encourage the C compiler (or assembly language
programmer) not to put a dependent instruction immediately after a load instruction.
Since the 3-stage pipeline ARM cores are not adversely affected by this code
sequence, existing ARM programs will often use it. Such programs will run correctly
on 5-stage ARM cores, but could probably be rewritten to run faster by simply
reordering the instructions to remove these dependencies.
7
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
8
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
When the instruction specifies the store of a byte data type, the 'data out' block
extracts the bottom byte from the register and replicates it four times across the 32-bit
data bus. External memory control logic can then use the bottom two bits of the
address bus to activate the appropriate byte within the memory system. Load
instructions follow a similar pattern except that the data from memory only gets as far
as the data in' register on the second cycle and a third cycle is needed to transfer the
data from there to the destination register.
9
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Branch instructions
1 Branch:B{<cond>}label
2 Branch with Link:BL{<cond>}sub_routine_label
Branch instructions compute the target address in the first cycle as shown in Figure 5.7. A
24-bit immediate field is extracted from the instruction and then shifted left two bit
positions to give a word-aligned offset which is added to the PC. The result is issued as an
instruction fetch address, and while the instruction pipeline refills the return address is
copied into the link register (r14) if this is required (that is, if the instruction is a 'branch
with link').The third cycle, which is required to complete the pipeline refilling, is also used
to make a small correction to the value stored in the link register in order that it points
directly at the instruction which follows the branch. This is necessary because r15 contains
pc + 8 whereas the address of the next instruction is pc +4 .
10
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
into an internal pipeline that mimics the behavior of the ARM instruction pipeline. As
each coprocessor instruction begins execution there is a 'hand-shake' between the
ARM and the coprocessor to confirm that they are both ready to execute it. The
handshake uses three signals:
cpi (from ARM to all coprocessors).
This signal, which stands for 'Coprocessor Instruction', indicates that the ARM
has identified a coprocessor instruction and wishes to execute it.
cpa (from the coprocessors to ARM).
This is the 'Coprocessor Absent' signal which tells the ARM that there is
no Coprocessor present that is able to execute the current instruction.
cpb (from the coprocessors to ARM).
This is the 'Coprocessor Busy' signal which tells the ARM that the coprocessor
cannot begin executing the instruction yet.
5.4.2Handshake outcomes
1.Once a coprocessor instruction has entered the ARM7TDMI and coprocessor pipe-
lines, there are four possible ways it may be handled depending on the handshake
signals: The ARM may decide not to execute it, either because it falls in a branch
shadow or because it fails its condition code test. (All ARM instructions are
conditionally executed, including coprocessor instructions.) ARM will not assert cpi,
and the instruction will be discarded by all parties.
2. The ARM may decide to execute it (and signal this by asserting cpi), but no present
coprocessor can take it so cpa stays active. ARM will take the undefined instruction
trap and use software to recover, possibly by emulating the trapped instruction.
3. ARM decides to execute the instruction and a coprocessor accepts it, but cannot
execute it yet. The coprocessor takes cpa low but leaves cpb high. The ARM will
'busy-wait' until the coprocessor takes cpb low, stalling the instruction stream at this
point. If an enabled interrupt request arrives while the coprocessor is busy, ARM will
12
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
break off to handle the interrupt, probably returning to retry the coprocessor
instruction later.
4. ARM decides to execute the instruction and a coprocessor accepts it for immediate
execution, cpi, cpa and cpb are all taken low and both sides commit to complete the
instruction.
5.4.3Data transfers
If the instruction is a coprocessor data transfer instruction the ARM is responsible for
generating an initial memory address (the coprocessor does not require any
connection to the address bus) but the coprocessor determines the length of the
transfer; ARM will continue incrementing the address until the coprocessor signals
completion. The cpa and cpb handshake signals are also used for this purpose. Since
the data transfer is not interruptible once it has started, coprocessors should limit the
maximum transfer length to 16 words (the same as a maximum length load or store
multiple instruction) so as not to compromise the ARM's interrupt response.
5.4.4Pre-emptive execution
A coprocessor may begin executing an instruction as soon as it enters its pipeline so
long as it can recover its state if the handshake does not ultimately complete. All
activity must be idempotent (repeatable with identical results) up to the point of
commitment.
• Allows the programmer to think in terms of abstractions that are above the
machine level
13
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
• The programmer may not even know on which machine the program will
ultimately run The RISC philosophy focusing instruction set design on flexible
primitive operations from which the complier can build its high level
operations
5.5.1Data types
ARM support for characters
• For handling characters is the unsigned byte load and store instruction
ANSI (American National Standards Institute) C basic data types
• Defines the following basic data types
– Signed and unsigned characters of at least eight bits
– Signed and unsigned short integers of at least 16 bits
– Signed and unsigned integers of at least 16 bits
– Signed and unsigned long integers of at least 32 bits
– Floating-point, double and long double floating-point numbers
– Enumerated types
– Bit fields (sets of Boolean variables)
• The ARM C complier adopts the minimum sizes for each of these types
• The standard integer uses 32-bit values
• Current versions of the ARM include signed byte and signed and unsigned 16-
bit loads and stores, providing some native support for short integer and signed
character types
5.5.2Expressions
Register use
• The key to the efficient evaluation of a complex expression is to get the
required values into the registers in the right order and to ensure that frequently
used values are normally resident in registers
• Optimizing this trade-off between the number of values that can be held in
registers and the number of registers remaining is a major task for the complier
ARM support
• The 3-address instruction format used by the ARM gives the compiler the
maximum flexibility
• Thumb instructions (generally 2-address)
– restricts the compiler‟s freedom to some extent
– smaller number of general registers also makes its job harder
Accessing operands
• A procedure will normally work with operands that are presented in one of the
following ways, and can be accessed as indicated as an argument passed
through a register
– The value is already in a register, so no further work is necessary as a
argument passed on the stack
– Stack pointer (r13) relative addressing with an immediate offset known at
compile-time allows the operand to be collected with a single LDR
• As a constant in the procedure‟s literal pool
– PC-relative addressing, again with an immediate offset known at compile-
time, gives access with a single LDR
• As a local variable
– Local variables are allocated space on the stack and are accessed by
• a stack pointer relative LDR
15
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
• As a global variable
– Global (and static) variables are allocated space in the static area and are
accessed by static base (is usually in r9) relative addressing
Pointer arithmetic
• Arithmetic on pointers depends on the size of the data types that the pointers
are pointing to If a variable is used as an offset it must be scaled at run-time
5.5.3Conditional statements
if…else
The ARM architecture offers unusually efficient support for conditional
expressions when the conditionally executed statement is small
16
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
switches
• ARM is to use a jump table
5.5.4Loops
for (i=0; i<10; i++) {a[i] = 0; }
17
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
• It can be further improved by moving the test to the bottom of the loop
While loops
• The standard conceptual arrangement of a „while‟ loop is as follow
• One fewer branch is executed each time the complete „while‟ structure is
encountered
18
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
A procedure is a function which returns no value but has only side-effects. The C
language, for example, has no procedures, only functions. ANSI C even defines a
type, void, for the result of a function that has no result.
You can define a Function procedure in a module, class, or structure. It is Public by
default, which means you can call it from anywhere in your application that has
access to the module, class, or structure in which you defined it.
A Function procedure can take arguments, such as constants, variables, or
expressions, which are passed to it by the calling code. Both functions and
procedures can return values. Apart from this following are the differences:
Functions are used for computations where as procedures can be used for
performing business logic.
Program design:
Good programming practice requires that large programs are broken down into
components that are small enough to be thoroughly tested; a large, monolithic
program is too complex to test fully and is likely to have 'bugs' in hidden corners that
do not emerge early enough in the program's life to be fixed before the program is
shipped to users.
19
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Program hierarchy:
A typical hierarchy is illustrated in Figure 4. The top of the hierarchy is the program
called main. The remaining hierarchy is fairly informal; lower-level routines may be
shared by higher-level routines, calls may skip levels, and the depth may vary across
the hierarchy.
Leaf routines:
At the lowest level of the hierarchy there are leaf routines; these are routines which
do not themselves call any lower-level routines. In a typical program some of the
bottom-level routines will be library or system functions; these are predefined
operations which may or may not be leaf routines (that is, they may or may not have
internal structure).
Terminology:
20
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
There are several terms that are used to describe components of this program
structure, often imprecisely. We shall attempt to apply terms as follows:
• Subroutine: a generic term for a routine that is called by a higher-level routine,
particularly when viewing a program at the assembly language level.
• Function: a subroutine which returns a value through its name. A typical invocation
looks like:
c = max (a, b);
• Procedure: a subroutine which is called to carry out some operation on
specified data item(s).
A typical invocation looks like:
printf ("Hello WorldXn");
C functions:
In C all subroutines are functions, but they can have side-effects in addition to
returning a
Value, and when the returned value is of type 'void' it is effectively suppressed and
only the side effects remain, giving a behaviour which looks just like a procedure.
Arguments and parameters:
An argument is an expression passed to a function call; a value received by the
function is a parameter.
A call by reference semantics would cause any change to a parameter within a
function to be passed back to the calling program, which clearly only makes sense
when the argument is a simple variable, but C does not support this.
ARM Procedure Call Standard
The APCS imposes a number of conventions on the otherwise 'vanilla' flavour of the
ARM architecture:
• It defines particular uses for the 'general-purpose' registers.
• It defines which form of stack is used from the full/empty,
ascending/descending Choices supported by the ARM instruction set.
• It defines the format of a stack-based data structure used for back-tracing
when Debugging programs.
21
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
• It defines the function argument and result passing mechanism to be used by all
externally visible functions and procedures. ('Externally visible' means that the
procedure interface is offered outside the current programming module. A function
which is used only within the Current module may be optimized by deviating from
this convention.)
• It supports the ARM shared library mechanism, which means it supports a standard
way for shared (re-entrant) code to access static data.
APCS variants:
There are several (16) different variants of the APCS which are used to generate code
for a range of different systems. They support:
• 32-or 26-bit PCs
Older ARM processors operated in a 26-bit address space and some later versions
continue to support this for backwards compatibility reasons.
Implicit or explicit stack-limit checking
Stack overflow must be detected if code is to operate reliably. The compiler can
insert instructions to perform explicit checks for overflow.
Re-entrant or non-re-entrant code.
Code specified as re-entrant is position-independent and addresses all data indirectly
through the static base register (sb).
ARM efficiency:
Overall the ARM supports functions and procedures efficiently and flexibly. The
various flavours of the procedure call standard match different application
requirements well, and all result in efficient code. The load and store multiple register
instructions are exploited to good effect by the compiler in this context; without them
calls to non-leaf functions would be much more costly.
22
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
5.5.6 USE OF MEMORY: An ARM system, like most computer systems, has its
memory arranged as a linear set of logical addresses.
The stack:
Whenever a (non-trivial) function is called, a new activation frame is created on the
stack containing a backtrace record, local (non-static) variables, and so on.
The heap:
The heap is an area of memory used to satisfy program requests (malloc ()) for more
memory for new data structures.
Address space model:
23
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Where an application can use the entire memory space (or where a memory
management unit can allow an application to think it has the entire memory space),
the application image is loaded into the lowest address, the heap grows upwards from
the top of the application and the stack grows downwards from the top of memory.
Chunked stack model:
Other address space models are possible, including implementing a 'chunked' stack
where the stack is a series of chained chunks within the heap.
Data storage:
The various data types supported in C require differing amounts of memory to store
their binary representations. The basic data types occupy a byte (chars), a half-word
(short ints), a word (ints, single precision float) or multiple words (double precision
floats).
Data alignment:
Therefore the ARM C compiler generally aligns data items on appropriate
boundaries:
• Bytes are stored at any byte address.
• Half-words are stored at even byte addresses.
• Words are stored on four-byte boundaries.
Packed structs:
Sometimes it is necessary to exchange data with other computers that follow different
alignment conventions, or to pack data tightly to minimize memory use even though
this will reduce performance.
24
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Several examples of ARM-based „system on chips‟
7. Two UARTs
8. Byte-wide FIFO buffers
9. Synchronous communications controller
10. Serial controller: I2C for battery-backed RAM, real-time clock, E2PROM,and audio
codec
11. External bus interface
12. 8-, 16-, and 32-bit data buses and flexible wait state generation
13. Counter/timer block
14. Three 8-bit counters connected to a 24-bit prescaler
15. Interrupt controller
16. Programmable control of all on- and off-chip interrupt sources
Ruby II power-management modes
1. On-line – all circuits are clocked at full speed
2. Command – the ARM core runs with 1 to 64 wait states but all other circuitry runs at full
speed. An interrupt switches the system into on-line mode immediately.
3. Sleep – all circuitry is stopped apart from the timers and oscillators. Particular interrupts
return the system to on-line mode.
4. Stopped – all circuits (including the oscillators) are stopped. Particular interrupts return
the system to on-line mode.
Packaging
1. 144- and 176-pin thin quad flat packs
2. Up to 32 MHz at 5 V.
3. At 20 MHz, 30 mA in on-line mode, 7.9 mA in command mode, 1.5 mA in sleep
mode, and 150 uA in stop mode.
27
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
ADCs
1. Based on timing how long it takes to discharge a capacitor to theinput
voltage level
2. Very simple way to measure slowly varying voltages
3. Comparator and counter
4. Measure the voltage from a volume control potentiometer or tocheck
the battery voltage in a portable application
Keypad interface
1. Parallel output ports to strobe the columns of the keypad
2. Parallel input ports with internal pull-down resistors to sense the rows
3. Key press will generate an interrupt:
ARM can activate individual columns and sense rows.
Clocks and timers
1. 38.864 MHz and 460.8 KHz during power-down
2. Watchdog at every 1.28 sec
3.2.5 ms timer interrupts for DRAM refresh and multitasking.
29
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
30
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
ARM7TDMI subsystem
Responsible for the system control functions
The user interface software
The GSM protocol stack
Power management
Driving the peripheral interface
Running some data applications
On-chip debug
Single JTAG interface
ARM7TDMI Embedded ICE module
31
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Bluetooth
De-facto standard for wireless data communication for the 2.4GHz band
Consortium of Ericsson, IBM, Intel, Nokia, and Toshiba
Support short-range communication (to 10 m range) using radio
communication with 1 Mbit/s
Robust communication in a noisy and uncoordinated environment
For laptop, cellular telephone, printer, PDA, desktop, fax, keyboards, and so
on Provide bridge to existing networks
Bluetooth ‘piconet’
Bluetooth units dynamically form ad hoc „piconets‟, which are groups of 2 to 8
units that operate the same frequency-hopping scheme
One of the units will operate as master: Defines the clock and hopping
sequence.
32
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Power management
On-line: all blocks are clocked at their normal speed. TheARM7TDMI core
clock 13 to 40 MHz. 40 mA max.
Command: The ARM7TDMI clock is slowed down by the insertion of wait
states
Sleep: The ARM7TDMI click is stopped. 0.3 mA.
Stopped: The clock oscillator is turned off.
33
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
34
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
CPU core
SA-1 processor core
Exception vector relocation mechanism (for Windows CE)
16 Kbyte instruction cache using a 32-way associative CAM-RAM structure
with 8-word lines
MMU with Process ID mechanism (for Windows CE)
35
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
8 Kbyte 32-way associative data cache in parallel with a512 byte 2-way set-
associative cache
Allow large data structures to be cached without causing major pollution
of the main data cache
Addition of read buffer
Memory controller
Up to 4 banks of 32-bit off-chip DRAM
PCMCIA interface
Two card slots are supported with some external „glue‟ logic
System control
On-chip
A reset controller
A power management controller that handles low-battery warnings and
switches the system between its various operating modes
system timer block that supports general timing and watchdog
An operating
functions
An interrupt controller
A real-time clock that runs from a 32 KHz crystal source
28 general-purpose I/O pins.
Peripherals
LCD controller
Serial ports: USB, SDLC, IrDA, codec, and standard
UART 6-channel DMA
Bus structure
36
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
and so on
Very simple at the PCB level, yet very powerful processing capability and
sophisticated system architecture.
SA-1100 silicon ->
Characteristics
Process 0.35 um
Transistors2,500,000
MIPS 220/250
Metal layers 3
Die area 75 mm2
Power 330/550 mW
Vdd 1.5/2V
Clock 180/220 MHz
MIPS/W 665/450
5.6.6The ARM7100
Features
Highly integrated microcontroller
Suited to a range of mobile applications
37
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
38
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
ARM7100 organization
1. ARM710a CPU
2. ARM MMU
3. 8 Kbyte 4-wayassociative quadword line cache
4. 4-address 8-dataword write buffer
5. AMBA bus
6. Peripherals
LCD controller
Serial & parallel I/O ports
Interrupt controller
32-bit external bus interface
39
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
DRAM controller
Power management
Levels
40
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
41
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
42
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
3 DMA controllers
Handle data streams for video, cursor, and audio channels
I/O controller
Manages 16-bit off-chip I/O bus
Number of on-chip interfaces: 4 comparators, 2 serial ports, counter/timers, 8
general-purpose open-drain I/O lines, and programmable interrupt controller.
Applications
Low-cost versions of the Acorn Risc PC
Online Media interactive video set-top box
High-resolution displays
1280 x 1024 and above the number of colors become restricted due to
the bandwidth limitations of standard DRAM
43
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Ideally suited
44
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
All instructions are 32 bits long.
Most instructions execute in a single cycle.
Every instruction can be conditionally executed.
A load/store architecture
Three operand format
Combined ALU and shifter for high speed bit manipulation
Specific memory access instructions with powerful auto-indexing addressing modes.
32 bit and 8 bit data types
and also 16 bit data types on ARM Architecture v4.
31 28 27 0
cond
46
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
For example to add two numbers and set the condition flags:
ADDS r0,r1,r2 ; r0 = r1 + r2 ; ... and set flags
Branch instructions
Branch : B{<cond>} label
Branch with Link : BL{<cond>} sub_routine_label
47
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
1: B{L}X{<cond>} Rm
2: BLX <target address>
In the first format the branch target is specified in a register, Rm. Bit[0] of
Rm
is copied into the T bit in the CPSR and bits[31:1] are moved into the PC:
If Rm[0] is 1, the processor switches to execute Thumb instructions and
begins executing at theaddress in Rm aligned to a half-word boundary by
clearing the bottom bit.
If Rm[0] is 0, the processor continues executing ARM instructions and begins
executing at the address in Rm aligned to a word boundary by clearing Rm[l].
Format (1) instructions may be executed conditionally or unconditionally, but format
(2) instructions are executed unconditionally.
'<target address>' is normally a label in the assembler code; the assembler will
generate the offset (which will be the difference between the word address of the
target and the address of the branch instruction plus 8) and set the H bit if
appropriate.
48
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
The software interrupt instruction is used for calls to the operating system and is
often called a 'supervisor call'. It puts the
processor into supervisor mode and begins
executing instructions from address 0x08.
In effect, a SWI is a user-defined instruction.
It causes an exception trap to the SWI hardware vector (thus causing a change to
supervisor mode, plus the associated
state saving), thus causing the SWI
exception handler to be called.
The handler can then examine the comment field of the instruction to decide
what operation has been requested.
By making use of the SWI mechansim, an operating system can implement a set
of privileged operations which applications running in user mode can request.
Largest family of ARM instructions, all sharing the same instruction format.
Contains:
Arithmetic operations
Comparisons (no results - just set condition codes)
Logical operations
Data movement between registers
Remember, this is a load / store architecture
These instruction only work on registers, NOT memory.
They each perform a specific operation on one or two operands.
First operand always a register - Rn
Second operand sent to the ALU via barrel shifter.
49
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
The ARM data processing instructions are used to modify data values in registers.
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
Multiplication Instructions
The Basic ARM provides two multiplication instructions.
Multiply
MUL{<cond>}{S} Rd, Rm, Rs ; Rd = Rm * Rs
Multiply Accumulate - does addition for free
MLA{<cond>}{S} Rd, Rm, Rs,Rn ; Rd = (Rm * Rs) + Rn
Restrictions on use:
Rd and Rm cannot be the same register
Can be avoid
by swapping Rm and Rs around. This works because multiplication is
commutative.
Cannot use PC.
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
A useful PC-relative form that leaves the assembler to do all the work:
LDRlSTR{<COnd>}{B} Rd, LABEL
52
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
P = 1 means pre-indexed, i.e. modify the address BEFORE use
P = 0 means post-indexed, i.e. modify the address AFTER use
B = 1 selects unsigned byte transfer (default is word transfer)
<offset> may be #+/- 12-bit immediate value (i.e. constant)
<offset> may also be +/- register
write-back (or "!") = 1 if the base register is updated
All the shift parameters are the same as before
53
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
31 28 27 23 2221 1615 12 11 0
cond 00010 R 001111 Rd 000000000000
destination register
SPSR/CPSR
54
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
55
www.rejinpaul.com
UNIT V - ARM ORGANIZATION
56
www.rejinpaul.com