H-Bridge Simulator

H-Bridge Simulator using Proteus and Keil UVisiuon5

BeginnerProtip106
H-Bridge Simulator

Things used in this project

Hardware components

Resistor 1k ohm
Resistor 1k ohm
×4
STM32F401CB
×1
Darlington High Power Transistor
Darlington High Power Transistor
×4
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2
Geared DC Motor, 12 V
Geared DC Motor, 12 V
×1

Software apps and online services

Proteus8Profesional
Keil UVision

Story

Read more

Schematics

H-Bridge Simulator

Schematic Circuit H-Bridge Simulator using STM32F401CB

Code

H-Bridge Simulator

C/C++
kode program simpel H-Bridge Simulator
#include "stm32f4xx.h"
 
void delay_ms(uint32_t ms) {
    SysTick->LOAD = 16000 - 1; // 1ms delay @ 16MHz
    SysTick->VAL = 0;
    SysTick->CTRL = 5;

    for (uint32_t i = 0; i < ms; i++) {
        while ((SysTick->CTRL & 0x10000) == 0);
    }

    SysTick->CTRL = 0;
}

void GPIO_PWM_Init(void) {
    // Enable clock GPIOA dan TIM2
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    // PA0 = TIM2_CH1 (PWM Maju), PA1 = TIM2_CH2 (PWM Mundur)
    GPIOA->MODER |= (2 << (0 * 2)) | (2 << (1 * 2)); // Alternate function
    GPIOA->AFR[0] |= (1 << (0 * 4)) | (1 << (1 * 4)); // AF1 (TIM2) untuk PA0 dan PA1
}

void TIM2_PWM_Init(void) {
    TIM2->PSC = 160 - 1;    // Prescaler: 16MHz / 160 = 100kHz
    TIM2->ARR = 1000 - 1;   // Auto-reload: 100Hz PWM (10ms period)

    TIM2->CCMR1 |= (6 << 4) | (1 << 3); // CH1: PWM mode 1, preload enable
    TIM2->CCMR1 |= (6 << 12) | (1 << 11); // CH2: PWM mode 1, preload enable
    TIM2->CCER |= TIM_CCER_CC1E | TIM_CCER_CC2E; // Enable output CH1 dan CH2

    TIM2->CCR1 = 0; // Duty cycle awal 50% (maju)
    TIM2->CCR2 = 0; // Duty cycle awal 50% (mundur)

    TIM2->CR1 |= TIM_CR1_CEN; // Enable timer
}

void GPIO_Input_Init(void) {
    // PA2 dan PA3 sebagai input tombol (pull-up)
    GPIOA->MODER &= ~((3 << (2 * 2)) | (3 << (3 * 2))); // input mode
    GPIOA->PUPDR &= ~((3 << (2 * 2)) | (3 << (3 * 2))); // clear
    GPIOA->PUPDR |= (1 << (2 * 2)) | (1 << (3 * 2));    // pull-up
}

int main(void) {
    GPIO_PWM_Init();
    GPIO_Input_Init();
    TIM2_PWM_Init();

    while (1) {
        uint8_t btn1 = !(GPIOA->IDR & (1 << 2)); // Tombol 1 aktif jika ditekan
        uint8_t btn2 = !(GPIOA->IDR & (1 << 3)); // Tombol 2 aktif jika ditekan

        if (btn1 && btn2) {
            TIM2->CCR1 = 0; // Stop
            TIM2->CCR2 = 0;
        } else if (btn1) {
            TIM2->CCR1 = 1000; // Maju (100% duty cycle)
            TIM2->CCR2 = 0;
        } else if (btn2) {
            TIM2->CCR1 = 0;
            TIM2->CCR2 = 1000; // Mundur (100% duty cycle)
        } else {
            TIM2->CCR1 = 0; // Stop
            TIM2->CCR2 = 0;
        }

        delay_ms(1);
    }
}

Credits

M. Randu
1 project • 0 followers
Juanda Ergiansyah
1 project • 0 followers
Aldo Revaldo
1 project • 0 followers
fitriah aj
1 project • 0 followers
Hendry Andreas
1 project • 0 followers
Jefri
1 project • 0 followers
Mahmud Maulana
1 project • 0 followers
Refli mahendra anuar
1 project • 0 followers
Faisal Anugrah
1 project • 0 followers

Comments