James Nick Sears wrote: > (1) > Set PORT register before TRIS register: > Set PORT register: > TRISD = xxxxxxx1 > PORTD = xxxxxxx1 > > Right here is where my question arises. Since there is a low > voltage connected to the pin which is configured as an input, > then won't the PIC just sample the pin low and overwrite the > change I just made? No, unless you do a read then write out that value back to the port register. > It seems that case 1 is what most people are recommending but what > keeps the pin from getting sampled low and overwriting the change to > the PORT register while the pin is still an input? Port latches don't just change magically for no reason (unless perhaps you wave a dead fish over them during a full moon). They only change when they are written to. The problem comes from instructions like BCF which reads the entire register, changes the specified bit, then writes the entire register back out again. When this operation is performed on a port register, it effectively writes the actual pin state to the output latch for all bits except the one being explicitly modified. The easy way to get around this during initialization is to write the whole port register to its initial state with one write. That will set the port latches regardless of the pin voltages. Then write the TRIS register as desired. At the risk of sounding like a broken record, this is yet another totally avoidable issues in my PIC development environment. The /INBIT and /OUTBIT preprocessor directives allow you to specify the initial state for all pins. These directives leave a bunch of assembler state lying around about how you want the ports to wake up. The PORT_INIT routine in the standard PORT module then uses this state to efficiently set up all the port, TRIS, and LAT registers. You can also specify the optional pullups available on some input pins. The PORT code sets up that state too, and gives you an assembly error if you specified a state that can't be realized by that particular PIC. This may sound a bit complicated, but all you have to do is write /INBIT and /OUTBIT directives in the project include file. The PORT module does have some complexity, but it's all canned. You shouldn't need to mess with it at all. Another advantage is that you get a level of abstraction for each I/O pin. Various symbols and string substitution macros are automatically created so that you can write code like dbankif led_reg ;set banks for access to port with LED pin bcf led_pin ;turn on the LED without coding the exact port and pin for the LED into the body of the source code. ***************************************************************** Embed Inc, embedded system specialists in Littleton Massachusetts (978) 742-9014, http://www.embedinc.com -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu