<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to use _WFI and WFE to enter idle mode</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/13639/how-to-use-_wfi-and-wfe-to-enter-idle-mode</link><description>Hi all:
To saving power I implement the function to read data after interrupt is triggered.. 
 below are my codes..
My expectation is.. 
 Let CPU enter idle mode to wait interrupt by call _WFI()
After interrupt is triggered start to read data and</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 04 May 2016 08:00:40 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/13639/how-to-use-_wfi-and-wfe-to-enter-idle-mode" /><item><title>RE: How to use _WFI and WFE to enter idle mode</title><link>https://devzone.nordicsemi.com/thread/52108?ContentTypeID=1</link><pubDate>Wed, 04 May 2016 08:00:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2caeb6d4-8c85-4dba-967b-ebee00d96663</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;You still have a race condition when you use bPoweSaving with __WFI.
Please read Anders comment &lt;a href="https://devzone.nordicsemi.com/question/1555/how-do-you-put-the-nrf51822-chip-to-sleep/"&gt;here&lt;/a&gt; Because you are using WFI and the flag in the same context, I think it is ok but i see something wrong in you code.&lt;/p&gt;
&lt;p&gt;I also do not understand your while state, since it will not allow the bPoweSaving to be set to true ever as while loop will never end&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int main(void)
{
  BSP_Init();

  static bool bPoweSaving = false;


    int16_t         XYZ_Data[3]={0};

    uint8_t reg = 0;
    ret_code_t err_code;
    static int i = 0;


    while(true)
    {

        /* Start transaction with a slave with the specified address. */
     if( bPoweSaving)
     {
       bPoweSaving = false;
       __WFI();

     }
     else
     {
       __WFE();
     }

     err_code = LIS3DH_ReadOutZYX(XYZ_Data);  
     
     if (i++ &amp;gt; 100)
     {
       i = 0;  
       bPoweSaving = true;
     }
    }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This above code will first sleep on WFI on every 100th reads and sleeps on WFE before 0 to 99 reads.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>