Microcontroller
Systems
Lecture 2
Dr. Basma Gh. Elkilany
Next Topics
• Microcontroller Architecture
• Registers
• Memory
• Instructions
• Timer/counter
• Interrupts
• Serial Communication
2
Microprocessors
PIC
Architecture
Lecture 1
Dr. Basma Gh. Elkilany
The PIC16F877
• Microchip PIC
―Programmable Interface Controller
4
PIC16F877A
• 5 ports
―Port A (6 pins)
―Port B,C,D (8 pins)
―Port E (3 pins)
• Flash memory: 8K word
(14 bits)
• RAM: 368 bytes
• EEPROM: 256 bytes
• A/D Converter
the PIC can address 8 K of
program memory 5
The PIC16F877
• ALU
―does arithmetic and logic operations
• RAM (F)
―called the “register-file”
• Program EEPROM (Flash Memory), the data EEPROM
• “W” register. memory ھﻧﺎ ﻣﺣطوط ﻟوﺣده ﻟﻛن ﻓﺎﻟﻐﺎﻟب ﺑﯾﺑﻘﻰ ﺟزء ﻣن ال
―The “W” register is not a part of the register-file but is a stand-alone, working register (also
called an “accumulator”).
• The ALU, the RAM, the “W” register, and the data EEPROM each manipulate
and hold 8-bit-wide data, which ranges in value from zero to 255 (or, in
hexadecimal, from 0x00 to 0xFF).
6
The PIC16F877
• The PIC’s RAM addresses range from zero to 511 but the user can only access a RAM
byte in a set of four “banks” of 128 bytes each and only one bank at a time.
512
• Not all of this RAM is available to the user as read-write memory, however. Many
ﻣﻛرﺳﺔ ل
addresses are dedicated to special functions within the processor but they “look-like”
RAM and are accessed the same way.
• The RAM is a read-write memory which can rapidly read and write the data.
• It is a volatile memory which means that it loses its memory when power is removed
(turned off).
7
The PIC16F877
The PIC16F877A microcontroller uses a 14-bit instruction word in its assembly language
• Electrically Erasable Programmable Read Only Memory (EEPROM) and Flash Memory,
which allow data words to be written as well as read.
• The writing of an EEPROM is not the same as a RAM since the data-writing time of the
EEPROM is about ten thousand times as long as the data-writing time of the RAM.
RAM is 10000 faster than EEPROM
• The ROM and its variants are non-volatile memories that preserve their memories when
the power is removed (turned off).
• The program EEPROM (Flash Memory) works with 14-bit-wide words and contains
each of the user’s instructions.
program EEPROM : works with 14-bit-wide
data EEPROM : works with 8-bit-wide 8
program EEPROM : works with 14-bit-wide
data EEPROM : works with 8-bit-wide
The PIC16F877
Common
• It is not uncommon for microcontrollers to have different sizes of data memory
and program memory (in the PIC: 8-bits for data and 14-bits for program words).
ﺗﺣﺗل ﻣﺳﺎﺣﺎت ﻣﻧﻔﺻﻠﮫ
• The key is that the data and program memories occupy separate spaces.
• This allows access to each at the same time.
• The PIC’s program EEPROM (Flash Memory) has addresses that range from
zero to 8191 (0x1FFF).
213
• The user’s program occupies this memory space.
9
Microprocessors
PIC
Programming
Lecture 1
Dr. Basma Gh. Elkilany
Programming The PIC
• All types of computer programs can be broken-down into
four main sets of actions:
1) Top-Down Execution
2) Conditional Branching
3) Loops
4) Subroutine Calls
11
Assembly
• Assembly language uses a one-to-one correspondence of mnemonic words with
the binary machine codes that the processor uses to code the instructions.
ﻛﻠﻣﺎت ذاﻛرة
• The user writes the program using the mnemonic words called the “source”
program and gives this to the program on the PC called the “assembler” which
converts it into the machine code of the PIC in the form of a list of hexadecimal
numbers.
12
Assembly
• This set of numbers is called the “object” program.
• The user then writes the object program into the PIC in the downloading process
of programming the PIC.
• When this is done, the PIC is ready to run its new program.
13
How the PIC works at the machine level
• The PIC executes instructions from program memory in sequential addresses,
starting from address zero, when the PIC is reset upon power-up.
• At first, the address of the current instruction being executed is given in a special
register called, the “program counter” (PC).
• The PIC’s control unit automatically increments the program-counter (PC), gets
the next instruction, decodes that instruction, and then executes it.
14
How the PIC works at the machine level
• If this is done on sequential addresses, this is called, “top-down” execution.
• There are also ways to do non-sequential-address executions.
• This is done with special instructions which load new addresses into the
program-counter.
• This is how conditional-branching, loops, and subroutines are done at the
machine language level.
15
Source Program
• Each line of source program code in assembly language has up to four parts:
LABEL: OPCODE OPERAND(S) ; COMMENT
• The label is an arbitrary name the user picks to mark a program address, a RAM
address, or a constant value.
• If the label has its first character (a letter) that starts in column one of the text,
the colon is optional.
16
Source Program
• Otherwise, the colon separates the label from the “Op-Code”.
• The “Op-Code” is short for, “Operation-Code”, and is the mnemonic name for
the instruction to be executed.
• The operand or operands are the data or the address that the instruction uses
when it is to be executed.
• This is where labels come into play such as when an instruction needs a new
address.
• Comments are optional and must begin with a semi-colon.
• Comments are for documenting the source program so that it will be easy to
read and understand.
17
Example
18
PIC16F877
• The basic function of these pins is for digital inputs
and digital outputs.
• The individual bits on each of the input/output ports
(A-through-E) can each be selected as “input” or
“output” by special configuration registers in the
RAM.
The software must set these bits before the ports can
be used.
19
Microprocessors
Example
Lecture 1
Dr. Basma Gh. Elkilany
Example RESET
Switches
Oscillator (Inputs)
Power on
LEDs
(Outputs)
21
Example
ﻓﺎﻟﺑداﯾﮫ ال PCﺑﯾﺑﻘﻰ ﻣﺗوﺟﮫ ﻋﻠﻰ ال 0ﻓﺑﺣط اﻟﺑرﻧﺎﻣﺞ ﺑﺗﺎﻋﻰ ھﻧﺎك ﻋﺷﺎن ﯾﺑﻘﻰ اﻟﺑرﻧﺎﻣﺞ ﺑﺗﺎﻋﻰ اول ﺣﺎﺟﮫ ﺗﺗﻧﻔذ
ال RAMﻣﺗﻘﺳﻣﮫ ﻟﻛذا ﺑﻧك ﻓﺑﺗﺧﺗﺎر اﻟﻰ ھﺷﺗﻐل ﻋﻠﯾﮫ
22
Example The statement, “ORG 0x0000”, is an assembler
directive which tells the assembler at what
address the following instructions will start.
Here they will start at address zero where
"0x0000” indicates “hexadecimal zero”.
The next statement is the “NOP” instruction.
It is a true instruction that gets translated into
machine code. “NOP” stands for “No Operation”
(it does nothing).
It is coded as 14 zero bits, or 0x0000 in
hexadecimal.
23
Example
The next statement is “BANKSEL PORTC”.
This is not a true instruction, perse, but it does get
translated into two machine instructions which select
the current register file bank to be used.
Here, “PORTC” is interpreted as “Bank Zero”.
Later “BANKSEL TRISC” will be interpreted as “Bank
One”.
24
• The next statement is the “MOVLW” instruction which means “move the literal
value that follows into the W register”.
• Remember that the W register is not a part of the RAM but is an “accumulator”
or “working register” within the PIC.
• The instruction: MOVLW B’00000000’ Says,
“move the binary value of all zeros to W”.
• The general machine coding of the “MOVLW” instruction is:
11 0000 kkkk kkkk
where the “k”s are the single binary bits of the literal data (the data byte).
25
The instruction:
MOVLW B’00000000’
Would be coded as:
11 0000 0000 0000 or 0x3000.
Later in the program is the instruction:
MOVLW B’11111111’
Which is coded as:
11 0000 1111 1111 or 0x30FF.
26
• The next instruction is, “MOVWF PORTC”, which means,
“move the value in the W register to the register-file byte at the address given
(address given here is PORTC)”.
• That is, “Move W to RAM”. In general, this is coded as:
00 0000 1fff ffff
where the “f”s are the binary bits of the address for the desired register-file byte.
• Since Port C has the address 0x07 the coding of “MOVWF PORTC” would be:
00 0000 1000 0111 or 0x0087
27
The label “MAIN” is there to mark the address
of the “MOVF” instruction so that we can come
Example
ﻻﻧﮭﺎ ھﻰ دى اﻟﺑرﻧﺎﻣﺞ اﻟﻰ ھﯾﻧﻔذة, ﻣش ھﯾطﻠﻊ ﻣﻧﮭﺎmain ﺑﻣﺟرد دﺧوﻟﮫ ﻓﺎل
back to that address.
move F to W
The “MOVF” instruction stands for, “move the
register-file byte value to the W register”.
Specifically, it moves the value in Port B to the
W register.
The next new instruction is the, “GOTO MAIN”,
which means, “put the address value at MAIN
into the program-counter to transfer to that
address”.
This causes a “loop” to occur in the program.
The instruction at the address “MAIN” is the
“MOVF”.
The last statement is the, “END”, which is an
assembler directive that means, “end the
program”. 28
Conclusion
PIC PIC
Architecture Programming Example
Ports Assembly Directives
ORG
END
Memory Instruction
Execution
Instructions
MOVLW
MOVWF
Loop
GO TO
29
30
Microprocessors
Thank You
Lecture 1
Dr. Basma Gh. Elkilany