Professional Documents
Culture Documents
MKT3811 Week4 03.11.2021 R
MKT3811 Week4 03.11.2021 R
Fall : 2021
MKT3811 Week4 3.11.2021
Recap (Week-3)
Power circuit,
Reset circuit
Oscillator circuit
Application specific peripheral circuit
2
Recap (Week-3)
Memory organisation of PIC 16FXXX Microcontroller
3
Recap (Week-3)
5 Ports – (A,B,C,D,E)
A: 6 pins
B: 8 pins
C: 8 pins
D: 8 pins
E: 3 pins
4
Recap (Week-3)
5
Recap (Week-3)
6
Recap (Week-3)
11/4/2021 7
Lecture Contents of Week 4
8
Microprocessors and Programming
Core (CPU) registers - control and monitor operation and processes in the
central processor. Even though there are only a few of them, the operation of
the whole microcontroller depends on their contents.
Peripheral SFRs-
11/4/2021
Core SFRs for PIC 16FXXX Microcontroller
11/4/2021
Recap (Week-3) and Warm Up
Status Register
The STATUS register contains: the arithmetic status of the W register, the
RESET status and the bank select bits for data memory. One should be
careful when writing a value to this register because if you do it wrong, the
results may be different than expected.
For example, if you try to clear all bits using the CLRF STATUS instruction,
the result in the register will be 000xx1xx instead of the expected
00000000. Such errors occur because some of the bits of this register are
set or cleared according to the hardware as well as because the bits 3 and
4 are readable only. For these reasons, if it is required to change its content
(for example, to change active bank), it is recommended to use only
instructions which do not affect any Status bits (C, DC and Z). Refer to
“Instruction Set Summary”.
11/4/2021 13
Core SFRs for PIC 16FXXX Microcontroller
Core SFRs for PIC 16FXXX Microcontroller
15
Core SFRs for PIC 16FXXX Microcontroller
16
Port B Pull Up Enable Example
17
Core SFRs for PIC 16FXXX Microcontroller
11/4/2021 18
Core SFRs for PIC 16FXXX Microcontroller
11/4/2021 19
Core SFRs for PIC 16FXXX Microcontroller
The Power Control (PCON) register contains flag bits to allow differentiation
between a Power-on Reset (POR), a Brown-out Reset (BOR), a Watchdog
Reset (WDT) and an external MCLR Reset.
11/4/2021 20
Microprocessors and Programming
Reseting Mechanisms
21
Reseting Mechanisms
23
Reseting Mechanisms
A logic zero (0) on the MCLR pin causes an immediate and regular reset.
24
Reseting Mechanisms
Power-on always causes reset. Since there are many transitional events
taking place when power supply is turned on (switch contact flashing and
sparkling, slow voltage rise, gradual clock frequency stabilization etc.), it is
necessary to provide a certain time delay for the microcontroller before it
starts to operate.
Two internal timers- PWRT and OST are in charge of that. The first one can
be enabled or disabled during the process of writing a program. Let’s take a
look what happens then:
11/4/2021 25
Reseting Mechanisms
Black-out reset takes place when the power supply normally goes off. The
microcontroller then has no time to do anything unpredictable simply because the
voltage drops very fast beneath its minimum value. In other words the light goes off,
curtain falls down and the show is over!
11/4/2021 26
Reseting Mechanisms
When the power supply voltage drops slowly (typical example is battery discharge,
although the microcontroller experiences far faster voltage drops as slow
processes), the internal electronics gradually stops to operate and the so called
Brown-out reset occurs. Here, before the microcontroller completely stops the
operation there is a real danger that circuits which operate at higher voltages start
to perform unpredictably. Brown-out reset can also cause fatal changes in the
program because it is saved in on-chip flash memory.
11/4/2021 27
Reseting Mechanisms
A special type of Brown-out reset occurs in industrial environment when the power
supply voltage 'blinks' for a moment and drops beneath minimum level. Even short,
such noise in power line may considerably affect the operation of the device.
11/4/2021 28
Microprocessors and Programming
Ports
29
PORTs
In order pins’ operation can match internal 8-bit organization, all of them are, similar
to registers, grouped into five so called ports denoted by A, B, C, D and E. They all
have several features in common:
For practical reasons, many I/O pins have two or three functions. If a pin is used as
any other function, it may not be used as a general purpose input/output pin; and
Every port has its “satellite”, i.e. the corresponding TRIS register: TRISA, TRISB,
TRISC etc. which determines performance, but not the contents of the port bits.
By clearing some bit of the TRIS register (bit=0), the corresponding port pin is
configured as output. Similarly, by setting some bit of the TRIS register (bit=1), the
corresponding port pin is configured as input. This rule is easy to remember 0 =
Output, 1 = Input.
30
31
PORTs
Two Bits of a Possible Digital Output Port Two Bits of a Possible Digital Input Port
11/4/2021 32
PORTs
33
PORTs
34
I/O Ports Sink and Source Currents
Sink and Source Currents – PIC16F877
Microprocessors and Programming
Assembly Code
37
INSTRUCTION SET
38
PIC ASSEMBLY CODE
Conssist of 4 fields
Operand
Instruction from to
43
INSTRUCTION SET
MOVLW b’00001010’
MOVLW h’0A’
MOVLW d’10’
45
Programming Tutorial: Directives
END: The END should always be the last statement in your program
EQU: The equate directive is used to assign labels to numeric values. They are
used to DEFINE CONSTANTS or to ASSIGN NAMES TO MEMORY
ADDRESSES OR INDIVIDUAL BITS IN A REGISTER and then use the name
instead of the numeric address.
INCLUDE:The include directive calls a file which has all the equate statements
defined for you and ready to use,
46
Adressing Modes
Addressing Modes
47
Addressing Modes
immediate addressing
direct addressing mode
indirect addressing mode
48
Addressing Modes
immediate
addressing
sample
49
Addressing Modes
Direct addressing sample
MOVWF 0X21
Address location,
direct address of RAM Memory 50
Indirect Addressing
he whole procedure is enabled because the INDF register is not true one
(physically does not exist), but only specifies the register whose address
is located in the FSR register.
Because of this, write or read from the INDF register actually means write
or read from the register whose address is located in the FSR register.
Example:
MOVLW h’20’
MOVWF FSR
51
MOVF INDF,0
Indirect Addressing
52
Microprocessors and Programming
53
Program Counter
PCL
Low byte of PC
PCLATH
High byte of PC
54
Program Counter
Program Counter
The low byte comes from the PCL register which is a readable and writable register.
The upper bits (PC<12:8>) are not readable, but are indirectly writable through the
PCLATH register.
If the program execution does not affect the program counter, the value of this register
is automatically and constantly incremented +1, +1, +1, +1... In that way, the program
is executed just as it is written instruction by instruction, followed by a constant
address increment.
55
Program Counter
In assembly language, the value of the program counter is marked with PCL, but it
obviously refers to 8 lower bits only. One should take care when using the “ADDWF
PCL” instruction. This is a jump instruction which specifies the target location by
adding some number to the current address. It is often used when jumping into a
look-up table or program branch table to read them.
56
PCL Usage Example
(Lookup table for 7Segment Display)
57
PCL Usage Example
(Lookup table for 7Segment Display)
Table which contains bit patterns for all numbers from 0-9
Number A B C D E F G (Hex)
0 0 0 1 1 1 1 1 1 0x3F
1 00000110 0x06
2 01011011 0x5B
3 01001111 0x4F
4 01100110 0x66
5 01101101 0x6D
6 01111101 0x7D
7 00000111 0x07
8 01111111 0x7F
9 01101111 0x6F
58
PCL Usage Example
(Lookup table for 7Segment Display)
60
Program Counter
On subroutine call or jump execution (instructions CALL and GOTO), the microcontroller is able
to provide only 11-bit addressing. For this reason, similar to RAM which is divided in “banks”,
ROM is divided in four “pages” in size of 2K each. Such instructions are executed within these
pages without any problems. Simply, since the processor is provided with 11-bit address from
the program, it is able to address any location within 2KB. Figure below illustrates this situation
as a jump to the subroutine PP1 address.
61
Program Counter
Blok Adres PCLATH
<4>,<3>
Page 0 0001-07FF 0 , 0
Page 1 0800-0FFF 0 , 1
Page 2 1000-17FF 1 , 0
Page 3 1800-1FFF 1 , 1
Datasheet
62