Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/08/00 13:23
Read: times


 
#2557 - RE: A question about using Inputs
You really should read the tutorial... it has valuable information.

But here is the quick answer.

INITIALIZE INPUT PINS
=====================
To read a pin as input, you must first assure that the pin was initialized by setting the same bit to a high or 1 state. Think of it as a pull-up and you'll remember this critical step.

The microprocessor helps you. When it first powers up, it automatically sets all inputs to high or 1 state. I recommend including a small peice of code in your initialization routine to set the high again as the microprocessor can run through the initialization code under conditions other than power-up.

A CONFUSING BUG SCENARIO
========================
Often, this causes software problems. It may be that ports properly initialized later get set to low or 0 state by anther error somewhere else in the program... maybe an accidental byte write to port of an mistaken boolean manipulation of the port. This can create a confusing situation wherein code that has been tested and working perfectly, suddenly appears to be all wrong. Remember, if any code sets the port bit used for input to low, it will no longer function as a proper input.

PORT BIT DIRECTS PROGRAM FLOW
=============================
There are several ways to read a port. If you want to jump if the bit is high or low use:
. jb p1.3,else ;skif port1 bit3 high
. jnb p2.4,go ;jif port2 bit3 low

(jif and skif are my terms for "jump if" and "skip if")

PORT BIT USED IN CALCULATION
============================
Often you need to read in the state of a bit and compute data based upon its state. For example, you may need to monitor its state looking for transitions. While the 8051 supports bit instructions operating with the carry flag, its common to use a full byte read to load the input data because the byte registers have more and quicker capabilities to manipulate data.
. mov a,p2 ;read in p2.3 and dontcares
. anl a,#00001000b ;strip out dontcares
. xrl a,r7 ;boolean compare to last bit
. mov r7,a ;update port bit memory
. jnz transition ;jif bit changed value
. ret ;else return
transtion:
. ;code reaches here on 0-1 or 1-0 transition as determined by the port polling.

MOVING A PORT BIT TO FLAG
=========================
Sometimes you need to move the state of the port bit into a flag. This may represent a snapshot look at the port and the flag is used by several subsequent routines.
bseg
level bit 0
cseg
. mov c,p3.4 ;load bit into carry
. mov level,c ;store snapshot into flag

THAT WILL GET YOU STARTED.
=========================
Look over the instructions above in the programmer's guide until you understand all their functions. If you are going to work with bits, you should also make sure you understand the boolean functions AND OR COMPLEMENT XOR.

Good Luck,
Jay C. Box

List of 3 messages in thread
TopicAuthorDate
A question about using Inputs            01/01/70 00:00      
RE: A question about using Inputs            01/01/70 00:00      
RE: A question about using Inputs            01/01/70 00:00      

Back to Subject List