研究生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-28
|
本帖最后由 Vicky 于 2015-9-25 11:32 编辑
/**
******************************************************************************
* @file bsp_pwm_output.c
* @author STMicroelectronics
* @version V1.0
* @date 2013-xx-xx
* @brief tim3êä3öËÄ·pwm bsp
******************************************************************************
* @attention
*
* êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å
* ÂÛì3 :http://www.firebbs.cn
* ìÔ±| :http://firestm32.taobao.com
*
******************************************************************************
*/
#include "bsp_pwm_output.h"
/**
* @brief ÅäÖÃTIM3¸′óÃêä3öWMê±óÃμ½μÄI/O
* @param ÎT
* @retval ÎT
*/
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* éèÖÃTIM3CLK Îa 72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &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)
*/
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMDÅoÅμçƽìø±äÖμ */
u16 CCR1_Val = 500;
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
/* -----------------------------------------------------------------------
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR+1)* 100% = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR+1)* 100% = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR+1)* 100% = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_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(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃÎ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(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà1
TIM_OC1PreloadConfig(TIM3, 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(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà2
TIM_OC2PreloadConfig(TIM3, 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(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3
TIM_OC3PreloadConfig(TIM3, 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(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3
}
/**
* @brief TIM3 êä3öWMDÅoÅ3õê¼»ˉ£¬Ö»òaμ÷óÃÕa¸öoˉêy
* TIM3μÄËĸöí¨μà¾í»áóDPWMDÅoÅêä3ö
* @param ÎT
* @retval ÎT
*/
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
/*********************************************END OF FILE**********************/
这程序没错啊,为什么我用软件仿真,出来的波形都是一条直线啊,全为低电平????到底为什么啊啊啊啊啊????????????????? |
|