1. Write a program that add 0x13 for 100 times by itself and store it in 0x30 an
ID: 3589631 • Letter: 1
Question
1. Write a program that add 0x13 for 100 times by itself and store it in 0x30 and 0x31 of the file register:
2. Write a program that search through the following 20 hex numbers and store the smallest number at address 0x30 and largest number at address 0x31 and compute the Range (max – min) and store the result in the address 0x40.
0x20,0x34,0xa2,0x4b,0xc2
0x12,0xc2,0xb2,0x9b,0xe2
0x29,0x90,0x02,0x8b,0xca
0xF2,0xb4,0x2c,0x9a,0x82
How many times the following loop will be executed calculate the delay for a 4 MHZ oscillator.
myreg equ 0x88
delay movlw 0x00 ; 1 instr cycle
movwf myreg ; 1 instr cycle
again nop ; 1 instr cycle
nop ; 1 instr cycle
nop ; 1 instr cycle
nop ; 1 instr cycle
decf myreg, f ; 1 instr cycle
bnz again ; 2 instr cycle
return ; 1 instr cycle
3. Write a code that makes the PORTB as output and PORTA as input. Check for the status of S2 (bit 4 on PORTA) if the switch is pressed turn on all of the LEDs attached to PORTB’s with enough delay.
4. (Extra Credit) Write a piece of code that counts from zer0 to 99 using four digit 7 segments.
Explanation / Answer
4)
btfss SEG_PORT, 7 ;check which LED was last
goto Do_tens
movfw ones
andlw 0x0F ;make sure in range of table
call LED_Table
andlw 0x7F ;set to correct LED
movwf SEG_PORT
goto INTX
Do_tens movfw tens
andlw 0x0F ;make sure in range of table
call LED_Table
iorlw 0x80 ;set to correct LED
movwf SEG_PORT
INTX
movfw p_temp
movwf PCLATH ; Restore PCLATH
swapf s_temp,W
movwf STATUS ; Restore STATUS register - restores bank
swapf w_temp,F
swapf w_temp,W ; Restore W register
retfie
LED_Table ADDWF PCL , f
RETLW b'10001000' ;0
RETLW b'10111011' ;1
RETLW b'11000001' ;2
RETLW b'10010001' ;3
RETLW b'10110010' ;4
RETLW b'10010100' ;5
RETLW b'10000100' ;6
RETLW b'10111001' ;7
RETLW b'10000000' ;8
RETLW b'10110000' ;9
RETLW b'11111111' ;blank
RETLW b'11111111' ;blank
RETLW b'11111111' ;blank
RETLW b'11111111' ;blank
RETLW b'11111111' ;blank
RETLW b'11111111' ;blank
Initialise movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS, RP0 ;select bank 1
movlw b'00000000' ;Set port data directions, data output
movwf SEG_TRIS
bcf STATUS, RP0 ;select bank 0
; Set up Timer 2.
;movlw b'01111110' ; Post scale /16, pre scale /16, TMR2 ON
;uncomment previous line, and comment next line, to slow multiplexing speed
;so you can see the multiplexing happening
movlw b'00010110' ; Post scale /4, pre scale /16, TMR2 ON
movwf T2CON
bsf STATUS, RP0 ;select bank 1
movlw .249 ; Set up comparator
movwf PR2
bsf PIE1,TMR2IE ; Enable TMR2 interrupt
bcf STATUS, RP0 ;select bank 0
; Global interrupt enable
bsf INTCON,PEIE ; Enable all peripheral interrupts
bsf INTCON,GIE ; Global interrupt enable
bcf STATUS, RP0 ;select bank 0
clrf tens
clrf ones
Main
call Delay255
call Delay255
call Delay255
call Delay255
incf ones, f
movf ones, w ;test first digit
sublw 0x0A
btfss STATUS, Z
goto Main
clrf ones
incf tens, f
movf tens, w ;test second digit
sublw 0x0A
btfss STATUS, Z
goto Main
clrf tens
goto Main ;loop for ever
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xC7 ;delay 1mS
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
END
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.