[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
Visit site
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?)
 

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

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;

 
Ok now I get it if bit is 1 puts 1 in bitVal otherwise puts 0. Thanks a lot!
 

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.
 

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.
 

If GPIO_PORTF_DATA_REGISTER is 0xFF then


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

 

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.
 

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.
 

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.
 

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

Cookies are required to use this site. You must accept them to continue using the site. Learn more…