112
330_01 1 Microprocessor and Microcontroller Fundamentals PIC18F4520 Harsh Shrimal IGS Labs Technology

PIC18

Embed Size (px)

Citation preview

Page 1: PIC18

330_01 1

Microprocessor and Microcontroller Fundamentals

PIC18F4520

Harsh Shrimal

IGS Labs Technology

Page 2: PIC18

Microcontrollers

Embedded Systems Operations managed behind the scenes by a

microcontroller

Microcontroller (MCU) An integrated electronic computing device that

includes three major components on a single chipMicroprocessor (MPU)Memory I/O (Input/Output) ports

2

Page 3: PIC18

Microcontrollers

Support Devices Timers A/D converter Serial I/O

All components connected by common communication lines called the system bus.

3330_01

Page 4: PIC18

Block Diagram

330_01 4

Page 5: PIC18

Microprocessor (MPU)

A group of electronic circuits fabricated on a semiconductor chip that can read binary instructions written in memory and process binary data according to those instructions

CPU and MPU

5330_01

Page 6: PIC18

Memory

A semiconductor storage device consisting of registers that store binary bits

Two major categories Read/Write Memory

(R/W) Read-only-Memory

(ROM)

D7 D0

6330_01

Page 7: PIC18

Input/Output (I/O)

Input devices such as switches and keyboards provide binary information to the microprocessor

Output devices such as LEDs, video screens, and printers receive information from the microprocessor

7330_01

Page 8: PIC18

Microprocessor-Based Systems

8330_01

Page 9: PIC18

Microprocessor Architecture

The MPU communicates with memory and I/O using the system bus consisting of: Address bus: unidirectional and carries memory

and I/O addresses Data bus: bidirectional; transfers binary data and

instructions between MPU and memory and I/O Control lines: Read and Write timing signals

asserted by MPU

9330_01

Page 10: PIC18

Microprocessor-Based System with Buses: Address, Data, and Control

10330_01

Page 11: PIC18

Data Format (8-bit)

Unsigned Integers: All eight bits (Bit7 to Bit0) represent the magnitude of a number Range 00 to FF in Hex and 0 to 255 in decimal

Signed Integers: Seven bits (Bit6 to Bit0) represent the magnitude of a number. The eighth bit (Bit7) represents the sign of a number.

The number is positive when Bit7 is zero and negative when Bit7 is one.

Positive numbers: 00 to 7F (0 to 127) Negative numbers: 80 to FF (-1 to -128) All negative numbers are represented in 2’s complement

11330_01

Page 12: PIC18

Data Format (8-bit)

Binary Coded Decimal Numbers (BCD) 8 bits of a number divided into groups of four,

and each group represents a decimal digit from 0 to 9

Four-bit combinations from A through F in Hex are invalid in BCD numbersExample: 0010 0101 represents the binary coding of

the decimal number 25 which is different in value from 25H.

12330_01

Page 13: PIC18

PIC18F Microcontroller Families

PIC microcontrollers are designed using the Harvard Architecture which includes: Microprocessor unit (MPU) Program memory for instructions Data memory for data I/O ports Support devices such as timers

Page 14: PIC18

PIC18F – MPU and Memory

Page 15: PIC18

Microprocessor Unit

Arithmetic Logic Unit (ALU) WREG – working register (8-bit accumulator) Status register that stores flags (5-bits) Instruction decoder (16-bit instructions)

Registers BSR (4-bit register used in direct addressing the data memory)

FSR (12-bit registers used as memory pointers in indirect addressing data memory)

Program Counter (PC) (21-bit register that holds the program memory address while executing programs)

Page 16: PIC18

PIC18F - Buses

Address bus 21-bit address bus for program memory addressing

capacity: 2 MB of memory 12-bit address bus for data memory addressing capacity:

4 KB of memory

Data bus 16-bit instruction/data bus for program memory 8-bit data bus for data memory

Control signals Read and Write

Page 17: PIC18

PIC18F452/4520 Memory

Program Memory: 32 K Address range: 000000 to 007FFFH

Data Memory: 4 K Address range: 000 to FFFH

Page 18: PIC18

PIC18F452—I/O Ports A and B

Page 19: PIC18

MCU Support Devices

Page 20: PIC18
Page 21: PIC18

PIC18F Special Features

Sleep mode Watchdog timer (WDT) Code protection In-circuit serial programming In-circuit debugger

Page 22: PIC18

Assembly code:

1 Problem statement: Write instructions to light up alternate LEDs at

PORTC

000000 0E00 MOVLW 00 ;Load W with 0s000002 6E94 MOVWF TRISC,0 ;Set PORTC as output000004 0E55 MOVLW 0x55 ;Load 55 to turn on

LEDs000006 6E82 MOVWF PORTC,0 ;Turn on LEDs000008 0003 SLEEP ;Power Down

Page 23: PIC18

Illustration

Execution of the instruction:

MOVWF PORTC

Page 24: PIC18
Page 25: PIC18

ASSEMBLY LANGUAGE

PIC18F

Page 26: PIC18

Addressing Modes

Method of specifying of an operand Immediate (Literal) addressing

The operand is a number that follows the opcode

MOVLW 0X10 Direct addressing

The address of the operand is a part of the instruction

MOVWF 0X20 Indirect addressing

An address is specified in a register (pointer) and the MPU looks up the address in that register

Page 27: PIC18

Instruction Format

The PIC18F instruction format divided into four groups Byte-Oriented operations Bit-Oriented operations Literal operations Branch operations

Label Opcode Operand Comment

START: MOVLW 0xF2 ;Load F2H in W

↑ ↑ ↑ ↑

Tab Tab Tab Semicolon

Page 28: PIC18

1. Data Transfer Instructions.:

MOVLW k ; Move constant to W

MOVLW 0X20MOVWF f ; Move W to f

MOVWF 0X20

MOVFF F,F ; Move f to f

MOVFF 0X20,0X30

Page 29: PIC18

2. Arithmetic Instructions.:

ADDLW K ;Add KH to W ADDWF f, d ;Add W and f W +f -> d SUBLW k ;Subtract W from constant SUBWF f,d ;Subtract W from f INCF F DECF F CLRF F SETF F

Page 30: PIC18

ADD instruction

LIST P=18F4520 ;directive to define processor#include <P18F4520.INC>;processor specific variable definitions

REG0 EQU 0X20REG1 EQU 0X30REG2 EQU 0X40ORG 00GOTO MAINMAINMOVLW 0x0FMOVWF REG0MOVLW 0x20MOVWF REG1ADDWF REG0,WMOVWF REG2SLEEP

Page 31: PIC18
Page 32: PIC18
Page 33: PIC18

3. Logic Instruction.:

ANDLW k ;Logical AND with W with K IORLW k ; Logical OR with W with constant XORLW k ; Logical exclusive OR with W with f ANDWF f, d ; Logical AND with W with f IORWF f, d ; Logical OR with W with f XORWF f, d ; Logical exclusive OR with W with

constant

Page 34: PIC18

4. Bit oriented instruction.:

BSF file reg, bit ; bit set file reg.

BCF file reg, bit ; bit clear file reg.

BTG file reg, bit ; bit toggle file reg.

BTFSS file reg, bit ; bit test file reg. skip next instruction if bit is set

BTFSC file reg, bit ; bit test file reg. skip next instruction if bit is clear

Page 35: PIC18

5. Program Control Instructions.:

BTFSC f ,b BTFSS f ,b DECFSZ f ,d INCFSZ f ,d GOTO k CALL k RETURN ;Return from subroutine RETLW k ;Return with constant in W RETFIE ;Return from interrupt

Page 36: PIC18

6. Other instructions.:

NOP ; No operation CLRWDT ; Clear watchdog timer SLEEP ; Go into sleep mode

Page 37: PIC18

1. Initializing a File Register

Page 38: PIC18

Problem on port:

Page 39: PIC18

2. Loop statements.:

INCFSZ f, dIncrement the contents of register f. Store the result in W (if d = 0) or in register f (if d =1). If the incremented value of f = 0, skip the next line of code.

DECFSZ f, dDecrement the contents of register f. Store the result in W (if d = 0) or in register f (if d =1). If the decremented value of f = 0, skip the next line of code.

BTFSC f, bTest bit b of register f. If bit b of register f is clear (that is, it equals zero), skip the next line of code.

BTFSS f, bTest bit b of register f. If bit b of register f is set (that is, it equals one), skip the next line of cod

Page 40: PIC18

Loop Example

COUNT1 EQU 0x01 ;Counter is REG01ORG 00GOTO START

ORG 0x20START: MOVLW 0x05 ;Initialize Counter to 5

MOVWF COUNT1LOOP1: DECFSZ COUNT1;Decrement Counter, Skip if 0

