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.

[SOLVED] How to access a specific bit in a register?

Status
Not open for further replies.

metalmisers

Junior Member level 2
Joined
Mar 4, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,457
Hi guys.

I am using a TI Tiva arm micro-controller. I want to access a specific bit of a 8-bit register (GPIO_PORTF_DATA_REGISTER) and put its value in an other register. I found examples where people did like

GPIO_PORTF_DATA_REGISTER &= 0x01;

to access the first bit, but doesnt this make the 0th bit of the register 1? i just want to put the 0th bit in say In.

unsigned char In;
In = GPIO_PORTF_DATA_REGISTER|0x01; ( Is this correct?)
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
49,158
Helped
14,391
Reputation
29,046
Reaction score
13,143
Trophy points
1,393
Location
Bochum, Germany
Activity points
283,313
Apparently you are confusing AND and OR operators. Consult a C text book.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Which Compiler are you using? If mikroC PRO ARM then something like GPIO_PORTF_DATA_REGISTER.F0, GPIO_PORTF_DATA_REGISTER.F1...
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Try these.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x01;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x02;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x04;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x05;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x10;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x20;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x40;
 
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x80;

 

Allen6502

Advanced Member level 4
Joined
Nov 22, 2008
Messages
119
Helped
33
Reputation
66
Reaction score
31
Trophy points
1,308
Location
Borneo Island
Activity points
2,047
Code:
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x05

Line 7 should be "char bitVal = GPIO_PORTF_DATA_REGISTER & 0x08;"
 

metalmisers

Junior Member level 2
Joined
Mar 4, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,457
Ok now I get it if bit is 1 puts 1 in bitVal otherwise puts 0. Thanks a lot!
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
bit Value doesn't contain 1 or 0 all the time.

It can contain 9 values. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00.
 

metalmisers

Junior Member level 2
Joined
Mar 4, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,457
bit Value doesn't contain 1 or 0 all the time.

It can contain 9 values. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00.

You mean it can contain the value of the registers 0x01 bit or 0x01 itself? That is what i am trying to understand from the beginning.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
If GPIO_PORTF_DATA_REGISTER is 0xFF then


Code C - [expand]
1
GPIO_PORTF_DATA_REGISTER & 0x80 = 0x80

 

metalmisers

Junior Member level 2
Joined
Mar 4, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,457
Ok GPIO_PORTF_DATA_REGISTER & 0x80= 0x80; sets the 7th bit.
Now have an integer say bitval, and I say to my compiler,
In = GPIO_PORTF_DATA_REGISTER; and when I use the In integer somewhere else, i can't see the current value of the 7th bit of GPIO_PORTF_DATA_REGISTER register when i use In. Alternatively i define the In variable at the 7th bit address and that works.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
No, It doesn't set the bit. If that GPIO was already set then you get that value else you get 0. See for example I took GPIO value as 0xFF. So all bits were set. Anding 7th bit resulted in true and the value after anding is 0x80 for 7th bit.
 

metalmisers

Junior Member level 2
Joined
Mar 4, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,457
After much thought I still cant seem to run the program correctly. I have a problem with learning ucontrollers, The think i want to do is to call a function if both switches(SW1 and SW2) are pressed simultaneously. Here is my main...

SW1 = GPIO_PORTF_DATA_R & 0x10;
SW2 = GPIO_PORTF_DATA_R & 0x01;


while((SW1 == 0x10) && (SW2 == 0x01))
{
Flash_SOS();
}

SW1 is at the 4th bit of the register.
SW2 is at the 0th bit of the register.
Both are negative logic(connected to pull-up resistors).
Flash_SOS is the function I want to call. The above program doesn't work and I cant understand why.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Bits 0 and 4 are switches. If they both go high then Flash_SOS() is called.


Code C - [expand]
1
2
3
4
5
6
if(GPIO_PORTF_DATA_R == 0b00010001) {
    //50 ms delay
    if(GPIO_PORTF_DATA_R == 0b00010001) {
        Flash_SOS();
    }   
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top