Files

290 lines
7.5 KiB
C
Raw Permalink Normal View History

2025-12-06 23:49:34 +01:00
/*
* pwm.c
*
* Created on: Dec 6, 2025
* Author: user
*/
2026-05-14 19:44:46 +02:00
#include <stdio.h>
#include <string.h>
2025-12-06 23:49:34 +01:00
#include "stm32f1xx_hal.h"
#include "main.h"
#include "pwm.h"
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
2026-05-14 19:44:46 +02:00
extern volatile uint16_t rtramp[4];
extern uint16_t mrampstart[4];
extern UART_HandleTypeDef huart1;
2025-12-06 23:49:34 +01:00
bool IsPwmRunning(TIM_HandleTypeDef *htim, uint32_t Channel)
{
HAL_TIM_ChannelStateTypeDef chState = HAL_TIM_GetChannelState(htim, Channel);
return (chState == HAL_TIM_CHANNEL_STATE_BUSY);
}
void StopMot(TIM_HandleTypeDef *htim,uint32_t Channel){
HAL_TIM_PWM_Stop(htim, Channel);
}
void StartMot(TIM_HandleTypeDef *htim,uint32_t Channel,uint16_t pwmval){
__HAL_TIM_SET_COMPARE(htim, Channel, pwmval);
HAL_TIM_PWM_Start(htim, Channel);
}
void SetMotPwm(TIM_HandleTypeDef *htim,uint32_t Channel,uint16_t pwmval){
__HAL_TIM_SET_COMPARE(htim, Channel, pwmval);
}
void SetMot(uint8_t mot,uint8_t dir,uint16_t pwmval){
2026-04-14 11:52:48 +02:00
switch(mot){
case 1:
if(dir==FW){
if(pwmval==0){
StopMot(timMot1,FWMot1);
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot1, FWMot1)){
SetMotPwm(timMot1,FWMot1,pwmval);
}else{
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
StartMot(timMot1,FWMot1,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot1,BWMot1);
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot1, BWMot1)){
SetMotPwm(timMot1,BWMot1,pwmval);
}else{
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
StartMot(timMot1,BWMot1,pwmval);
}
}
break;
case 2:
if(dir==FW){
if(pwmval==0){
StopMot(timMot2,FWMot2);
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot2, FWMot2)){
SetMotPwm(timMot2,FWMot2,pwmval);
}else{
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
StartMot(timMot2,FWMot2,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot2,BWMot2);
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot2, BWMot2)){
SetMotPwm(timMot2,BWMot2,pwmval);
}else{
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
StartMot(timMot2,BWMot2,pwmval);
}
}
break;
case 3:
if(dir==FW){
if(pwmval==0){
StopMot(timMot3,FWMot3);
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot3, FWMot3)){
SetMotPwm(timMot3,FWMot3,pwmval);
}else{
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
StartMot(timMot3,FWMot3,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot3,BWMot3);
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot3, BWMot3)){
SetMotPwm(timMot3,BWMot3,pwmval);
}else{
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
StartMot(timMot3,BWMot3,pwmval);
}
}
break;
case 4:
if(dir==FW){
if(pwmval==0){
StopMot(timMot4,FWMot4);
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot4, FWMot4)){
SetMotPwm(timMot4,FWMot4,pwmval);
}else{
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
StartMot(timMot4,FWMot4,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot4,BWMot4);
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot4, BWMot4)){
SetMotPwm(timMot4,BWMot4,pwmval);
}else{
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
StartMot(timMot4,BWMot4,pwmval);
}
}
break;
default:
break;
}
}
void SetMotPerc(uint8_t mot,uint8_t dir,uint8_t perc){
uint16_t pwmval;
uint32_t tempval;
tempval=perc;
tempval*=16384;
tempval/=100;
pwmval=(uint16_t)tempval;
2025-12-06 23:49:34 +01:00
switch(mot){
case 1:
if(dir==FW){
if(pwmval==0){
StopMot(timMot1,FWMot1);
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot1, FWMot1)){
SetMotPwm(timMot1,FWMot1,pwmval);
}else{
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
StartMot(timMot1,FWMot1,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot1,BWMot1);
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot1, BWMot1)){
SetMotPwm(timMot1,BWMot1,pwmval);
}else{
HAL_GPIO_WritePin(INH1_GPIO_Port, INH1_Pin, GPIO_PIN_SET);
StartMot(timMot1,BWMot1,pwmval);
}
}
break;
case 2:
if(dir==FW){
if(pwmval==0){
StopMot(timMot2,FWMot2);
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot2, FWMot2)){
SetMotPwm(timMot2,FWMot2,pwmval);
}else{
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
StartMot(timMot2,FWMot2,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot2,BWMot2);
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot2, BWMot2)){
SetMotPwm(timMot2,BWMot2,pwmval);
}else{
HAL_GPIO_WritePin(INH2_GPIO_Port, INH2_Pin, GPIO_PIN_SET);
StartMot(timMot2,BWMot2,pwmval);
}
}
break;
case 3:
if(dir==FW){
if(pwmval==0){
StopMot(timMot3,FWMot3);
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot3, FWMot3)){
SetMotPwm(timMot3,FWMot3,pwmval);
}else{
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
StartMot(timMot3,FWMot3,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot3,BWMot3);
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot3, BWMot3)){
SetMotPwm(timMot3,BWMot3,pwmval);
}else{
HAL_GPIO_WritePin(INH3_GPIO_Port, INH3_Pin, GPIO_PIN_SET);
StartMot(timMot3,BWMot3,pwmval);
}
}
break;
case 4:
if(dir==FW){
if(pwmval==0){
StopMot(timMot4,FWMot4);
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot4, FWMot4)){
SetMotPwm(timMot4,FWMot4,pwmval);
}else{
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
StartMot(timMot4,FWMot4,pwmval);
}
}else{
if(pwmval==0){
StopMot(timMot4,BWMot4);
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_RESET);
}else if(IsPwmRunning(timMot4, BWMot4)){
SetMotPwm(timMot4,BWMot4,pwmval);
}else{
HAL_GPIO_WritePin(INH4_GPIO_Port, INH4_Pin, GPIO_PIN_SET);
StartMot(timMot4,BWMot4,pwmval);
}
}
break;
default:
break;
}
}
2026-04-15 23:12:13 +02:00
uint8_t ramp(uint8_t mot,uint8_t dir,uint8_t percStart,uint8_t percEnd,uint8_t tr,uint8_t mode){
static uint8_t memrtramp[4];
2026-05-14 19:44:46 +02:00
// char s[10];
2026-04-15 23:12:13 +02:00
uint16_t delta;
switch(mode){
case RAMPINIT:
2026-05-14 19:44:46 +02:00
memrtramp[mot-1]=rtramp[mot-1]=tr;
SetMotPerc(mot,dir,percStart);
2026-04-15 23:12:13 +02:00
return DONE;
case RAMPRUN:
if(memrtramp[mot-1]!=rtramp[mot-1]){
memrtramp[mot-1]=rtramp[mot-1];
if(rtramp[mot-1]){
2026-05-14 19:44:46 +02:00
if(percStart>percEnd){
delta=percStart-percEnd;
2026-04-15 23:12:13 +02:00
delta=delta*(uint16_t)(tr-rtramp[mot-1]);
delta=delta/tr;
2026-05-14 19:44:46 +02:00
SetMotPerc(mot,dir,percStart-delta);
2026-04-15 23:12:13 +02:00
return RUNNING;
2026-05-14 19:44:46 +02:00
}else if(percStart<percEnd){
delta=percEnd-percStart;
2026-04-15 23:12:13 +02:00
delta=delta*(uint16_t)(tr-rtramp[mot-1]);
delta=delta/tr;
2026-05-14 19:44:46 +02:00
SetMotPerc(mot,dir,percStart+delta);
// sprintf((char*)s,"\n%d",percStart+delta);
// HAL_UART_Transmit(&huart1,(uint8_t*) s, strlen(s), HAL_MAX_DELAY);
2026-04-15 23:12:13 +02:00
return RUNNING;
}else{
SetMotPerc(mot,dir,percEnd);
return DONE;
}
}else{
SetMotPerc(mot,dir,percEnd);
return DONE;
}
}else return RUNNING;
break;
}
return DONE;
}