# 🏭 STM32 Modbus RTU Master Implementation (Keil MDK)

This project features a **Modbus RTU Master** stack developed for **STM32** microcontrollers. It is designed to interface with industrial hardware such as PLCs, HMIs, and sensors over the **RS485** physical layer.

---

## ⚠️ Disclaimer & Current Status

> **Important:** This project is currently in the **development/testing phase**. The code may contain bugs or logical errors. If you plan to use this in a production environment or connect it to expensive industrial hardware, please:
> 1. Review the CRC-16 calculation and UART timing.
> 2. Test the communication with a Modbus Simulator (e.g., Modbus Poll) before connecting to real slaves.
> 3. Verify the RS485 direction control logic for your specific transceiver.
> 
> *Use at your own risk.*

---

## 🚀 Key Features
* **Standard:** Modbus RTU (Binary) over Serial.
* **Master Functionality:** Full control over read/write cycles to slave devices.
* **Supported Function Codes:**
    * `03 (0x03)` Read Holding Registers
    * `06 (0x06)` Write Single Register
    * `16 (0x10)` Write Multiple Registers
* **CRC-16 Calculation:** Software-based Cyclic Redundancy Check for data integrity.
* **Half-Duplex Management:** Automatic RS485 direction control (DE/RE pins).

---

## 💻 Development Environment & Toolchain

* **IDE:** **Keil MDK (uVision) v5.x** – Used for core firmware development and ARM compiler optimization.
* **Configuration:** **STM32CubeMX** – Utilized for clock tree setup and peripheral (UART/GPIO) initialization.
* **Analysis Tool:** Initial tests were performed using **Hercules Setup Utility** and **Modbus Poll**.

---

## 🔌 Hardware Setup (RS485)

| STM32 Pin | Function | Connection |
| :--- | :--- | :--- |
| **TX (PA2)** | UART Transmit | Connected to DI (Driver Input) |
| **RX (PA3)** | UART Receive | Connected to RO (Receiver Output) |
| **DE/RE (PA1)** | Direction Control | High for TX / Low for RX |
| **GND** | Reference | Common Ground with Slave |

---

## 📂 Project Structure
- `/MDK-ARM`: Keil uVision project files and startup code.
- `/Core/Src`: Modbus protocol logic, CRC algorithms, and UART handlers.
- `/Core/Inc`: Header files and register definitions.
- `README.md`: Project documentation.

---
**Developer:** **Onur Bakır** **Hardware:** STM32 Series (Cortex-M)  
**IDEs:** Keil MDK & STM32CubeMX
