rtc-reveil.c

STM32F103 "Blue Pill" (STM32F103C8T6) · Archivo : rtc-reveil.c · Lenguaje : C · Uso : Réveil périodique avec la RTC et mode veille bas courant

La RTC mantiene la hora aunque se corte la alimentación (pila de botón o supercondensador de la placa): este programa imprime el reloj continuamente y lanza un despertador cada 10 s, base de las estaciones de despertador periódico de bajo consumo.

Hardware

Una Blue Pill STM32F103C8T6 (Cortex-M3 @ 72 MHz, 64 KB flash) alimentada por micro-USB o 3,3 V externo; programacion SWD o bootloader serie. Opcional: pila de botón o supercondensador para mantener el reloj sin alimentación.

Cableado

Sin cableado. Activa la RTC en CubeMX (fuente LSE de 32,768 kHz si hay cristal, si no LSI) y ajusta fecha/hora iniciales.

Funcionamiento

HAL_RTC_GetTime/GetDate leen los contadores; el orden GetTime y luego GetDate lo exige el bloqueo interno de la HAL. El despertador también puede usar el WakeUp timer para salir de Stop.

Ejecución

Proyecto STM32CubeIDE (STM32F103C8) o Arduino (core STM32): programar por SWD (ST-LINK, adaptador DAPLink) o por el bootloader serie (BOOT0 a 1). Atencion: el LED PC13 es activo LOW.

Variantes

Pasa a modo Stop con despertador WakeUp timer (corriente dividida por ~100), o sincroniza la hora por UART (protocolo tipo NMEA).

rtc-reveil.c

// RTC : affichage de l'heure + reveveil toutes les 10 s.
#include "main.h"
#include <stdio.h>

extern RTC_HandleTypeDef hrtc;
extern UART_HandleTypeDef huart2;

int __io_putchar(int ch) {
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

RTC_TimeTypeDef t;
RTC_DateTypeDef d;
uint32_t dernierReveil = 0;

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_RTC_Init();   // horloge LSE/Lsi + date/heure initiales dans CubeMX

  for (;;) {
    HAL_RTC_GetTime(&hrtc, &t, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &d, RTC_FORMAT_BIN);
    // NB: GetDate apres GetTime (obligation HAL pour le lock).

    printf("%02u:%02u:%02u  %02u/%02u/%02u\r\n",
           t.Hours, t.Minutes, t.Seconds, d.Date, d.Month, d.Year);

    // reveveil logiciel simple (ou RTC_Alarm / WakeUp timer)
    if (HAL_GetTick() - dernierReveil >= 10000) {
      dernierReveil = HAL_GetTick();
      printf("--- REVEIL ---\r\n");
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
    }
    HAL_Delay(1000);
  }
}

← Volver a la ficha