• AVR Freaks

Hot!Testing a variable bit if set or clear

Page: 12 > Showing page 1 of 2
Author
NotTooSmart
Junior Member
  • Total Posts : 104
  • Reward points : 0
  • Joined: 2011/11/15 11:19:37
  • Location: 0
  • Status: offline
2019/07/11 21:40:02 (permalink)
0

Testing a variable bit if set or clear

Hi There, I am a newbie and this macro appears to be working fine:
#define IFSET(var,bits) (var & (bits))
 I also created: 
#define IFNOTSET(var, bits) (!(var & (bits)))
but there is something wrong. It compiles but when I traced it, does not makes sense as I watch the variable. Here is how I use it:
 
#define at_index        0b00000010000000000000000000000000 
int  xflag;
if IFNOTSET(xflag, at_index) {...}
 
Any help will be appreciated, 
Thanks
#1

21 Replies Related Threads

    PStechPaul
    Super Member
    • Total Posts : 2342
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/11 23:30:52 (permalink)
    0
    You need to provide more (maybe all) of your code. What you have shown, asked, and experienced, does not make sense to me.

     
    #2
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/12 11:32:35 (permalink)
    1 (1)
    Well, it is a macro construccion issue and its logic result; but the FINOTSET is not working correctly; Why? 
     
    #define IFSET(var,bits) (var & (bits)) //this one works well.
     I also created: 
    #define IFNOTSET(var, bits) (!(var & (bits)))
    but there is something wrong. It compiles but when I traced it, does not makes sense as I watch the variable. Here is how I use it:
     
    #define at_index    0b00000010000000000000000000000000 
    int  xflag;

    if IFNOTSET(xflag, at_index) {...}
     
    Thanks for helping
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 17601
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Testing a variable bit if set or clear 2019/07/12 13:38:38 (permalink)
    0
    You are not saying what you expect to see , or what you are seeing.  Or even what you are using to see it.
     
     
    This may be more readable:
    #define AT_INDEX   (1U << 26)
    #4
    Larry.Standage
    Super Member
    • Total Posts : 904
    • Reward points : 0
    • Joined: 2011/12/30 09:50:47
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/12 13:52:19 (permalink)
    0
    It might help if you can post the disassembly at the code in question, and what the value of xflag is.
     
    #5
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/12 16:28:28 (permalink)
    0
    Setup: 
    #define at_index    0b00000000000000000000000000000010 
    #define IFNOTSET(var, bits) (!(var & (bits))) 
    xflag=0b00000000000000000000000000000010  //first case
    xflag=0b00000000000000000000000000000000   //second case
    Test:
    if IFNOTSET(xflag, at_index) {...} //will this macro produce a correct logic for each of the xflag cases?
     
    Thanks for helping
    #6
    NorthGuy
    Super Member
    • Total Posts : 5541
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/12 19:22:09 (permalink)
    0
    NotTooSmart
    Setup: 
    #define at_index    0b00000000000000000000000000000010 
    #define IFNOTSET(var, bits) (!(var & (bits))) 
    xflag=0b00000000000000000000000000000010  //first case
    xflag=0b00000000000000000000000000000000   //second case
    Test:
    if IFNOTSET(xflag, at_index) {...} //will this macro produce a correct logic for each of the xflag cases?


    Assuming xflag is a variable of "int" type, the "if" will execute in the second case.
     
    However, this is not ubiquitous. Say, in this case:
     
    signed char at_index = 1 << 7;
    int xflag = 0x0400;

     
    your construction will not work as you expect because at_index will expand into 0xff80.
     
     
    post edited by NorthGuy - 2019/07/12 19:23:16
    #7
    LdB_ECM
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/13 02:47:31 (permalink)
    0
    The IFNOTSET is not required it is just !IFSET .. fairly obvious if you think about it if its not set then its clear :-)

    To safety up the macro cast each entry to unsigned and to really be safe return only 0 or 1(Your code currently returns 0 or some "AND" result)
    #define IFSET(var,bits) ( (((unsigned)(var) & (unsigned)(bits)) == 0)? 0: 1 )

     
    I should add your test is not exactly clear to me if I have a flag and I have two bits do you want the true only when both bits are set  so for example  b01  b10 report as false  b11 report as true. If that is the case you need the macro to be
    #define IFSET(var,bits) ( (((unsigned)(var) & (unsigned)(bits)) == (unsigned)(bits))? 1: 0 )

    post edited by LdB_ECM - 2019/07/13 03:03:45
    #8
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/15 11:42:53 (permalink)
    0
    I would never have 2 bits to test.
    But I have also seen this macro:
    IFNOTSET(var, bits)  (!(var | ~(bits)));
    Would this macro be safer than mine: 
    IFNOTSET(var, bits) (!(var & (bits)))  
    Thanks
    #9
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/15 12:38:18 (permalink)
    0
    How did you determine it does not work?
     
    #10
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/27 06:26:59 (permalink)
    0
    Here is a case:
    #define IFNOTSET(var,bits) (!(var & bits))

    wait11: if IFNOTSET(xflag,step_done) {goto wait11;}

    The program hangs_up on the above line even though the same bit of variable and the test bit are set. The Boolean logic is not followed. I do not understand and welcome any help
    Thanks 
    #11
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/27 06:35:18 (permalink)
    0
    NotTooSmart
    Here is a case:
    #define IFNOTSET(var,bits) (!(var & bits))

    wait11: if IFNOTSET(xflag,step_done) {goto wait11;}

    The program hangs_up on the above line even though the same bit of variable and the test bit are set. The Boolean logic is not followed. I do not understand and welcome any help

    You will have to post more to be of any help. Perhaps a complete minimal program that demonstrates this problem!
     
    #12
    andersm
    Super Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/27 06:36:10 (permalink)
    0
    You have to post a complete example.
    #13
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/27 21:42:59 (permalink)
    0
    Yes, I would like to upload my complete app project here, but I don't see how it is done in this form. Is there a way? any help? 
    #14
    PStechPaul
    Super Member
    • Total Posts : 2342
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/27 21:53:17 (permalink)
    4 (1)
    Use MPLABX to save your project to a zipfile. Then use the Full Version editor to upload the file to the forum server so we can download it and build it.
     
    If it's a simple project with a single source file you can just use [ code][ /code] tags (without spaces) and insert your code for formatting that is easier to read.
     
    void main(void) {
      initialize();
      while(1) {
        run(); }
    }


     
    #15
    nigelwright7557
    Super Member
    • Total Posts : 272
    • Reward points : 0
    • Joined: 2006/11/06 08:15:51
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/28 17:17:09 (permalink)
    0
     I usually define all the bits set and all the bits reset.
    Then and with bitreset to see if it is reset.
    Or and with bitset to see if it set.
    define setbit0 1
    define setbit1 2
    define resetbit0 0xfe
    define resetbit1 00fd
     
     if (var & setbit0)
    bit is set
    if (var & resetbit0)
    bit is reset
    Gets a bit more complicated with 32 bit variables but this is just an example.
     
     
     
    #16
    ric
    Super Member
    • Total Posts : 23187
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Testing a variable bit if set or clear 2019/07/28 17:24:30 (permalink)
    0
    You can OR with your "setbit" to set a bit,
    You can AND with your "resetbit" to clear a bit.
    You can AND with your "setbit" to test if a bit is set,
    but you certainly can't AND with your resetbit to test if it is clear.
    That is testing if any bit EXCEPT the nominated bit is set.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    NotTooSmart
    Junior Member
    • Total Posts : 104
    • Reward points : 0
    • Joined: 2011/11/15 11:19:37
    • Location: 0
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/29 16:21:33 (permalink)
    0
    I I have my files zipped but uploading to the server seems a bit complicated. I am surprised that a forum such as this one does not have a button to upload a file. I will paste the app.c file and wonder if enough to see a problem?
     


    #include "app.h"
    int xflag,blinkdelay;
    #define step_done                BIT_9    //     0b0000001000000000

    #define mLED_1              LATCbits.LATC13
    #define mLED_2              LATDbits.LATD0
    #define mLED_3              LATDbits.LATD11
    #define mLED_1_On()         mLED_1 = 0;
    #define mLED_2_On()         mLED_2 = 0;
    #define mLED_3_On()         mLED_3 = 0;
    #define mLED_1_Off()        mLED_1 = 1;
    #define mLED_2_Off()        mLED_2 = 1;
    #define mLED_3_Off()        mLED_3 = 1;
    #define mLED_1_Toggle()     mLED_1 = !mLED_1;
    #define mLED_2_Toggle()     mLED_2 = !mLED_2;
    #define mLED_3_Toggle()     mLED_3 = !mLED_3;
    APP_DATA appData;
    /* Receive data buffer */
    uint8_t receivedDataBuffer[64];
    /* Transmit data buffer */
    uint8_t  transmitDataBuffer[64];
    /* The endpoint size is 64 for FS and 512 for HS */
    uint16_t endpointSize;
    // *****************************************************************************
    // *****************************************************************************
    // Section: Application Callback Functions
    // *****************************************************************************
    // *****************************************************************************
    /*********************************************
     * Application USB Device Layer Event Handler
     *********************************************/
    void APP_USBDeviceEventHandler(USB_DEVICE_EVENT event, void * eventData, uintptr_t context)
    {
        uint8_t * configurationValue;
        USB_SETUP_PACKET * setupPacket;
        switch(event)
        {
            case USB_DEVICE_EVENT_RESET:
            case USB_DEVICE_EVENT_DECONFIGURED:
                appData.deviceIsConfigured = false;
                break;
            case USB_DEVICE_EVENT_CONFIGURED:
                /* Check the configuration */
                configurationValue = (uint8_t *)eventData;
                if(*configurationValue == 1 )
                {
                    /* Reset endpoint data send & receive flag  */
                    appData.deviceIsConfigured = true;
                }
                break;
            case USB_DEVICE_EVENT_SUSPENDED:
                break;

            case USB_DEVICE_EVENT_POWER_DETECTED:
                /* VBUS is detected. Attach the device */
                USB_DEVICE_Attach(appData.usbDevHandle);
                break;
            case USB_DEVICE_EVENT_POWER_REMOVED:
                /* VBUS is removed. Detach the device */
                USB_DEVICE_Detach (appData.usbDevHandle);
                break;
            case USB_DEVICE_EVENT_CONTROL_TRANSFER_SETUP_REQUEST:
                /* This means we have received a setup packet */
                setupPacket = (USB_SETUP_PACKET *)eventData;
                if(setupPacket->bRequest == USB_REQUEST_SET_INTERFACE)
                {
                    /* If we have got the SET_INTERFACE request, we just acknowledge
                     for now. This demo has only one alternate setting which is already
                     active. */
                    USB_DEVICE_ControlStatus(appData.usbDevHandle,USB_DEVICE_CONTROL_STATUS_OK);
                }
                else if(setupPacket->bRequest == USB_REQUEST_GET_INTERFACE)
                {
                    /* We have only one alternate setting and this setting 0. So
                     * we send this information to the host. */
                    USB_DEVICE_ControlSend(appData.usbDevHandle, &appData.altSetting, 1);
                }
                else
                {
                    /* We have received a request that we cannot handle. Stall it*/
                    USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_ERROR);
                }
                break;
            case USB_DEVICE_EVENT_ENDPOINT_READ_COMPLETE:
               /* Endpoint read is complete */
                appData.epDataReadPending = false;
                break;
            case USB_DEVICE_EVENT_ENDPOINT_WRITE_COMPLETE:
                /* Endpoint write is complete */
                appData.epDataWritePending = false;
                break;
            /* These events are not used in this demo. */
            case USB_DEVICE_EVENT_RESUMED:
            case USB_DEVICE_EVENT_ERROR:
            default:
                break;
        }
    }
     
    // *****************************************************************************
    // *****************************************************************************
    // Section: Application Local Functions
    // *****************************************************************************
    // *****************************************************************************
    static void APP_StartCoreTimer(uint32_t period)
    {
        /* Reset the counter */
        volatile uint32_t loadZero = 0;
        asm volatile("mtc0 %0, $9" : "+r"(loadZero));
        asm volatile("mtc0 %0, $11" : "+r" (period));
    }

    void APP_Initialize ( void )
    {
        /* Place the App state machine in its initial state. */
        appData.state = APP_STATE_INIT;
        appData.usbDevHandle = USB_DEVICE_HANDLE_INVALID;
        appData.deviceIsConfigured = false;
        appData.endpointRx = (1 | USB_EP_DIRECTION_OUT);
        appData.endpointTx = (1 | USB_EP_DIRECTION_IN);
        appData.epDataReadPending = false;
        appData.epDataWritePending = false;
        appData.altSetting = 0;
    }

    void APP_Tasks (void )
    {
        int index = 0;
        switch(appData.state)
        {
            case APP_STATE_INIT:
               
                APP_StartCoreTimer(CORE_TICK_RATE);
                DRV_TMR0_Start();
               
                /* Open the device layer */
                appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,
                        DRV_IO_INTENT_READWRITE );
                if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID)
                {
                    /* Register a callback with device layer to get event notification (for end point 0) */
                    USB_DEVICE_EventHandlerSet(appData.usbDevHandle,  APP_USBDeviceEventHandler, 0);
                    appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
                }
                else
                {
                    /* The Device Layer is not ready to be opened. We should try
                     * again later. */
                }
                break;
            case APP_STATE_WAIT_FOR_CONFIGURATION:
                /* Check if the device is configured */
                if(appData.deviceIsConfigured == true)
                {
                    if (USB_DEVICE_ActiveSpeedGet(appData.usbDevHandle) == USB_SPEED_FULL)
                    {
                        endpointSize = 64;
                    }
                    else if (USB_DEVICE_ActiveSpeedGet(appData.usbDevHandle) == USB_SPEED_HIGH)
                    {
                        endpointSize = 512;
                    }
                    if (USB_DEVICE_EndpointIsEnabled(appData.usbDevHandle, appData.endpointRx) == false )
                    {
                        /* Enable Read Endpoint */
                        USB_DEVICE_EndpointEnable(appData.usbDevHandle, 0, appData.endpointRx,
                                USB_TRANSFER_TYPE_BULK, endpointSize);
                    }
                    if (USB_DEVICE_EndpointIsEnabled(appData.usbDevHandle, appData.endpointTx) == false )
                    {
                        /* Enable Write Endpoint */
                        USB_DEVICE_EndpointEnable(appData.usbDevHandle, 0, appData.endpointTx,
                                USB_TRANSFER_TYPE_BULK, endpointSize);
                    }
                    /* Indicate that we are waiting for read */
                    appData.epDataReadPending = true;
                    /* Place a new read request. */
                    USB_DEVICE_EndpointRead(appData.usbDevHandle, &appData.readTranferHandle,
                            appData.endpointRx, &receivedDataBuffer[0], sizeof(receivedDataBuffer) );
                    /* Device is ready to run the main task */
                    appData.state = APP_STATE_MAIN_TASK;
                }
                break;
            case APP_STATE_MAIN_TASK:
                if(!appData.deviceIsConfigured)
                {
                    /* This means the device got deconfigured. Change the
                     * application state back to waiting for configuration. */
                    appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
                    /* Disable the endpoint*/
                    USB_DEVICE_EndpointDisable(appData.usbDevHandle, appData.endpointRx);
                    USB_DEVICE_EndpointDisable(appData.usbDevHandle, appData.endpointTx);
                    appData.epDataReadPending = false;
                    appData.epDataWritePending = false;
                }
                else if (appData.epDataReadPending == false)
                {
                    for (index = 0; index < 64; index++)
                        transmitDataBuffer[index] = receivedDataBuffer[index] + 1;
                  //  backtohost=0;
                  //  do_commands();
                    if(appData.epDataWritePending == false)
                    {
                        /* Send the data to the host */
                        appData.epDataWritePending = true;
                        USB_DEVICE_EndpointWrite ( appData.usbDevHandle, &appData.writeTranferHandle,
                                appData.endpointTx, &transmitDataBuffer[0],
                                sizeof(transmitDataBuffer),
                                USB_DEVICE_TRANSFER_FLAGS_MORE_DATA_PENDING);
                    }
                    appData.epDataReadPending = true ;
                    /* Place a new read request. */
                    USB_DEVICE_EndpointRead ( appData.usbDevHandle, &appData.readTranferHandle,
                            appData.endpointRx, &receivedDataBuffer[0], sizeof(receivedDataBuffer) );
                }
                break;
            case APP_STATE_ERROR:
                break;
            default:
                break;
        }
        do_self_test();
    }
    //=========================================================================================
    void __ISR(_CORE_TIMER_VECTOR, ipl2AUTO) CoreTimerHandler(void) {
        BIT_SET(xflag,step_done);
        mLED_2_Toggle();
        UpdateCoreTimer(30000000);
        SYS_INT_SourceStatusClear(INT_SOURCE_TIMER_CORE);
    }
    void __ISR(_TIMER_1_VECTOR, ipl1AUTO) IntHandlerDrvTmrInstance0(void)
    {
        PLIB_INT_SourceFlagClear(INT_ID_0,INT_SOURCE_TIMER_1);
        if (blinkdelay){blinkdelay--;}
        else                                        //0b0000100000000000  12 counts
        {   blinkdelay=2000;// BIT_SET(xflag,do_selftest);
            {mLED_3_Toggle();}
        }
    }
     void do_self_test(void)
    {  
    cont11:
         BIT_SET(xflag,step_done);// 0b1111110111111111 //9
      mLED_1_On();//mLED_1_Off();
    wait11: if (!IFSET(xflag,step_done)) {goto wait11;}
        BIT_CLR(xflag,step_done);
        mLED_1_Off();//never gets here even though xflag,step_done is set
    wait12: if (!IFSET(xflag,step_done)) {goto wait12;}
    goto cont11;
    }
     
    #18
    ric
    Super Member
    • Total Posts : 23187
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Testing a variable bit if set or clear 2019/07/29 16:31:08 (permalink)
    0
    NotTooSmart
     I am surprised that a forum such as this one does not have a button to upload a file.

    It does, but only on the "Full editor" screen, not the "Quick Reply" which I think you are using.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #19
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Testing a variable bit if set or clear 2019/07/29 19:05:53 (permalink)
    0
    NotTooSmart
    I will paste the app.c file and wonder if enough to see a problem?

    Most of what you posted are "junk"!!!
     

    void do_self_test(void)
    {   
    cont11:
        BIT_SET(xflag,step_done);// 0b1111110111111111 //9
    mLED_1_On();//mLED_1_Off();
    wait11: if (!IFSET(xflag,step_done)) {goto wait11;}
       BIT_CLR(xflag,step_done);
       mLED_1_Off();//never gets here even though xflag,step_done is set
    wait12: if (!IFSET(xflag,step_done)) {goto wait12;}
    goto cont11;
    }


    Post the code for BIT_SET() and BIT_CLR().
     
    NotTooSmart
    #define step_done                BIT_9    //     0b0000001000000000

        BIT_SET(xflag,step_done);// 0b1111110111111111 //9

    Also, step_done has one bit set or one bit cleared?
    post edited by 1and0 - 2019/07/29 19:14:12
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5