0% found this document useful (0 votes)
8 views

AMESunit.no.1modified

Uploaded by

shreemanti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AMESunit.no.1modified

Uploaded by

shreemanti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

303145A: ELECTIVE-I

ADVANCED MICROCONTROLLER
AND
EMBEDDED SYSTEM

UNIT NO.1
PIC ARCHITECTURE AND
EMBEDDED C
SUBJECT DETAILS
Teaching Scheme
Theory -03 Hr/Week
Credits -TH -03
Examination Scheme
ISE -30 Marks
ESE -70 Marks
PREREQUISITE
1. Knowledge of Number system and Basic logic
components.
2. Programming basics of C language.
3. Advantage of Microcontroller over
Microprocessor.
CONTENTS
❖ Comparison of CISC and RISC Architectures
❖ Data and Program memory organization
❖ Program Counters
❖ Stack pointer
❖ Bank Select Register
❖ Status register
❖ Embedded C concepts
❖ Header and source files and pre-processor
directives
❖ Data types, data structures, Control loops,
functions, bit operations.
WHAT IS A MICROCONTROLLER?
CLASSIFICATION OF
MICROCONTROLLERS
IMPORTANCE OF ARCHITECTURE
The Central processing unit, referring to both
microprocessor and microcontroller, performs specific tasks with the
help of a Control Unit (CU) and Arithmetic Logical Unit (ALU).
As the instructions are delivered from RAM, the CPU acts with the help
of its two helping units by creating variables and assigning them
values and memory.
It’s really important to know how the CPU performs all this action with
the help of its architecture.
Every CPU has a memory associated with it to store the program and
data. Program and data work with the CPU to get the output. The
Program provides the instruction while the data provides the
information to be worked upon.
VON NEUMANN ARCHITECTURE
The Von Neumann Model uses a single storage structure called
memory for instructions and data. So, the CPU can process either
instructions or data one at a time.
A single bus for getting the code and data means, they come to get
in each other’s way and slow down the processing speed of the
CPU because each had to wait for the other to finish the fetching.
HARVARD ARCHITECTURE
The Harvard architecture has physically separate memory units and
buses for instructions and data. So, the CPU can access data while
the execution of an instruction. Thus ,Harvard computers are faster
than that of computers designed in Von Neumann architecture.
In this architecture a separate data buses for data and program are
present. So it means this architecture proposed the use of four
buses
1. A set of data bus carrying the data into and out of the CPU.
2. A set of address bus for accessing the data.
3. A set of data bus for carrying code into the CPU.
4. An address bus for accessing the code.
WHY CISC & RISC ?
In addition CPU designers added more and complex instructions.
The design of control unit and the instructions decoder became
very complex, occupying more than 50% of the chip area.
This left less space for registers ,resulting in the reduction of the
number of registers on the chip.Therefore to execute the code
MPU needed to access memory more frequently which slowed
execution.
Due to which CISC(complex Instruction set computing or computers
design came into picture.
Researchers then realised that programs uses 20% of the
instructions frequently and remaining 80% is used rarely. Hence
a new philosophy came into the picture
CISC VS RISC

Caches are used to store temporary files, using hardware and


software components.
COMPARISON OF CISC AND RISC ARCHITECTURES
S.N Characteristics CISC RISC
o
1 Instruction size Varies Fixed
2 Instruction Length 1,2,3,4 bytes 4 bytes
3 No of Instruction More Less
4 Instruction decoding Slow to decode Quick
5 Instruction Varies from simple to Simple operation
semantics complex
6 Addressing Modes Complex addressing mode Complex addressing mode
is synthesized
7 Instruction execution Slow Medium
8 Registers Few Special purpose Many general purpose

9 Memory references Combined with different Not combined


operations
10 Hardware Complex Simple
11 Memory access frequently Rarely
12 Instruction format Filed placement varies Regular consistent
placement of fields
13 Pipelined Not pipelined or less Highly
CISC VS RISC
PIC 18 Microcontroller
In 1989,Microchip Technology Corporation introduced 8 bit microcontroller
called the PIC ,which stands for Peripheral Interface Microcontroller
PIC 18 has a RISC architecture
1. On chip program /code ROM- 4K to 128K
2. Data RAM-256 to 4096 bytes
3. Data EEPROM-128 byte and 1KB
4. Timers-4
5. ADC-10 bits
6. USART(Universal Synchronous Asynchronous Receiver
Transmitter)
7. I/O Ports-16 to 72 pins
Family of PIC18
Comparison of PIC18 and 8051
Classification of MEMORY
A memory unit is an integral part of any microcontroller, and its primary purpose is to
hold instructions and data.
RAM
RAM BANK SCHEME :This divides the memory space into 16
contiguous banks (bank 0 through 15) of 256 bytes. Depending
on the instruction, each location can be addressed directly by its
full 12-bit address, or an 8-bit low-order address and a 4-bit Bank
Pointer (also called Bank Select Register, BSR).
There are two types of RAM: static RAM (SRAM), and dynamic
RAM (DRAM). Static RAM stores data in flip-flops.
Therefore, this memory does not need
to be refreshed.
RAMs are volatile unless backed up by battery.
The PIC18F uses SRAM for its data memory
Dynamic RAM stores data in capacitors.
That is, it can hold data for a few milliseconds.
Hence, dynamic RAMs are refreshed typically
by using external refresh circuitry.
ROM
ROMs (Read-Only memories) can only be read, so it is nonvolatile
memory.
CMOS technology is used to fabricate ROMs.
ROMs are divided into two common types:
mask ROM and Erasable Programmable ROM (EPROM) such as the
2732, and EAROM (Electrically Alterable ROM) [also called
EEPROM or E2 PROM (electrically erasable PROM)] such as the
2864.
The contents of mask ROMs are permanent and cannot be
changed by the user.
EPROMs can be programmed, and their contents can also be
altered by using special equipment, called an EPROM programmer
EPROMs can be reprogrammed and erased. The EPROM chip must
be removed from the system before programming. This memory
is erased by exposing the chip to ultraviolet light via a lid or
window on the chip.
Flash memory (nonvolatile), invented in the mid-1980s by
Toshiba, is designed using a combination of EPROM and E2 PROM
technologies. Flash memory can be reprogrammed electrically
while embedded on the board. One can change multiple bytes at
a time.
Block Diagram of CPU and Memory
Registers in PIC18
Registers are used to stored information temporarily.
This information can be byte of data or address to fetch
the data
All registers in the PIC18F are mapped as addresses in
the data memory, and are called SFRs (Special Function
Registers). Hence, each register is assigned with a unique
12-bit memory address.
1. WREG: The WREG (Working register) is 8-bit wide. This
is the PIC18F accumulator, and has its usual meaning.
Most arithmetic and logic operations are performed
using the WREG.
example:MOVLW 25H (mov a literal value into W
register)
2. BSR :The BSR (Bank Select Register) is 8-bit wide.
The low four bits are used to provide the bank
address from 0 to F; upper four bits of BSR are zero.
The BSR provides the upper four bits of a 12-bit
address of data memory. BSR is used for directly
addressing the data SRAM.
File Register
File Register-4096 bytes(4K)
Divided into 16 banks.Each bank is 256 bytes called access
bank
Each bank =
a.) 128 bytes = GPR(scratchpad)-00 to 7F H
b.) 128 bytes= SFR- F80H to FFF H
File Registers
Data Memory( read/write memory) File
register

