STC15W408AS library 0.16.0
Loading...
Searching...
No Matches
Macros
Power management

Macros

#define WIRC_H_ADDRESS   0xf8
 Wakeup timer internal clock frequency value high byte address.
 
#define WIRC_L_ADDRESS   0xf9
 Wakeup timer internal clock frequency value low byte address.
 
#define WAKEUP_DEFAULT_FREQ   32768U
 Default internal wakeup generator frequency in Hz.
 
#define idle()   (bit_set(PCON, SBIT0))
 Set MCU idle mode.
 
#define power_down()
 Set MCU power down mode.
 
#define wakeup_timer_init(ticks)
 Init and run wakeup timer.
 
#define wakeup_timer_init_seconds(seconds)
 Initialize wakeup timer with a specified duration in seconds.
 
#define wakeup_timer_internal_clk_freq()   (((uint16_t)*wirc_h_ptr << 8) | *wirc_l_ptr)
 Get wakeup timer internal clock frequency.
 
#define get_power_low_voltage_flag()   (get_bit(PCON, 5))
 Retrieves the status of the low voltage detection flag.
 
#define clear_power_low_voltage_flag()   (bit_clr(PCON, CBIT5))
 Clears the low voltage detection flag in the PCON register.
 

Detailed Description

Functions and data structures related for MCU power management

Author
Michael Golovanov

Macro Definition Documentation

◆ clear_power_low_voltage_flag

#define clear_power_low_voltage_flag ( )    (bit_clr(PCON, CBIT5))

Clears the low voltage detection flag in the PCON register.

This macro clears the low voltage detection flag by resetting bit 5 (CBIT5) of the PCON (Power Control) register. This flag is set when a low voltage condition is detected, and must be manually cleared by software.

◆ get_power_low_voltage_flag

#define get_power_low_voltage_flag ( )    (get_bit(PCON, 5))

Retrieves the status of the low voltage detection flag.

This macro reads the state of bit 5 (SBIT5) in the PCON (Power Control) register to determine if the low voltage detection flag is set. It is typically used in microcontrollers to check whether a low voltage condition has been detected during operation.

Returns
Returns the value of bit 5 in the PCON register:
  • 0 if the bit is not set (no low voltage condition),
  • 1 if the bit is set (low voltage condition detected).

◆ idle

#define idle ( )    (bit_set(PCON, SBIT0))

Set MCU idle mode.

Shuts off clock to CPU, but clock to Timers, Interrupts, Serial Ports, and Analog Peripherals are still active. The CPU status is preserved in its entirety: the RAM, Stack Pointer, Program Counter, Program Status Word, Accumulator, and all other registers maintain their data during Idle. The port pins hold the logical states they had at the time Idle was activated.

◆ power_down

#define power_down ( )
Value:
do { \
bit_set(PCON, SBIT1); \
__asm__("nop"); \
__asm__("nop"); \
__asm__("nop"); \
} while(0)

Set MCU power down mode.

In the Stop/Power-Down mode, the on-chip oscillator and the Flash memory are stopped in order to minimize power consumption. Only the power-on circuitry will continue to draw power during Stop/Power-Down. The contents of on-chip RAM and SFRs are maintained.

◆ WAKEUP_DEFAULT_FREQ

#define WAKEUP_DEFAULT_FREQ   32768U

Default internal wakeup generator frequency in Hz.

See also
wakeup_timer_internal_clk_freq() - function that attempts to read the actual internal clock frequency from RAM
wakeup_timer_init_seconds() - function that uses this default value when the actual frequency cannot be determined

◆ wakeup_timer_init

#define wakeup_timer_init (   ticks)
Value:
do { \
WKTCH = (ticks >> 8) | 0x80; \
WKTCL = ticks & 0xff; \
} while(0)

Init and run wakeup timer.

Wakeup time is calculated as (10^6/wakeup_timer_internal_clock_frequency) * 16 * ticks

See also
wakeup_timer_internal_clk_freq()
Parameters
ticksuint16_t wakeup timer ticks count

◆ wakeup_timer_init_seconds

#define wakeup_timer_init_seconds (   seconds)
Value:
do { \
uint16_t wakeup_freq = wakeup_timer_internal_clk_freq(); \
if (!wakeup_freq) { wakeup_freq = WAKEUP_DEFAULT_FREQ; } \
uint16_t timer_ticks = seconds * (wakeup_freq >> 4); \
wakeup_timer_init(timer_ticks); \
} while(0)
#define WAKEUP_DEFAULT_FREQ
Default internal wakeup generator frequency in Hz.
Definition power_management.h:42
#define wakeup_timer_internal_clk_freq()
Get wakeup timer internal clock frequency.
Definition power_management.h:145

Initialize wakeup timer with a specified duration in seconds.

This routine configures the wakeup timer to wake up the MCU after a specified number of seconds. It calculates the appropriate timer ticks based on the internal wakeup clock frequency.

The calculation uses the formula: timer_ticks = seconds * (wakeup_freq >> 4) where wakeup_freq >> 4 represents the clock frequency divided by 16.

The function first attempts to read the actual internal clock frequency using wakeup_timer_internal_clk_freq(). If this returns 0 (which happens when RAM is cleared by the compiler startup code), it falls back to using WAKEUP_DEFAULT_FREQ (32,768 Hz).

Parameters
secondsuint16_t Number of seconds to set the wakeup timer for
See also
wakeup_timer_internal_clk_freq() - function to get the actual internal clock frequency from RAM addresses 0xf8-0xf9
WAKEUP_DEFAULT_FREQ - default frequency used when actual frequency is unavailable
wakeup_timer_init() - lower-level function that initializes the timer with ticks

◆ wakeup_timer_internal_clk_freq

#define wakeup_timer_internal_clk_freq ( )    (((uint16_t)*wirc_h_ptr << 8) | *wirc_l_ptr)

Get wakeup timer internal clock frequency.

wakeup timer internal clock frequency after MCU powerup is placed in RAM __idata 0xf8-0xf9 adresses.

By default SDCC compiler generate firmware part that before main() call clear RAM and 0xf8-0xf9 contains 0x00 values.

To avoid memory clearing implement empty
void _mcs51_genRAMCLEAR() {} routine. Otherwise routine call return result 0x00.

Default value is 0x8c0a = 35850Hz.

Returns
uint16_t wakeup timer internal clock frequency from 0xf8-0xf9 RAM