Sie sind auf Seite 1von 6

ESE2202 Introduction to Microcontrollers

Lab1
1. Fill Bank1 RAM with value 0x55.
To fill Bank 1 with 0x55, one has to first put 0x55 in the writing register. The memory
address to be addressed first is set to the first memory space of bank 1. POSTINC0 is a
register which uses the contents of FSR0 to address data memory. If the program has not
yet reached the next bank ( bank 2) which is checked by the condition btfss
FSR0H,1; ,
the value in the writing register will be moved to the memory space which POSTINC0 is
pointing to.
When bank 1 is full and bank 2 is reached, the condition btfss
FSR0H,1; becomes true,
therefore it is skipped and the program enters an infinite loop goto $
LIST p=18f45k50
include <p18f45k50.inc>

LOOP:

org 0

;set code entry point to 0

clrf
movlw
lfsr

WREG
0x55
0,0x100 ;move literal to fsr,fsr is a special register used
for memory addressing, 100 is starting address
POSTINC0
FSR0H,1 ; checking bank memory number
LOOP

movwf
btfss
goto

goto $
;endless loop, dollar sign means the current address, so
continuously going to current address
END

2. Add two 16-bit binary values (e.g. R1:0 + R3:2 = R5:4).


To add two 16 bit numbers, each number must first be placed in the writing register, and
then placed in its respective variable. The command add is used to perform the addition.
add can only take one argument from the writing register and the other from a register in
memory, so one of the values was to be placed again in the writing register. Since when
adding two numbers, the result might not fit in one single register due to overflow, the carry
bit is added into another register. The test shown is the addition of 0xFFFF with 0x1, giving
the answer of 0x10000.
LIST p=18f45k50
include <p18f45k50.inc>
org 0
val1a
val1b
val2a
val2b
ansa
ansb
ansc

;set code entry point to 0


equ 0x01
equ 0x02
equ 0x11
equ 0x12
equ 0x21
equ 0x22
equ 0x20

clrf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
movf
addwf
movwf
movf
addwfc
movwf
clrf
addwfc

WREG
b'11111111'
val1a
b'11111111'
val1b
b'00000000'
val2a
b'00000001'
val2b
val2b, W
val1b, W
ansb
val2a, W
val1a, W
ansa
WREG
ansc, f

goto $
END

3. Convert a 12-bit unsigned binary value into 4 BCD digits.


To convert the 12-bit unsigned binary value into 4 BCD digits, the shift add 5 algorithm was
used. Each time the number is shifted, the value in the register is checked whether it is
greater of equal to 3, if this condition is true, the program would skip to another loop in
which 5 is added to that number.
One of the values which the program was tested with is shown below, oxFFF equivalent to
BCD 4095
LIST p=18f45k50
#include <p18f45k50.inc>
org 0
valueone
equ 0x22
valuetwo
equ 0x23
answerone
equ 0x20
answertwo
equ 0x21
addthreelow equ 0x28
addthreehigh
equ 0x29
chkfivehigh equ 0x2A
chkfivelow equ 0x2B
andreg equ 0x2C
counter equ 0x2D
checker equ 0x2E
clrf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
movlw
movwf
LOOP:

movlw
mulwf
the shifted bit
movf
movwf
movlw
mulwf
movf
movwf
movf
addwf
movlw
shifted bit

WREG
b'00000011'
addthreelow
b'00110000'
addthreehigh
b'01010000'
chkfivehigh
b'00000101'
chkfivelow
b'00001111'
andreg
b'00000100'
counter
b'11111111'
valueone
b'11110000'
valuetwo
b'00000010'
answerone
PRODL, W
answerone
b'00000010'
answertwo
PRODL, W
answertwo
PRODH, W
answerone, F
b'00000010'

;shifting hundreds and thousands and adding

;shifting inputted number(s) and adding the

mulwf
movf
movwf
movf
addwf
movlw
mulwf
movf
movwf
movf
addwf

valueone
PRODL, W
valueone
PRODH, W
answertwo, F
b'00000010'
valuetwo
PRODL, W
valuetwo
PRODH, W
valueone, f

incf
btfsc
goto

counter
counter, 4
CONT

clrf
addwf
cpfslt
goto

WREG
chkfivehigh, W
answerone
ADDINGHIGHONE

cpfslt
goto

answertwo
ADDINGHIGHTWO

movff
addwf
andwf
cpfsgt
goto

answerone, checker
answerone, W
andreg, W
chkfivelow
ADDINGLOWONE

clrf
movff
addwf
andwf
cpfsgt
goto
goto

WREG
answertwo, checker
answertwo, W
andreg, W
chkfivelow
ADDINGLOWTWO
LOOP

ADDINGHIGHONE:

clrf
addwf
addwf
clrf
addwf
cpfslt
goto
goto

WREG
addthreehigh, W
answerone, F
WREG
chkfivehigh, W
answertwo
ADDINGHIGHTWO
ADDINGLOWONE

ADDINGHIGHTWO:

clrf
addwf
addwf
clrf
addwf
andwf
cpfsgt
goto
clrf
addwf
andwf
cpfsgt
goto
goto

WREG
addthreehigh, W
answertwo, F
WREG
answerone, W
andreg, W
chkfivelow
ADDINGLOWONE
WREG
answertwo, W
andreg, W
chkfivelow
ADDINGLOWTWO
LOOP

ADDINGLOWONE:

clrf
addwf
addwf
clrf
addwf
andwf
cpfsgt
goto
goto

WREG
addthreelow, W
answerone, F
WREG
answertwo, W
andreg, W
chkfivelow
ADDINGLOWTWO
LOOP

ADDINGLOWTWO:

clrf
addwf
addwf
goto

WREG
addthreelow, W
answertwo, F
LOOP

CONT:

goto $

END

4. Convert a BCD digit into a decoded 7-segment pattern using table lookup instructions.
To convert a BCD digiti into a decoded 7-segment pattern, a look up table was used. The
look up table was saved starting from memory address 0x7000. Initially the table pointer
was set to point to 0x7000 memory address. The number which the user want to convert to
a 7- segment pattern is multiplied by 2 (hence rotated to the left by one place) since
memory is organized in two byte words. The resultant rotated number is moved to the low
table pointer and this value then returns the pattern from the look up table which is then
saved in variable test 1. The pattern assumed is for a common anode display displaying 4.
LIST p=18f45k50
include <p18f45k50.inc>
bcd1
test1

equ 0x01
equ 0x05

org 0x0
movlw
0x04
movwf
bcd1
movlw
movwf
movlw
movwf
movlw
movwf

LOW _TABLE
TBLPTRL
HIGH _TABLE
TBLPTRH
UPPER _TABLE
TBLPTRU

rlncf
movff

bcd1, f
bcd1, TBLPTRL

tblrd*
movf
movwf
goto

TABLAT, W
test1
$

org 0x7000
_TABLE

DB
DB
DB
DB
DB
DB
DB
DB
DB
DB

b'11000000'
b'11111001'
b'10100100'
b'10110000'
b'10011001'
b'10010010'
b'10100000'
b'11111000'
b'00000000'
b'10011000'

;0
;1
;2
;3
;4
;5
;6
;7
;8
;9

END

The value which this program is testing for is 0x04, the number 0x08 is the rotated value of
0x04 ( hence multiplied by 2) to get the corresponding pattern in the table. Memory
addresses always end in an even value since the memory is arranged as a set of words each
containing two bytes of data, hence the value had to be multiplied by two since the next
value was found in the next memory space located one word from the current one.

Das könnte Ihnen auch gefallen