Convert the following pseudocode into PIC 18 assembly language code. NOTE: Part
ID: 1714963 • Letter: C
Question
Convert the following pseudocode into PIC 18 assembly language code. NOTE: Part of the pseudocode is already converted into PIC 18 assembly language code in Figure 2.2.
Figure 2.1. Pseudocode for Ping-Pong program
shift_left_label:
function_shift_left();
if(SW1 is pressed)
goto shift_right_label;
else
goto shift_left_label;
shift_right_label:
function_shift_right();
if(SW2 is pressed)
goto shift_left_label;
else
goto shift_right_label;
function_shift_left()
{
pattern=1;
for(i=0; i<8; i++)
{
patter=patter<<1;
function_delay();
}
}
function_shift_right()
{
pattern=0x80;
for(i=0; i<8; i++)
{
patter=patter>>1;
function_delay();
}
}
function_delay()
{
for(i=0; i<=255; i++)
for(j=0; j<=255; j++)
nop;
}
Figure 2.2. Assembly code for Ping-Pong program
list p=18F87J11
#include p18F87J11.inc
config XINST=OFF
pattern equ 0x25
counter equ 0x26
delay_count1equ 0x27
delay_count2equ 0x28
org 0x0
goto start
start:
;configuration of PORTD(LED) PORTB0(push button1), and PORTA5(push button2)
bsf WDTCON,ADSHR ;Shared SFR
setf ANCON0
bcf WDTCON,ADSHR
movlw 0x00
movwf TRISD ;LEDs are connected to PORTD
bsf TRISB, 0
bsf TRISA, 5
start_loop_left:
call shift_left
btfsc PORTB, 0 ;if SW1 is pressed, then RB0 is zero
bra start_loop_left
;sw1 is pressed, pattern should change
start_loop_right:
...
shift_left:
...
return
shift_right:
...
return
delay:
...
return
end
Explanation / Answer
#include p18F87J11.inc
config XINST=OFF
pattern equ 0x25
counter equ 0x26
delay_count1equ 0x27
delay_count2equ 0x28
org 0x0
goto start
start:
;configuration of PORTD(LED) PORTB0(push button1), and PORTA5(push button2)
bsf WDTCON,ADSHR ;Shared SFR
setf ANCON0
bcf WDTCON,ADSHR
movlw 0x00
movwf TRISD ;LEDs are connected to PORTD
bsf TRISB, 0
bsf TRISA, 5
start_loop_left:
call shift_left
btfsc PORTB, 0 ;if SW1 is pressed, then RB0 is zero
bra start_loop_left
;sw1 is pressed, pattern should change
start_loop_right:
...
shift_left:
...
return
shift_right:
...
return
delay:
...
return
end