Question: write a ping-pong program. The program receives two inputs through SW1 and SW2 in PIC18 explorer board. SW1 is connected to RB0 and SW2 is

write a ping-pong program. The program receives two inputs through SW1 and SW2 in PIC18 explorer board. SW1 is connected to RB0 and SW2 is connected to RA5. The program turns on/off eight LEDs that are connected to RD0~RD7. Initially, the LED in the far right is on and it moves to the left. When it reaches to the far left, if SW1 is pressed, then the direction changes and the on LED moves towards the right. Similarly, when the on LED reaches to the far right, if SW2 is pressed, then the direction changes. If none of the switches is pressed, then the direction of the on LED does not change. The pseudo code of the ping-pong program is in Figure 2.1. Figure 2.2 shows an assembly code for the ping-pong program. You need to complete the assembly code based on the pseudo code.

PSEUDOCODE:

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++)

{

Switches

3

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;

}

ASSEMBLY CODE FOR PING PONG:

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

4

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!