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

Status
Not open for further replies.

#### metalmisers

##### Junior Member level 2
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
Apparently you are confusing AND and OR operators. Consult a C text book.

#### milan.rajik

##### Banned
Which Compiler are you using? If mikroC PRO ARM then something like GPIO_PORTF_DATA_REGISTER.F0, GPIO_PORTF_DATA_REGISTER.F1...

#### metalmisers

##### Junior Member level 2
Keil uVision 4 V4.73.0.0

#### milan.rajik

##### Banned
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;

metalmisers

### metalmisers

Points: 2

#### Allen6502

Code:
char bitVal = GPIO_PORTF_DATA_REGISTER & 0x05

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

metalmisers

### metalmisers

Points: 2

#### metalmisers

##### Junior Member level 2
Ok now I get it if bit is 1 puts 1 in bitVal otherwise puts 0. Thanks a lot!

#### milan.rajik

##### Banned
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
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
If GPIO_PORTF_DATA_REGISTER is 0xFF then

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

#### metalmisers

##### Junior Member level 2
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
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
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
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.