cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030F4P6 - PWM output voltage

boulganamed
Associate

I am working on a project to replace a legacy ST62 IC in a industrial charger 24V/45A charger with an STM32 microcontroller (STM32F0 series). The main issue is with the PWM signal generation using TIM1 and TIM3. Both timers have 16-bit prescalers (value 0 to 65535), and I need to generate a PWM at 1 kHz with about 1000 steps of resolution.

My timer clock is 8 MHz. Based on calculations, prescaler and ARR values can generate correct frequencies, but the PWM output voltage is only around 2.7V instead of the expected 5V, and there is no effective voltage output for charging.

I suspect this issue is linked to the STM32CubeMX pinout or timer configuration:

  • Correct PA6 pin configuration for TIM3 PWM output.

  • Proper timer frequency setup: prescaler around 725 with ARR 0 tested, but PWM not as expected.

  • Possible problem in software initialization or timer start sequence affecting PWM output.

  • Relay control pins and PWM outputs may not be correctly activated or sequenced.

I have tested a new STM32 board without programming, same issue persists, suggesting it’s not hardware damage but configuration.

Would appreciate help reviewing timer config, pin assignments, and software steps to properly initialize and start PWM on TIM1/TIM3 for reliable voltage output.

26 REPLIES 26

@Andrew Neil @Ozone  @TDK   thanks guys about your responds 

and this an introduction about my project: 

STM32F030F4 24V/45A Battery Charger Controller - Project Overview

Executive Summary

This project involves developing a modern microcontroller-based control system for a SHARP industrial battery charger, replacing an obsolete ST62 microcontroller with a contemporary STM32F030F4 solution. The system manages a high-power 24V/45A battery charging process with comprehensive safety features and intelligent regulation.

Problem Statement

The original charger uses an outdated ST62T20CB microcontroller that is no longer manufactured, making maintenance and repairs increasingly difficult. A modern replacement was needed that could:

  • Maintain compatibility with existing power electronics
  • Improve reliability and safety features
  • Enable future upgrades and diagnostics
  • Reduce component costs

Solution Approach

Complete redesign of the control board using the STM32F030F4P6 microcontroller, maintaining electrical compatibility with the original power stage while adding modern safety features and improved control algorithms.

Technical Overview

Core Functionality

The controller manages a high-power battery charging system through:

  • Intelligent Power Regulation: PWM-based control of charging current and voltage
  • Multi-Stage Charging Process: Soft-start sequence followed by regulated charging
  • Comprehensive Monitoring: Continuous measurement of voltage, current, temperature, and mains power
  • Safety Systems: Multiple protection mechanisms against overcurrent, overvoltage, overheating, and reverse polarity

Key Features

  1. Adaptive Charging Control
    • Automatic adjustment of charging parameters based on battery state
    • Soft-start capability to prevent inrush current
    • PWM regulation from 10% to 90% duty cycle
  2. Safety Mechanisms
    • Temperature monitoring with automatic shutdown
    • Current limiting to 45A maximum
    • Voltage range protection (24V-30V)
    • Reverse polarity detection
    • Emergency shutdown capability
  3. User Interface
    • Three-LED status indication (Red/Green/Orange)
    • Manual reset button
    • Visual feedback for all operating states
  4. Power Management
    • Dual relay control (soft-start and main power)
    • Active cooling with fan control
    • Efficient PWM switching at 1kHz

System Architecture

Hardware Platform

  • Microcontroller: STM32F030F4P6 (ARM Cortex-M0, 16KB Flash, 4KB RAM)
  • Operating Voltage: 3.3V (regulated from 5V supply)
  • Package: TSSOP-20 (space-efficient design)
  • Clock: 8MHz internal oscillator (no external crystal required)

Control Logic

The system operates through a state machine with four primary states:

  1. Standby: System ready, monitoring for start conditions
  2. Soft-Start: Gradual power application to prevent damage
  3. Charging: Active regulation maintaining optimal charging parameters
  4. Error: Fault condition with protective shutdown

