Skip to content
ngcixPublic

About

STM8-based Industrial Module: NTC Temperature Sensing, 7-Segment Display, and Modbus RTU Communication with PLC

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌡️ STM8 Temperature Controller

A temperature controller based on STM8S105K4 — reads temperature via NTC thermistor, controls Heater/Cooler relays, displays on 4-digit 7-segment LED, and communicates with PLC via Modbus RTU/ASCII.


📦 Key Features

Feature Description
Temperature measurement NTC thermistor via 10-bit ADC, Steinhart-Hart equation
Temperature control Heater / Cooler relays with hysteresis (delta)
Display 4-digit 7-segment LED (multiplexed)
PLC Communication Modbus RTU / ASCII over UART2 (9600 baud)
Persistent config Internal EEPROM (1 KB)
NTC calibration Recalibrate coefficients A, B, C based on reference temperature

🧩 Software Architecture

┌─────────────────────────────────────────────────┐
│                    main.c                        │
│  ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │
│  │  ADC/NTC │ │ 7-Seg UI │ │ Button Handler   │ │
│  │ measure  │ │ display  │ │ BT1: Power/Reset │ │
│  │ & calc°C │ │ temp     │ │ BT2: Setting     │ │
│  └──────────┘ └──────────┘ │ BT3: Up   (▲)    │ │
│  ┌──────────┐ ┌──────────┐ │ BT4: Down (▼)    │ │
│  │  EEPROM  │ │ Timers   │ └──────────────────┘ │
│  │ save/load│ │ T1: LED  │                      │
│  │ config   │ │ T2: Debounce                     │
│  └──────────┘ │ T3: 1s   │ ┌──────────────────┐ │
│               │ T4: Modbus│ │   modbus.c       │ │
│               └──────────┘ │ RTU: 03/06/10    │ │
│                            │ ASCII: 03/10     │ │
│                            │ CRC16 / LRC      │ │
│                            └──────────────────┘ │
└─────────────────────────────────────────────────┘

⚙️ MCU Specs — STM8S105K4

Spec Value
Clock 8 MHz (HSE external)
Flash 16 KB
RAM 2 KB
ADC 10-bit, 9 channels
EEPROM 1 KB
Interfaces UART, I²C, SPI
Voltage 2.95 – 5.5 V

🔌 Pin Map

Pin         Function            Pin         Function
────────────────────────      ────────────────────────
PA1         OSC IN             PC1         LED digit 4
PA2         OSC OUT            PC2         Seg G
PB0         Button 1 (Power)   PC3         Seg C
PB1         Button 2 (Setting) PC4         Seg DP
PB2         Button 3 (Up ▲)    PC5         Seg D
PB3         Button 4 (Down ▼)  PC6         Seg E
PB4         ADC IN4 (NTC)      PC7         Seg B
PB5         Relay Heat         PD0         LED digit 3
PD1         SWIM (Debug)       PD2         LED digit 2
PD5         UART2 TX           PD3         Seg F
PD6         UART2 RX           PD4         Seg A
PF4         Relay Cool         PD7         LED digit 1

🎮 Button Guide

Button Short Press Long Press
BT1 (Power) Exit setting / Reset show mode Toggle display on/off (sleep)
BT2 (Setting) Enter setting mode Hold BT2 + ▲/▼ = select Fn → release = edit Fn
BT3 (▲) Increase value / Switch mode —
BT4 (▼) Decrease value / Switch mode —

Factory reset: Long press BT1 → reset all defaults (T=20°C, delta=3, addr=1, sample=3)

Display Mode (show_mode)

Mode Display Note
0 Current temperature (°C) Default
1 Modbus slave address Prefix A
2 Delta (hysteresis) Prefix d

Setting Mode (setting_mode)

Mode Setting Prefix
1 Modbus slave address (1–99) A
2 Delta / hysteresis (0–99) d
3 Target temperature (setting_T) —
4 Sample time in minutes (1–99) S
5 Buffer_T — calibration offset —

Auto-exits setting after 15 seconds of inactivity.


📡 Modbus (PLC Communication)

  • Baud: 9600, 8N1
  • Used to communicate with PLC
Code Function Description
0x03 Read Holding Registers Read data from EEPROM
0x06 Write Single Register Write 1 register to EEPROM
0x10 Write Multiple Registers Write multiple registers to EEPROM

💾 EEPROM Map

Address Size Content
0x00 1 byte Modbus slave address
0x01 1 byte Delta (hysteresis)
0x02–0x05 4 bytes Setting temperature (float)
0x06 1 byte Relay heat state
0x07 1 byte Relay cool state
0x08 1 byte Sample time (minutes)
0x09–0x0C 4 bytes Buffer temperature (float)
0x0D–0x2D 4×7 bytes NTC coefficients: A, B, C, L1, L2, L3, Y1, Y2, Y3

📁 File Structure

STM8/
├── main.c      # Main program: ADC, LED, buttons, timers, EEPROM, NTC
├── main.h      # MCU declarations, pin map, constants, prototypes
├── modbus.c    # Modbus RTU/ASCII handler, CRC16, LRC
└── modbus.h    # Function code definitions, Modbus prototypes

About

STM8-based Industrial Module: NTC Temperature Sensing, 7-Segment Display, and Modbus RTU Communication with PLC

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages