1. Overall System Design Scheme
This design uses the STM32F103C8T6 as the control core, integrating real-time clock, temperature and humidity detection, human-computer interaction, and display functions to build a fully functional smart calendar system. The system adopts a modular design philosophy, divided into main control module, display module, time management module, environmental monitoring module, and user interaction module, with each module communicating through standard interfaces to ensure system stability and scalability.
1.1 Hardware System Architecture
The system hardware consists of the following core modules:
- Main Control Module: Minimum system of STM32F103C8T6, operating at 72MHz, with 64KB Flash and 20KB SRAM built-in.
- Display Module: 128×64 resolution OLED display, using SPI interface for high-speed data transmission.
- Time Management Module: Built-in RTC of STM32 + backup battery, supporting power-off retention function.
- Environmental Monitoring Module: DS18B20 temperature sensor, designed with a single bus interface.
- User Interaction Module: 3 independent buttons, supporting time adjustment and mode switching functions.
- Power Module: AMS1117-3.3 linear voltage regulator, providing stable 3.3V output.
For the hardware principle diagram of the article system, please follow my public account.
Contact me via private message to receive it; the schematic shows the connection relationship between STM32 and various peripheral modules.
1.2 Software Function Architecture
The software system adopts a layered design, including:
- Driver Layer: Hardware abstraction layer, implementing peripheral initialization and control.
- Function Layer: Core algorithms for time calculation, temperature acquisition, button processing, etc.
- Application Layer: Human-computer interaction logic and display control.
Main functions include:
- Real-time time display (Year/Month/Day/Hour/Minute/Second/Week)
- Environmental temperature monitoring (-55℃~125℃, accuracy ±0.5℃)
- Multiple alarm settings and buzzer alerts
- Display of Gregorian/Lunar calendar conversion
- Support for low-power mode to extend battery life
2. Detailed Hardware Circuit Design
2.1 Main Control Module Circuit
The circuit design of the minimum system for STM32F103C8T6 is as follows:
- Power Circuit: Uses AMS1117-3.3 voltage regulator, converting 5V input to 3.3V output, with a 10μF electrolytic capacitor and a 0.1μF ceramic capacitor for filtering at the input.
- Reset Circuit: RC reset circuit, reset button connected to PA0 pin, with an external pull-down resistor of 10kΩ.
- Crystal Oscillator Circuit: 8MHz external crystal oscillator, matched with 18pF load capacitors; 32.768kHz RTC crystal oscillator to ensure time accuracy.
- Boot Configuration: BOOT0 grounded, BOOT1 floating, default boot from Flash.
Key component selection parameters:
| Component Type | Model | Parameter Description |
|---|---|---|
| Voltage Regulator | AMS1117-3.3 | Output 3.3V/1A, TO-220 package |
| Crystal Oscillator | 8MHz | HC-49S package, ±20ppm, 18pF load |
| Decoupling Capacitor | 0.1μF | X7R material, 0805 package |
2.2 Display Module Interface Circuit
Using a 12864 OLED display, connected via SPI interface:
- Pin Definitions:
- SCK – PA5 (SPI_SCK)
- SDA – PA7 (SPI_MOSI)
- DC – PA2 (Data/Command Select)
- RST – PA3 (Reset)
- CS – PA4 (Chip Select)
Circuit design points:
- All control signals are in series with a 100Ω current-limiting resistor to enhance anti-interference capability.
- Decoupling capacitors of 0.1μF are paralleled at the power supply end to reduce power noise.
- Using 3.3V logic level, directly compatible with STM32.
2.3 Temperature Sensor Interface Circuit
The DS18B20 temperature sensor uses a single bus interface:
- Circuit Design:
- DQ pin connected to PA1, with a 4.7kΩ pull-up resistor to 3.3V.
- Using external power mode, VCC connected to 3.3V, GND grounded.
- 100pF capacitor is paralleled on the data line to filter out high-frequency interference.
Timing parameters guarantee:
- Reset pulse: Low level ≥ 480μs
- Response pulse: 15~60μs detection window
- Temperature conversion time: Maximum 750ms (12-bit precision)
2.4 Button and Buzzer Circuit
Button Circuit:
- 3 independent buttons connected to PB0 (mode switch), PB1 (increase), PB2 (decrease).
- Using pull-up input mode, the pin is low when the button is pressed.
- Hardware debounce: Each button is paralleled with a 100nF capacitor.
Buzzer Circuit:
- Passive buzzer driven by transistor 8050.
- Control signal connected to PB12, triggered by high level.
- Current-limiting resistor of 1kΩ to protect GPIO pin.
3. Software System Implementation
3.1 Development Environment and Project Configuration
- Development Tools: Keil uVision5 + STM32CubeMX
- Library Version: STM32 Standard Peripheral Library v3.5.0
- Clock Configuration:
- SYSCLK = 72MHz (PLL multiplier 9 times, HSE=8MHz)
- HCLK = 72MHz, PCLK2=72MHz, PCLK1=36MHz
- RTC clock: LSE=32.768kHz, division factor 32768, counting frequency 1Hz
3.2 Core Function Module Code Implementation
3.2.1 RTC Real-Time Clock Module
RTC initialization function:
void RTC_Init(void)
{ // Enable PWR and BKP clocks RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); PWR_BackupAccessCmd(ENABLE); // Allow access to backup registers // Check if first configuration if (BKP_ReadBackupRegister(BKP_DR1) != 0x5A5A)
{ RCC_LSEConfig(RCC_LSE_ON); // Enable LSE while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // Wait for LSE to stabilize RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select LSE as RTC clock RCC_RTCCLKCmd(ENABLE); // Enable RTC clock RTC_WaitForLastTask(); // Wait for RTC register synchronization RTC_WaitForSynchro(); // Wait for RTC APB interface synchronization RTC_SetPrescaler(32767); // Set division factor, 32768Hz->1Hz RTC_WaitForLastTask(); // Set initial time 2025-11-24 20:00:00 RTC_SetTime(20, 00, 00); RTC_SetDate(24, 11, 2025, 1); // 1 indicates Monday BKP_WriteBackupRegister(BKP_DR1, 0x5A5A); // Write flag } else { RTC_WaitForSynchro(); // Wait for synchronization }}
3.2.2 Temperature Acquisition Module
DS18B20 driver code:
// Read temperature datafloat DS18B20_ReadTemp(void)
{ uint8_t temp_h, temp_l; int16_t temp; DS18B20_Init(); DS18B20_WriteByte(0xCC); // Skip ROM command DS18B20_WriteByte(0x44); // Start temperature conversion Delay_Ms(750); // Wait for conversion to complete DS18B20_Init(); DS18B20_WriteByte(0xCC); // Skip ROM command DS18B20_WriteByte(0xBE); // Read scratchpad temp_l = DS18B20_ReadByte(); // Temperature low byte temp_h = DS18B20_ReadByte(); // Temperature high byte temp = (temp_h << 8) | temp_l; // Calculate temperature value (12-bit precision) return (float)temp / 16.0;}
3.2.3 OLED Display Module
Character display function:
// Display string at specified positionvoid OLED_ShowString(uint8_t x, uint8_t y, uint8_t *str)
{ while (*str)
{ OLED_ShowChar(x, y, *str); x += 8; if (x > 120) { // Automatically wrap to next line if exceeding screen width x = 0; y += 2; } str++; }}// Update time displayvoid OLED_UpdateTime(RTC_TimeTypeDef *time, RTC_DateTypeDef *date)
{ char buf[32]; // Display date sprintf(buf, “%04d-%02d-%02d”, 2000 + date->RTC_Year, date->RTC_Month, date->RTC_Date); OLED_ShowString(0, 0, buf); // Display time sprintf(buf, “%02d:%02d:%02d”, time->RTC_Hours, time->RTC_Minutes, time->RTC_Seconds); OLED_ShowString(0, 2, buf); // Display week const char *weekdays[] = {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”}; OLED_ShowString(96, 0, (uint8_t *)weekdays[date->RTC_WeekDay – 1]);}
3.2.4 Button Processing and Time Adjustment
Using external interrupt to handle buttons:
// Button interrupt service routinevoid EXTI0_IRQHandler(void)
{ if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{ Delay_Ms(10); // Debounce processing if (KEY_SCAN() == KEY_UP) { // Confirm button pressed g_key_state = KEY_STATE_ADJ_YEAR; // Enter year adjustment mode g_adj_flag = 1; // Set adjustment flag } EXTI_ClearITPendingBit(EXTI_Line0); // Clear interrupt flag }}// Time adjustment processing functionvoid Time_AdjustProcess(void)
{ if (g_adj_flag) { switch (g_key_state) { case KEY_STATE_ADJ_YEAR: // Year adjustment logic if (KEY_ADD_PRESS()) { g_date.RTC_Year = (g_date.RTC_Year + 1) % 100; RTC_SetDate(RTC_Format_BIN, &g_date); } if (KEY_SUB_PRESS()) { g_date.RTC_Year = (g_date.RTC_Year – 1 + 100) % 100; RTC_SetDate(RTC_Format_BIN, &g_date); } // Blink display year OLED_BlinkYear(g_date.RTC_Year); break; // Other adjustment state processing… } }}
3.3 Main Function and Task Scheduling
Main function implementation:
Cint main(void)
{ // Initialize peripherals SystemInit(); Delay_Init(); OLED_Init(); KEY_Init(); RTC_Init(); DS18B20_Init(); BEEP_Init(); // Clear screen and display startup information OLED_Clear(); OLED_ShowString(32, 2, “WELCOME”); Delay_Ms(1000); OLED_Clear(); // Main loop while (1)
{ // Read time and temperature RTC_GetTime(RTC_Format_BIN, &g_time); RTC_GetDate(RTC_Format_BIN, &g_date); temp = DS18B20_ReadTemp(); // Display update OLED_UpdateTime(&g_time, &g_date); OLED_ShowTemp(temp); // Button processing Time_AdjustProcess(); // Alarm check Alarm_Check(); // Low power processing if (g_idle_count++ > 30)
{
// Enter sleep mode after 30 seconds of inactivity Enter_StandbyMode(); g_idle_count = 0; } Delay_Ms(100); // Main loop delay 100ms }}
4. System Integration and Testing
4.1 Hardware Assembly and Debugging
Key points for hardware debugging:
- Power Check: Measure the supply voltage of each module to ensure stable 3.3V output.
- Communication Test: Use an oscilloscope to check SPI communication waveforms to ensure normal data transmission (frequency about 2MHz).
- Timing Verification: DS18B20 initialization pulse width ≥ 480μs to ensure the sensor responds correctly.
- Low Power Test: Current in sleep mode should be less than 50μA, and ≤1μA when RTC is powered separately.
4.2 Software Function Testing
Test results of each functional module:
- Time Accuracy: 24-hour error ≤ 2 seconds (LSE crystal ±20ppm@25℃).
- Temperature Detection: Error ≤ ±0.3℃ compared to standard thermometer.
- Button Response: No false triggers after debounce processing, response time < 100ms.
- Display Effect: OLED contrast adjustable, viewing angle ≥ 160°, refresh frequency 10Hz.
4.3 System Performance Indicators
| Item | Indicator Parameter |
|---|---|
| Operating Voltage | DC 5V (allowable range 4.5V~5.5V) |
| Operating Current | Normal mode ≤ 80mA, sleep mode ≤ 50μA |
| Display Resolution | 128×64 pixels, blue background with white text |
| Temperature Measurement Range | -55℃~125℃, accuracy ±0.5℃ |
| Time Display Format | 24-hour format, Year/Month/Day/Hour/Minute/Second/Week |
| Number of Alarms | Supports 3 independent alarms |
5. Summary and Outlook
This design implements a fully functional multifunctional electronic calendar based on STM32F103C8T6. Through reasonable hardware selection and software optimization, it achieves core functions such as real-time time display, temperature monitoring, and alarm reminders. The system adopts a low-power design strategy, balancing performance and power consumption needs, and can be widely used in home and office scenarios.
Future expansion directions include:
- Adding a WiFi module to achieve network time synchronization.
- Integrating air quality detection sensors.
- Developing mobile app interconnectivity features.
- Incorporating voice control and reporting functions.
Through this project practice, you can gain in-depth mastery of STM32 peripheral development.