-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMC34931.c
More file actions
68 lines (59 loc) · 2.3 KB
/
Copy pathMC34931.c
File metadata and controls
68 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "MC34931.h"
void Motor_Init(
Motor_Config_t *motor,
TIM_HandleTypeDef *htim,
uint32_t ch1,
uint32_t ch2,
GPIO_TypeDef *D1_Port,
uint16_t D1_Pin,
GPIO_TypeDef *E2_Port,
uint16_t E2_Pin
){
motor->htim = htim;
motor->channel_IN1 = ch1;
motor->chanel_IN2 = ch2;
motor->D1_Port = D1_Port;
motor->D1_Pin = D1_Pin;
motor->E2_Port = E2_Port;
motor->E2_Pin = E2_Pin;
}
// 2. Inicjalizacja sprzętowa (start PWM, ustawienie pinów Enable/Disable)
void Motor_Init_Hardware(Motor_Config_t *motor) {
// Ustawienie pinów sterujących zgodnie z datasheetem MC34931
// D1 (Disable) -> LOW (aby mostek był aktywny)
HAL_GPIO_WritePin(motor->D1_Port, motor->D1_Pin, GPIO_PIN_RESET);
// E2 (Enable/Sleep) -> HIGH (aby wybudzić układ ze Sleep)
HAL_GPIO_WritePin(motor->E2_Port, motor->E2_Pin, GPIO_PIN_SET);
// Start PWM z wypełnieniem 0
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN1, 0);
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN2, 0);
HAL_TIM_PWM_Start(motor->htim, motor->channel_IN1);
HAL_TIM_PWM_Start(motor->htim, motor->channel_IN2);
}
// 3. Główna funkcja sterująca
// speed_percent: 0 - 100
void Motor_SetSpeed(Motor_Config_t *motor, Motor_Dir_t dir, uint8_t speed_percent) {
// Zabezpieczenie zakresu
if (speed_percent > 100) speed_percent = 100;
// Sterowanie mostkiem H: Jeden pin PWM, drugi LOW (GND)
// Zgodnie z tabelą prawdy: H/L = Forward, L/H = Reverse [cite: 566]
if (dir == DIR_CW) {
// IN1 = PWM, IN2 = LOW
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN1, speed_percent*63);
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN2, 0);
} else {
// IN1 = LOW, IN2 = PWM
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN1, 0);
__HAL_TIM_SET_COMPARE(motor->htim, motor->channel_IN2, speed_percent*63);
}
}
// Funkcja wprowadzająca driver w tryb Sleep (oszczędzanie energii < 12uA) [cite: 18, 207]
void Motor_Sleep(Motor_Config_t *motor, uint8_t enable_sleep) {
if (enable_sleep) {
// EN/D2 = LOW -> Sleep Mode
HAL_GPIO_WritePin(motor->E2_Port, motor->E2_Pin, GPIO_PIN_RESET);
} else {
// EN/D2 = HIGH -> Wake up
HAL_GPIO_WritePin(motor->E2_Port, motor->E2_Pin, GPIO_PIN_SET);
}
}