Richard Seriani, Sr. wrote: > ----- Original Message ----- > From: "Moses McKnight" > To: "Microcontroller discussion list - Public." > Sent: Thursday, August 30, 2007 10:15 PM > Subject: [PIC] - I/O question on PIC16F818 > > >> Howdy, >> >> I've got a question about the operation of an I/O pin that seems a >> little funny to me. For background, I'm using BoostC >> (www.sourceboost.com), and a PIC16F818 running at 8Mhz on the internal >> oscillator. >> First I'm using pin B5 as an output, pulling it low to turn on a >> segment in a 7-segment display. Then I change the pin to an output and >> look for a high which would indicate that a switch connected to the same >> line has been pressed. Here's the code: >> >> //portb = 0b10011110; >> trisb = 0b01100001; >> >> nop(); >> nop(); >> if (1 == portb.5) // SW1 - button 1 >> { >> button_pressed = 1; >> } >> >> I removed the debounce code for debugging. What this does is read a >> high every time unless I add/uncomment the first line pulling the pin >> low first. Why is this? There are no caps on the line coming in to the >> pin to store a charge - is there something else I'm missing on how the >> PIC works? >> >> Thanks, >> Moses >> -- > > Moses, > > Something my professor tried to get through my old, thick skull: You WRITE > to the LATCH and READ from the PIN. > > You wrote a high to the LATCH on RB5, which was then latched to the RB5 pin. > You next read the PIN, which is still latched high. > > It seems as though you are correct that you have to set that pin low (clear > it) before making it a switch input if you expect it to be low when you read > it (switch not pressed). > > Please see section 9, specifically section 9.10.2, Successive Operations on > an I/O Port, in the PICMicro Mid-Range MCU Family Reference Manual for more > information. Thanks for the information! It's making more sense to me now. I guess I expected the charge on the pin/line to magically go away when I changed the pin to high impedance. Duh! I'll read that section of the manual again. Thanks, Moses -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist