#include "stm32f1xx_hal.h"
// -------------------- Konfigurasi Hardware --------------------
#define STEPPER_PORT GPIOB
#define IN1_PIN GPIO_PIN_8
#define IN2_PIN GPIO_PIN_9
#define IN3_PIN GPIO_PIN_10
#define IN4_PIN GPIO_PIN_11
#define TOUCH_SENSOR_PORT GPIOB
#define TOUCH_SENSOR_PIN GPIO_PIN_0
#define MOTOR_DC_PORT GPIOB
#define MOTOR_DC_PIN GPIO_PIN_7
// -------------------- Variabel Global --------------------
ADC_HandleTypeDef hadc1;
uint8_t current_mode = 0; // 0 = CW, 1 = CCW
volatile uint8_t motor_dc_active = 0; // Flag status motor DC
// -------------------- Stepper Motor Sequence --------------------
const uint8_t STEP_SEQ_CW[4] = {
(1 << 0), // IN1
(1 << 1), // IN2
(1 << 2), // IN3
(1 << 3) // IN4
};
const uint8_t STEP_SEQ_CCW[4] = {
(1 << 3), // IN4
(1 << 2), // IN3
(1 << 1), // IN2
(1 << 0) // IN1
};
// -------------------- Fungsi Prototipe --------------------
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_ADC1_Init(void);
void RunStepper(const uint8_t *sequence, uint8_t speed);
void Error_Handler(void);
// -------------------- Fungsi Utama --------------------
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
while (1) {
if (!motor_dc_active) {
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
uint16_t adc_val = HAL_ADC_GetValue(&hadc1);
current_mode = (adc_val < 2048) ? 0 : 1;
}
if (current_mode == 0) {
RunStepper(STEP_SEQ_CW, 5);
} else {
RunStepper(STEP_SEQ_CCW, 5);
}
}
HAL_Delay(1);
}
}
// -------------------- Fungsi Stepper Motor --------------------
void RunStepper(const uint8_t *sequence, uint8_t speed) {
static uint8_t step = 0;
HAL_GPIO_WritePin(STEPPER_PORT, IN1_PIN, (sequence[step] & (1 << 0)) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(STEPPER_PORT, IN2_PIN, (sequence[step] & (1 << 1)) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(STEPPER_PORT, IN3_PIN, (sequence[step] & (1 << 2)) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(STEPPER_PORT, IN4_PIN, (sequence[step] & (1 << 3)) ? GPIO_PIN_SET : GPIO_PIN_RESET);
step = (step + 1) % 4;
HAL_Delay(speed);
}
// -------------------- Inisialisasi GPIO --------------------
void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_AFIO_REMAP_SWJ_NOJTAG(); // Nonaktifkan JTAG jika perlu
// Konfigurasi Touch Sensor sebagai input dengan interrupt
GPIO_InitStruct.Pin = TOUCH_SENSOR_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(TOUCH_SENSOR_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
// Konfigurasi Motor DC
GPIO_InitStruct.Pin = MOTOR_DC_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(MOTOR_DC_PORT, &GPIO_InitStruct);
// Konfigurasi Stepper Motor (PB8-PB11)
GPIO_InitStruct.Pin = IN1_PIN | IN2_PIN | IN3_PIN | IN4_PIN;
HAL_GPIO_Init(STEPPER_PORT, &GPIO_InitStruct);
}
// -------------------- Inisialisasi ADC --------------------
void MX_ADC1_Init(void) {
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
}
// -------------------- Konfigurasi Clock --------------------
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
// -------------------- Callback Interrupt EXTI --------------------
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == TOUCH_SENSOR_PIN) {
GPIO_PinState pinState = HAL_GPIO_ReadPin(TOUCH_SENSOR_PORT, TOUCH_SENSOR_PIN);
if (pinState == GPIO_PIN_SET) {
motor_dc_active = 1;
HAL_GPIO_WritePin(MOTOR_DC_PORT, MOTOR_DC_PIN, GPIO_PIN_SET);
// Matikan stepper
HAL_GPIO_WritePin(STEPPER_PORT, IN1_PIN | IN2_PIN | IN3_PIN | IN4_PIN, GPIO_PIN_RESET);
} else {
motor_dc_active = 0;
HAL_GPIO_WritePin(MOTOR_DC_PORT, MOTOR_DC_PIN, GPIO_PIN_RESET);
}
}
}
// -------------------- Interrupt Handler --------------------
void EXTI0_IRQHandler(void) {
HAL_GPIO_EXTI_IRQHandler(TOUCH_SENSOR_PIN);
}
// -------------------- Error Handler --------------------
void Error_Handler(void) {
while (1) {
// Loop forever
}
}
Komentar
Posting Komentar