Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

offset instructions

Status
Not open for further replies.

4electros

New Member
LIST p=16F84 ; PIC16F844 is the target processor

#include "P16F84.INC" ; Include header file

CBLOCK 0x10 ; Temporary storage
state
l1,l2
ENDC

org 0 ; Start up vector.
goto setports ; Go to start up code.

org 4 ; Interrupt vector.
halt goto halt ; Sit in endless loop and do nothing.

setports clrw ; Zero in to W.
movwf PORTA ; Ensure PORTA is zero before we enable it.
movwf PORTB ; Ensure PORTB is zero before we enable it.
bsf STATUS,RP0 ; Select Bank 1
clrw ; Mask for all bits as outputs.
movwf TRISB ; Set TRISB register.
bcf STATUS,RP0 ; Reselect Bank 0.

initialise clrw ; Initial state.
movwf state ; Set it.

loop call getmask ; Convert state to bitmask.
movwf PORTB ; Write it to port.
incf state,W ; Increment state in to W.
andlw 0x03 ; Wrap it around.
movwf state ; Put it back in to memory.
call wait ; Wait :)
goto loop ; And loop :)

; Function to return bitmask for output port for current state.
; The top nibble contains the bits for one set of lights and the
; lower nibble the bits for the other set. Bit 1 is red, 2 is amber
; and bit three is green. Bit four is not used.
getmask movf state,W ; Get state in to W.
addwf PCL,F ; Add offset in W to PCL to calc. goto.
retlw 0x41 ; state==0 is Green and Red.
retlw 0x23 ; state==1 is Amber and Red/Amber
retlw 0x14 ; state==3 is Red and Green
retlw 0x32 ; state==4 is Red/Amber and Amber.

; Function using two loops to achieve a delay.
wait movlw 5
movwf l1

w1 call wait2
decfsz l1
goto w1

return


wait2 clrf l2
w2 decfsz l2
goto w2
return
END
--------------------------------------------------------------------------

I wonder about the instruction ANDLw 0x03,why it's used like that in this program??
what is the mechanism of choosing the RETLW instruction (used three times sequentially)?


thanks in advance!
 
Last edited:
ANDLW 0x03 makes sure the W register can't ever exceed 0x03, as it's incremented just before that it means that the register will count, 0, 1, 2, 3, 0, 1, 2, 3, 0 ..... and so on.
 
4electros said:
I wonder about the instruction ANDLw 0x03,why it's used like that in this program??
what is the mechanism of choosing the RETLW instruction (used three times sequentially)?

As nigel said, just makes sure the counters upper limit is 3. The 'RETLW's which appear sequential, forms a crude (but effective) look up table. And it is 'return with literal, (k) in W'. Thats is, return from a subroutine (that was called) with a literal value, k, in W.

getmask
movf state,W ; Get state in to W.
addwf PCL,F ; Add offset in W to PCL to calc. goto.
retlw 0x41 ; state==0 is Green and Red.
retlw 0x23 ; state==1 is Amber and Red/Amber
retlw 0x14 ; state==3 is Red and Green
retlw 0x32 ; state==4 is Red/Amber and Amber.

The subroutine is 'getmask'. We move a number into W, that indicates the entry of the table we want back, and call the routine. the 'addwf PCL,F' adds this number to the program counter, effectively skipping a certain number of instructions, so we can go straight to the table entry we want. The 'retlw' instructions do not execute sequentially, because they are a 'return'. I'm not sure why they have the 'state' as 0,1,3,4....wheres 2? or am I missing something...

LUT's are damn handy, and are not just restricted to single instructions either..I have a few programs where the number is multiplied by 4 before being added to the PCL, meaning each 'entry' can execute 3 instructions before returning.

Hope thats what you were after anyway.

Blueteeth.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top