In most every project your microcontroller does not run all the time. You also don´t want to waste battery capacity, so you should set your microcontroller into a sleep mode.
In a sleep mode the microcontroller shut down some components to save energy. The Mega 4808 has two interesting sleep modes. One of it is the power down mode. In this mode you can wake up from an external interrupt source via a GPIO.
In the following picture you can see a very simple code which sets the Mega 4808 in power down mode. With this code you Mega 4808 sleeps for ever.
Now we add also an external interrupt on pin PF0. If this pin change its state from high to low or from low to high it goes in the subroutine subr(). In the interrupt routine we change the logic state of the variable state. If state is low we set it in the interrupt routine to high and if state is high we set it to low. This state variable is the logic voltage on pin PA2.
So every time the Mega 4808 wakes up from an pinchange interrupt on PF0 it goes in the subroutine subr() and changes the pinstate of pin PA2.
On the pins you can see the function of the code.
You can also use the standby mode instead of the power down mode. In the standby mode you can also use the internal RTC timer. With the RTC timer you can wake up you controller after a programmable time without a external interrupt. For this i use the RocketScream LowPowerAVRZero and the RocketScream_RTCAVRZero library which is available on github.
In the following picture is a code which wakes up every 5 seconds. If the Mega 4808 wakes up from standby it goes in the subroutine subr() and toggle the pin PA2. After this he goes into standby mode.
On the pin PA2 you can see the function of the code.
- Not every pin of the Mega 4808 has the interrupt function. For this see the Mega 4808 Arduino pin usage / pinout
- You can only wake up from an high, low or change interrupt in sleep mode.
Other posts about Mega 4808: