Low power consumption application of STM8L051X series RTC

Publisher:SerendipitySoulLatest update time:2019-12-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I haven't written a blog on CSDN for a long time. Recently, I have some free time to learn about the low-power microcontroller STM8L051F3. The main application of what I made this time is to control the on/off operation of the power supply. The main peripherals used are RTC, PWR, AWU, ADC, WWDG, etc. I will first give a brief introduction to the overall functions, so that it will be easier for everyone to understand the following program. First, use the RTC alarm interrupt to control the on/off operation of the power supply in a specified time period, ADC is used to detect when the power is less than a certain percentage and turn off the power supply, WWDG is used to monitor the program, the serial port is used to print debugging information, and AWU is used to achieve automatic wake-up in 10 minutes. After the function introduction, let's start to gradually introduce the specific implementation of each functional module.


RTC alarm interrupt function:

From the above figure, we can see that there are four clock sources: HSE, HSI, LSE, and LSI. However, for the accuracy of the RTC clock, it is more appropriate to choose an external crystal oscillator as the clock source, so we choose the external low-speed clock LSE (32.768KHz). If you choose the internal LSI, the RTC time has a large error in actual testing. And the RTC of this chip also has the following functions


A calendar with microseconds, seconds, minutes, hours (12 or 24 hour format), day of the week, day of the week, month and year

A programmable alarm with interrupt

An automatic wake-up unit

We just happen to use all of these functions. The main initialization steps are as follows:

1) Select LSE as the clock source

2) Turn on the RTC peripheral clock

3) Set the desired RTC time

4) Initialize the calendar

5) Initialization time


void RTC_Init(void)

{

  int temp=0;

  

  RTC_InitTypeDef RTC_InitStr;

  RTC_DateTypeDef RTC_DateStr;

  RTC_TimeTypeDef RTC_TimeStr;

 

  //Select LSE (32.768KHz) as the clock source

  CLK_RTCClockConfig(CLK_RTCCLKSource_LSE, CLK_RTCCLKDiv_1);

  /* Wait for LSE clock to be ready */

  while (CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);

  //Turn on the RTC clock

  CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE); 

  /* RTC clock source: LSE, timing time: 32768/128/256 = 1S */

  temp = EEPROM_Read_Byte(0); //Read data from the built-in EEPROM

  if(temp != 0X73) //Judge whether the RTC time has been set

  { 

    printf("Set INGrn");

    RTC_InitStr.RTC_HourFormat = RTC_HourFormat_24; //24-hour format

    RTC_InitStr.RTC_AsynchPrediv = 0x7F; //Asynchronous divider 128 division

    RTC_InitStr.RTC_SynchPrediv = 0x00FF; // Synchronous divider 256 division

    RTC_Init(&RTC_InitStr); //Initialize RTC parameters

    /* Initialize the RTC_DateStr structure and set the date data*/

    RTC_DateStructInit(&RTC_DateStr); //Initialize RTC_DateStr structure

    RTC_DateStr.RTC_WeekDay = RTC_Weekday_Thursday; //Thursday

    RTC_DateStr.RTC_Date = 28; //22nd

    RTC_DateStr.RTC_Month = RTC_Month_September; //September

    RTC_DateStr.RTC_Year = 18; //2018

    RTC_SetDate(RTC_Format_BIN,&RTC_DateStr); //Set date data

    /* Initialize the RTC_TimeStr structure and set the time data*/

    RTC_TimeStructInit(&RTC_TimeStr); //Initialize RTC_TimeStr structure

    RTC_TimeStr.RTC_Hours = 17; //23H

    RTC_TimeStr.RTC_Minutes = 21; //52 minutes

    RTC_TimeStr.RTC_Seconds = 0; //0 seconds

    RTC_SetTime(RTC_Format_BIN,&RTC_TimeStr); //Set time data

   //RTC initialization is complete. The following is the EEPROM write operation

    FLASH_SetProgrammingTime(FLASH_ProgramTime_Standard);

    FLASH_Unlock(FLASH_MemType_Data);

    while(FLASH_GetFlagStatus(FLASH_FLAG_DUL)==RESET);

    EEPROM_Write_Byte(0,0X73); //Write 0x73

  }

  else

  {   

    printf("RTC has been set rn");

  } 

}

The above is the initialization function of RTC. There is one thing that needs to be noted. First, we make a judgment before initialization. If this judgment is not added, then when the chip is powered on or reset, the time will be reinitialized. This is naturally not possible, so we write specific data to the EEPROM as a flag, so that the RTC is initialized only once. The specific sub-functions for reading and writing EEPROM are given below and will not be explained.


void EEPROM_Write_Byte(uint8_t addr, uint8_t data)

{

  /* Write data to EEPROM address 0x1000 + addr*/

  FLASH_ProgramByte(FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + addr, data);

  /* Wait for writing to complete */

while(FLASH_GetFlagStatus(FLASH_FLAG_EOP)==RESET);

}

uint8_t EEPROM_Read_Byte(uint8_t addr)

{

  uint8_t data;

  /* Read the data at EEPROM address 0x1000 + addr*/

  data = FLASH_ReadByte(FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + addr);

return data; //Return the read data

}

 This just initializes the RTC. Is there any alarm set? The code is given directly as follows:


void Alarm_Set(int h,int m,int s)

{

    RTC_AlarmTypeDef RTC_AlarmStr;

    

/* Initialize the RTC alarm structure and set the time data*/

    RTC_AlarmCmd(DISABLE); //Disable before enabling  

        

    RTC_AlarmStructInit(&RTC_AlarmStr); 

    

    RTC_AlarmStr.RTC_AlarmTime.RTC_Hours=h;

    RTC_AlarmStr.RTC_AlarmTime.RTC_Minutes=m;

    RTC_AlarmStr.RTC_AlarmTime.RTC_Seconds=s; 

    RTC_AlarmStr.RTC_AlarmTime.RTC_H12=RTC_H12_PM;     

    RTC_AlarmStr.RTC_AlarmMask=RTC_AlarmMask_DateWeekDay; //Shield date

    RTC_AlarmStr.RTC_AlarmDateWeekDaySel=RTC_AlarmDateWeekDaySel_Date;

    RTC_AlarmStr.RTC_AlarmDateWeekDay=RTC_AlarmDateWeekDaySel_Date;

    RTC_SetAlarm(RTC_Format_BIN,&RTC_AlarmStr);   

    RTC_AlarmCmd(ENABLE); //Enable RTC alarm function  

    RTC_ClearFlag(RTC_FLAG_ALRAF); //Clear the alarm interrupt flag

    RTC_ITConfig(RTC_IT_ALRA,ENABLE);   

}

 h,m,s correspond to hours, minutes, and seconds. There are also two points to note. First, you need to turn off the enable to specify the alarm time when setting the alarm time. Second, its structure member RTC_AlarmMask is the interrupt option to be masked. For example, if you want to set an alarm at 10 o'clock, but don't care about the minutes and seconds, then you can mask the minutes and seconds.


Low Power Consumption:


As a low-power chip, the highlight of low power consumption is worth understanding and learning. Next, I will talk about my understanding and application of STM8L low power consumption mode.

STM8L and 5 low power modes:


1. Wait mode (Wait): Wait mode can be divided into WFI and WFE. Their similarities are that all internal interrupts, external interrupts and resets can cause them to exit wait mode. The difference is that WFE can also be awakened by events. Entering this mode is by executing WFI or WFE instructions. The power consumption in this mode is MVR, which is more than ULP (ultra-low power consumption). In this low-power mode, the CPU is not working. How to understand it? In actual tests, when the CPU enters low-power mode, the code is not running, which is equivalent to the program being blocked there.


2. Low power run mode: The method to enter this mode is through software sequence configuration. In this mode, the CPU is running, Flash and EEPROM are stopped, and the corresponding peripherals can be selected to run. The operation of RAM is completed by the low-speed oscillator clock (LSI or LSE), and the voltage manager mode is configured as ultra-low voltage mode (ULP). Exiting this mode is through software or reset.


3. Low power wait mode: This mode is entered by executing the WFE instruction in low power operation mode. In this mode, the CPU stops running, and other features are the same as Low power run mode. The way to exit this mode is to trigger an internal or external event, and when triggered, it returns to low power operation mode.


4. Active-halt mode: This mode is the mode I need to use this time. In this mode, the CPU and peripheral clocks are stopped except for the RTC. It can be exited by triggering an RTC interrupt, external interrupt or reset.


5. Halt mode: In this mode, the CPU and peripheral clocks are stopped and can only be awakened by triggering an external interrupt or reset.


Through the introduction of the above 5 modes, after comparison, I choose the active shutdown mode which is more suitable for what I am doing. The specific code is implemented by the following instructions.


halt();

Executing the above instructions means that the microcontroller enters active stop mode.


Automatic wake-up:


In active stop mode, to detect the power of ADC once every 10 minutes, the RTC automatic wake-up function is needed. This function will cause the MCU to exit active stop mode, detect the power, and then enter active stop mode again. This cycle will only wake up once each time to detect the power, and the CPU will not work in other time periods, so as to achieve the lowest power consumption.


Here is the automatic wakeup code:


void AWU_RTC(int s)

{

      RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);

      RTC_ITConfig(RTC_IT_WUT,ENABLE);

      RTC_SetWakeUpCounter(s); //Set the wake-up interval (seconds)

      RTC_WakeUpCmd(ENABLE);  

}

Note that after setting the wake-up time once, you do not need to call this function again. If you do not need to wake up, just enable DISABLE. The clock of the automatic wake-up is 16 bits, so the longest wake-up time cannot exceed 2800 seconds;


Window Watchdog:


In low power mode, if the CPU stops working, what should we do if we need to add the watchdog function? Then the window watchdog can be realized. When the MCU enters low power mode, it will stop counting the watchdog. In this way, there is no need to worry about the MCU not being able to feed the watchdog due to the CPU stopping working, resulting in a program reset.


The main function code is attached:


void main(void)

{

  long ADC_Value=0,ADC_Value_1=0;

  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); // CPU frequency @ 1MHz

  GPIO_Init(GPIOC, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Slow); //Relay (power supply)

  GPIO_SetBits(GPIOC,GPIO_Pin_4); //Turn on the power relay

 /*************Unused IO ports are set to low speed push-pull output low*******************/ 

[1] [2]
Reference address:Low power consumption application of STM8L051X series RTC

Previous article:STM8L ultra-low power programming tutorial, easy to understand
Next article:STM8L052C6 low power consumption + LCD display experience sharing

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号