|
Okay, the next thing to look at is our main routine.
We have a main routine with the heading begin just like in our other examples. You will find in the main routine that we don't do anything but call a couple of sub-routines and that's it! This can be very handy when programming because you can piece your program together in chunks that fit in their own relevent space.
The first sub-routine we call is check_left, here is the code:
check_left
btfsc PORTB, 7
return
btfss PORTA, 0
return
rlf PORTB, f
call delay
return
the first two lines in this sub-routine are there to prevent us moving the LED any further left IF the left most LED is turned ON. to do this, we use the btfsc instruction.
The btfsc instruction means 'have a look at whatever bit i say, within whatever register or port that i say. If the digit in there is a ZERO then skip the next line but if the digit in there is a ONE, then continue on with the very next line.'
So as you can see, this is quite an in-depth instruction. In our case, we want to check to see if the left most LED is turned on (PORTB pin 7) if it is turned on then it will do the very next line, which is a return instruction so we never get to move left again one space if this left most LED is on. BUT if the LED is not turned on (so PORTB pin 7 IS a ZERO) then we skip the next line (I.E. we skip the return instruction) and we go on with the next piece of code which is where we check if the button has been pressed.
Now this is where the btfss instruction comes into it. btfss means exactly the same thing as btfsc EXCEPT if the answer is ONE it will now skip the next line, but if the answer is ZERO it will go on with the very next line.
So, if we managed to get to this point (I.E. the left most led was not turned on) then we now check to see if the push-button has been pressed. If it has NOT been pressed, we will have a logic ZERO at PORTA pin, 0. This means that we will go on with the very next line - in this case the very next line is a return instruction, so we don't move the LED at all. BUT if we had pressed the button, this means that we would have a logic ONE at the input of PORTA pin, 0 and this means SKIP the next line and go on with the code from there.
Then all we do is a simple rlf instruction to move the data in PORTB left one space. We then call the delay to slow things down a bit - and finally, we return back to our main routine.
The next line in our main routine is to call the check_right routine. This is very VERY similar to the check_left routine. Let's have a look:
check_right
btfsc PORTB, 0
return
btfss PORTA, 1
return
rrf PORTB, f
call delay
return
You can see here that we are checking if the rightmost LED is turned on by checking PORTB pin, 0 (btfsc PORTB, 0) Remember, we do this to prevent us from moving the LED any further right when we are already at the extreme right. If if is not at the extreme right, it will allow us to continue on by skipping the return instruction.
Now we check to see if the button has been pressed. We are this time checking PORTA pin, 1 (remember PORTA pin, 0 was for the left button) if the button has NOT been pressed, we just return to our main program. But if it HAS been pressed - we shift the DATA in PORTB right one space, then call the delay to slow things down and finally, we return back to our main program. |
|