[ARM] STM32_PWM, Driver(Motor)ARM/2_Study2024. 4. 29. 23:14
Table of Contents
1. PWM
- Prescaler 설정 : 100-1 → 100MHz / 100 : 1MHz
- ARR(Auto Reload Register)
- Counter Period 조절하여 주파수 조절 (top 값)
- 1MHz → 1 count = 1us
- @ ARR=1000-1) 0 ~ 999 count하고 overflow → 1kHz
- CCR (Channel Compare Register) : Pulse 값 조절하여 duty rate 조절
- PWM mode : mode 1) CNT < CCR → 출력 High(시작 High) / mode 2) CNT > CCR → 출력 High(시작 Low)
- Output compare preload : Enable → CCR 값 변경시 다음 주기부터 변경 적용 / Disable → CCR 값 변경시 바로 적용(오류 가능성 있음)
- CH Polarity 조절하여 시작을 LOW or HIGH
⇒ TIME 1개로 여러개의 모터(TIM Channel) 제어 가능
1-1. PWM 사용하여 LED 제어
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 50);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 50);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
`__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 500)` : CCR Register 설정 할 수 있는 매크로
⇒ CCR 0 ~ 999 중 50으로 설정
2. Motor Driver
더보기
// 헤더 파일
/*
* Motor.h
*
* Created on: Apr 25, 2024
* Author: k1min
*/
#ifndef DRIVER_MOTOR_MOTOR_H_
#define DRIVER_MOTOR_MOTOR_H_
#include "stm32f4xx_hal.h"
typedef struct{
TIM_HandleTypeDef *htim;
uint32_t Channel;
GPIO_TypeDef *Dir1_GPIO;
uint16_t Dir1_GPIO_Pin;
GPIO_TypeDef *Dir2_GPIO;
uint16_t Dir2_GPIO_Pin;
}Motor_t;
void Motor_init(Motor_t *Motor,
TIM_HandleTypeDef *htim,
uint32_t Channel,
GPIO_TypeDef *Dir1_GPIO, uint16_t Dir1_GPIO_Pin,
GPIO_TypeDef *Dir2_GPIO, uint16_t Dir2_GPIO_Pin);
void Motor_Stop(Motor_t *Motor);
void Motor_Forward(Motor_t *Motor);
void Motor_Backward(Motor_t *Motor);
void Motor_SetSpeed(Motor_t *Motor, int speedVal);
#endif /* DRIVER_MOTOR_MOTOR_H_ */
// 소스 파일
/*
* Motor.c
*
* Created on: Apr 25, 2024
* Author: k1min
*/
#include "Motor.h"
void Motor_init(Motor_t *Motor,
TIM_HandleTypeDef *htim,
uint32_t Channel,
GPIO_TypeDef *Dir1_GPIO, uint16_t Dir1_GPIO_Pin,
GPIO_TypeDef *Dir2_GPIO, uint16_t Dir2_GPIO_Pin)
{
Motor->htim = htim;
Motor->Channel = Channel;
Motor->Dir1_GPIO = Dir1_GPIO;
Motor->Dir1_GPIO_Pin = Dir1_GPIO_Pin;
Motor->Dir2_GPIO = Dir2_GPIO;
Motor->Dir2_GPIO_Pin = Dir2_GPIO_Pin;
}
void Motor_Stop(Motor_t *Motor)
{
HAL_TIM_PWM_Stop(Motor->htim, Motor->Channel);
}
void Motor_Forward(Motor_t *Motor)
{
HAL_GPIO_WritePin(Motor->Dir1_GPIO, Motor->Dir1_GPIO_Pin, RESET);
HAL_GPIO_WritePin(Motor->Dir2_GPIO, Motor->Dir2_GPIO_Pin, SET);
HAL_TIM_PWM_Start(Motor->htim, Motor->Channel);
}
void Motor_Backward(Motor_t *Motor)
{
HAL_GPIO_WritePin(Motor->Dir1_GPIO, Motor->Dir1_GPIO_Pin, SET);
HAL_GPIO_WritePin(Motor->Dir2_GPIO, Motor->Dir2_GPIO_Pin, RESET);
HAL_TIM_PWM_Start(Motor->htim, Motor->Channel);
}
void Motor_SetSpeed(Motor_t *Motor, int speedVal)
{
__HAL_TIM_SET_COMPARE(Motor->htim, Motor->Channel, speedVal);
}
전체 코드
Made By Minseok KIM
'ARM > 2_Study' 카테고리의 다른 글
[ARM] STM32_FreeRTOS, Shared Memory (0) | 2024.04.30 |
---|---|
[ARM] STM32_Serial Communication(UART), Queue (0) | 2024.04.29 |
[ARM] STM32_Driver(CLCD I2C, Ultrasonic) (0) | 2024.04.26 |
[ARM] STM32_메모리 저장&실행 과정, Driver(Button, LED) (0) | 2024.04.26 |
[ARM] STM32_Low level Programming (0) | 2024.04.24 |
@민바Minba :: Minba's blog
Let's Be Happy!
도움이 되었으면 좋겠어요 :)