Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Microprocessors and Programming

Dr. Hüseyin Ayhan Yavaşoğlu

YILDIZ TEKNİK ÜNİVERSİTESİ

Fall : 2021
MKT3811 Week4 3.11.2021
Recap (Week-3)

Basic Application Circuit

Power circuit,
Reset circuit
Oscillator circuit
Application specific peripheral circuit

2
Recap (Week-3)
Memory organisation of PIC 16FXXX Microcontroller

3
Recap (Week-3)

• Support for I2C


• Support for SPI
• Support for UART
• 3 Timers
• 10 bit Analog digital convertors
• Brown out reset (BOR)

5 Ports – (A,B,C,D,E)
A: 6 pins
B: 8 pins
C: 8 pins
D: 8 pins
E: 3 pins

Total 33 digital I/O

4
Recap (Week-3)

Simplified internal block diagram of


PIC 16FXXX mC;

5
Recap (Week-3)

6
Recap (Week-3)

11/4/2021 7
Lecture Contents of Week 4

 Core and Peripheral SFR Registers


 Reseting Mechanisms
 Ports
 Assembly Code
 Adressing Modes
 Program Counter and Subroutins

8
Microprocessors and Programming

 Core and Peripheral SFR Registers


Core SFRs for PIC 16FXXX Microcontroller

Features and Function

The special function registers can be classified into two categories:

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- control the operation of peripheral units (serial


communication module, A/D converter etc.). Each of these registers is mainly
specialized for one circuit and for that reason they will be described along with
the circuit they are in control of.
Since their bits control several different circuits within the chip, it is not possible
to classify them into some special group. These bits are described along with
the processes they control.
Core SFRs for PIC 16FXXX Microcontroller

Core (CPU) registers

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

the weak pull-ups on PORTB.

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

The PIC16F87XA differentiates between various kinds of Reset:


• Power-on Reset (POR)
• MCLR Reset during normal operation
• MCLR Reset during Sleep
• WDT Reset (during normal operation) http://ww1.microchip.com/downl
oads/en/devicedoc/31003a.pdf
• WDT Wake-up (during Sleep)
• Brown-out Reset (BOR) 22
Reseting Mechanisms

Reset condition causes the microcontroller to immediately stop


operation and clear its registers. A reset signal may be generated
externally at any moment (low logic level on the MCLR pin). If needed,
it can also be generated by internal control logic.

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

One of the most important feature of the microcontroller is a number of input/output


pins used for connection with peripherals. In this case, there are in total of thirty-
three general purpose I/O pins available, which is quite enough for the most
applications.

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

The 16F877A has only 35 Instructions


This instruction set organized by functional groups as

38
PIC ASSEMBLY CODE

Conssist of 4 fields
Operand

Label OpCode f, d ;comments

Instruction from to

if d = W or 0 means wirte result on W


if d = F or 1 means wirte result on F
39
INSTRUCTION SET

f Register file address (0x00 to 0x7F)


W Working register (accumulator)
b Bit address within an 8-bit file register 40
PIC16F87X Data Sheet k Literal field, constant data or label
https://ww1.microchip.com/downloads/en/DeviceDoc/30292c.pdf d Destination select; d = 0: store result in W, d = 1: store result in file register f.
INSTRUCTION SET

Assuming W=h’FB’ and PORTB=h’03’

ADDWF PORTB,1 ; add W and PortB write result on PortB

W=h’FB’ and PORTB=h’FE’

ADDWF PORTB,0 ; add W and PortB write result on W


W=h’FE’ and PORTB=h’03’

P.S.: ADDWF PORTB,1 same meaning => ADDWF PORTB,F


ADDWF PORTB,0 same meaning => ADDWF PORTB,W
41
INSTRUCTION SET

f Register file address (0x00 to 0x7F)


W Working register (accumulator)
b Bit address within an 8-bit file register
k Literal field, constant data or label
d Destination select; d = 0: store result in W, d = 1: store result in file register f.
42
INSTRUCTION SET

Assuming PORTB=h’03’ => PORTB=b’00000011

BCF PORTB,1 ; clear first bit of PortB


PORTB=h’01’ => PORTB=b’00000001

Assuming PORTB=h’03’ => PORTB=b’00000011

BSF PORTB,2 ; set second bit of PortB as high


PORTB=h’07’ => PORTB=b’00000111

43
INSTRUCTION SET

f Register file address (0x00 to 0x7F)


W Working register (accumulator)
b Bit address within an 8-bit file register
k Literal field, constant data or label
d Destination select; d = 0: store result in W, d = 1: store result in file register f.
44
INSTRUCTION SET

Move literal value of 10 to W register

MOVLW b’00001010’

MOVLW h’0A’

MOVLW d’10’

45
Programming Tutorial: Directives

ORG:The origin directive is used to place the instruction which exactly


comes after it at the location it specifies.

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,

Example: include “P16F877A.inc”

46
Adressing Modes

 Addressing Modes

47
Addressing Modes

PIC16FXXX supports mainly 3 addressing modes;

immediate addressing
direct addressing mode
indirect addressing mode

48
Addressing Modes

immediate
addressing
sample

Whenever the mnemonic of the


instruction is having ‘L’,
İt indicates that the instruction is of
immidiate addressing mode

49
Addressing Modes
Direct addressing sample

MOVWF 0X21

Address location,
direct address of RAM Memory 50
Indirect Addressing

PIC16FXXX is able to perform indirect addressing by means of the INDF


and FSR registers.

It sometimes considerably simplifies program writing. T

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.

FSR: Address value


INDF: Data content

Example:
MOVLW h’20’
MOVWF FSR
51
MOVF INDF,0
Indirect Addressing

52
Microprocessors and Programming

 Program Counter and Subroutins

53
Program Counter

The size of the program memory of the PIC16F877 is 8K.


Therefore, it has 8192 locations for program storing.
For this reason, the Program Counter (PC) is 13 bits wide.

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)

Displaying number three with 7Seg connected to Port B


INCLUDE "PIC16F877A.INC "
BSF STATUS,5 ; Select BANK1 (RP0=1, RP1=0)
CLRF TRISB ; All pins of the Port B are set as Output
BCF STATUS,5 ;Select BANK0 (RP0=0, RP1=0)
CLRF PORTB ; Clear Port B (No output is active)
BASLA
MOVLW h’03’
CALL TABLO7
MOVWF PORTB
DONGU
GOTO DONGU
TABLO7
ADDWF PCL,F ; PCL+W -> PCL
RETLW h’3F’ ; if W=0 , would jump to this row
RETLW h’06’ ; if W=1 , would jump to this row
RETLW h’5B’ ; if W=2 , would jump to this row
RETLW h’4F’ ; W=3 , so this row will be executed.
RETLW h’66’ ;
RETLW h’6D’ ;
RETLW h’7D’ ;
RETLW h’07’ ;
RETLW h’7F’ ;
RETLW h’6F’ ; 59
END
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.

However, if a subroutine or jump


address are not within the same
page as the location from where the
jump is, two “missing”- higher bits
should be provided by writing to the
PCLATH register. It is illustrated in
figure as a jump to the subroutine
PP2 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

You might also like