Skip to content

Instantly share code, notes, and snippets.

@galch
Created May 12, 2015 10:29
Show Gist options
  • Save galch/9863572fc3f24e28b8bc to your computer and use it in GitHub Desktop.
Save galch/9863572fc3f24e28b8bc to your computer and use it in GitHub Desktop.
pd_pwm for 4 QEP & 4 Motors
#include "pd_pwm.h"
/* ==============================================================================================
// Configuration
============================================================================================== */
void Encoder_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd (ENC_FL_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd (ENC_FR_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd (ENC_RL_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd (ENC_RR_GPIO_CLK, ENABLE);
GPIO_StructInit (&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // pull-up for Encoder Signal
GPIO_InitStructure.GPIO_Pin = ENC_FL_PIN_A;
GPIO_Init (ENC_FL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_FL_PIN_B;
GPIO_Init (ENC_FL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_FR_PIN_A;
GPIO_Init (ENC_FR_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_FR_PIN_B;
GPIO_Init (ENC_FR_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_RL_PIN_A;
GPIO_Init (ENC_RL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_RL_PIN_B;
GPIO_Init (ENC_RL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_RR_PIN_A;
GPIO_Init (ENC_RR_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ENC_RR_PIN_B;
GPIO_Init (ENC_RR_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig (ENC_FL_PORT, ENC_FL_SOURCE_A, ENC_FL_AF);
GPIO_PinAFConfig (ENC_FL_PORT, ENC_FL_SOURCE_B, ENC_FL_AF);
GPIO_PinAFConfig (ENC_FR_PORT, ENC_FR_SOURCE_A, ENC_FR_AF);
GPIO_PinAFConfig (ENC_FR_PORT, ENC_FR_SOURCE_B, ENC_FR_AF);
GPIO_PinAFConfig (ENC_RL_PORT, ENC_RL_SOURCE_A, ENC_RL_AF);
GPIO_PinAFConfig (ENC_RL_PORT, ENC_RL_SOURCE_B, ENC_RL_AF);
GPIO_PinAFConfig (ENC_RR_PORT, ENC_RR_SOURCE_A, ENC_RR_AF);
GPIO_PinAFConfig (ENC_RR_PORT, ENC_RR_SOURCE_B, ENC_RR_AF);
RCC_APB2PeriphClockCmd (ENC_FL_TIMER_CLK, ENABLE);
RCC_APB1PeriphClockCmd (ENC_FR_TIMER_CLK, ENABLE);
RCC_APB1PeriphClockCmd (ENC_RL_TIMER_CLK, ENABLE);
RCC_APB1PeriphClockCmd (ENC_RR_TIMER_CLK, ENABLE);
//TIM_TIxExternalClockConfig(ENC_FL_TIMER, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
//TIM_TIxExternalClockConfig(ENC_FR_TIMER, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
//TIM_TIxExternalClockConfig(ENC_RL_TIMER, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
//TIM_TIxExternalClockConfig(ENC_RR_TIMER, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_EncoderInterfaceConfig (ENC_FL_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_SetAutoreload (ENC_FL_TIMER, 0xffff);
TIM_EncoderInterfaceConfig (ENC_FR_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_SetAutoreload (ENC_FR_TIMER, 0xffff);
TIM_EncoderInterfaceConfig (ENC_RL_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_SetAutoreload (ENC_RL_TIMER, 0xffff);
TIM_EncoderInterfaceConfig (ENC_RR_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising,
TIM_ICPolarity_Rising);
TIM_SetAutoreload (ENC_RR_TIMER, 0xffff);
// turn on the timer/counters
TIM_Cmd (ENC_FL_TIMER, ENABLE);
TIM_Cmd (ENC_FR_TIMER, ENABLE);
TIM_Cmd (ENC_RL_TIMER, ENABLE);
TIM_Cmd (ENC_RR_TIMER, ENABLE);
}
void PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd (MOTOR_FL_CLK_A, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_FL_CLK_B, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_FR_CLK_A, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_FR_CLK_B, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_RL_CLK_A, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_RL_CLK_B, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_RR_CLK_A, ENABLE);
RCC_AHB1PeriphClockCmd (MOTOR_RR_CLK_B, ENABLE);
GPIO_StructInit (&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // pull-up for Encoder Signal
GPIO_InitStructure.GPIO_Pin = MOTOR_FL_PIN_A;
GPIO_Init (MOTOR_FL_PORT_A, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_FL_PIN_B;
GPIO_Init (MOTOR_FL_PORT_B, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_FR_PIN_A;
GPIO_Init (MOTOR_FR_PORT_A, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_FR_PIN_B;
GPIO_Init (MOTOR_FR_PORT_B, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_RL_PIN_A;
GPIO_Init (MOTOR_RL_PORT_A, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_RL_PIN_B;
GPIO_Init (MOTOR_RL_PORT_B, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_RR_PIN_A;
GPIO_Init (MOTOR_RR_PORT_A, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MOTOR_RR_PIN_B;
GPIO_Init (MOTOR_RR_PORT_B, &GPIO_InitStructure);
GPIO_PinAFConfig (MOTOR_FL_PORT_A, MOTOR_FL_SOURCE_A, MOTOR_FL_AF_A);
GPIO_PinAFConfig (MOTOR_FL_PORT_B, MOTOR_FL_SOURCE_B, MOTOR_FL_AF_B);
GPIO_PinAFConfig (MOTOR_FR_PORT_A, MOTOR_FR_SOURCE_A, MOTOR_FR_AF_A);
GPIO_PinAFConfig (MOTOR_FR_PORT_B, MOTOR_FR_SOURCE_B, MOTOR_FR_AF_B);
GPIO_PinAFConfig (MOTOR_RL_PORT_A, MOTOR_RL_SOURCE_A, MOTOR_RL_AF_A);
GPIO_PinAFConfig (MOTOR_RL_PORT_B, MOTOR_RL_SOURCE_B, MOTOR_RL_AF_B);
GPIO_PinAFConfig (MOTOR_RR_PORT_A, MOTOR_RR_SOURCE_A, MOTOR_RR_AF_A);
GPIO_PinAFConfig (MOTOR_RR_PORT_B, MOTOR_RR_SOURCE_B, MOTOR_RR_AF_B);
RCC_APB1PeriphClockCmd (MOTOR_FL_TIMER_CLK_A, ENABLE);
RCC_APB1PeriphClockCmd (MOTOR_FR_TIMER_CLK_A, ENABLE);
RCC_APB1PeriphClockCmd (MOTOR_RL_TIMER_CLK_A, ENABLE);
RCC_APB1PeriphClockCmd (MOTOR_RR_TIMER_CLK_A, ENABLE);
RCC_APB2PeriphClockCmd (MOTOR_FL_TIMER_CLK_B, ENABLE);
RCC_APB2PeriphClockCmd (MOTOR_FR_TIMER_CLK_B, ENABLE);
RCC_APB2PeriphClockCmd (MOTOR_RL_TIMER_CLK_B, ENABLE);
RCC_APB1PeriphClockCmd (MOTOR_RR_TIMER_CLK_B, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 168 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(MOTOR_FL_TIMER_A, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_FL_TIMER_B, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_FR_TIMER_A, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_FR_TIMER_B, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_RL_TIMER_A, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_RL_TIMER_B, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_RR_TIMER_A, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(MOTOR_RR_TIMER_B, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(MOTOR_FL_TIMER_A, &TIM_OCInitStructure);
TIM_OC3Init(MOTOR_FL_TIMER_B, &TIM_OCInitStructure);
TIM_OC3Init(MOTOR_FR_TIMER_A, &TIM_OCInitStructure);
TIM_OC4Init(MOTOR_FR_TIMER_B, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(MOTOR_RL_TIMER_A, &TIM_OCInitStructure);
TIM_OC2Init(MOTOR_RL_TIMER_B, &TIM_OCInitStructure);
TIM_OC2Init(MOTOR_RR_TIMER_A, &TIM_OCInitStructure);
TIM_OC3Init(MOTOR_RR_TIMER_B, &TIM_OCInitStructure);
TIM_SetCompare2(MOTOR_FL_TIMER_A, 0);
TIM_SetCompare3(MOTOR_FL_TIMER_B, 0);
TIM_SetCompare3(MOTOR_FR_TIMER_A, 0);
TIM_SetCompare4(MOTOR_FR_TIMER_B, 0);
TIM_SetCompare1(MOTOR_RL_TIMER_A, 0);
TIM_SetCompare2(MOTOR_RL_TIMER_B, 0);
TIM_SetCompare2(MOTOR_RR_TIMER_A, 0);
TIM_SetCompare3(MOTOR_RR_TIMER_B, 0);
TIM_ARRPreloadConfig(MOTOR_FL_TIMER_A, ENABLE);
TIM_ARRPreloadConfig(MOTOR_FL_TIMER_B, ENABLE);
TIM_ARRPreloadConfig(MOTOR_FR_TIMER_A, ENABLE);
TIM_ARRPreloadConfig(MOTOR_FR_TIMER_B, ENABLE);
TIM_ARRPreloadConfig(MOTOR_RL_TIMER_A, ENABLE);
TIM_ARRPreloadConfig(MOTOR_RL_TIMER_B, ENABLE);
TIM_ARRPreloadConfig(MOTOR_RR_TIMER_A, ENABLE);
TIM_ARRPreloadConfig(MOTOR_RR_TIMER_B, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_FL_TIMER_A, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_FL_TIMER_B, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_FR_TIMER_A, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_FR_TIMER_B, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_RL_TIMER_A, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_RL_TIMER_B, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_RR_TIMER_A, ENABLE);
TIM_CtrlPWMOutputs(MOTOR_RR_TIMER_B, ENABLE);
TIM_Cmd(MOTOR_FL_TIMER_A, ENABLE);
TIM_Cmd(MOTOR_FL_TIMER_B, ENABLE);
TIM_Cmd(MOTOR_FR_TIMER_A, ENABLE);
TIM_Cmd(MOTOR_FR_TIMER_B, ENABLE);
TIM_Cmd(MOTOR_RL_TIMER_A, ENABLE);
TIM_Cmd(MOTOR_RL_TIMER_B, ENABLE);
TIM_Cmd(MOTOR_RR_TIMER_A, ENABLE);
TIM_Cmd(MOTOR_RR_TIMER_B, ENABLE);
}
/* ==============================================================================================
// PWM Set Method
============================================================================================== */
void PWM_Set_FL_A(uint32_t us)
{
MOTOR_FL_TIMER_A->MOTOR_FL_CCR_A = us;
}
void PWM_Set_FL_B(uint32_t us)
{
MOTOR_FL_TIMER_B->MOTOR_FL_CCR_B = us;
}
void PWM_Set_FR_A(uint32_t us)
{
MOTOR_FR_TIMER_A->MOTOR_FR_CCR_A = us;
}
void PWM_Set_FR_B(uint32_t us)
{
MOTOR_FR_TIMER_B->MOTOR_FR_CCR_B = us;
}
void PWM_Set_RL_A(uint32_t us)
{
MOTOR_RL_TIMER_A->MOTOR_RL_CCR_A = us;
}
void PWM_Set_RL_B(uint32_t us)
{
MOTOR_RL_TIMER_B->MOTOR_RL_CCR_B = us;
}
void PWM_Set_RR_A(uint32_t us)
{
MOTOR_RR_TIMER_A->MOTOR_RR_CCR_A = us;
}
void PWM_Set_RR_B(uint32_t us)
{
MOTOR_RR_TIMER_B->MOTOR_RR_CCR_B = us;
}
void PWM_MotorFL_Set(int16_t output)
{
if (output > 0)
{
if (output > 1000)
output = 1000;
PWM_Set_FL_B(output);
PWM_Set_FL_A(0);
}
else
{
if (output < -1000)
output = -1000;
PWM_Set_FL_B(0);
PWM_Set_FL_A(-output);
}
}
void PWM_MotorFR_Set(int16_t output)
{
if (output > 0)
{
if (output > 1000)
output = 1000;
PWM_Set_FR_B(output);
PWM_Set_FR_A(0);
}
else
{
if (output < -1000)
output = -1000;
PWM_Set_FR_B(0);
PWM_Set_FR_A(-output);
}
}
void PWM_MotorRL_Set(int16_t output)
{
if (output > 0)
{
if (output > 1000)
output = 1000;
PWM_Set_RL_A(output);
PWM_Set_RL_B(0);
}
else
{
if (output < -1000)
output = -1000;
PWM_Set_RL_A(0);
PWM_Set_RL_B(-output);
}
}
void PWM_MotorRR_Set(int16_t output)
{
if (output > 0)
{
if (output > 1000)
output = 1000;
PWM_Set_RR_A(output);
PWM_Set_RR_B(0);
}
else
{
if (output < -1000)
output = -1000;
PWM_Set_RR_A(0);
PWM_Set_RR_B(-output);
}
}
int16_t PWM_MotorFL_Get(){ return MOTOR_FL_TIMER_A->MOTOR_FL_CCR_A != 0 ? MOTOR_FL_TIMER_A->MOTOR_FL_CCR_A : MOTOR_FL_TIMER_B->MOTOR_FL_CCR_B ;}
int16_t PWM_MotorFR_Get(){ return MOTOR_FR_TIMER_A->MOTOR_FR_CCR_A != 0 ? MOTOR_FR_TIMER_A->MOTOR_FR_CCR_A : MOTOR_FR_TIMER_B->MOTOR_FR_CCR_B ;}
int16_t PWM_MotorRL_Get(){ return MOTOR_RL_TIMER_A->MOTOR_RL_CCR_A != 0 ? MOTOR_RL_TIMER_A->MOTOR_RL_CCR_A : MOTOR_RL_TIMER_B->MOTOR_RL_CCR_B ;}
int16_t PWM_MotorRR_Get(){ return MOTOR_RR_TIMER_A->MOTOR_RR_CCR_A != 0 ? MOTOR_RR_TIMER_A->MOTOR_RR_CCR_A : MOTOR_RR_TIMER_B->MOTOR_RR_CCR_B ;}
/* ==============================================================================================
// Motor Drive Method
============================================================================================== */
void PWM_Motor_Set(int16_t outFL, int16_t outFR, int16_t outRL, int16_t outRR)
{
PWM_MotorFL_Set(outFL);
PWM_MotorFR_Set(outFR);
PWM_MotorRL_Set(outRL);
PWM_MotorRR_Set(outRR);
}
/* ==============================================================================================
// Encder Read Method
============================================================================================== */
int16_t Enc_FL_reg;
int16_t Enc_FR_reg;
int16_t Enc_RL_reg;
int16_t Enc_RR_reg;
int16_t Encoder_Read_FL (void)
{
return Enc_FL_reg;
}
int16_t Encoder_Read_FR (void)
{
return Enc_FR_reg;
}
int16_t Encoder_Read_RL (void)
{
return Enc_RL_reg;
}
int16_t Encoder_Read_RR (void)
{
return Enc_RR_reg;
}
int16_t Encoder_ReadOut_FL (void)
{
int16_t Count = TIM_GetCounter (ENC_FL_TIMER);
TIM_SetCounter (ENC_FL_TIMER, 0);
Enc_FL_reg = Count;
return Count;
}
int16_t Encoder_ReadOut_FR (void)
{
int16_t Count = TIM_GetCounter (ENC_FR_TIMER);
TIM_SetCounter (ENC_FR_TIMER, 0);
Enc_FR_reg = Count;
return Count;
}
int16_t Encoder_ReadOut_RL (void)
{
int16_t Count = TIM_GetCounter (ENC_RL_TIMER);
TIM_SetCounter (ENC_RL_TIMER, 0);
Enc_RL_reg = Count;
return Count;
}
int16_t Encoder_ReadOut_RR (void)
{
int16_t Count = TIM_GetCounter (ENC_RR_TIMER);
TIM_SetCounter (ENC_RR_TIMER, 0);
Enc_RR_reg = Count;
return Count;
}
#include "stm32f4xx.h"
#ifndef __PD_PWM_H
#define __PD_PWM_H
// define Encoder Port Parameter
#define ENC_FL_PIN_A GPIO_Pin_9
#define ENC_FL_SOURCE_A GPIO_PinSource9
#define ENC_FL_PIN_B GPIO_Pin_10
#define ENC_FL_SOURCE_B GPIO_PinSource10
#define ENC_FL_PORT GPIOE
#define ENC_FL_GPIO_CLK RCC_AHB1Periph_GPIOE
#define ENC_FL_AF GPIO_AF_TIM1
#define ENC_FR_PIN_A GPIO_Pin_6
#define ENC_FR_SOURCE_A GPIO_PinSource6
#define ENC_FR_PIN_B GPIO_Pin_7
#define ENC_FR_SOURCE_B GPIO_PinSource7
#define ENC_FR_PORT GPIOB
#define ENC_FR_GPIO_CLK RCC_AHB1Periph_GPIOB
#define ENC_FR_AF GPIO_AF_TIM4
#define ENC_RL_PIN_A GPIO_Pin_14
#define ENC_RL_SOURCE_A GPIO_PinSource14
#define ENC_RL_PIN_B GPIO_Pin_15
#define ENC_RL_SOURCE_B GPIO_PinSource15
#define ENC_RL_PORT GPIOB
#define ENC_RL_GPIO_CLK RCC_AHB1Periph_GPIOB
#define ENC_RL_AF GPIO_AF_TIM12
#define ENC_RR_PIN_A GPIO_Pin_4
#define ENC_RR_SOURCE_A GPIO_PinSource4
#define ENC_RR_PIN_B GPIO_Pin_5
#define ENC_RR_SOURCE_B GPIO_PinSource5
#define ENC_RR_PORT GPIOB
#define ENC_RR_GPIO_CLK RCC_AHB1Periph_GPIOB
#define ENC_RR_AF GPIO_AF_TIM3
#define ENC_FL_TIMER TIM1
#define ENC_FL_TIMER_CLK RCC_APB2Periph_TIM1
#define ENC_FR_TIMER TIM4
#define ENC_FR_TIMER_CLK RCC_APB1Periph_TIM4
#define ENC_RL_TIMER TIM12
#define ENC_RL_TIMER_CLK RCC_APB1Periph_TIM12
#define ENC_RR_TIMER TIM3
#define ENC_RR_TIMER_CLK RCC_APB1Periph_TIM3
//TIM5_CH2 CH3
#define MOTOR_FL_PIN_A GPIO_Pin_1
#define MOTOR_FL_PIN_B GPIO_Pin_2
#define MOTOR_FL_SOURCE_A GPIO_PinSource1
#define MOTOR_FL_SOURCE_B GPIO_PinSource2
#define MOTOR_FL_PORT_A GPIOA
#define MOTOR_FL_PORT_B GPIOA
#define MOTOR_FL_CLK_A RCC_AHB1Periph_GPIOA
#define MOTOR_FL_CLK_B RCC_AHB1Periph_GPIOA
#define MOTOR_FL_AF_A GPIO_AF_TIM5
#define MOTOR_FL_AF_B GPIO_AF_TIM5
//TIM4_CH3 CH4
#define MOTOR_FR_PIN_A GPIO_Pin_8
#define MOTOR_FR_PIN_B GPIO_Pin_9
#define MOTOR_FR_SOURCE_A GPIO_PinSource8
#define MOTOR_FR_SOURCE_B GPIO_PinSource9
#define MOTOR_FR_PORT_A GPIOB
#define MOTOR_FR_PORT_B GPIOB
#define MOTOR_FR_CLK_A RCC_AHB1Periph_GPIOB
#define MOTOR_FR_CLK_B RCC_AHB1Periph_GPIOB
#define MOTOR_FR_AF_A GPIO_AF_TIM4
#define MOTOR_FR_AF_B GPIO_AF_TIM4
//TIM8_CH1 CH2
#define MOTOR_RL_PIN_A GPIO_Pin_6
#define MOTOR_RL_PIN_B GPIO_Pin_8
#define MOTOR_RL_SOURCE_A GPIO_PinSource6
#define MOTOR_RL_SOURCE_B GPIO_PinSource8
#define MOTOR_RL_PORT_A GPIOC
#define MOTOR_RL_PORT_B GPIOC
#define MOTOR_RL_CLK_A RCC_AHB1Periph_GPIOC
#define MOTOR_RL_CLK_B RCC_AHB1Periph_GPIOC
#define MOTOR_RL_AF_A GPIO_AF_TIM8
#define MOTOR_RL_AF_B GPIO_AF_TIM8
//TIM4_CH2 TIM8_CH3
#define MOTOR_RR_PIN_A GPIO_Pin_7
#define MOTOR_RR_PIN_B GPIO_Pin_9
#define MOTOR_RR_SOURCE_A GPIO_PinSource7
#define MOTOR_RR_SOURCE_B GPIO_PinSource9
#define MOTOR_RR_PORT_A GPIOB
#define MOTOR_RR_PORT_B GPIOC
#define MOTOR_RR_CLK_A RCC_AHB1Periph_GPIOB
#define MOTOR_RR_CLK_B RCC_AHB1Periph_GPIOC
#define MOTOR_RR_AF_A GPIO_AF_TIM4
#define MOTOR_RR_AF_B GPIO_AF_TIM8
#define MOTOR_FL_TIMER_A TIM5
#define MOTOR_FL_TIMER_B TIM5
#define MOTOR_FL_TIMER_CLK_A RCC_APB1Periph_TIM5
#define MOTOR_FL_TIMER_CLK_B RCC_APB1Periph_TIM5
#define MOTOR_FL_CCR_A CCR2
#define MOTOR_FL_CCR_B CCR3
#define MOTOR_FR_TIMER_A TIM4
#define MOTOR_FR_TIMER_CLK_A RCC_APB1Periph_TIM4
#define MOTOR_FR_TIMER_B TIM4
#define MOTOR_FR_TIMER_CLK_B RCC_APB1Periph_TIM4
#define MOTOR_FR_CCR_A CCR3
#define MOTOR_FR_CCR_B CCR4
#define MOTOR_RL_TIMER_A TIM8
#define MOTOR_RL_TIMER_CLK_A RCC_APB2Periph_TIM8
#define MOTOR_RL_TIMER_B TIM8
#define MOTOR_RL_TIMER_CLK_B RCC_APB2Periph_TIM8
#define MOTOR_RL_CCR_A CCR1
#define MOTOR_RL_CCR_B CCR2
#define MOTOR_RR_TIMER_A TIM4
#define MOTOR_RR_TIMER_CLK_A RCC_APB1Periph_TIM4
#define MOTOR_RR_TIMER_B TIM8
#define MOTOR_RR_TIMER_CLK_B RCC_APB2Periph_TIM8
#define MOTOR_RR_CCR_A CCR2
#define MOTOR_RR_CCR_B CCR3
void Encoder_Configuration(void);
void PWM_Configuration(void);
int16_t Encoder_Read_FL (void);
int16_t Encoder_Read_FR (void);
int16_t Encoder_Read_RL (void);
int16_t Encoder_Read_RR (void);
int16_t Encoder_ReadOut_FL (void);
int16_t Encoder_ReadOut_FR (void);
int16_t Encoder_ReadOut_RL (void);
int16_t Encoder_ReadOut_RR (void);
void PWM_Set_FL_A(uint32_t us);
void PWM_Set_FL_B(uint32_t us);
void PWM_Set_FR_A(uint32_t us);
void PWM_Set_FR_B(uint32_t us);
void PWM_Set_RL_A(uint32_t us);
void PWM_Set_RL_B(uint32_t us);
void PWM_Set_RR_A(uint32_t us);
void PWM_Set_RR_B(uint32_t us);
void PWM_MotorFL_Set(int16_t output);
void PWM_MotorFR_Set(int16_t output);
void PWM_MotorRL_Set(int16_t output);
void PWM_MotorRR_Set(int16_t output);
int16_t PWM_MotorFL_Get(void);
int16_t PWM_MotorFR_Get(void);
int16_t PWM_MotorRL_Get(void);
int16_t PWM_MotorRR_Get(void);
void PWM_Motor_Set(int16_t outFL, int16_t outFR, int16_t outRL, int16_t outRR);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment