Brown Out Detect / Power Reset

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

 

I would like to flash an LED when the microprocessor has been reset due to a BOD reset.
Every time I cycle the power it goes into this routine instead of just a BOD. 

I have the micro running on a battery at -10°C and when I apply a high current load the Li-Ion battery voltage drops below 7V.  This is to be expected because of the cold temperature.

I am using a LM340 regulator therefor it could be dropping out at 7V. 

I want to know if it the microcontroller is resetting and if so what is causing it to reset, BOD or Power Reset 

 

Been playing with this for a few days and really stuck.

 

Here is some of the code I have tried.  I have tried so many different ways to make it work but this was my final attempt.

 

    /*  Read Brown Out Detect Reset Flag
    ***************************************/
   // if (BORF && (!PORF))
    if ((MCUSR = 0x04))
    {
        //  BORF Flag is cleared during Power-On Reset
        //  However, to be safe clear it anyway.
        MCUSR = 0;
                
        //Trouble Indicator
        while(1) LED_Trouble();
    }

 

Any help would be appreciated, thanks David Troike

 

This topic has a solution.
Last Edited: Tue. Nov 1, 2016 - 06:26 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

wd8cxb wrote:
if ((MCUSR = 0x04))

C Programmng 101 -- the comparison operator is ==

 

But you don't want to do it that way, anyway -- there could be more than one bit set.  So look for each in the priority order you might desire.

 

Note that some C prologues will clear WDRF before main() is invoked, to avoid cascading watchdog resets.  If important to you then you need to address that.

 

It helps if you tell which AVR model you are working with.  Also (but not critical here) it is good to post Vcc level, clock speed, toolchain, version, optimization settings, ...

 

IME "battery-powered" and "regulator" don't really go together.

 

CodeVision Wizard generates a sequence such as:

// Reset Source checking
if (MCUSR & (1<<PORF))
   {
   // Power-on Reset
   MCUSR=0;
   // Place your code here

   }
else if (MCUSR & (1<<EXTRF))
   {
   // External Reset
   MCUSR=0;
   // Place your code here

   }
else if (MCUSR & (1<<BORF))
   {
   // Brown-Out Reset
   MCUSR=0;
   // Place your code here

   }
else
   {
   // Watchdog Reset
   MCUSR=0;
   // Place your code here

   }

...which is tailored for WDRF already being cleared.   If BORF is "more important" than PORF then change the order of the comparisons.

 

 

wd8cxb wrote:
// if (BORF && (!PORF))

Remember that BORF and PORF are just numbers...

/* MCUSR - MCU Status Register */
#define    PORF            0       // Power-on reset flag
#define    EXTRF           1       // External Reset Flag
#define    BORF            2       // Brown-out Reset Flag
#define    WDRF            3       // Watchdog Reset Flag
//#define  JTRF            4       // JTAG Reset Flag

... so that commented line makes no sense.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Tue. Nov 1, 2016 - 05:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The application is using a ATmega328P.

 

Can't believe I didn't use ==, I know better than that but sometimes you get a mental block and don't see little stuff like that.

 

The if (BORF && (!PORF)) was an attempt to see if the flags were set.  I jump around between processors family and I am able to check to see if an individual bit is set with that notation. 

 

I was going about it all wrong and your sample code will get me back on track!  Thanks very much for the help.

 

David

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wd8cxb wrote:
I jump around between processors family and I am able to check to see if an individual bit is set with that notation.

Not true.  Not true at all.

 

Substitute from above and you get "if (2 && !0)".  2 is true, and !0 is true, so it will always be true.

 

If you shift the bit numbers it still isn't valid, but will always be false.  "if ((1<<BORF) && !(1<<PORF))"...

 

As you found, the "right" way for brown-out only is e.g. "if (  (MCUCR & (1<<BORF)) && !(MCUCR & (1<<PORF)) )" ...

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.