Sunday, April 12, 2009

Learn PIC Assembly Language Part - 6


Calculating the Delay Accurately

All instructions are a single word, except for three double- word instructions. These three instructions were made double-word instructions so that all the required information is available in these 32 bits. In the second word, the 4-MSbs are 1’s. If this second word is executed as an instruction (by itself), it will execute as a NOP.

All single word instructions are executed in a single instruction cycle, unless a conditional test is true or the counter is changed as a result of the instruction.In these cases, the execution takes two instruction cycles with the additional instruction cycle(s) executed as a NOP.

The double-word instructions execute in two instruction cycles. One instruction cycle consists of four oscillator periods. Thus, for an oscillator frequency of 4 MHz, the normal instruction execution time is 1 μs. If a conditional test is true or the program counter is changed as a result of an instruction, the instruction execution time is 2 μs. Two-word branch instructions (if true) would take 3 μs.

With 4 Mhz clock, the time required to execute one single cycle intsruction is given by (¼ MHz)* 4. Hence one single cycle instruction will take 1 μs to execute. An instruction cycle is made up of 4 clock cycles.

Program7.asm

list p = 18F452

#include < P18F452.INC >

Count1 equ 0x00

Count2 equ 0x01

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x00

MOVWF TRISD,0

Pos1 :

MOVLW 0x00

MOVWF PORTD,0

CALL Delay

CALL Delay

MOVLW 0xFF

MOVWF PORTD,0

CALL Delay

CALL Delay

goto Pos1

Delay :

MOVLW 0xFF ; Execution time = 1 μs

MOVWF Count1, 0 ; Execution time = 1 μs

Pos2 :

DECFSZ Count1,1,0 ; Execution time = 1 μs or 2 μs or 3 μs

goto Pos3 ; Execution time = 2 μs

RETURN ; Execution time = 2 μs

Pos3 :

MOVLW 0xFF ; Execution time = 1 μs

MOVWF Count2,0 ; Execution time = 1 μs

Pos4 :

DECFSZ Count2,1,0 ; Execution time = 1 μs or 2 μs or 3 μs

goto Pos4 ; Execution time = 2 μs

goto Pos2 ; Execution time = 2 μs

end

Here I am loading Count1 and Count2 with value 0xFF( In decimal 255 ). Please note that I have used assembler directive ‘equ’ earlier with MEM1 and MEM2. It represents a memory location in regitser file map or data memory. For making the delay, I am decreasing Count1 from 255 to 0 and withe each decrement in Count1, I will be decrementing Count2 from 255 to 0.

Now our task is to find the time required for executing the delay routines once. For that just all the instruction execution times. I have mentioned that in the comment section. Just add all those values like this..

1 μs + 1 μs + ( (1 μs + 2 μs ) * 255 + (1 μs + 1 μs) + (1 μs + 2 μs ) ) 255 + 2 μs
This gives delay of 196354 μs. i.e it will gives approximately delay of 0.196 seconds of 196 mS.

Please note that the delay routines in approximate only because i have calculated the instruction time for DECFSZ Count2,1,0 as 1 μs. When the conditional test becomes true the time taken by this instruction will be 2 μs. So some deviation will be in the delay routine execution time., but its only minimal. i.e 196 milli-seconds may actually be 200 or 202 milli-seconds.

So you have studied how to insert exact delays in your program. You dont have to do trial and error methold. Calculate the delay...

My next article will be on rotating the LEDs.... So dont change the hardware settings. In my next article I will show you how to rotate LEDs on PORTD.

If you have any suggestion regarding my articles, please feel free to contact me at my email address given in the blog..

No comments: