PIC16F268A Digital Output issue

Discussion in 'The Projects Forum' started by string_656, Jul 15, 2012.

  1. string_656

    Thread Starter New Member

    Jul 15, 2012
    2
    0
    Hello.

    I'm having issues with the digital Outputs. I’m using Hi Tech C with MPLAB and a PICKIT 3.

    I have successfully created a program, reading a Digital input to turn a light on.

    The issue is that when activate another digital output when there is already one on it turns it off...

    For example I have RA0 = 1, (Delay for a second)... then RA1 = 1, (delay for a second).. it turns the first one off when the second is activated and vice versa, I have looked in the header file and from what I understand the command is meant to read the current portA status and "or" them together so that only the target bit is affected.. For some reason it is reading any activated ports as off..


    Each LED works during operation but one after the other... i can rewrite the code and have both on, but re using the RA# for another output resets any that were already on.

    I am powering the target board from the PICKIT 3.
    here is my code....
    Code ( (Unknown Language)):
    1.  
    2. #define _LEGACY_HEADERS
    3. #include <HTC.H>
    4. #define _XTAL_FREQ 4000000
    5.  
    6.  
    7. __CONFIG(LVPDIS & BORDIS & INTCLK & WDTDIS & PWRTEN);
    8.  
    9. static void flashit()
    10. {
    11.         RA1 = 1;
    12.         __delay_ms(1000);
    13.         RA2 = 1;
    14.         __delay_ms(1000);
    15. }
    16.  
    17. void main(void)
    18. {
    19.     int latch1;
    20.     PORTA = 0;
    21.     TRISA = 0b11110000;
    22.     while(1)
    23.     {
    24.         if(RB0 == 1)
    25.         {
    26.             if(latch1 == 1)
    27.             {
    28.                 latch1 = 0;
    29.             }
    30.             else
    31.             {
    32.                 latch1 = 1;
    33.             }
    34.         while(RB0 = 1)
    35.             {
    36.             }
    37.         }
    38.    
    39.         if(latch1 == 1)
    40.         {
    41.             flashit();
    42.         }
    43.         else
    44.         {
    45.             PORTA = 0;
    46.         }    
    47.     }    
    48. }
    49.  
    I literally have searched everywhere and cannot find a solution...
     
    Last edited: Jul 15, 2012
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Your code will lock up when it gets to this line;
    while(RB0 = 1)
    as it always evaluates to TRUE.

    you should use
    while(RB0 == 1)

    if your goal is to test the value of pin RB0.
     
  3. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    As THE_RB wrote, this:
    Code ( (Unknown Language)):
    1.         while(RB0 = 1)
    2.             {
    3.             }
    will lock up, but this:
    Code ( (Unknown Language)):
    1.         while(RB0 == 1)
    2.             {
    3.             }
    will lock up if RB0 is equal to 1. Is either what you intended to do?
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    You'll have to write 07h to CMCON to make RA0-RA3 digital. At reset, the pins are analog.

    You can make the port an output with TRIS and write to it even when comparators are on but when the port is read back to modify a bit, all of the RA0-RA3 bits will read '0' regardless of what you wrote to them and be written back as '0' which accounts for what you are seeing.

    See 5.1 and 10.1 in the databook.
     
  5. string_656

    Thread Starter New Member

    Jul 15, 2012
    2
    0

    THANKS! .. that was the problem.. the RA# commands won't work unless i write 07h to CMCON. To the others, yes i intended that part of the code...

    Code ( (Unknown Language)):
    1.  
    2. while(RB0 = 1)
    3.             {
    4.             }
    5.  
    as it was a push button, this just halts the code untill it is released.

    Thanks.
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Well at the moment it halts the code forever, unless you change it to ==.
    ;)
     
Loading...