Access GPIO using bit field

Access GPIO and Register using the Bit field

In the embedded system, bit-field is used to represent the GPIO Pins of a microcontroller or the bits of the hardware register. A bit-field is the property of the structure, it is useful to create a bitmap structure which directly maps with the bit of register.

In this article, I am not describing the features of bit-field, I will only discuss the procedure of how to map a bit field to GPIO pins or the hardware register. We will also learn how to access GPIO using bit field.

For a better understanding, I am taking a register of LPC2119 and performing the read and write operation on its individual bits.

Before going to example code, I am discussing few steps, describe the how-to map bit field with hardware register or of above

Note:  Here, I am only describing, how is the bit-field work. I am not suggesting to use bit-field in the mapping of a hardware register because the allocation of bit-field depends upon the compiler.

Might be the result of one compiler is different from another compiler.  So we should avoid the compiler-dependent code, In simple words, avoid using of bit fields in the mapping of a hardware register.

Step to map hardware register with bit-field

/* define structure of Port Pin*/
typedef struct {

volatile unsigned int Bit0:1;
volatile unsigned int Bit1:1;
volatile unsigned int Bit2:1;
volatile unsigned int Bit3:1;
.
.
volatile unsigned int Bit31:1;
}SPortPin;

 

  • Create a pointer to the above describe bit-field and assign the address of the register to the pointer which you want to access.
volatile  SPortPin *psGpioPort  = (volatile  SPortPin *)0xE002C000;

 

Note: You must have permission to access the bits of the register using the pointer.

  • Now your bit-field structure mapped with hardware register which you want to access.
  • Procedure to read the value of the bits ( register) using the bit-field structure.
    Value  =  psGpioPort-> Bit1;
  • Procedure to write a value on the bits of the register using the bit-field structure.
     psGpioPort-> Bit1  =  1;

                 OR

    psGpioPort-> Bit1  =  0;

 

Note: To access the register in a more convenient way we put a bit-field structure and integral data type in a union, which enables the way to access the entire register or individual bits.

typedef union {

 volatile unsigned char PORT;
 SPortPin GPIO_PIN;

}UGpioPort;
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

C Program to describe how to access GPIO using bit field (32-bit register):

In the below C code, I am trying to write ‘1’ on the 3rd bit of register (0xE002C000), and after that, I will try to read the written value.

 

Access GPIO using bit field

 

#include <LPC21xx.H>


/* define structure of Port Pin*/
typedef struct { 

 volatile unsigned int Bit0:1; 
 volatile unsigned int Bit1:1; 
 volatile unsigned int Bit2:1; 
 volatile unsigned int Bit3:1; 
 volatile unsigned int Bit4:1; 
 volatile unsigned int Bit5:1; 
 volatile unsigned int Bit6:1; 
 volatile unsigned int Bit7:1; 

}SPortPin; 

 /*Union for port*/
typedef union {

 volatile unsigned int PORT;
 SPortPin GPIO_PIN;

}UGpioPort;



/* Function  to write on the pin*/
void WriteOnPin(UGpioPort* puPort, unsigned char ucPin, unsigned char value)
{
 switch(ucPin)   /* ucPin can be 0,1,2,3,..7 */
 {
 case 0: puPort->GPIO_PIN.Bit0 = value; 
 break;
 case 1: puPort->GPIO_PIN.Bit1 = value; 
 break;
 case 2: puPort->GPIO_PIN.Bit2 = value; 
 break;
 case 3: puPort->GPIO_PIN.Bit3 = value; 
 break;
 case 4: puPort->GPIO_PIN.Bit4 = value; 
 break;
 case 5: puPort->GPIO_PIN.Bit5 = value; 
 break;
 case 6: puPort->GPIO_PIN.Bit6 = value; 
 break;
 case 7: puPort->GPIO_PIN.Bit7 = value; 
 break;
 }
}

/* Function  to read the pin*/
unsigned char ReadFromPin(UGpioPort* puPort, unsigned char ucPin)
{
 unsigned char PinValue;

 switch(ucPin)  /* ucPin can be 0,1,2,3,..7 */
 {
 case 0: PinValue = puPort->GPIO_PIN.Bit0;
  break;
 case 1: PinValue = puPort->GPIO_PIN.Bit1; 
 break;
 case 2: PinValue = puPort->GPIO_PIN.Bit2; 
 break;
 case 3: PinValue = puPort->GPIO_PIN.Bit3; 
 break;
 case 4: PinValue = puPort->GPIO_PIN.Bit4; 
 break;
 case 5: PinValue = puPort->GPIO_PIN.Bit5;
 break;
 case 6: PinValue = puPort->GPIO_PIN.Bit6;
 break;
 case 7: PinValue = puPort->GPIO_PIN.Bit7;
 break;
 }

 return PinValue;
}

/* Main */
int  main(void) {
   
 unsigned char  PinValue;
 volatile  UGpioPort   *pUGpioPort =(volatile  UGpioPort*)0xE002C000;/*Address*/
 
 pUGpioPort->PORT=0x00000000;
 //write on the 3rd pin 
 WriteOnPin(pUGpioPort,2, 1);
 //read the value of 3rd pin         
 PinValue = ReadFromPin(pUGpioPort,2);
return 0;
}

 

Recommended Post: