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.

How to store 5bits in a port to a variable ?

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
How to store 5bits in a port to a variable
 

can u show one c code example ?
 

How about a variable assignment
variable = port_input_register
or
variable = port_input_register & BITMASK
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
#define PORT3 P3

main()
{
unsigned char variable;
variable = PORT3;
}

The above code just reads from the port register and copies to the variable. If you want that particular variable should hold only 5 bits then use bit field option

unsigned char variable : 5;
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
#define PORT3 P3

main()
{
unsigned char variable;
variable = PORT3;
}

The above code just reads from the port register and copies to the variable. If you want that particular variable should hold only 5 bits then use bit field option

unsigned char variable : 5;

Any other option ?, the method u said is a nice trick
 

In my previous post i have missed to configure to input port. by the way in order to read the port values, you have to use only port register. I don't know if there is some other option to read from port.

May be i can help further if i know the purpose.

Thanks
 

I'm not aware of bit fields supported outside of structures. Masking the bits of interest is the usual way, if you intend to suppress unwanted bits.
 

Thanks FvM... other than structures, bit field is not applicable.. i was just in the intend to convey him there is option of bit field... I forgot to mention that point thanks
 

Is there an easy way to store nibbles to a variable?.
like lower nibble to one variable and higher nibble to another ?
 

Is there an easy way to store nibbles to a variable?
The C language has plenty of ways to do it, I think. I use to write code according to a problem. But the problem isn't clear in the present case.
 

I want to store the state of an array of sensors to one variable (in one direction) and to another nibble , an array of sensors in another direction.
 

Everything can be done with a combination of bit masking and shift operations, e.g.

Code:
variable1 = port_input_register & 0xf;
variable2 = port_input_register >>4;
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
so if have to save the upper nibble of portd to variable1, then how can i attain that ?
 

I don't understand the question. In my example, the upper nibble is written to variable2. The data is shifted to the lower nibble of the variable, which implicitely masks the bits.

I suggest to visualize the operation by pencil and paper.
 

I don't understand the question. In my example, the upper nibble is written to variable2. The data is shifted to the lower nibble of the variable, which implicitely masks the bits.I suggest to visualize the operation by pencil and paper.
sorry,
I didnt noticed your variable naming,
I really meant, how to do it into variable2
 

Alternatively, the upper nibble can be simply masked, which involves less machine instructions for most micros. The shift operation can be short cut by a SWAP instruction, but it's not necessarily utilized by the compilers.

In most applications, that don't need to squeze processor speed up to the very limits, you don't care for these details.

Code:
variable2 = port_input_register & 0xf0;
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top