Monitoring Capabilities

  • Battery Voltage: Precise measurement for charge control
  • Charging Current: Real-time monitoring up to 45A
  • System Temperature: Thermal protection
  • Mains Voltage: Input power verification
  • Polarity Detection: Safety check before energizing

Project Status

Completed Milestones

  • :white_heavy_check_mark: Hardware design and pin mapping finalized
  • :white_heavy_check_mark: Software architecture implemented
  • :white_heavy_check_mark: State machine logic tested
  • :white_heavy_check_mark: ADC measurements operational
  • :white_heavy_check_mark: Safety systems integrated
  • :white_heavy_check_mark: Code compilation successful (97% Flash utilization)

Current Challenge

The system software is fully functional, but the charger is not producing output voltage. Investigation indicates this is likely a hardware issue rather than software, as:

  • PWM generation code is correct
  • Relay control logic is implemented
  • All safety checks are passing
  • ADC readings are accurate

Next Steps

  1. Hardware verification with oscilloscope
  2. Power stage circuit validation
  3. Component-level testing of relays and drivers
  4. Integration testing with actual battery load

Value Proposition

Benefits Over Original System

  • Modern Components: Uses readily available parts
  • Enhanced Safety: Multiple protection layers
  • Maintainability: Well-documented code structure
  • Expandability: Room for future features (communication, logging)
  • Cost Effective: Lower component cost than original

Future Enhancement Possibilities

  • Serial communication for diagnostics
  • Parameter storage in non-volatile memory
  • Remote monitoring capabilities
  • Advanced charging algorithms
  • Data logging for maintenance tracking

Technical Competencies Demonstrated

  • Embedded systems programming
  • Real-time control systems
  • Power electronics integration
  • Safety-critical system design
  • Hardware abstraction layer (HAL) utilization
  • Efficient resource management in constrained environments

Conclusion

This project successfully modernizes a critical industrial charging system, replacing obsolete technology with a contemporary solution that maintains backward compatibility while adding significant safety and functionality improvements. The software implementation is complete and validated, with final hardware integration as the remaining milestone for full deployment.

Missing from "Technical Competencies Demonstrated" appears to be an understanding that a 3.3V microcontroller is not going to give a 5V output!

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@Andrew Neil 

When the charger is powered by the 220V mains, I measured the output voltage of the original ST62 IC on pin 15 and found it to be 14V DC. After I replaced the IC with an STM32F030F4P6 and programmed it, I noticed that pin 15 now outputs only 12V DC. This is just one example; there are other pins with similar behavior

15V on an STM32 pin will damage it.

You should certainly not have 15V on any STM32 pin !!

You still haven't shown any schematics of your system.

We have no idea what this mystery "charger" is.

 

Again, please read this:  How to write your question to maximize your chances to find a solution

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

> When the charger is powered by the 220V mains, I measured the output voltage of the original ST62 IC on pin 15 and found it to be 14V DC. 

This is implausible either.

The ST62xx are 5V devices, the datasheet gives a maximum of 7V.
The old design must have contained a level-shifting logic, which could be quite simple.
But connecting voltages twice or more the maximum rating to a pin will kill every MCU.

boulganamed
Associate

@Ozone @Andrew Neil  @TDK 

First, I do not have the schematic of the charger. We have a 24V/45A charger that uses an ST62T20CB IC, but this circuit is damaged. After some research, I found that the ST62 is an old technology and there is no longer a compiler available to reprogram it. Then, I discovered that the STM32F030F4P6 microcontroller is a suitable equivalent to the ST62.

I took a working charger with the ST62 and made the following measurements:

  • Pin 8 - VSS = 14 V

  • Pin 12 - VSS = 2 V

These are examples of values I included in my program.

So, my problem is how to get an output voltage to charge the batteries, since the pin voltages I measured are the same as those with the ST62.

 

How To Use Transistors For Voltage Level Shifting

 

You will most likely need to modify the hardware. Or at the very least, understand what you currently have.

If you feel a post has answered your question, please click "Accept as Solution".
boulganamed
Associate

for more understanding project @Ozone @TDK @Andrew Neil : 

Industrial 24V/45A Battery Charger Controller Project

Complete Technical Documentation

Project Overview

Objective: Replace legacy ST62T20CB microcontroller with STM32F030F4P6 in Industrial 24V/45A battery charger system.

MCU Specifications:

  • STM32F030F4P6 (Cortex-M0, 32MHz max)
  • 16KB Flash Memory
  • 4KB SRAM
  • TSSOP-20 Package
  • 3.3V Operating Voltage

Pin Configuration & Voltage Details

Power Supply

Pin Function Voltage Current Notes

VDD (Pin 1)MCU Power3.3V ±5%~50mARegulated from 5V input
VSS (Pin 16)Ground0V-Common ground
VDDA (Pin 5)ADC Power3.3V~5mAClean analog supply
VSSA (Pin 4)ADC Ground0V-Analog ground

GPIO Output Pins (Digital Control)

Pin GPIO Function Voltage Levels Load Notes

14PB1LED Red0V/3.3V20mAError/charging indicator
20PA14LED Green0V/3.3V20mACharged/ready indicator
19PA13LED Orange0V/3.3V20mAStandby indicator
10PA4Relay Soft-Start0V/3.3VVia driverControls 24V relay coil
13PA7Relay Main0V/3.3VVia driverControls main power relay
11PA5Fan Control0V/3.3VVia driverCooling fan control

PWM Output

Pin GPIO Function Voltage Levels Frequency Duty Cycle Notes

12PA6PWM Regulation0V/3.3V1kHz10-90%TIM3_CH1 output

Analog Input Pins (ADC)

Pin GPIO ADC Channel Input Voltage Range Measured Parameter Scaling Factor

6PA0ADC_IN00-3.3VBattery Voltage24V → ~1.2V via divider
7PA1ADC_IN10-3.3VCharge Current45A → ~2.4V via shunt
8PA2ADC_IN20-3.3VTemperatureNTC → 0.5-2.5V
9PA3ADC_IN30-3.3VMains VoltageAC detection

Digital Input Pins

Pin GPIO Function Input Voltage Pull-up/Pull-down Notes

15PA8Reset Button0V/3.3VInternal pull-upManual reset input
17PA9Polarity Detection0V/3.3VInternal pull-upBattery polarity check

Programming/Debug Pins

Pin GPIO Function Voltage Levels Notes

18PA10SWDIO0V/3.3VSWD data line
2PF0SWCLK0V/3.3VSWD clock line
3PF1NRST0V/3.3VReset line

Voltage Level Analysis

ADC Voltage Scaling

12-bit ADC Resolution: 4096 steps for 0-3.3V range

  • 1 LSB = 3.3V / 4096 = 0.806mV

Battery Voltage Measurement (PA0)

  • Full Scale Battery: 30V maximum
  • Voltage Divider: R1=10kΩ, R2=1.5kΩ (approx 8:1 ratio)
  • ADC Input: 30V → 3.26V (near full scale)
  • 21V Battery: 21V → 2.28V → ADC value ~2830
  • 24V Battery: 24V → 2.61V → ADC value ~3240
  • 27V Charged: 27V → 2.94V → ADC value ~3650

Current Measurement (PA1)

  • Shunt Resistor: 0.1mΩ (estimated)
  • Operational Amplifier: Gain = 50x
  • 45A Current: 45A × 0.1mΩ × 50 = 2.25V → ADC value ~2790
  • Safety Threshold: 2.4V → ADC value 2976

Temperature Measurement (PA2)

  • NTC Thermistor: 10kΩ @ 25°C
  • Voltage Divider: With 10kΩ fixed resistor
  • 25°C: ~1.65V → ADC value ~2048
  • 60°C: ~1.2V → ADC value ~1489
  • 80°C Critical: ~0.9V → ADC value ~1117

