0% found this document useful (0 votes)
47 views60 pages

MCU 03 Unlocked

Uploaded by

Sude Öztürk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views60 pages

MCU 03 Unlocked

Uploaded by

Sude Öztürk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Microprocessors and Programming

Dr. Kadir Erkan

YILDIZ TEKNİK ÜNİVERSİTESİ

Fall : 2024
MKT3811 Week3 27.10.2021
Recap (Week-2)

Bus Architecture Instruction Cycle

System bus is consist of data bus,


address bus and control bus. Fetch

Execute Decode

Pipelining:

CPUs the fetch execute cycle is executed


simultaneously, in parallel via an instruction
pipeline,

which means the next command starts being


executed before the previous command has
completed.
2
Recap (Week-2)

Memory Types

Volatile Data memory

Non- Volatile Program memory


• PROM (OTP)
• EPROM (UV-EPROM)
• EEPROM)
• Flash Memory EPROM

3
Recap (Week-2)

Hardware Architecture Software Architecture

Von Neumann (princeston) CISC Complex Instruction Set Computer

Harward RISC Reduced Instruction Set Computer

4
Lecture Contents of Week 3

 Microcontroller Types
 Criteria for Choosing a Microcontroller
 PIC Microcontrollers

5
Microprocessors and Programming

 Microcontroller Types

6
Types of Microcontroller

7
Microprocessors and Programming

 Criterias for Choosing a microcontroller

8
Criteria for Choosing a Microcontroller

The main criteria in selecting a microcontroller are given below in the order of
importance;

A. System Requirement;
The design should start with a blank sheet of paper specifying the application
needs, if the system requires a single chip MCU or additional peripherals are to
be used with it.

Depending upon number of bits processed:


Selection process starts with a decision of choosing either the 4-bit, 8-bit, 16-bit
or 32-bit microcontroller required by the application..

To develop code for 4-bit architectures is harder and handling 4-bit instructions
and data widths can limit arithmetic capabilities. Most of the small scale
embedded applications are implemented using 8-bit microcontroller as the
technology has been around a long time and huge number of controllers is
available in the market.
9
Criteria for Choosing a Microcontroller

B. Memory Architecture
This is very important factor while designing any microcontroller based system.
Three different types of memory play an important role in the selection process
are

(a) Program memory i.e. Flash, OTP, ROM and ROM less components,
(b) Data memory i.e. on-chip SRAM or external SDRAM and
(c) Non Volatile Memory i.e. EEPROM or Flash.

Whether the memory is on-chip or off-chip and how much the size is required
may be a key factor defines cost of building the system and speed of operation.

10
Criteria for Choosing a Microcontroller

C. Availability
Before going to implement the system, the availability of the device should be
checked for both now and future. If sufficient quantities are available with bright future
then no need to be worried about the failure of the project.

D. Size
If IC of 15 I/O pins is required to develop the system there is no need to use 40 pin IC
with 32 I/O pins. In this way the size of the IC can be reduced and thus physical
space required to implement the system is also reduced. So, physical size of
IC may well be critical factor for specific applications.

E. Cost
This is the most important factor. If the system is to be implemented within the limit
of the budget calculated earlier the cost of each and every component (selected MCU
along with supporting ICs) used to build the system should be minimized tactfully to
fulfill the requirements. On chip features will trade with inventory and assembly cost
of using extra supporting external components. They can also cut development time
and effort by providing a ready integrated solution.
11
Criteria for Choosing a Microcontroller

F. Security
Hacking which targets IoT devices is rising, a threat that is especially relevant to
microcontrollers used in automobiles. In response, microcontroller makers are
implementing layers of security such as cryptography and physical security. Now,
users can purchase microcontrollers that have been certified to the latest security
standards or use MCUs with on-chip secure hardware.

G. Manufacturer’s Support
During the step by step implementation of the system if the design engineer faces
any kind of problem he/she may communicate with the marketing/sales, field
application engineers. The manufacturers should have some facilities like a help line,
toll free number, fax number, after-sales support, sufficient knowledgeable and helpful
expertise support personnel

12
Criteria for Choosing a Microcontroller

H. Availability of Development Support


Here the key consideration includes Assembler, Debugger, a code efficient C
compiler, emulator, technical support.

Trend towards programming in high level language like C is increasing day by day.
This language allows using of some portability of code and libraries. This provides
more practical consideration using different microcontroller family. Choosing the
appropriate Hardware and software development tools is also important while
selecting MCU.

I. Compatibility
The function of a system can be changed or upgraded by changing the software or
replacing one IC with another one without incurring heavy additional cost.
The new one will be pin compatible as well as function compatible.

13
Criteria for Choosing a Microcontroller

J. Power Management
Most microcontrollers have the features of power down, idle & sleep modes and
consideration needs to be given to maximize the use of intelligent power
management systems to reduce power consumption.

K. Manufacturer’s Track Record


Manufacturers should ensure the stability, good performance, better throughput,
Reliability; better Serviceability, software support, correctness, wide and
timely availability of their products.

For an example product of Intel, NXP, Zilog, Renesas, Infineon, TI, ST and
Microchip Technology are stable, mature and single sourced.

L. Functionality Testing
To check the function of the implemented system correctly the function of the
MCU should be checked within the test circuit designed earlier before going to
develop the overall system. The function testing circuit should be in affordable
cost.
14
Microprocessors and Programming

 Microchip Microcontrollers

15
PIC Microcontrollers

First PIC mC was released to the market in 1994 by the manufacturer


MICROCHIP INC.

PIC MCs were designed firstly to help and lighten pheripheral processing
work of microprocessors…

PERIPHERAL INTERFACE CONTROLLER


(Çevresel arabirim denetleyicisi!!!).

16
Why PIC Microcontroller

Advantages of Utilizing PIC

 Cost effective
 high performance for arithmetic and as well as logic operations
 quick access to data and program memory by using separate bus systems
 the family of PIC microcontrollers includes 4-bit, 8-bit, 16-bit and 32-bit mC
 availability of running in high clock frequencies
 in sleep mode 1μA electric current is required
 primitive oscillator architecture is supported such as 2 cap. + 1 res.
 high and low level programmability; (C – Basic – JAL, … and PIC Assembly)
 based on Harvard architecture by reduced instruction set…
 MPLAB-X PIC assembly compiler is free of charge
 from 6 to 80 pin I/O model is supported…

17
PIC Microcontrollers

Microchip provides an extensive


product portfolio to meet the needs of
hundreds of diverse customers
applications worldwide.

Microchip portfolio includes 8, 16 and


32 bit PIC.

So whether you are designing a simple


application or a complex and highly
intricate design microchip likely has the
perfect solution for your embedded
design needs.

Secondly, to offer customers a low-risk


developer environment PIC
microcontroller offers to seem less
migration within the complete range of
product

18
PIC Microcontrollers

8-Bit PIC® Microcontrollers

19
PIC 16FXXX Microcontroller

16F84 Vs 16F877

20
PIC 16F887 Basic Features

•RISC architecture •256 bytes EEPROM memory


• Only 35 instructions to learn •368 bytes RAM memory
• All single-cycle instructions except branches •A/D converter:
•Operating frequency 0-20 MHz • 14-channels
•Precision internal oscillator • 10-bit resolution
• Factory calibrated •3 independent timers/counters
• Software selectable frequency range of 8MHz to 31KHz •Watch-dog timer
•Power supply voltage 2.0-5.5V •Analogue comparator module with
• Consumption: 220uA (2.0V, 4MHz), • Two analogue comparators
11uA (2.0 V, 32 KHz) 50nA (stand-by mode) • Fixed voltage reference (0.6V)
•Power-Saving Sleep Mode • Programmable on-chip voltage reference
•35 input/output pins •PWM output steering control
• High current source/sink for direct LED drive •Enhanced USART module
• software and individually programmable pull-up resistor • Supports RS-485, RS-232 and LIN2.0
• Interrupt-on-Change pin • Auto-Baud Detect
•8K ROM memory in FLASH technology •Master Synchronous Serial Port (MSSP)
•In-Circuit Serial Programming Option • supports SPI and I2C mode
• Chip can be programmed even embedded in the target device

21
PIC 16F887

Overall Features

• 8 bit controller
• 40 pins (35 I/O)
• RISC software architecture
• 35 instructions
• Support for interrupts
• Direct, indirect and relative addressing modes
• Power saving features
• Watch dog timer
• Harvard hardware architecture
• Support for
 8k x (14bit) words of Flash Program Memory
 368 x (8bit) bytes of Data Memory (RAM)
 256 x (8bit) bytes of EEPROM Data Memory
22
PIC 16F877A

16F877A Some Peripheral Features

• Support for I2C


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

23
PIC 16F887

DIP package 16F887 40 Pins

24
PIC 16F8x

Maximum operating frequency is 20MHz.


The PIC instruction clock:
On most It is FOsc/4.

For example;

• Utilizing 20-MHz crystal oscillator


PIC16 will have a 5 MHz instruction clock, or a 200-ns pulse rate to process time.