GOTO LOOP1SLEEP ;DoneEND

Page 41: PIC18

Nested Loop.:

Page 42: PIC18

Data on Port C.:

Page 43: PIC18

Special Function Registers

Data registers associated with I/O ports, support devices, and processes of data transfer

I/O Ports (A to E) InterruptsEEPROMSerial I/OTimersCapture/Compare/PWM (CCP)Analog-to-Digital (A/D) Converter

Page 44: PIC18

File register (data RAM):

The file register data RAM in PIC is divided into two sections:

• Special Function Registers (SFR)

• General-Purpose Registers (GPR)

SFRs (Special Function Registers)

The Special Function Registers (SFRs) are dedicated to specific functions such as ALU status, timers, serial communication, I/O ports, ADC, and so on. The function of each SFR is fixed by the CPU designer at the time of design because it is used for control of the microcontroller or peripheral. The PIC SFRs are 8-bit register.

Page 45: PIC18

P18F4520

Timers

Page 46: PIC18

Timer0 Timer1 Timer2 Timer3

Page 47: PIC18

Timer0.:

Software selectable operation as a timer or counter in both 8-bit or 16-bit modes

Readable and writable registers Dedicated 8-bit, software programmable

prescaler Selectable clock source (internal or external) Edge select for external clock Interrupt-on-overflow

Page 48: PIC18

Registers associated with Timer0.:

Page 49: PIC18
Page 50: PIC18

Prescaler.:

By default the timer is incremented by one in every instruction cycle.

Say that we are using the crystal of 4MHz i.e. timer will increment at instruction cycle rate of

1MHz. So TMR0 will increment in every 1us. So maximum we can make a delay of 255us. To increase this period we use prescaler.

Let PS<2:0>=111 Now TMR0 will increase in 256 instruction cycle. i.e. We can produce the delay of

255 * 255us= 65.28ms

Page 51: PIC18

Period.:

Equation to calculate TMR0L value for period.:

Period = (256 - TMR0)*(4/fosc)*(Prescaler)

For example if Period = 1 ms and fosc = 4 MHz and using

Prescaler 1:41ms = (256 - TMR0)(1us)(1)TMR0 = 6 = 0x06

Page 52: PIC18
Page 53: PIC18

Timer1.:

Software selectable operation as a 16-bit timer or counter

Readable and writable 8-bit registers (TMR1H and TMR1L)

Selectable clock source (internal or external) Interrupt-on-overflow Reset on CCP Special Event Trigger Device clock status flag (T1RUN)

Page 54: PIC18

Registers associated with Timer1.:

Page 55: PIC18
Page 56: PIC18
Page 57: PIC18

Timer2.:

8-bit timer and period registers (TMR2 and PR2, respectively)

Readable and writable (both registers) Software programmable prescaler (1:1, 1:4 and

1:16) Software programmable postscaler (1:1 through

1:16) Interrupt on TMR2-to-PR2 match Optional use as the shift clock for the MSSP or

PWM module

Page 58: PIC18

Registers associated with Timer2.:

Page 59: PIC18

Timer2 Operation.:

In normal operation, TMR2 is incremented from 00h on each clock (FOSC/4).

The value of TMR2 is compared to that of the period register, PR2 on each clock cycle.

When the two values match, the comparator generates a match signal as the timer output.

This signal also resets the value of TMR2 to 00h on the next cycle and drives the output counter/ postscaler.

Page 60: PIC18
Page 61: PIC18

T2CON.:

Page 62: PIC18
Page 63: PIC18

Timer3.:

Software selectable operation as a 16-bit timer or counter

Readable and writable 8-bit registers (TMR3H and TMR3L)

Selectable clock source (internal or external) Interrupt-on-overflow Module Reset on CCP Special Event Trigger

Page 64: PIC18

Timer3.:

Page 65: PIC18

The Watchdog Timer.:

Suppose you have written a program that is continuously running on a PIC.

Now consider this two situations.:1. Let us say that the PIC is monitoring an

input. When this input goes high, it jumps to another part of the program. What happen when its not getting high?

2. The another situation after a long period of time, the program gets stuck somewhere and the PIC gets caught in a loop.

Page 66: PIC18

So what is a Watchdog Timer?

What’s needed in both cases is some kind of  reset if the program gets stuck.

This is the purpose of a watchdog circuit.

Page 67: PIC18

How WDT works?

