Simple source code problem for PIC16F628A

Discussion in 'Embedded Systems and Microcontrollers' started by erickoh1985, Feb 28, 2008.

  1. erickoh1985

    Thread Starter New Member

    Feb 28, 2008
    7
    0
    i have a problem in program PIC16F628A. The output result getting a strange result.
    Below is my source code:

    #include <16F628A.h>
    #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
    #use delay(clock=4000000)

    void main()
    {
    set_tris_b(0x0F); //RB7-4 is output, RB3-0 is input port
    while (true)
    {
    if( input(PIN_b0) ) //if input pin RB0 is high
    output_high (pin_b6); //Only output pin RB6 is high
    else
    output_high (pin_b7); //output pin RB6 is low
    }
    }

    From what i understanding,
    when RB0 is low, RB7 is high and if RB0 is high, RB6 is high also. Which mean if RB0 is shifting from low to high, both RB6 and RB7 should be high right?

    but the actual result is, if i shift RB0 to low, RB7 high and RB6 low. if i shift RB0 to high, RB6 high and RB7 low. Since i didn't set RB6 and RB7 to low, how come RB6 and RB7 wil goes low??

    Please give any suggestion. Thanks for your help
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Did you do the test with pin RB6 and RB7 floating, and not connected to anything? If not, then isolate RB6 and RB7 and do the test again.

    What compiler do you use? And if possible could you create an assembly listing and post it here?
     
  3. erickoh1985

    Thread Starter New Member

    Feb 28, 2008
    7
    0
  4. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Floating means the pins are not connected to anything. If you've tried this, you would see that your code worked as it should be.

    The problems are the LEDs do not have current limiting resistors in series and coupled with writing to a PIC port is internally a read-modify-write operation. Read-modify-write means that when changing the value of an output pin, the microcontroller actually reads the actual values (not the PORTx register) of all pins in that port first to an internal register, changes the value of the requested pin in the internal register, and then write all the pins values in the internal register back to PORTx.

    Because the pins that are connected to the LEDs in your circuit do not have current limiting resistors, the voltages would be dictated by the Vf of the LEDs, which in this case lower than the logic hi threshold of the PIC.

    So, when RB0 is high and the code tries to set pin RB6 high, the microcontroller actually reads the values of all pins first. It will reads both pins RB6 and RB7 as low, eventhough one or both of them might be set to high. Then the microcontroller changes the value of RB6 in the internal register to high, leaving the value of pin RB7 low, and then writes these new values in the internal register back to the port register, thus setting RB6 high and RB7 low.

    The same thing happens when RB0 is low, except now RB6 is low and RB7 is high.

    What you need to do is to put resistors in series with the LEDs. You should've put them in the first place anyway, LED current needs to be limited by series resistor.
     
  5. erickoh1985

    Thread Starter New Member

    Feb 28, 2008
    7
    0
    i face another problems again.

    my picture link is: http://www.imagehosting.com/show.php/1605516_16f628.JPG.html

    this is my code:
    #include <16F628A.h>
    #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
    #use delay(clock=4000000)

    Void Main()
    {
    Set_Tris_B(0x0F);
    While(1)
    {
    if (Input(PIN_B0))
    Output_Bit(PIN_B6,1);

    else
    Output_Bit(PIN_B5,1);
    }
    }

    At default, before press RB0's switch, oni RB5's LED will "High" right?!
    But why RB5 and RB7 will high oso??
    Since i didn't declare RB7 to "High", why RB7 will "High"?
     
  6. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Default values.
     
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    As n9352527 says "Defaults"

    If you don't set your port default conditions the lines can come up in any state.
    Also your program sets b7 to b4 as outputs and b3 to b0 as inputs whereas your circuit shows that b7 to b2 are outputs.
     
Loading...