• Utilizing 4-MHz crystal oscillator


PIC16 will have a 1 MHz instruction clock, or a 1μs pulse rate to process time.

Other than call and goto, all the instructions processed in one instruction cycle.
25
PIC 16F887 I/O

E MCLR

5 Ports – (A,B,C,D,E) A0
A1
A2
AA3 B
A4

E VCC
GND

VCC
GND
OSC
D
A OSC

C C

D D

26
PIC 16F887 I/O

PORT A & PORT E


are ADC
A
B
A: 5 pins
E: 3 pins
E
B: 6 pins

Total 14 Analog pins

Resolution : 10 bit

27
PIC 16F887 Special Protocols

UART
Universal Asynchronous
Receiver and Transmitter

Requires two special pins


TX and RX to operate

UART connection diagram

C7
C6

28
PIC 16F887 Special Protocols

ISPI
2C

Inter-Integrated Circuit
Requires two pins to operate

• SDA : Any data sent from one device to another


goes through the SDA wire.

• SCL : Synchronization clock for the data transfer.

Pin 18 Clock C3 C4

29
PIC 16F887 Special Protocols

SPI
Serial Peripheral Interface
Requires special three pins to operate
• Clock (SCK : Serial Clock)
• Data In (SDI : SPI Data In )
• Data Out (SDO : SPI Data Out)
• Chip Enable or Slave Select (SS)

C5
Pin 18 Clock C3 C4

30
PIC 16F887 External Int & PWM

Interrupt
SPI

External Interrupt pin : 33 (Port B0)


You cannot use any other pin other than
this pin33 for external interrupt

PWM B0 Ext. Int.


Pulse Width Modulation

There are two PWM Modules


CCP2 (Capture, Compare, PWM ) Pin 17 Port C2
CCP1 (Capture, Compare, PWM ) Pin 16 Port C1

Pin 16 PWM 2 C1
Pin 17 PWM 1 C2

31
PIC 16F8X Memory Types

•Program Memory - A memory that contains the program(which we had written), after
we've burned it. As a reminder, Program Counter executes commands stored in the
program memory, one after the other.

•Data Memory – This is RAM memory type, which contains a special registers like SFR
(Special Faction Register) and GPR (General Purpose Register). The variables that we store in
the Data Memory during the program are deleted after we turn of the micro.

•Data EEPROM (Electrically Erasable Programmable Read-Only


Memory) -
A memory that allows storing the variables as a result of burning the written program.
Each one of them has a different role. Program Memory and Data Memory two memories
that are needed to build a program, and Data EEPROM is used to save data after the
microcontroller is turn off.

Program Memory and Data EEPROM they are non-volatile memories, which store the information
even after the power is turn off. These memories called Flash or EEPROM. In contrast, Data Memory
does not save the information because it needs power in order to maintain the information stored
in the chip.

32
PIC 16FXXX Microcontroller

Simplified internal block diagram of PIC 16FXXX mC;

PROGRAM REGISTER
ADDRESS ADDRESS
SPECIAL
FUNCTION
8-BIT
13-BIT REGISTER
Program (SFR)
CPU
Memory
DATA
14-BIT 8-BIT MEMORY –
GPR
INSTRUCTION DATA

33
PIC 16FXXX Microcontroller

34
PIC 16FXXX Microcontroller
Simplified internal block diagram of PIC 16FXXX mC;

RAM = 128x4=512byte GPR:368byte


35
PIC 16FXXX Microcontroller

36
PIC 16FXXX Microcontroller

The PIC16F87XA devices have a 13-bit


program counter capable of addressing
an 8K word x 14 bit program memory
space.

The Reset vector is at 0000h

the interrupt vector is at 0004h.

There 8 stacks for pushed and popped

10/13/2024 37
PIC 16FXXX Microcontroller

STACK
Ana Program

Goto Program 1
1000h 1000h 2050h 1000h
1000h
4
1
Son

Program 1
2000h

Goto Program 2
2050h
Dön
2 3
Program 2
3000h
38
Dön
PIC 16FXXX Microcontroller

39
PIC 16FXXX Microcontroller

SFR: Special Function Registers


GPR: General Purpose Registers
Total RAM memory = GPR(368 bytes) + SFR(96 bytes) = 512 bytes
40
PIC 16FXXX Microcontroller

The data memory is partitioned into multiple banks which contain the General
Purpose Registers and the Special Function Registers. Bits RP1 (Status<6>) and
RP0 (Status<5>) are the bank select bits.

