- #include "hard_pwm.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_tim.h"
- #define MOTOR_PERIOD 15000
- // pwm¹Ü½Å·ÖÅä
- // TIM2_CH1 PWM1 PA0
- // TIM2_CH2 PWM2 PA1
- // TIM2_CH3 PWM3 PB10
- // TIM2_CH4 PWM4 PB11
- // TIM3_CH1 PWM5 PA6
- // TIM3_CH2 PWM6 PA7
- // TIM4_CH1 PWM7 PB6
- // TIM4_CH2 PWM8 PB7
- char is_pwm_output_enable = 0;
- void pwm_output(char m_ch, short m_value)
- {
- switch (m_ch)
- {
- case 1:
- TIM2->CCR1 = m_value;
- break;
- case 2:
- TIM2->CCR2 = m_value;
- break;
- case 3:
- TIM2->CCR3 = m_value;
- break;
- case 4:
- TIM2->CCR4 = m_value;
- break;
- case 5:
- TIM3->CCR1 = m_value;
- break;
- case 6:
- TIM3->CCR2 = m_value;
- break;
- case 7:
- TIM4->CCR1 = m_value;
- break;
- case 8:
- TIM4->CCR1 = m_value;
- break;
- default:
- break;
- }
- }
- static void pwm_gpio_init(void)
- {
- GPIO_InitTypeDef gpio;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
- RCC_APB2Periph_AFIO,
- ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
- RCC_APB1Periph_TIM4,
- ENABLE);
- GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);
- gpio.GPIO_Mode = GPIO_Mode_AF_PP;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_Init(GPIOA, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_Init(GPIOB, &gpio);
- }
- static void pwm_tim_init(void)
- {
- TIM_TimeBaseInitTypeDef tim_timebase_structure;
- TIM_OCInitTypeDef tim_ocinit_stucture;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
- RCC_APB1Periph_TIM4,
- ENABLE);
- // TIMʱÖÓ·ÖƵ£¬Êä³öPWMÖÜÆÚ50Hz
- tim_timebase_structure.TIM_Period = MOTOR_PERIOD;
- tim_timebase_structure.TIM_Prescaler = 72 - 1;
- tim_timebase_structure.TIM_ClockDivision = TIM_CKD_DIV1;
- tim_timebase_structure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &tim_timebase_structure);
- TIM_TimeBaseInit(TIM3, &tim_timebase_structure);
- TIM_TimeBaseInit(TIM4, &tim_timebase_structure);
- tim_ocinit_stucture.TIM_OCMode = TIM_OCMode_PWM1;
- tim_ocinit_stucture.TIM_OutputNState = TIM_OutputState_Enable;
- tim_ocinit_stucture.TIM_Pulse = 1000;
- tim_ocinit_stucture.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM2, &tim_ocinit_stucture);
- TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
- TIM_OC2Init(TIM2, &tim_ocinit_stucture);
- TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
- TIM_OC3Init(TIM2, &tim_ocinit_stucture);
- TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
- TIM_OC4Init(TIM2, &tim_ocinit_stucture);
- TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
- TIM_OC1Init(TIM3, &tim_ocinit_stucture);
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_OC2Init(TIM3, &tim_ocinit_stucture);
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_OC1Init(TIM4, &tim_ocinit_stucture);
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_OC2Init(TIM4, &tim_ocinit_stucture);
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM2, ENABLE);
- TIM_ARRPreloadConfig(TIM3, ENABLE);
- TIM_ARRPreloadConfig(TIM4, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- TIM_Cmd(TIM4, ENABLE);
- }
- static void pwm_gpio_deinit(void)
- {
- GPIO_DeInit(GPIOA);
- GPIO_DeInit(GPIOB);
- }
- static void pwm_tim_deinit(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
- RCC_APB1Periph_TIM4,
- ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
- RCC_APB1Periph_TIM4,
- DISABLE);
- }
- void pwm_output_init(void)
- {
- if (is_pwm_output_enable == 0)
- {
- pwm_gpio_init();
- pwm_tim_init();
- for (int i = 1; i < 9; ++i)
- pwm_output(i, 1000);
- is_pwm_output_enable = 1;
- }
- }
- void pwm_output_deinit(void)
- {
- if (is_pwm_output_enable == 1)
- {
- pwm_tim_deinit();
- // pwm_gpio_deinit();
- is_pwm_output_enable = 0;
- }
- }
复制代码 以上是我的配置代码,单pwm没有输出,请问各位是哪配置错了吗?
|
不是那的问题,写错了一个地方
tim_ocinit_stucture.TIM_OutputNState 应该是 tim_ocinit_stucture.TIM_OutputState,自动补全一滑丢点偏了
评分
查看全部评分