AWU是用来当MCU进入低功耗的活跃停机(Active Halt)模式时提供一个内部的唤醒时间基准。该时间基准的时钟是由内部的低速RC振荡器时钟(LSI)或者通过预分频的HSE晶振时钟来提供的。STM8S的LSI时钟测量在使用LSI低速内部时钟时,为了确保最好的精度,它的频率可以通过TIM1_CH1来测定。
1、复制官方AWU工程文件:
2、打开IAR工程,先清除上一工程的编译信息:
3、修改main.c文件:- /* Includes ------------------------------------------------------------------*/
- #include "stm8s.h"
- #include "main.h"
- /**
- * @addtogroup AWU_ActiveHaltMode
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Global variables ----------------------------------------------------------*/
- bool ButtonPressed = FALSE;
- /* Private function prototypes -----------------------------------------------*/
- void Delay (uint16_t nCount);
- uint32_t LSIMeasurment(void);
- static void CLK_Config(void);
- void AWU_Config(void);
- static void GPIO_Config(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Example main entry point.
- * @param None
- * @retval None
- */
- void main(void)
- {
- uint16_t i = 0;
- uint16_t j = 0;
- /* 时钟配置 -----------------------------------------*/
- CLK_Config();
- /* 端口配置 ---------------------------------------*/
- GPIO_Config();
- /*AWU配置 --------------------------------------------*/
- AWU_Config();
- /* 使能总中断 */
- enableInterrupts();
- while (1)
- {
- /* 按键扫描 */
- if (ButtonPressed == TRUE)
- {
- ButtonPressed = FALSE;
- /* LED快速闪烁 */
- for (j = 0; j < 20; j++)
- {
- GPIO_WriteReverse(LEDS_PORT, (GPIO_Pin_TypeDef)LED1_PIN);
- for (i = 0; i < 5; i++)
- {
- Delay((uint16_t)60000);
- }
- }
- /* LED熄灭 */
- GPIO_WriteLow(LEDS_PORT,LED1_PIN);
- /* 进入停机模式 */
- halt();
- }
- else
- {
- /* LED慢速闪烁 */
- for (i = 0; i < 15; i++)
- {
- Delay((uint16_t)60000);
- }
- /* LED反转 */
- GPIO_WriteReverse(LEDS_PORT, (GPIO_Pin_TypeDef)LED1_PIN);
- }
- }
- }
- /**
- * @brief Configure system clock to run at 16Mhz
- * @param None
- * @retval None
- */
- static void CLK_Config(void)
- {
- /* Initialization of the clock */
- /* 配置HSI分频率,CPU分频率1 */
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- }
- /**
- * @brief Configure the AWU time base to 12s
- * @param None
- * @retval None
- */
- void AWU_Config(void)
- {
- /* 初始化 AWU */
- /* 通过LSI 校准AWU唤醒时间*/
- AWU_LSICalibrationConfig(LSIMeasurment());
- /* 停机12S后唤醒 */
- AWU_Init(AWU_TIMEBASE_12S);
- }
- /**
- * @brief Configure GPIO for LEDs and buttons available on the evaluation board
- * @param None
- * @retval None
- */
- static void GPIO_Config(void)
- {
- /* 设置外部中断端口C模式为下降沿触发 */
- EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
- /* 初始化外部中断IO口 ,悬空输入并使能中断*/
- GPIO_Init(BUTTON_PORT, (GPIO_Pin_TypeDef)(BUTTON_PIN), GPIO_MODE_IN_FL_IT);
- /* 配置LED1 IO口为输出模式 */
- GPIO_Init(LEDS_PORT, (GPIO_Pin_TypeDef)(LED1_PIN), GPIO_MODE_OUT_PP_HIGH_FAST);
- }
- /**
- * @brief Measure the LSI frequency using timer IC1 and update the calibration registers.
- * @NOTE It is recommended to use a timer clock frequency of at least 10MHz in order
- * to obtain a better in the LSI frequency measurement.
- * @param None
- * @retval None
- */
- uint32_t LSIMeasurment(void)
- {
- uint32_t lsi_freq_hz = 0x0;
- uint32_t fmaster = 0x0;
- uint16_t ICValue1 = 0x0;
- uint16_t ICValue2 = 0x0;
- /* 读取主时钟频率 */
- fmaster = CLK_GetClockFreq();
- /* 使能 LSI 测量: LSI时钟连接到TIM1_CH1 */
- AWU->CSR |= AWU_CSR_MSR;
- /* 每8次捕捉一次!!! */
- /* 使能TIM1捕捉 */
- TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0);
- /* 使能 TIM1 */
- TIM1_Cmd(ENABLE);
- /* 等待 CC1 捕捉 */
- while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
- /* 读取 CCR1 值*/
- ICValue1 = TIM1_GetCapture1();
- TIM1_ClearFlag(TIM1_FLAG_CC1);
- /* 等待 CC1 捕捉 */
- while ((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
- /* 读取 CCR1 值*/
- ICValue2 = TIM1_GetCapture1();
- TIM1_ClearFlag(TIM1_FLAG_CC1);
- /* 关闭CC1捕捉 */
- TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1E);
- /* 关闭TIM1 */
- TIM1_Cmd(DISABLE);
- /* 计算LSI时钟频率 */
- lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1);
- /* 禁用LSI测量: LSI时钟从TIM1_CH1断开 */
- AWU->CSR &= (uint8_t)(~AWU_CSR_MSR);
- /* 返回计算的LSI值 */
- return (lsi_freq_hz);
- }
- /**
- * @brief Delay.
- * @param nCount
- * @retval None
- */
- void Delay(uint16_t nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
4、编译运行:
write by zhdzhd-174422 [ST论坛]
|