Well, inside the PIC there is a resistor/capacitor network. 

This provides a unique clock, which is independent of any external clock that you provide in your circuit.

Now, when the Watchdog Timer is enabled, a counter starts at 00 and increments by 1 until it reaches FF.

The only way we can stop the WDT from resetting the PIC is to periodically reset the WDT back to 00 throughout our program.

Page 68: PIC18

Registers associated with WDT.:

Page 69: PIC18

P18F4520

INTERRUPTS

Page 70: PIC18

Interrupts.:

Interrupts are situations that the CPU can't predict when they will happen, they can happen any time, so the CPU does not wait for them. So the CPU keeps on doing its normal job unless and interrupt occurs.

External interrupts TIMER interrupts Analog to Digital Converter Interrupts Data Interrupts 

Page 71: PIC18

Interrupts.:

The PIC18F devices have multiple interrupt sources.:

High priority - Vector at 0008h

Low priority - Vector at 0018h

High priority interrupt events will interrupt any low priority interrupts that may be in progress.

Page 72: PIC18

Registers associated with interrupts.:

RCON INTCON INTCON2 INTCON3 PIR1, PIR2 PIE1, PIE2 IPR1, IPR2

Flag bit Enable bit Priority bit

Page 73: PIC18
Page 74: PIC18

Timer0 interrupt

Page 75: PIC18

Timer0 interrupt

Page 76: PIC18

Analog to Digital Converter (ADC).: 

Many electrical signals around us are Analog in nature. That means a quantity varies directly with some other quantity.

The first quantity is mostly voltage while that second quantity can be anything like temperature, pressure, light, force or acceleration.

For example in LM35 temperature sensor the output voltage varies according to the temperature, so if we could measure voltage, we can measure temperature.

Page 77: PIC18

ADC.:

But most of our computer (or Microcontrollers) are digital in nature.

They can only differentiate between HIGH or LOW level on input pins. For example:

If input is more than 2.5v it will be read as 1 and if it is below 2.5 then it will be read as 0 .

So we cannot measure voltage directly from MCUs. To solve this problem most modern MCUs have an ADC

unit. In PIC18F4520 there are 13 analog input channels, they are

named AN0, AN1 etc.

Page 78: PIC18

Specifications of ADCs.:

Most important specification of ADCs is the resolution. This specifies how accurately the ADC measures the analog input signals.

Common ADCs are 8 bit, 10 bit and 12 bit. For example: If the reference voltage of ADC is 0 to 5v then

1. A 8-bit ADC will break it in 256 divisions so it can measure it accurately up to 5/256 v= 19mV approx.

2. While the 10-bit ADC will break the range in 5/1024 = 4.8mV approx.

So you can see that the 8-bit ADC can't tell the difference between 1mV and 18mV. The ADC in PIC18 are 10-bit.

Page 79: PIC18

Reference Voltage.:

The reference voltage specifies the minimum and maximum voltage range of analog input. In PIC 18 there are two reference voltage, one is the Vref- and one is Vref+.

The Vref+ and Vref- pins are available in PIN5 and PIN4 of the PIC18F4520 chip.

Page 80: PIC18

Acquisition Time.:

When an specific channel is selected the voltage from that input channel is stored in an internal holding capacitor.

It takes some time for the capacitor to get fully charged and become equal to the applied voltage.

This time is called acquisition time.  A safe value is 2.45uS, so acquisition time

must be set to any value more than this.

Page 81: PIC18

ADC Clock (Conversion time).:

 ADC Requires a clock source to do its conversion, this is called ADC Clock. The time period of the ADC Clock is called TAD.

There are Seven possible option.1. 2 x TOSC2. 4 x TOSC3. 8 x TOSC4. 16 x TOSC5. 32 x TOSC6. 64 x TOSC7. Internal RC

Page 82: PIC18

PIC18 ADC.:

The Analog-to-Digital (A/D) converter module has 10 inputs for the 28-pin devices and 13 for the 40/44-pin devices.

This module allows conversion of an analog input signal to a corresponding 10-bit digital number.

The module has five registers:1. A/D Result High Register (ADRESH)2. A/D Result Low Register (ADRESL)3. A/D Control Register 0 (ADCON0)4. A/D Control Register 1 (ADCON1)5. A/D Control Register 2 (ADCON2)

Page 83: PIC18
Page 84: PIC18
Page 85: PIC18
Page 86: PIC18

330_01 86

Page 87: PIC18