Each bank extends up to 7Fh (128 bytes). The lower locations of each bank are
reserved for the Special Function Registers. Above the Special Function Registers
are General Purpose Registers, implemented as static RAM. All implemented
banks contain Special Function Registers.

Some frequently used Special Function Registers from one bank may be mirrored
in another bank for code reduction and quicker access.

41
PIC 16FXXX Microcontroller

(Flag Register)

Bank0 Bank1 Bank2 Bank3

42
PIC 16FXXX Microcontroller
Status Register (Flag Register)

bit 0 (C): Carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions)


1 = A carry-out from the Most Significant bit of the result occurred
0 = No carry-out from the Most Significant bit of the result occurred

bit 1 (DC): Digit carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF


instructions)
(for borrow, the polarity is reversed)
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
43
PIC 16FXXX Microcontroller
Status Register (Flag Register)

bit 2 (Z): Zero bit


1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero

bit 3 (PD): Power-down bit


1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction
This bit is Read Only and after a reset it will get the value 1.
The Power Down bit watches if the 'SLEEP' instruction was executed. The values it can get are:

44
PIC 16FXXX Microcontroller
Status Register (Flag Register)

bit 4 (TO): Time-out bit


1 = After power-up, CLRWDT instruction, or SLEEP instruction
0 = A WDT time-out occurred
This bit is Read Only and after a reset it will get the value 1. The Power Down bit
watches if a Watchdog timer time out occurred.

45
PIC 16FXXX Microcontroller
Status Register (Flag Register)

bit 7 (IRP): Register Bank Select bit (used for indirect addressing)
1 = Bank 2, 3 (100h - 1FFh)
0 = Bank 0, 1 (00h - FFh)

bit 6-5 (RP1:RP0): Register Bank Select bits (used for direct addressing)
11 = Bank 3 (180h - 1FFh)
10 = Bank 2 (100h - 17Fh)
01 = Bank 1 (80h - FFh)
00 = Bank 0 (00h - 7Fh)
Each bank is 128 bytes

46
PIC 16FXXX Microcontroller

General-Purpose Registers

General-Purpose registers are used for storing temporary data and results created
during operation. For example, if the program performs a counting (for example,
counting products on the assembly line), it is necessary to have a register which
stands for what we in everyday life call “sum”. Since the microcontroller is not
creative at all, it is necessary to specify the address of some general purpose
register and assign it a new function.

A simple program to increment the value of this register by 1, after each product
passes through a sensor, should be created.

Therefore, the microcontroller can execute that program because it now knows
what and where the sum which must be incremented is. Similarly to this simple
example, each program variable must be pre-assigned some of general-purpose
register.
47
PIC 16FXXX Microcontroller

ACCESSES

16 of the registers in bank 0,


however, are known as "common
RAM".
The reason they are known as
common RAM is that unlike the
other general purpose registers,
these registers are mapped in all 4
banks so we can always access
them regardless of the currently
active register bank. These
locations reside at addresses 0x70-
0x7F in bank 0.

48
PIC 16FXXX Microcontroller

SFR Registers

Special-Function registers are also RAM memory locations, but unlike general-
purpose registers, their purpose is predetermined during manufacturing process
and cannot be changed. Since their bits are physically connected to particular
circuits on the chip (A/D converter, serial communication module, etc.), any change
of their contents directly affects the operation of the microcontroller or some of its
circuits.

For example, by changing the TRISA register, the function of each port A pin can be
changed in a way it acts as input or output. Another feature of these memory
locations is that they have their names (registers and their bits), which considerably
facilitates program writing. Since high-level programming language can use the list
of all registers with their exact addresses, it is enough to specify the register’s name
in order to read or change its contents.

49
SFR Registers in Bank 0

50
SFR Registers in Bank 1

51
SFR Registers in Bank 2,3

52
INSTRUCTION SET

The 16F8X has only 35 Instructions


This instruction set organized by functional groups as
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
INSTRUCTION SET

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

ADDWF PORTB,1 ; add W and PortB than 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
INSTRUCTION SET

f Register file address (0x00 to 0x7F)


W Working register (accumulator)
b Bit address within an 8-bit file register
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

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.
INSTRUCTION SET

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

BCF PORTB,1 ; clear first bit of PortB

PORTB=b’00000001
(PORTB=h’01’ )

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

BSF PORTB,5 ; set 5th bit of PortB as high


PORTB=h’23’ => PORTB=b’00100011
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.
INSTRUCTION SET

Move literal value of 10 to W register

MOVLW b’00001010’

MOVLW h’0A’

MOVLW d’10’

You might also like