PIC16F268A Digital Output issue

Thread Starter

string_656

Joined Jul 15, 2012
2
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....
Rich (BB code):
#define _LEGACY_HEADERS
#include <HTC.H>
#define _XTAL_FREQ 4000000


__CONFIG(LVPDIS & BORDIS & INTCLK & WDTDIS & PWRTEN);

static void flashit()
{
        RA1 = 1;
        __delay_ms(1000);
        RA2 = 1;
        __delay_ms(1000);
}

void main(void)
{
    int latch1;
    PORTA = 0;
    TRISA = 0b11110000;
    while(1)
    {
        if(RB0 == 1)
        {
            if(latch1 == 1)
            {
                latch1 = 0;
            }
            else
            {
                latch1 = 1;
            }
        while(RB0 = 1)
            {
            }
        }
    
        if(latch1 == 1)
        {
            flashit();
        }
        else
        {
            PORTA = 0; 
        }    
    }    
}
I literally have searched everywhere and cannot find a solution...
 
Last edited:

THE_RB

Joined Feb 11, 2008
5,438
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.
 

SgtWookie

Joined Jul 17, 2007
22,230
As THE_RB wrote, this:
Rich (BB code):
        while(RB0 = 1)
            {
            }
will lock up, but this:
Rich (BB code):
        while(RB0 == 1)
            {
            }
will lock up if RB0 is equal to 1. Is either what you intended to do?
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
 

Thread Starter

string_656

Joined Jul 15, 2012
2
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.

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...

Rich (BB code):
while(RB0 = 1)
            {
            }
as it was a push button, this just halts the code untill it is released.

Thanks.
 
Top