PIC Inputs On & Off

Discussion in 'Embedded Systems and Microcontrollers' started by solpic, Aug 3, 2009.

  1. solpic

    Thread Starter Member

    Jul 23, 2009
    Hello everyone,

    I have another strange experience with PIC16Fxx microcontrollers. The chip was supposed to send a 1 to RA1, RA2, and a 0 to RA3 when RA0 was 1 and a 0 to RA1, RA2, and a 1 to RA3 when RA0 was 0. So when I connected RA0 to power RA1 and RA2 (with their LEDs) lit up, and when I connected RA0 to ground, RA3 lit up. But then when I attached RA0 to nothing, just in the air, all three LEDs lit up. I don't have the exact code but it was something like this.

    (This was a PIC16F84A 20I/P chip)

    Code ( (Unknown Language)):
    1. PORTA   equ 0x05
    2. TRISA       equ 0x85
    3. STATUS  equ 0x03
    5. main:   bsf STATUS, 5
    6.     movlw   0x01
    7.     movwf   TRISA
    8.     bcf STATUS, 5
    10. loop:   btfss   PORTA, 0
    11.     goto    is_off
    12.     goto    is_on
    14. is_off: bcf PORTA, 1
    15.     bcf PORTA, 2
    16.     bsf PORTA, 3
    17.     goto    loop
    19. is_on:  bsf PORTA, 1
    20.     bsf PORTA, 2
    21.     bcf PORTA, 3
    22.     goto    loop
    24.     end
    Also, what are the designations for on and off for the inputs on the PIC?

  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    For started use an include file. No reason to define manually anymore.
    Also run it through MPLAB's Simulator and see if PORTA works as you expect it to.
    Plus the 16F84 is pretty ancient, the 16F628A is cheaper and better.

    Note: your problem might be what is known as the "Read / Modify / Write" bug. Google it.
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    What you are seeing is more or less what you would expect.
    An input pin must be either pulled high (usually a 10K resistor from pin to Vdd) or low (10K to Vss), it must never be left floating.
    If you float an input pin it will be read as either a high, a low, or as probably as in your case, a rapid oscillation between a high and a low. And no, this effect will not show up in a simulation!

    As for the PIC16F84, yes its an ancient chip and you would be foolish to use it in any new project but it's a very good chip for learning the basics of PIC programming because you don't have the added complications of analog ports and advanced peripherals.
  4. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    Not sure if this answers your question, but you read the inputs using btfsc and btfss (bit test f, skip if clear; bit test f, skip if set). The set and clear mean high and low, respectively, just as when you write to a port.

  5. solpic

    Thread Starter Member

    Jul 23, 2009
    I mean what does the PIC read as on and off for a pin. Like is it when the pin is connected to power or ground, or is 5v and 0v.
  6. solpic

    Thread Starter Member

    Jul 23, 2009
    Thanks, this is what I was looking for! So this means if for instance I wanted to detect whether a SPST switch was on or off I couldn't just check if the pin was on or off, I would have to use some sort analog measurement?
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    The usual way I would connect a switch to an input pin is with a 10K resistor from Vdd to the pin and then the switch going from the pin to Vss(earth). When the switch is open the pin sits at 5 volts and when the switch is closed the pin is pulled down to 0 volts.

    Your second question about the what voltage levels the PIC sees as high and low, this will vary with supply voltage and the chip type so its best to look it up in the data sheet under "Electrical Specification / DC Characteristics" but as a general rule 0.8 volt and below is taken to be a low and 2 volt and above is taken as a high.
  8. fanie

    Active Member

    Jan 20, 2007
  9. solpic

    Thread Starter Member

    Jul 23, 2009
    Sorry, but I don't want to pay just yet for programming. I'm in love with freeware.