学前班
最后登录1970-1-1
在线时间 小时
注册时间2018-4-27
|
我想的是使用TIM4的PB6 7 8 9四个端口输出PWM,main函数里面只是控制占空比的函数,下面两个是设置时钟和输出PWM的代码,附图的第一张是PB6 7输出的波形,第二张是PB8 9输出的波形,我真的不知道问题出在哪里了,求大神帮我解决一下吧,在线等,很急,谢谢了。
//bsp_pwm_output.h
#include "stm32f10x.h"
/********************TIM2Îêy¶¨ò壬ֻÏTTIM2¡¢3¡¢4»ò5**************************/
#define macTIMx TIM4
#define macTIM_APBxClock_FUN RCC_APB1PeriphClockCmd
#define macTIM_CLK RCC_APB1Periph_TIM4
#define macTIM_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define macTIM_GPIO_CLK ( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB )
#define macTIM_CH1_PORT GPIOB
#define macTIM_CH1_PIN GPIO_Pin_6
#define macTIM_CH2_PORT GPIOB
#define macTIM_CH2_PIN GPIO_Pin_7
#define macTIM_CH3_PORT GPIOB
#define macTIM_CH3_PIN GPIO_Pin_8
#define macTIM_CH4_PORT GPIOB
#define macTIM_CH4_PIN GPIO_Pin_9
/************************** TIM oˉêyéùÃ÷********************************/
void TIMx_PWM_Init (void);
//bsp_pwm_output.c
#include "bsp_pwm_output.h"
static void TIMx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);//TIM4¸′óö˿ú
// GPIO_PinRemapConfig(GPIO_Remap_TIM4, DISABLE);
/* éèÖÃTIM4CLK Îa 72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* ----------------------------------------------------------------------- */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4,ENABLE); //í¨1y¶Ôó|λéèÖÃê1TIM4¼Ä′æÆ÷¸′λ
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4,DISABLE); //íË3ö¸′λ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);//¿aÆôGPIOBê±Öó
/* RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);*/
/*----------------------------------------------------------------------- */
macTIM_APBxClock_FUN (macTIM_CLK, ENABLE);
/* GPIOA and GPIOB clock enable */
macTIM_GPIO_APBxClock_FUN (macTIM_GPIO_CLK, ENABLE);
/*GPIOA Configuration: TIM3 channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = macTIM_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(macTIM_CH1_PORT, &GPIO_InitStructure);
/*GPIOB Configuration: TIM3 channel 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = macTIM_CH2_PIN;
GPIO_Init(macTIM_CH2_PORT, &GPIO_InitStructure);
/*GPIOB Configuration: TIM3 channel 3 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = macTIM_CH3_PIN;
GPIO_Init(macTIM_CH3_PORT, &GPIO_InitStructure);
/*GPIOB Configuration: TIM3 channel 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = macTIM_CH4_PIN;
GPIO_Init(macTIM_CH4_PORT, &GPIO_InitStructure);
}
/**
* @brief ÅäÖÃTIM3êä3öμÄWMDÅoÅμÄģ꽣¬èçÖüÆú¡¢¼«DÔ¡¢Õ¼¿Õ±è
* @param ÎT
* @retval ÎT
*/
/*
* TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> TIMxCNT ÖØD¼Æêy
* TIMx_CCR(μçƽ·¢éú±ä»ˉ)
* DÅoÅÖüÆú=(TIMx_ARR +1 ) * ê±ÖóÖüÆú
* Õ¼¿Õ±è=TIMx_CCR/(TIMx_ARR +1)
*/
extern int a,b,c,d;
static void TIMx_Mode_Config( )
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMDÅoÅμçƽìø±äÖμ */
/*u16 CCR1_Val = 500; */ //50%
u16 CCR1_Val = a;
u16 CCR2_Val = b;
u16 CCR3_Val = c;
u16 CCR4_Val = d;
/* -----------------------------------------------------------------------
macTIMx Channel1 duty cycle = (macTIMx_CCR1/ macTIMx_ARR+1)* 100% = 50%
macTIMx Channel2 duty cycle = (macTIMx_CCR2/ macTIMx_ARR+1)* 100% = 37.5%
macTIMx Channel3 duty cycle = (macTIMx_CCR3/ macTIMx_ARR+1)* 100% = 25%
macTIMx Channel4 duty cycle = (macTIMx_CCR4/ macTIMx_ARR+1)* 100% = 12.5%
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999; //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(macTIMx, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //ÅäÖÃÎaPWMÄ£ê½1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
TIM_OC1Init(macTIMx, &TIM_OCInitStructure); //ê1Äüí¨μà1
TIM_OC1PreloadConfig(macTIMx, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //éèÖÃí¨μà2μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC2Init(macTIMx, &TIM_OCInitStructure); //ê1Äüí¨μà2
TIM_OC2PreloadConfig(macTIMx, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC3Init(macTIMx, &TIM_OCInitStructure); //ê1Äüí¨μà3
TIM_OC3PreloadConfig(macTIMx, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC4Init(macTIMx, &TIM_OCInitStructure); //ê1Äüí¨μà4
TIM_OC4PreloadConfig(macTIMx, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(macTIMx, ENABLE); // ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR
/*----------------------------------------------------------------------- */
TIM_ITConfig(macTIMx,TIM_IT_Update,DISABLE); //1رÕÖD¶Ï
TIM_ClearFlag(macTIMx,TIM_FLAG_Update); //Çå3yÅäÖÃê±oò2úéúμĸüD±êÖ¾
/*----------------------------------------------------------------------- */
/* TIM3 enable counter */
TIM_Cmd(macTIMx, ENABLE); //ê1Äü¶¨ê±Æ÷3
}
/**
* @brief TIM3 êä3öWMDÅoÅ3õê¼»ˉ£¬Ö»òaμ÷óÃÕa¸öoˉêy
* TIM3μÄËĸöí¨μà¾í»áóDPWMDÅoÅêä3ö
* @param ÎT
* @retval ÎT
*/
void TIMx_PWM_Init ()
{
TIMx_GPIO_Config();
TIMx_Mode_Config();
}
/*********************************************END OF FILE**********************/
|
-
PB6 7输出波形
-
PB8 9输出波形
|