+ All Categories
Home > Documents > PIC Discussion By Eng. Tamar Jomaa -...

PIC Discussion By Eng. Tamar Jomaa -...

Date post: 01-Dec-2018
Category:
Upload: duongthien
View: 217 times
Download: 1 times
Share this document with a friend
37
PIC Discussion By Eng. Tamar Jomaa 1
Transcript
Page 1: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

PIC Discussion

By Eng. Tamar Jomaa

1

Page 2: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2

Write assembly language instructions to clear the general

purpose registers of PIC16F84A microcontroller

(don’t write the whole program)

Page 3: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

Chapter#2

Programming Microcontroller Using Assembly

Language

Islamic university

Electrical engineering

PIC course

3

Page 4: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

Outlines for part#2:

2.4 Delay

2.5 Interrupts

2.6 TMR0

4

Page 5: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.4 Delay

In this section we will learn how building delays.

A delay is a mechanism that keeps the processor from

executing a part of its program for a specific amount of

time.

Methods for creating these delays are: "NOP" Delay : A simple delay can be achieved by throwing in a

few instructions that don't do anything useful except waste

machine cycles.

Basic Loop Delay: The next example show how to make delay for

41 cycles.

5

Page 6: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.4 Delay

6

Example#1:

Will be evaluated one cycle

Will be evaluated one cycle

Take one cycle but it repeat 10 times

Decrease from 9 to 1 take one

cycle repeat 9 times.

When decrease from 1 to 0

a skip will be happen, so

take 2 cycles.

Take 2 cycles repeat 9 times

1x9+2

1x10

1

1

Total delay = (1+1+1x10+1x9+2+2x9)cycle= 41 cycle

Note: 1cycle=4Tosc=4x(1/Fosc)=4/(4MHz)=1µsec.

so total delay=41µsec

Page 7: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.4 DelayExample#2 :is a simple example consist of connecting a led to RB0, then turning on/off

this led in a flasher way:

7

Outer loop

Inner loop

10000000W

00000000PORTB

10000000XORWF PORTB, F

Delay

10000000

10000000

00000000

W

PORTB

XORWF PORTB, F

Page 8: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

8

2.4 Delay

1x255

1x255

1x255

1x255x255

(1x254+2)x255

254x255x2

1x254+2

2x254

1

1

Note:

ODH register

will decrease from

255 to 0 then it

goes out from

LOOP0.

Then the OCH

register will

decrease by 1

then the ODH

register will full

again in then begin

decrease and so

on…

The inner loop

will repeat

255x255, but the

outer loop will

repeat only 255. 2

Page 9: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

Total delay

=[1+1+1x255+1x255+1x255+1x255x255+(1

x254+2)x255+254x255x2+1x254x2+2x254

+2]x(4/(4MHz))=0.26sec

Important note:

This method of delay is not sufficient especially at long delay so we learn another method by using TMR0.

9

2.4 Delay

Page 10: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

Example#3:Write a PIC subroutine in assembly language to give a fixed delay of

200us. Assume that a 4MHz oscillator is being used.

10

2.4 Delay

1

1

1X(K-1)

22X(K-1)

2

Total delay=

1+1+(K-1)+2+2X(K-

1)+2=3K+3 Cycles

(3K+3)µsec=200µsec

K=65

Page 11: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.5 Interrupts

11

Interrupts are a mechanism of a microcontroller which

enables it to respond to some events at the moment they

occur, regardless of what microcontroller is doing at the

time.

This is a very important part, because it provides

connection between a microcontroller and environment

which surrounds it.

Generally, each interrupt changes the program flow,

interrupts it and after executing an interrupt subprogram

(interrupt routine) it continues from that same point on.

Page 12: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.5 Interrupts

12

Sources of interrupt:

In the PIC16F84A, there are four sources of interrupt:

Internal interrupt External interrupt

Termination of writing data to

EEPROM.

TMR0 interrupt caused by

timer overflow.

Interrupt during

alteration on RB4, RB5,

RB6 and RB7 pins of port

B.

External interrupt from

RB0/INT pin of

microcontroller.

Page 13: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

Registers used with interrupt:1. Control interrupt register (INTCON) at 0Bh address.

2. Option register at 81h address.

3. Control EEPROM register (EECON 1) at 88h address.

The most important register is (INTCON), so you can see

the datasheet to know more about this register.

13

2.5 Interrupts

Page 14: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

14

2.5 Interrupts

flags

Page 15: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

15

2.5 Interrupts

Page 16: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

16

2.5 Interrupts

Page 17: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

17

2.5 Interrupts

The bellow Figure shows the most important interrupt

register INTCON, which controls the usage and selection

of interrupt sources in the PIC16F84A.

Page 18: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

18

2.5 Interrupts

We can use the bits of INTCON register to make interrupt as follow:

1) First we set GIE bit which must be enabled for any interrupt.

2) Second we enable one of this bits( T0IE ,EEIE , RBIE,INTE) as we

like to use the interrupt:

If we use external interrupt , we set INTE to 1.

If we use TMR0 interrupt , we set T0IE to 1.

If we use RB interrupt , we set RBIE to 1.

If we use EEPROM interrupt , we set EEIE 1.

