GPIO read

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
soumajit
Posts: 12
Joined: Thu Feb 03, 2011 3:26 am

GPIO read

Post by soumajit »

I am using sub_gpio_read() function to read the status of GPIO pins. But it returns a hex value. If I want the individual pin status how do i get it? Basically I want to poll individual pins and want to know its value but since the status obtained is in hex how do i get the individual pin status..?

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: GPIO read

Post by serg »

Hi soumajit,

You can pick out individual pin values by using the C language "bitwise and" operator. Let's say you need to poll the pin #22.
The bitmask corresponding to the bit #22 is (2^22) or (1<<22). Next, you should apply your bitmask to the current pin values returned by the sub_gpio_read() function. The code below demonstrates this.

Code: Select all

#define PIN(x)  (1<<(x))

int curr_pin_state;
status = sub_gpio_read( h, &curr_pin_state);
if(status) goto error;
if( curr_pin_state & PIN(22))
   printf("Bit 22 is set\n");
else
   printf("Bit 22 is not set\n");

soumajit
Posts: 12
Joined: Thu Feb 03, 2011 3:26 am

Re: GPIO read

Post by soumajit »

Thank you so much for the reply :D

Post Reply