How to set bits on the TI TM4C123G launchpad

0

I have a question about how bits are set(or cleared) on the TI launchpad registers. It seems sometimes they are bitwise or'd and other times they are set by just an assignement statement. For example, there is the register that is the clock gate and bit 5 must be set in order to be able to use GPIO Port F:

#define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))
 SYSCTL_RCGC2_R = 0x00000020;  //What are the values of all the bits now?

Also, I've seen bits set by bitwise or:

 SYSCTL_RCGC2_R |=  0x00000020;
embedded
asked on Stack Overflow May 5, 2019 by Alex

2 Answers

2
SYSCTL_RCGC2_R = 0x00000020 ; 

Sets all bits regardless of their current state. In this case all but b5 are zeroed.

SYSCTL_RCGC2_R |=  0x00000020 ;

Sets only b5, leaving all other bits unchanged. The |= assignment is equivalent to:

SYSCTL_RCGC2_R = SYSCTL_RCGC2_R | 0x00000020 ;

i.e. whatever SYSCTL_RCGC2_R contains is OR'ed with 0x00000020. So b5 must become 1 while all other bits remain unchanged because x OR 0 = x while x OR 1 = 1.

Similarly you can clear an individual bit by AND'ing an inverted bit-mask thus:

SYSCTL_RCGC2_R &= ~0x00000020 ;

because ~ reverses the bits (0xffffffdf), and x AND 0 = 0 while x AND 1 = x.

Note that none of this is specific to TI Launchpad or GPIO registers, it is universal to the programming language for any platform or integer data object.

answered on Stack Overflow May 5, 2019 by Clifford • edited May 7, 2019 by Clifford
1

This is basic C language operator behavior and nothing special about TI Launchpad. The assignment operator sets or clears every bit of the register. The bitwise OR operator sets the bits specified but doesn't clear any bits that were already set. Use a bitwise OR when you want to set a portion of the register without changing the rest. (A bitwise AND operator can be used to clear a portion without changing the rest.)

answered on Stack Overflow May 5, 2019 by kkrambo

User contributions licensed under CC BY-SA 3.0