3) When interrupt occurred , the one of flag bits (INTF, RBIF, TOIF) is

set physically so it must be cleared in software.

Page 19: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

19

2.5 Interrupts

Keeping the contents of important registers:

• An important part of the interrupt process is the stack.

• Its basic role is to keep the value of program counter after a

jump from the main program to an address of a subprogram.

• In order for a program to know how to go back to the point where it

started from, it has to return the value of a program counter from a

stack.

• When moving from a program to a subprogram, program counter is

being pushed onto a stack.

Page 20: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

20

2.5 Interrupts

• When executing instructions such as RETURN, RETLW or RETFIE

which were executed at the end of a subprogram, program counter

was taken from a stack so that program could continue from where it

stopped before the interrupt.

• These operations of placing on and taking off from a program counter

stack are called PUSH and POP.

• The step of push and pop is performed because the contents of the

most important registers may change during the interrupt.

• The most important registers we must keep is the work register and

status register and this is done by storing the work register in any

bank and storing the status register in bank 0.

Page 21: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

21

2.5 Interrupts

• After writing the main body of the interrupt and we can restore the

status register then restore the W register.

Page 22: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

22

2.5 Interrupts

The following example shows how to use the external interrupt to turn

on a led connected to RB2 when a pushbutton connected RB0

(External interrupt source) is pressed

Page 23: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

23

2.5 Interrupts

Page 24: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

24

2.5 Interrupts

Page 25: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

25

problem#1:

using external interrupt INT0 to implement a counter to counts from

0000 to 1111 at falling edge input.

Page 26: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

26

problem#2:

using external interrupt INT0 to implement a counter to counts from

1111 to 0000 at RISING edge input

Page 27: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Timer0 is an internal 8bit register that increments automatically with

every PIC instruction cycle until the count over flows timer capacity .

This takes place when the timer count goes from 0xff to 0x00 .

At that time ,the timer restarts the count .

The timer has the following characteristics:

– A timer register that is readable and writeable by software.

– Can be powered by an external or internal clock.

– Timing edge for external clock can be selected.

– 8-bit software programmable prescaler.

– Interrupt capability.

– Can be used as a timer or as a counter.

27

Page 28: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Two register are used for control timer0:

1) INTCON register: is used in timer mode to enable timer0 interrupt as showed in pervious section.

2) OPTION register: is used for control timer operation as select mode, postescaler for timer0 or WDT timer ,select the value of prescaler and

counter work in fall edge or high edge.

28

Page 29: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Select mode:To choose timer0 work on timer mode or counter mode, we use

bit5(T0CS). If it is set ,timer0 take external clock ( work as counter). Else it take

internal clock & work as timer.

Select edge: If trigger TMR0 was enabled with impulses from a RA4/T0CKI pin, bit

4(T0SE) would determine whether it would be on the rising or falling edge of a signal.

1= Falling edge

0= Rising edge

Prescale: To select prescale for a WDT timer or for TMR0 we use bit3 (PSA):

1=prescaler is assigned to WDT.

0=prescaler is assigned to free timer TMR0

The counter prescaler consists of the three low-order bits in the OPTION

register.

These bits allow selecting eight possible values that serve as advisor for the

counter rate .When the prescaler is disabled, the counter rate is one-fourth the

processor’s clock speed .If the prescaler is set to the maximum value (255) then

one of 255 clock. Signals actually reach the timer.

29

Page 30: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Table below shows the prescaler settings and their action on the rate of theTimer0

module and the Watchdog Timer:

30

Page 31: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Delay with TIMER0:

A general formula for calculating the number of timer beats per second is as

follows :

T =C/(4*P*R)

31

Where

T: is the number of clock beats per second

C :is the system clock speed in Hz

P: is value stored in the prescaler

R: is the number of iterations counted in

theTMR0 register.

The range of both P and R in this formula is

1 to 256 . Example how to make

delay with timer0:

Page 32: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

Counter mode:

The PIC16F84A can be programmed so hat portRA4/T0CKI is used to

count events or pulses by initializing the Timer0 module as a counter.

Without interrupts , the process requires the following preparatory steps:

Port-A , line4 , (RA4/T0CKI) is defined for input.

The Timer0 register (TMR0) is cleared.

The OPTION register bits PSA and PS0:PS2 are initialized if the

prescaler is to be used.

The OPTION register bit T0SE is set so as to increment the

count on the high-to-low transition of the port pin if the port

source is active low .Otherwise the bit is cleared.

The OPTION register bit T0CS is set to select action on the

RA4/T0CKI pin.

32

Page 33: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

2.6 TMR0

The following example is using TMR0 as a counter to count from 0

to 99 as shown in Figure:

33

Page 34: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

34

Page 35: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

35

problem#1:

using interrupt technique with tmr0 to implement 15 usec delay

2.6 TMR0

Page 36: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

36

Homework:

Using interrupt technique with tmr0 to implement 15 msec delay

Delivery date: next discussion lecture.

Page 37: PIC Discussion By Eng. Tamar Jomaa - site.iugaza.edu.pssite.iugaza.edu.ps/tjomaa/files/PIC-discussion-chapter-2-part22.pdf · PIC Discussion By Eng. Tamar Jomaa 1. 2 Write assembly

37

Be free to ask any question


Recommended