0

I would like to configure my switch input type as either active high or active low. I wrote below code and I know it will not work because I when I multiply by 0, nothing works. One way is to write code to check ACTIVE LOW using if statement and handle. But I am sure there could be a simple method to perform logical NOT operation based on configurable bits. How to make the readSwitch() function configurable depending on switch input types?

#define ACTIVE_LOW 0
#define ACTIVE_HIGH 1
#define SWITCH_TYPE ACTIVE_LOW

uint8_t readSwitch(uint8_t pinNum)
{
    uint8_t state=0;
    if(SWITCH_TYPE*(PINC&(1<<pinNum)))
    {
        _delay_ms(20);
        if(SWITCH_TYPE*(PINC&(1<<pinNum)))
        {
           state=1;
        }
    }
    return(state);
}
Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • Why not to use "exclusive or"? `if (((PINC & (1 << pinNum)) != 0) ^ (ACTIVE_HIGH == 0)) .... ` if PINC bit is zero, and active high is not zero, or bit is not zero and ACTIVE_HIGH is zero then result would be true, and false otherwise – AterLux Feb 19 '18 at 12:10

1 Answers1

0

Based on @AterLux comment and further analysis, below code could take care.

if(!(PINC&(1<<pinNum))^SWITCH_TYPE)
Rajesh
  • 1,085
  • 1
  • 12
  • 25