Mains Detection (PA3)

  • AC Input: 220V RMS rectified and divided
  • Detection Level: >1V indicates mains present
  • ADC Threshold: >1240 (corresponds to ~1V)

PWM Output Voltage (PA6)

  • Logic High: 3.3V
  • Logic Low: 0V
  • Frequency: 1kHz (Period = 1ms)
  • Resolution: 1000 steps (0.1% precision)
  • Drive Capability: 25mA maximum (requires external driver for power circuits)

State Machine & Control Logic

System States

  1. STATE_STANDBY: Orange LED blinking, all outputs OFF
  2. STATE_SOFT_START: Red LED ON, soft-start relay active, low PWM
  3. STATE_CHARGING: Red LED (charging) or Green LED (charged), main relay active
  4. STATE_ERROR: Red LED blinking, emergency shutdown

Charging Algorithm

Battery Voltage (ADC) | PWM Duty | LED Status | Action
---------------------|----------|------------|--------
< 1200 (< ~26V)      | 10-75%   | Red ON     | Active charging
≥ 1200 (≥ ~26V)      | 30-35%   | Green ON   | Maintenance mode
> 3650 (> 27V)       | Reduced  | Green ON   | Float charging

Safety Thresholds

  • Overcurrent: ADC > 2976 (~45A)
  • Overtemperature: ADC < 1117 (~80°C)
  • Overvoltage: ADC > 4000 (~30V)
  • Undervoltage: ADC < 500 (~15V)

Hardware Interface Requirements

Relay Driver Circuit

  • Input: 3.3V logic from MCU
  • Output: 24V relay coil drive
  • Component: ULN2003 or similar Darlington array
  • Protection: Flyback diode for inductive load

PWM to Analog Converter

  • Input: 3.3V PWM from PA6
  • Output: 0-5V analog control signal
  • Filter: RC low-pass filter (R=1kΩ, C=1μF)
  • Buffer: Op-amp voltage follower for drive capability

Current Sensing

  • Shunt: 0.1mΩ, 50W power rating
  • Amplifier: Instrumentation amplifier (INA219 or similar)
  • Gain: 50x to scale 45A → 2.25V
  • Filtering: 100nF ceramic + 10μF electrolytic

Temperature Sensing

  • Sensor: NTC 10kΩ thermistor on heatsink
  • Divider: 10kΩ fixed resistor to 3.3V
  • Response: Non-linear, requires lookup table or polynomial

Power Supply Design

Input Power

  • Primary: 220V AC mains input
  • Transformer: 220V → 26V CT, 200VA minimum
  • Rectification: Bridge rectifier with 10,000μF filter

Control Supply

  • Regulation: 26V → 5V switching regulator (LM2596)
  • MCU Supply: 5V → 3.3V LDO regulator (AMS1117-3.3)
  • Isolation: Optocouplers for mains sensing

Load Characteristics

  • Battery: 24V nominal, 80Ah capacity
  • Charge Current: 0-45A continuously variable
  • Power Output: Up to 1080W (24V × 45A)

Software Architecture

Memory Usage

  • Flash: 12,148 bytes / 16,384 bytes (74% utilized)
  • RAM: 1,736 bytes / 4,096 bytes (42% utilized)
  • Stack: 1024 bytes reserved
  • Heap: Minimal (no dynamic allocation)

Timer Configuration

  • TIM3: PWM generation, 1kHz output frequency
  • SysTick: 1ms timebase for HAL_Delay() and timing

ADC Configuration

  • Resolution: 12-bit (4096 steps)
  • Sampling: 239.5 cycles per conversion
  • Conversion Time: ~20μs per channel at 8MHz clock
  • Trigger: Software start, polled conversion

Communication Interfaces

  • SWD: Programming and debug interface
  • GPIO: All user interfaces via discrete I/O
  • Future: UART can be added on PA2/PA3 for diagnostics

Calibration & Testing Procedures

