[MOD]: 4 Channels PWM

This commit is contained in:
but0n 2016-09-30 23:31:15 +08:00
parent bf09f675a7
commit 6be256a135
4 changed files with 38 additions and 11 deletions

View File

@ -5,6 +5,7 @@
[UAV] - [STM32F103] - 6 Axes sensor MPU6050 - Quaternion & Euler Angles Compute - PID Controller
# GPIO List
MPU6050 | BLDC | Wi-Fi | GPS
:----: |:----: |:----: |:---:

View File

@ -3,8 +3,10 @@
#define MOTOR_H_
#define MOTOR1 TIM3->CCR2
#define MOTOR2 TIM3->CCR1
#define MOTOR1 TIM3->CCR1
#define MOTOR2 TIM3->CCR2
#define MOTOR3 TIM3->CCR3
#define MOTOR4 TIM3->CCR4
#define MOTOR_MAX (unsigned short)27360
#define MOTOR_MIN (unsigned short)10080
@ -12,8 +14,14 @@
#define MOTOR_SETTING() {\
motor_PWM_Init(28800,5);\
MOTOR1 = MOTOR_MAX;\
MOTOR2 = MOTOR_MAX;\
MOTOR3 = MOTOR_MAX;\
MOTOR4 = MOTOR_MAX;\
delay(3000);\
MOTOR1 = MOTOR_MIN;\
MOTOR2 = MOTOR_MIN;\
MOTOR3 = MOTOR_MIN;\
MOTOR4 = MOTOR_MIN;\
delay(8000);\
}

View File

@ -8,22 +8,34 @@ void motor_PWM_Init(unsigned short arr, unsigned short psc) {
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; //TIM3 Enable
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //IO Port A Enable
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
GPIOA->CRL &= 0x00FFFFFF; //Clean
GPIOA->CRL |= 0xBB000000; //复用推挽输出
GPIOA->ODR |= 1<<7; //CH2 GPIO Config
GPIOA->ODR |= 1<<6; //CH1 GPIO Config
GPIOB->CRL &= 0xFFFFFF00;
GPIOB->CRL |= 0x000000BB;
GPIOB->ODR |= 1;
GPIOB->ODR |= 1<<1;
TIM3->ARR = arr - 1;
TIM3->PSC = psc - 1;
TIM3->CCMR1 |= 6<<4; //CH1 Set OC1M[2:0]: PWM Mode
TIM3->CCMR1 |= 1<<3; //CH1 Set OC1PE: Enable
TIM3->CCMR1 |= 6<<12; //CH2 Set OC2M[2:0]: PWM Mode
TIM3->CCMR1 |= 1<<11; //CH2 Set OC2PF: Enable
TIM3->CCMR1 |= 7<<4; //CH1 Set OC2M[2:0]: PWM Mode
TIM3->CCMR1 |= 1<<3; //CH1 Set OC2PF: Enable
TIM3->CCMR1 |= 1<<11; //CH2 Set OC2PE: Enable
TIM3->CCMR2 |= 6<<4; //CH3 Set OC3M[2:0]: PWM Mode
TIM3->CCMR2 |= 1<<3; //CH3
TIM3->CCMR2 |= 6<<12; //CH4
TIM3->CCMR2 |= 1<<11;
TIM3->CCER |= 1<<4; //CH2 Output Enable
TIM3->CCER |= 1; //CH1 Output Enable
TIM3->CCER |= 1<<4; //CH2 Output Enable
TIM3->CCER |= 1<<8;
TIM3->CCER |= 1<<12;
TIM3->CR1 = 0x80; //APRE Enable
TIM3->CR1 |= 1; //Set CEN, Allow to Count

View File

@ -30,11 +30,17 @@ void pid(float setPoint, float d) {
short resu = (short)(g_iErro * K_P + g_sumErro * K_I + d * K_D); //PID输出
if((MOTOR1 + resu) > MOTOR_MAX)
MOTOR1 = MOTOR_MAX;
else if((MOTOR1 + resu) < MOTOR_LOW)
MOTOR1 = MOTOR_LOW;
else MOTOR1 += resu;
if((MOTOR2 + resu) > MOTOR_MAX)
MOTOR2 = MOTOR_MAX;
else if((MOTOR2 + resu) < MOTOR_LOW)
MOTOR2 = MOTOR_LOW;
else MOTOR2 += resu;
if((MOTOR4 + resu) > MOTOR_MAX)
MOTOR4 = MOTOR_MAX;
else if((MOTOR4 + resu) < MOTOR_LOW)
MOTOR4 = MOTOR_LOW;
else MOTOR4 += resu;
}
//ms