From 6be256a1356f7f819706ec082750a9a987a8ce00 Mon Sep 17 00:00:00 2001 From: but0n Date: Fri, 30 Sep 2016 23:31:15 +0800 Subject: [PATCH] [MOD]: 4 Channels PWM --- README.md | 1 + .../STM32F10x_StdPeriph_Driver/inc/motor.h | 12 +++++++++-- .../STM32F10x_StdPeriph_Driver/src/motor.c | 20 +++++++++++++++---- src/main.c | 16 ++++++++++----- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cfadaf4..a7f6d51 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [UAV] - [STM32F103] - 6 Axes sensor MPU6050 - Quaternion & Euler Angles Compute - PID Controller + # GPIO List MPU6050 | BLDC | Wi-Fi | GPS :----: |:----: |:----: |:---: diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h index 5f77417..0bc4be7 100644 --- a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h @@ -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);\ } diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c index 4a8fda4..15b64c5 100644 --- a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c @@ -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 diff --git a/src/main.c b/src/main.c index e471a4b..36a94b7 100644 --- a/src/main.c +++ b/src/main.c @@ -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