ADC Calibration

  1. Battery Voltage: Apply known voltages 20V, 24V, 28V
  2. Current: Use calibrated current source 10A, 20A, 45A
  3. Temperature: Heat sink to 25°C, 50°C, 75°C
  4. Record ADC values and create scaling factors

PWM Verification

  1. Oscilloscope: Verify 1kHz frequency, 0-3.3V levels
  2. Duty Cycle: Test 10%, 50%, 90% settings
  3. Linearity: Ensure proportional analog output after filtering

System Integration Test

  1. Connect 21V battery: Should show Red LED (charging)
  2. Monitor current: Should ramp up with PWM increase
  3. Temperature rise: Fan should activate, temp monitoring
  4. Full charge: LED should change to Green at ~26-27V

Safety Testing

  1. Overcurrent: Inject >45A signal, verify shutdown
  2. Overtemp: Heat NTC >80°C, verify protection
  3. Overvoltage: Apply >30V, verify PWM reduction
  4. Polarity: Reverse battery, verify no start

Troubleshooting Guide

No MCU Response

  • Check 3.3V supply voltage
  • Verify SWD connections
  • Test with minimal blink program
  • Check reset circuit (NRST)

Incorrect ADC Readings

  • Verify voltage divider ratios
  • Check ADC reference voltage (VDDA)
  • Calibrate with known input voltages
  • Inspect for noise coupling

PWM Not Working

  • Verify GPIO alternate function
  • Check TIM3 clock enable
  • Measure with oscilloscope
  • Confirm pin configuration

Charging Issues

  • Verify relay operation (24V coil drive)
  • Check power stage response to PWM
  • Monitor battery voltage scaling
  • Validate current measurement circuit

Component Specifications

Critical Components

  • MCU: STM32F030F4P6TR (TSSOP-20)
  • Regulator: AMS1117-3.3 (SOT-223)
  • Relay Driver: ULN2003A (DIP-16)
  • Current Sensor: INA219 (SOT-23-8)
  • Shunt: 0.1mΩ ±1%, 50W
  • NTC: NTCLE100E3103JB0 (10kΩ ±5%)

PCB Requirements

  • Layer Count: 4 layers minimum
  • Copper Weight: 2oz minimum for power traces
  • Via Size: 0.2mm minimum for high current
  • Spacing: 5mm minimum for 24V isolation
  • Ground Plane: Separate analog/digital grounds

Performance Specifications

Electrical Specifications

  • Input Voltage: 220V AC ±10%
  • Output Voltage: 24-28V DC regulated
  • Output Current: 0-45A continuously variable
  • Regulation: ±2% voltage, ±5% current
  • Efficiency: >85% at full load
  • Ripple: <100mV peak-to-peak

Environmental Specifications

  • Operating Temperature: 0°C to +60°C
  • Storage Temperature: -20°C to +85°C
  • Humidity: 5% to 95% non-condensing
  • Cooling: Forced air, temperature controlled
  • Altitude: Up to 2000m above sea level

Safety & Compliance

  • Isolation: 4kV AC between mains and control
  • Protection: Overcurrent, overvoltage, thermal
  • EMC: EN 61000 series compliance
  • Safety: IEC 61010-1 electrical safety
  • Efficiency: Energy Star compliant

This documentation provides complete technical details for implementation, testing, and troubleshooting of the Industrial 24V battery charger controller system.

Is there a question here?

If you feel a post has answered your question, please click "Accept as Solution".

@boulganamed wrote:

@Ozone @Andrew Neil  @TDK 

First, I do not have the schematic of the charger.


But you must, surely, have a schematic of the ST62 part?

otherwise, how can you have built an STM32 substitute?

You need to show at least 

  • schematic of the original ST62 part
  • schematic of the corresponding STM32 part
  • where, exactly, you are making your voltage measurements.

A block diagram of the system would help - focussing around the MCU.

 


@boulganamed wrote:

SHARP industrial battery charger


Do you have a model number for that? documentation?

Are you trying to hack the internals of this commercial charger, or just making some sort of external controller for it?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.