Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Dynamic port bit in PIC 16F877a using loop

Status
Not open for further replies.

varunme

Advanced Member level 3
Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,765
How can i access port bits using loop like the below ?

Code:
#define _TOTAL_LIGHTS 5

for(i=0; i<_TOTAL_LIGHTS; i++)
             { 
                     if (PORTD.[i] == 1)
                        {
                                 PORTC.[i] = 1;
                        }
                     else
                       {
                                 PORTC.[i] = 0;
                       }
            }
 

Re: Dynmaic port bit in PIC 16F877a using loop

bitwise and operation will be easier
 

Re: Dynmaic port bit in PIC 16F877a using loop

in mikroC , is there any function or something for that ?
 

Re: Dynmaic port bit in PIC 16F877a using loop

First store ....previous port values in characters and then newly assign the whole port to different character and then compare it with the previous one..... How ever you need to alter every time the previous character to new one too.....

Good Luck
 

Re: Dynmaic port bit in PIC 16F877a using loop

or you may use,:-

unsigned long testValue = 0xFF00FF00;


// Check if the fifth bit is equal to 1 (hint: it isn't!)
if (testValue & (1 << 4))
{
// Fifth bit is equal to 1
}
else
{
// Fifth bit is equal to 0
}

Using a for loop to supply bit nos.

for(i=0;i<=4; i++){ //for upto 5th bit


if(testValue&(1<< i )){

}
else
{
}
}
 
Last edited:

Re: Dynmaic port bit in PIC 16F877a using loop

Code:
unsigned char BitMask[]= {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

define _TOTAL_LIGHTS 5

for(i=0; i<_TOTAL_LIGHTS; i++)
             {
                     if ( (PORTD & BitMask[i]) >= 1)    // Check if bit i is set
                        {
                                 PORTC|= BitMask[i];    // Set bit i
                        }
                     else
                       {
                                 PORTC&= ~BitMask[i];   // Clear bit i
                       }
            }


}

this too not working
 

Re: Dynmaic port bit in PIC 16F877a using loop

It may be like this:-

1. change the if condition like this:- if(PORTD & (1<< i)) //i=0 to i=4 (one less than required bit no.

this change will give you desired result if you move 0s to PORTC before starting bitwise "or" operation--that means just before for loop.

Another way to do it.:-

It appears that all you want to do is to move the content of PORTD to PORTC.

Then,

unsigned char mybyte;
while(1){
mybyte = PORTD;
Delay(5); // check for actual delay
PORTC = mybyte;
}

If you want 5 bits, then use bitwise operator to place 0s in the upper bits(optional)
 
Last edited:

Re: Dynmaic port bit in PIC 16F877a using loop

How can i access port bits using loop like the below ?

Code:
#define _TOTAL_LIGHTS 5

for(i=0; i<_TOTAL_LIGHTS; i++)
             { 
                     if ((PORTD&(1<<i) )!= 0)
                        {
                                 PORTC| = (1<<i);
                        }
                     else
                       {
                                 PORTC&(~(1<<i));
                       }
            }
Try this.
Should work.
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Re: Dynmaic port bit in PIC 16F877a using loop

Try this.
Should work.
But in mikroC , its not working

---------- Post added at 09:28 ---------- Previous post was at 09:19 ----------

says

PORTC| = (1<<i);

invalid expression
 

Re: Dynmaic port bit in PIC 16F877a using loop

Attach your project, please. I'm using the same compiler.
Try PORTC=PORTC|(1<<i);
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Re: Dynmaic port bit in PIC 16F877a using loop

Just to check... did you set proper TRISC and TRISD values, and turn off Parallel port on PORTD (because it is done in TRISE)?
 

Re: Dynmaic port bit in PIC 16F877a using loop

PORTC| = (1<<i);

invalid expression

The space between the | and = is most likely the issue.

Try

Code:
PORTC |= (1<<i);

BigDog

---------- Post added at 20:21 ---------- Previous post was at 19:57 ----------

The following compiles without error, the space between | and = was an issue. There was also no actual assignment taking place in the "else" block of the "if" statement.

Code:
void main() {

#define _TOTAL_LIGHTS 5

int i;

for(i=0; i<_TOTAL_LIGHTS; i++)
{
    if (PORTD&(1<<i))
    {
       PORTC |= (1<<i);
    }
    else
    {
       PORTC &= (~(1<<i));
    }
}

}

BigDog
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Re: Dynmaic port bit in PIC 16F877a using loop

yes , Turned off PSP too , TRISE=0;
TRISC=0;
TRISD=0;


in proteus its not working

---------- Post added at 21:22 ---------- Previous post was at 21:10 ----------

My whole code is

Code:
void main() {


TRISE = 0;
TRISD=0;
TRISC=0;
PORTD=1;
PORTC=0;


for(i=0; i<_TOTAL_LIGHTS; i++)
{
    if (PORTD&(1<<i))
    {
       PORTC |= (1<<i);
    }
    else
    {
       PORTC &= (~(1<<i));
    }
}

}
 
Last edited:

Re: Dynmaic port bit in PIC 16F877a using loop

Attach your project, please. I'm using the same compiler.
Try PORTC=PORTC|(1<<i);
Here is my project
 

Attachments

  • MyProject.zip
    16.9 KB · Views: 73

Re: Dynmaic port bit in PIC 16F877a using loop

But it is compiling well. Whats the problem?
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Re: Dynmaic port bit in PIC 16F877a using loop

TRISD=0;
TRISC=0;
In the attached code, both ports are initialised with 0!

Write TRISC=0xFF; to make PORTC input (or the port you want to configure as input,connected to switches)
 
Last edited:

Re: Dynmaic port bit in PIC 16F877a using loop

But it is compiling well. Whats the problem?
after BIGDOG pointed out the mistake in code , i corrected it , now its compiling but its not working in proteus

TRISD=0;
TRISC=0;
In the attached code, both ports are initialised with 0!

Write TRISC=1 to make PORTC input

yes , now i made that correction , but too not working (PORTC is output)
 
Last edited:

Another thing to note : - PORTD is not present on 28pin version of 16F876A. It is present on 40 pin version
 

is the logic correct ?,
when the port is made for input, it is high
and we are writing code for

if(PORTD.F1==1)
{
}

---------- Post added at 02:01 ---------- Previous post was at 01:56 ----------

so , the switch should be connected to +5V ?

---------- Post added at 02:01 ---------- Previous post was at 02:01 ----------

Another thing to note : - PORTD is not present on 28pin version of 16F876A. It is present on 40 pin version
using 40pin DIP
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top