File register

Data
storage Scratchpad Registers

SFR GPR
SFR
Program Counter(PC)
PC The maximum size of the PIC18F’s PC (Program
Counter) is 21-bit wide.
The size of the PC varies from one PIC18F family member to
another. For example, the size of the PC of the PIC18F4321 is
13-bit wide.
The PC normally points to the next instruction.
the 21-bit PC provides the PIC18F with direct addressing
capability of a maximum of 2MB (2) of program memory.
Upon hardware reset, the PC is loaded with zero so that the
PIC18F CPU fetches the first instruction from address 0.
The 21-bit PC is comprised of three 8-bit registers, namely PCL
(PC Low byte), PCLATH (PC latch High byte), and PCLATU (PC
Latch Upper 5 bits).
The 21-bit PC is stored in these registers as follows: bits 0
through 7 in PCL, bits 8 through 15 in PCLATH,
and bits 16 through 20 in the low five bits of PCATU.
Registers PCL, PLATCH, and PCLATU are mapped as special
function registers in the data SRAM by the PIC18F.
File Select Register(FSR)

FSR The FSR (File Select Register) consists of three 16-


bit registers (FSR0, FSR1, FSR2) with the upper four bits of
each FSR as zero.
The low 12 bits of FSR0, FSR1, or FSR2 are used to hold the
12-bit memory address of the data SRAM.
These registers are basically memory pointers. Hence, they
are typically used for handling arrays and pointer based
data accessing.
The PIC18F indirectly uses these registers to access data in
data SRAM.
Each of these three registers are divided into two 8-bit
registers as follows: FSR0H (high byte of FSR0) and FSR0L
(low byte of FSR0),
FSR1H (high byte of FSR1) and FSR1L (low byte of FSR1),
FSR2H (high byte of FSR2) and FSR2L (low byte of FSR2)
The SR (Status Register) is 8-bit wide.
the PIC18F status register which contains the flags.
The meaning of these flags will be explained in the following: ∙
C (Carry flag) is set to 1 if there is a carry from addition or a borrow
from subtraction; otherwise, C = 0. ∙
DC (Digit Carry flag) is set to 1 if there is a carry due to addition of the
low 4 bits into the high 4 bits or a borrow due to the subtraction of the
low 4 bits from the high 4 bits of a number; otherwise,
DC = 0. This flag is used by BCD arithmetic instructions. ∙
Z (Zero flag) is set to 1 if the result is zero; Z = 0 for a nonzero result.
OV (Overflow flag) is set to 1 if there is an arithmetic overflow (i.e., if
the size of the result exceeds the capacity of the destination location);
otherwise, OV = 0. Note that in overflow, OV = Cf ⊕Cp where Cf is the
final carry and Cp is the previous carry. Overflow is used for signed
arithmetic (2’s Complement). ∙
N (Negative flag) is set to 1 if the most significant bit of the result is 1
indicating a negative number; N = 0 if the most significant of the result
is 0 indicating a positive number. ∙ Bits 5 through 7 are not
implemented, and are read as zero.
STACK POINTER(SP)
SP The SP (Stack Pointer) register (called STKPTR in the
PIC18F) is 8-bit wide. The PIC18F hardware stack is a group of
31 21-bit registers to hold memory addresses. Note that the
PIC18F program counter is 21-bit wide. Hence the stack is 21-
bit wide. The low five bits of the STKPTR are used to address
the stack.
The PIC18F hardware stack is accessed from the bottom.
Hence, the SP is incremented by 1 after PUSH and
decremented by 1 after POP
The 31 stack registers are neither part of program memory nor
data memory.
Embedded C Programming
Embedded C concepts,
Header and source files and
pre-processor directives,
Data types,
data structures,
Control loops,
functions,
bit operations
Programming in Microcontroller
● Computer programming languages can typically be divided
into three main types: machine language, assembly
language, and high-level language.
● A machine language program consists of either binary or
hexadecimal op-codes.
● Programs in assembly and high-level languages are
represented by instructions that use English-language-type
statements.
● An assembler translates a program written in assembly
language into a machine language program.
● A compiler or interpreter, on the other hand, converts a
high-level language program such as C into a machine
language program.
● Assembly or high-level language programs are called
source codes.
● Machine language programs are known as object codes.
● A translator converts source codes to object codes.
C Programming
C, a very popular language, is widely used for microcontrollers.
A high-level language is a problem-oriented language.
The programmer does not have to know the details of the architecture
of the microcontroller and its instruction set.
Basically, the programmer follows the rules of the particular language
being used to solve the problem at hand.
A second advantage is that a high level language such as C is
portable.
That is, a program written in a particular high-level language can be
executed by two different microcontrollers, provided that they both
understand that language.
The interpreter converts each statement into machine language codes
but does not convert the entire program into machine language codes
prior to execution. Hence, it does not generate an object program.
Therefore, an interpreter is a program that executes a set of machine
language instructions in response to each high-level statement in
order to carry out the function.
A compiler, however, converts each statement into a set of machine
language instructions and also produces an object program that is
stored in memory.
This program must then be executed by the CPU to perform the
required task in the high-level program.
Structure of C
# include main ( )
{
printf (“I wrote”);
printf (“a C-”);
printf (“program);
printf (“\n”);
}
Program includes:
1. C Preprocessor Directive( #include):
“# include” is a preprocessor directive for the C compiler.
These directives give instructions to the compiler that are performed
before the program is compiled.
The directive #include inserts additional statements in the program.
These statements are contained in the file stdio.h.
The file stdio.h is included with the standard C library. The stdio.h file
contains information related to the input/output statement.
Structure of C
2.) Functions: main () :
● This is where the program begins execution
● main () is a function of no arguments, indicated by ( )
● main (void) means no arguments are required for function call.
● The” void” type does not provide a result value to the caller.
● The void before the name indicates this function returns nothing and
the void inside the () indicates this function gets nothing from the
caller.
● Hence, “main ( )” means that it does not return a value.

3.) Declarations:
● The “int i” is a data declaration for the variable named with the
identifier
● int indicates the variable is an integer.

4.) Statements and Expressions:


a.The for line is a statement:
It executes the first expression : i=1
b) i<=10
c) i=i+1
Structure of C
4.) Statements and Expressions:
A.] The for line is a statement:
It executes the first expression :
a) i=1
b) i<=10
c) i=i+1

B.] Expressions
Expressions are some combination of constants, variables,
operators, and function calls.
Expressions always have a resulting value.
Some simple examples of an operator are + -*/ and the very
special =

Functions have local data declarations and statements all


enclosed in { }.
Functions may return data and the caller may pass data known
as arguments to the caller and parameters to the function
Writing a working C program involves four steps as
follows:

Step 1: Using a text editor, prepare a file containing the C code.


This file is called the “source file.”
Step 2: Preprocess the code. The preprocessor makes the code
ready for compiling. The preprocessor looks through the source
file for lines that start with a #.
#include is a preprocessor. This preprocessor instruction copies
the contents of the standard header file stdio.h into the source
code.
This header file stdio.h describes typical input/output functions
such as scanf( ) and printf( )functions.
Step 3: The compiler translates the preprocessed code into
machine code. The output from the compiler is called object
code.
Step 4: The linker combines the object file with code from the
C libraries. For instance, in the examples shown here, the
actual code for the library function printf( ) is inserted from the
standard library to the object code by the linker. The linker
generates an executable file. Thus, the linker makes a
complete program
Data types

