初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-7-9
|
【新手求助】
想用3个定时器12个通道来分别控制6个驱动(每两个通道控制一个驱动),但是接在示波器上只有一个定时器(TIM8)的四个通道好用,其他都没有波形,求问这是怎么一回事呢,应该怎样改?
- /********************motor.c**************************/
- #include "motor.h"
- // ʹÓà TIM1
- static void ADVANCE_TIM1_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(ADVANCE_TIM1_CH1_GPIO_CLK|\
- ADVANCE_TIM1_CH2_GPIO_CLK|\
- ADVANCE_TIM1_CH3_GPIO_CLK|\
- ADVANCE_TIM1_CH4_GPIO_CLK, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM1_CH1_PIN|\
- ADVANCE_TIM1_CH2_PIN|\
- ADVANCE_TIM1_CH3_PIN|\
- ADVANCE_TIM1_CH4_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init(ADVANCE_TIM1_CH1_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM1_CH2_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM1_CH3_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM1_CH4_PORT, &GPIO_InitStructure);
- }
- static void ADVANCE_TIM1_Mode_Config(void)
- {
- ADVANCE_TIM1_APBxClock_FUN(ADVANCE_TIM1_CLK,ENABLE);
-
- /* Time base Configuration */
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
- TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSC;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(ADVANCE_TIM1, &TIM_TimeBaseStructure);
-
- /* PWM1 Mode Configuration */
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
-
- /* Channel 1 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC1Init(ADVANCE_TIM1, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);
-
- /* Channel 2 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC2Init(ADVANCE_TIM1, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);
-
- /* Channel 3 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC3Init(ADVANCE_TIM1, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);
- /* Channel 4 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC4Init(ADVANCE_TIM1, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);
-
-
- TIM_Cmd(ADVANCE_TIM1, ENABLE);
- TIM_CtrlPWMOutputs(ADVANCE_TIM1, ENABLE);
- }
- static void ADVANCE_TIM1_Init(void)
- {
- ADVANCE_TIM1_GPIO_Config();
- ADVANCE_TIM1_Mode_Config();
- }
- // ʹÓà TIM8
- static void ADVANCE_TIM8_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(ADVANCE_TIM8_CH1_GPIO_CLK|\
- ADVANCE_TIM8_CH2_GPIO_CLK|\
- ADVANCE_TIM8_CH3_GPIO_CLK|\
- ADVANCE_TIM8_CH4_GPIO_CLK, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM8_CH1_PIN|\
- ADVANCE_TIM8_CH2_PIN|\
- ADVANCE_TIM8_CH3_PIN|\
- ADVANCE_TIM8_CH4_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init(ADVANCE_TIM8_CH1_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM8_CH2_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM8_CH3_PORT, &GPIO_InitStructure);
- GPIO_Init(ADVANCE_TIM8_CH4_PORT, &GPIO_InitStructure);
- }
- static void ADVANCE_TIM8_Mode_Config(void)
- {
- ADVANCE_TIM8_APBxClock_FUN(ADVANCE_TIM8_CLK,ENABLE);
-
- /* Time base Configuration */
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
- TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSC;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(ADVANCE_TIM8, &TIM_TimeBaseStructure);
-
- /* PWM1 Mode Configuration */
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
-
- /* Channel 1 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC1Init(ADVANCE_TIM8, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);
-
- /* Channel 2 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC2Init(ADVANCE_TIM8, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);
-
- /* Channel 3 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC3Init(ADVANCE_TIM8, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);
- /* Channel 4 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC4Init(ADVANCE_TIM8, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);
-
-
- TIM_Cmd(ADVANCE_TIM8, ENABLE);
- TIM_CtrlPWMOutputs(ADVANCE_TIM8, ENABLE);
- }
- static void ADVANCE_TIM8_Init(void)
- {
- ADVANCE_TIM8_GPIO_Config();
- ADVANCE_TIM8_Mode_Config();
- }
- // ʹÓà TIM3
- static void GENERAL_TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(GENERAL_TIM3_CH1_GPIO_CLK|\
- GENERAL_TIM3_CH2_GPIO_CLK|\
- GENERAL_TIM3_CH3_GPIO_CLK|\
- GENERAL_TIM3_CH4_GPIO_CLK, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GENERAL_TIM3_CH3_PIN|\
- GENERAL_TIM3_CH3_PIN|\
- GENERAL_TIM3_CH3_PIN|\
- GENERAL_TIM3_CH3_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init(GENERAL_TIM3_CH1_PORT, &GPIO_InitStructure);
- GPIO_Init(GENERAL_TIM3_CH2_PORT, &GPIO_InitStructure);
- GPIO_Init(GENERAL_TIM3_CH3_PORT, &GPIO_InitStructure);
- GPIO_Init(GENERAL_TIM3_CH4_PORT, &GPIO_InitStructure);
- }
- static void GENERAL_TIM3_Mode_Config(void)
- {
- GENERAL_TIM3_APBxClock_FUN(GENERAL_TIM3_CLK,ENABLE);
-
- /* Time base Configuration */
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
- TIM_TimeBaseStructure.TIM_Prescaler=TIM_PSC;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(GENERAL_TIM3, &TIM_TimeBaseStructure);
-
- /* PWM1 Mode Configuration */
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
-
- /* Channel 1 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC1Init(GENERAL_TIM3, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);
-
- /* Channel 2 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC2Init(GENERAL_TIM3, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);
-
- /* Channel 3 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC3Init(GENERAL_TIM3, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);
-
- /* Channel 4 */
- TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
- TIM_OC4Init(GENERAL_TIM3, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);
-
- TIM_Cmd(GENERAL_TIM3, ENABLE);
- }
- void ADVANCE_TIM_Init(void)
- {
- ADVANCE_TIM1_Init();
- ADVANCE_TIM8_Init();
- }
- void GENERAL_TIM_Init(void)
- {
- GENERAL_TIM3_GPIO_Config();
- GENERAL_TIM3_Mode_Config();
- }
复制代码
- /*******************************motor.h******************************/
- #ifndef __MOTOR_H__
- #define __MOTOR_H__
- #include "stm32f10x.h"
- /* ---------------- PWMÐźŠÖÜÆÚºÍÕ¼¿Õ±ÈµÄ¼ÆËã--------------- */
- // ARR £º×Ô¶¯ÖØ×°ÔؼĴæÆ÷µÄÖµ
- // CLK_cnt£º¼ÆÊýÆ÷µÄʱÖÓ£¬µÈÓÚ Fck_int / (psc+1) = 72M/(psc+1)
- // PWM ÐźŵÄÖÜÆÚ T = ARR * (1/CLK_cnt) = ARR*(PSC+1) / 72M
- // PWM ÐźŵÄƵÂÊ F = TIM_CLK/{(ARR+1)*(PSC+1)}
- // Õ¼¿Õ±ÈP=CCR/(ARR+1)
- #define TIM_PERIOD (1800-1) // AAR
- #define TIM_PSC (1-1)
- #define TIM_PULSE 900 // CCR
- // ʹÓø߼¶¶¨Ê±Æ÷ TIM1 <Ç°ÂÖ¿ØÖÆ>
- #define ADVANCE_TIM1 TIM1
- #define ADVANCE_TIM1_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define ADVANCE_TIM1_CLK RCC_APB2Periph_TIM1
- // ʹÓø߼¶¶¨Ê±Æ÷ TIM8 <ºóÂÖ¿ØÖÆ>
- #define ADVANCE_TIM8 TIM8
- #define ADVANCE_TIM8_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define ADVANCE_TIM8_CLK RCC_APB2Periph_TIM8
- // TIM1 Êä³ö±È½ÏͨµÀ
- #define ADVANCE_TIM1_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM1_CH1_PORT GPIOA
- #define ADVANCE_TIM1_CH1_PIN GPIO_Pin_8
- #define ADVANCE_TIM1_CH2_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM1_CH2_PORT GPIOA
- #define ADVANCE_TIM1_CH2_PIN GPIO_Pin_9
- #define ADVANCE_TIM1_CH3_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM1_CH3_PORT GPIOA
- #define ADVANCE_TIM1_CH3_PIN GPIO_Pin_10
- #define ADVANCE_TIM1_CH4_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM1_CH4_PORT GPIOA
- #define ADVANCE_TIM1_CH4_PIN GPIO_Pin_11
- // TIM8 Êä³ö±È½ÏͨµÀ
- #define ADVANCE_TIM8_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM8_CH1_PORT GPIOC
- #define ADVANCE_TIM8_CH1_PIN GPIO_Pin_6
- #define ADVANCE_TIM8_CH2_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM8_CH2_PORT GPIOC
- #define ADVANCE_TIM8_CH2_PIN GPIO_Pin_7
- #define ADVANCE_TIM8_CH3_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM8_CH3_PORT GPIOC
- #define ADVANCE_TIM8_CH3_PIN GPIO_Pin_8
- #define ADVANCE_TIM8_CH4_GPIO_CLK RCC_APB2Periph_GPIOA
- #define ADVANCE_TIM8_CH4_PORT GPIOC
- #define ADVANCE_TIM8_CH4_PIN GPIO_Pin_9
- // ʹÓÃͨÓö¨Ê±Æ÷ TIM3 <ÖÐÂÖ¿ØÖÆ>
- #define GENERAL_TIM3 TIM3
- #define GENERAL_TIM3_APBxClock_FUN RCC_APB1PeriphClockCmd
- #define GENERAL_TIM3_CLK RCC_APB1Periph_TIM3
- // TIM3 Êä³ö±È½ÏͨµÀ1
- #define GENERAL_TIM3_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
- #define GENERAL_TIM3_CH1_PORT GPIOA
- #define GENERAL_TIM3_CH1_PIN GPIO_Pin_6
- // TIM3 Êä³ö±È½ÏͨµÀ2
- #define GENERAL_TIM3_CH2_GPIO_CLK RCC_APB2Periph_GPIOA
- #define GENERAL_TIM3_CH2_PORT GPIOA
- #define GENERAL_TIM3_CH2_PIN GPIO_Pin_7
- // TIM3 Êä³ö±È½ÏͨµÀ3
- #define GENERAL_TIM3_CH3_GPIO_CLK RCC_APB2Periph_GPIOB
- #define GENERAL_TIM3_CH3_PORT GPIOB
- #define GENERAL_TIM3_CH3_PIN GPIO_Pin_0
- // TIM3 Êä³ö±È½ÏͨµÀ4
- #define GENERAL_TIM3_CH4_GPIO_CLK RCC_APB2Periph_GPIOB
- #define GENERAL_TIM3_CH4_PORT GPIOB
- #define GENERAL_TIM3_CH4_PIN GPIO_Pin_1
- void ADVANCE_TIM_Init(void);
- void GENERAL_TIM_Init(void);
- #endif /* __MOTOR_H__ */
复制代码
- /**************************main.c***************************/
- #include "stm32f10x.h"
- #include "motor.h"
- int main(void)
- {
- ADVANCE_TIM_Init();
- GENERAL_TIM_Init();
-
- while(1)
- {
- TIM_SetCompare1(TIM1,TIM_PULSE); // GPIO->PA8
- TIM_SetCompare2(TIM1,TIM_PULSE); // GPIO->PA9
- TIM_SetCompare3(TIM1,TIM_PULSE); // GPIO->PA10
- TIM_SetCompare4(TIM1,TIM_PULSE); // GPIO->PA11
-
- TIM_SetCompare1(TIM8,TIM_PULSE); // GPIO->PC6
- TIM_SetCompare2(TIM8,TIM_PULSE); // GPIO->PC7
- TIM_SetCompare3(TIM8,TIM_PULSE); // GPIO->PC8
- TIM_SetCompare4(TIM8,TIM_PULSE); // GPIO->PC9
-
- TIM_SetCompare1(TIM3,TIM_PULSE); // GPIO->PA6
- TIM_SetCompare2(TIM3,TIM_PULSE); // GPIO->PA7
- TIM_SetCompare3(TIM3,TIM_PULSE); // GPIO->PB0
- TIM_SetCompare4(TIM3,TIM_PULSE); // GPIO->PB1
- }
- }
复制代码
|
|