我想用STM32F051的单片机的PB13(复用功能为TIM1_1N)输出一PWM波形,搞了一下午没有搞定,麻烦高手给指点一下,看一下下面的程序问题出在哪里,谢谢了!
uint16_t PrescalerValue = 0;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStruct;
/* (#)TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* (#)Configure the TIM pins by configuring the corresponding GPIO pins */
/* GPIOB Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure pins as Output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* TIM1_CH1N Pins configuration Connect pin to Periph PB13 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_2);
/* (#) Configure the Time base unit as described in the first part of this
driver, if needed, else the Timer will run with the default
configuration:
(++) Autoreload value = 0xFFFF.
(++) Prescaler value = 0x0000.
(++) Counter mode = Up counting.
(++) Clock Division = TIM_CKD_DIV1.
*/
/* (#) Fill the TIM_TimeBaseInitStruct with the desired parameters.*/
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (48000000/4800000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
/*(#) Call TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct) to configure
the Time Base unit with the corresponding configuration.
*/
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM1,ENABLE);
/*(#) Enable the NVIC if you need to generate the update interrupt.*/
/*(#) Enable the corresponding interrupt using the function
TIM_ITConfig(TIMx, TIM_IT_Update).
*/
/* (#) Fill the TIM_OCInitStruct with the desired parameters including:
(++) The TIM Output Compare mode: TIM_OCMode.
(++) TIM Output State: TIM_OutputState.
(++) TIM Pulse value: TIM_Pulse.
(++) TIM Output Compare Polarity : TIM_OCPolarity.
*/
/* Output Compare Timing Mode configuration: Channel1N */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 4800; // CCR1: 1000HZ
//TIM_OCInitStructure.TIM_Pulse = 2400; // CCR1: 2000HZ
//TIM_OCInitStructure.TIM_Pulse = 1200; // CCR1: 4000HZ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
/* (#) Call TIM_OCxInit(TIMx, &TIM_OCInitStruct) to configure the desired
channel with the corresponding configuration.
*/
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
/* Prescaler configuration */
//TIM_PrescalerConfig(TIM1, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* Init TIM_OCInitStructure */
//TIM_OCStructInit(&TIM_OCInitStructure);
/* (#) Fill the TIM_BDTRInitStruct with the desired parameters for the Timer
Break Polarity, dead time, Lock level, the OSSI/OSSR State and the
AOE(automatic output enable)
*/
TIM_BDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRInitStruct.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStruct.TIM_DeadTime = 0x90;
TIM_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Enable;
/* (#) Call TIM_BDTRConfig(TIMx, &TIM_BDTRInitStruct) to configure the Timer */
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStruct);
/* (#) Enable the Main Output using TIM_CtrlPWMOutputs(TIM1, ENABLE) */
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* (#) Call the TIM_Cmd(ENABLE) function to enable the TIM counter.*/
/* TIM1 Enable counter */
TIM_Cmd(TIM1, ENABLE);
|