ADC BLOCK DIAGRAM.:

Page 88: PIC18
Page 89: PIC18

Serial

Communication

Page 90: PIC18

Types of Serial Communication.:

Synchronous (I2C) Asynchronous (UART) In USART a start bit and stop bits are used to

synchronize the incoming data the.

Page 91: PIC18

Serial Communication.:

What a level converter:

RS232 level signals (HIGH=-12V LOW=+12V) from PC

TTL level signal (HIGH=+5V LOW=0V) to be fed to MCU

Page 92: PIC18

Synchronization.:

Page 93: PIC18

Baud Rate.:

As there is no "clock" line so for synchronization accurate timing is required so transmissions are carried out with certain standard speeds.

The speeds are measured in bits per second. The speed is also known as baud rate. 

Some standard baud rates are1200 192002400 384004800 576009600 115200... etc

Page 94: PIC18

Transmitting device.:

For example we chose the speed as 9600bps(bits per second).

As we are sending 9600 bits per second one bits takes 1/9600 seconds or 0.000104 sec or 104 uS

then it send each bits with duration = 104uS

Page 95: PIC18

Receiving device.:

Page 96: PIC18

ENHANCED UNIVERSAL SYNCHRONOUS RECEIVER TRANSMITTER (EUSART).:

Also known as a Serial Communications Interface or SCI.

The EUSART can be configured as a full-duplex asynchronous system that can communicate with peripheral devices, such as

CRT terminals personal computers

It can also be configured as a half-duplex synchronous system that can communicate with peripheral devices, such as

A/D or D/A integrated circuits serial EEPROMs, etc.

330_01 96

Page 97: PIC18

The pins of the Enhanced USART are multiplexed with PORTC. In order to configure RC6/TX/CK and RC7/RX/DT as a USART:

bit SPEN (RCSTA<7>) must be set (= 1) bit TRISC<7> must be set (= 1) bit TRISC<6> must be set (= 1)

Registers.: Transmit Status and Control (TXSTA) Receive Status and Control (RCSTA) Baud Rate Control (BAUDCON)

330_01 97

Page 98: PIC18
Page 99: PIC18
Page 100: PIC18
Page 101: PIC18

CAPTURE/COMPARE/PWM(CCP) MODULES.:

A capture, compare, and PWM, module, or CCP for short, is designed into the PicMicro to assist with measurement or control of time based pulse signals, module contains:-

a 16-bit register which can operate as a Capture register

a 16-bit Compare register a 10-bit PWM Master/Slave Duty Cycle

register.

Page 102: PIC18

CCP Module Configuration.:

Each Capture/Compare/PWM module is associated with a control register (generically, CCPxCON) and a data register (CCPRx).

The data register, in turn, is comprised of two 8-bit registers: CCPRxL (low byte) and CCPRxH (high byte).

All registers are both readable and writable.

Page 103: PIC18

CCP MODE – TIMER RESOURCE.:

Page 104: PIC18

Capture Mode.:

In Capture mode, the CCPRxH:CCPRxL register pair captures the 16-bit value of the TMR1 or TMR3 registers when an event occurs on the corresponding CCPx pin.

An event is defined as one of the following:• every falling edge• every rising edge• every 4th rising edge• every 16th rising edge

The event is selected by the mode select bits, CCPxM3:CCPxM0 (CCPxCON<3:0>).

When a capture is made, the interrupt request flag bit, CCPxIF, is set; it must be cleared in software.

Page 105: PIC18
Page 106: PIC18

Compare Mode.:

In Compare mode, the 16-bit CCPRx register value is constantly compared against either the TMR1 or TMR3 register pair value. When a match occurs, the CCPx pin can be:

• driven high

• driven low

• toggled (high-to-low or low-to-high)

• remain unchanged (that is, reflects the state of the I/O latch) The action on the pin is based on the value of the mode select

bits (CCPxM3:CCPxM0). At the same time, the interrupt flag bit, CCPxIF, is set.

330_01 106

Page 107: PIC18
Page 108: PIC18
Page 109: PIC18
Page 110: PIC18

PWM Mode.:

In Pulse Width Modulation (PWM) mode, the CCPx pin produces up to a 10-bit resolution PWM output.

Since the CCP2 pin is multiplexed with a PORTB or PORTC data latch, the appropriate TRIS bit must be cleared to make the CCP2 pin an output.

Page 111: PIC18
Page 112: PIC18

THANK YOU