MCU 04 Unlocked
MCU 04 Unlocked
2
Recap (Week-3)
3
Lecture Contents of Week 4
4
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/9/2024
Core SFRs for PIC 16FXXX Microcontroller
11/9/2024
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”.
9
Core SFRs for PIC 16FXXX Microcontroller
Core SFRs for PIC 16FXXX Microcontroller
Core SFRs for PIC 16FXXX Microcontroller
12
Core SFRs for PIC 16FXXX Microcontroller
13
Microprocessors and Programming
Ports
14
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.
15
16
PORTs
Two Bits of a Possible Digital Output Port Two Bits of a Possible Digital Input Port
17
PORTs
18
PORTs
19
I/O Ports Sink and Source Currents
Sink and Source Currents – PIC16F877
Microprocessors and Programming
PortA
PortB
Microprocessors and Programming
PORT B
PORT A
23
PORT – A
PORT – B
PORT – A
26
PORT – A
27
PORT – A
It has two input thresholds, with the positive-going higher than the negative-going.
A signal starting from a low value has to pass the negative-going threshold (at which
point nothing happens) and then cross the positive-going threshold, at which point
the output changes state.
The output will not reverse until the input (now negative-going) has got right back
down to the negative-going threshold. Thus, small fluctuations that recross a
threshold just crossed do not cause any change in output.
PORT – A
29
29
PORT – A
EXAMPLE
Port A
7 6 5 4 3 2 1 0
BSF STATUS,5 ;Select Bank3
BSF STATUS,6
OUTPUT
INPUT
CLRF ANSEL ;PortA Digital I/O
30
PORT – A
EXAMPLE
Port A
7 6 5 4 3 2 1 0
BSF STATUS,5 ;Select Bank3
BSF STATUS,6
OUTPUT
INPUT
CLRF ANSEL ;PortA Digital I/O
31
Microprocessors and Programming
Assembly Code
32
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,
33
INSTRUCTION SET
35
INSTRUCTION SET
Each processor has its own set of instruction codes and corresponding
mnemonics.
38
General Format for Instructions
39
Single Register Operations
The processor operates on data stored in registers, which typically contain 8 bits.
The data can originate in three ways:
1. A literal (numerical value) provided in the program;
2. An input via a port data register;
3. The result of a previous operation.
40
Register Pair Operations
41
Decision Making by Assembly Instructions
42
Decision Making by Assembly Instructions
TEST_PORTA TEST_PORTA
BTFSC PORTA,1
GOTO TEST_PORTA
BSF PORTB,0
43
Loop Programming by Assembly Instructions
BTFSS
44
Loop Programming by Assembly Instructions
45
Microprocessors and Programming
Reseting Mechanisms
46
Reseting Mechanisms
48
Reseting Mechanisms
A logic zero (0) on the MCLR pin causes an immediate and regular reset.
49
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:
50
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!
51
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.
52
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.
53