The data types in C language include char, int, float, and double.
A variable declared as a char (character) usually holds eight bits of
data.
A variable of int (integer) type, on the other hand, can hold 16 or
32 bits of data.
The type float specifies a 32-bit single precision floating point
number.
The type double can be used to declare a data as a 64-bit double
precision floating point number.
I.] char are two types: A.Unsigned char(0 to
255)
B.
Signed char(-128 to +127)
II.] int are two types: A.Unsigned char(0 to
65535)
B.
Signed char(- 32,768 to + 32767)

The lengths of integers can be modified using the qualifiers


shortlong and long.
Bit manipulation operators
Bit manipulation operators:
The AND operator is typically used for clearing one or more bits
to 0.
For example, the C statement PORTC = PORTC & 0x7F; will
clear bit 7 of PORTC to 0 without changing the other bits of
PORTC.
The OR operator is typically used to set one or more bits to 1.
For example, the C statement PORTD = PORTD | 0x05; will
insert 1’s at bits 0 and 2 of PORTD without changing the other
bits of PORTD.
The XOR is typically used to find one’s complement of (toggle)
one or more bits. For example, the C statement PORTC = PORTC
^ 0xFF; will toggle all bits of PORTC
Control structures
Control structures
Typical structured control structures in C include if-else, switch,
while, for and, do-while
A.] The if-else construct :
The syntax for the if-else construct is as follows:
if (cond)
statement1;
else
statement2;
This means that if the condition is true, statement1 is
executed;
else (if the condition is false), statement1 is skipped, and
statement2 is executed.
Example:unsigned char x, y, z;
if (x < y)
z = x + y;
else
z = x - y;
Switch construct
The while construct

The while construct allows a programmer to describe loops.


The syntax for the while construct is provided below:
while (condition) {
statements
}
next statement;
In the above, if the condition is true, the statements are
executed, and then control is returned to the top of the loop.
The condition is tested again.
If the condition is true again, statements are executed, and
control is returned to the top of the loop.
The process is repeated as long as the condition is true.
However, if the condition is false at the start or the duration of
repeating the process when the condition is checked, the
statements in the braces are not executed.
The next statement following the second brace is executed.
The while construct

As an example, consider the following with a single statement


in the loop:
int n = 4;
while ( n <= 16)
n = n + 4;
z = n;
In the above, n is 4 before entering the while loop.
The condition n<= 16 is true the first time.
The expression n = n + 4 continuously
changes the value of n in increments of 4 to 8, 12, 16, 20.
As soon as n = 16, the condition n <= 16
is false, the next statement z = n is executed
which assigns z with the value of 20.
The for construct
The syntax of a for construct is as follows:
for (e1; e2; e3) {
s;
}
where e1, e2, and e3 are C expressions and ‘s’ represents one or
more valid C statements.
the expression e1 is evaluated first.
The expression e2 is then evaluated; if it is false the loop
terminates. Otherwise, ‘s’ representing one or more C statements
will be executed.
The expressions e3, and e2 are then evaluated, and the process
continues. The expression e1 normally contains code to initialize
the loop.
The expression e2 describes the exit condition.
The purpose of the expression e3 is to modify such as increment
the initialized value of expression e1.
This will be the exit condition so that the loop terminates at some
point

You might also like