本帖最后由 rensongwang 于 2017-7-21 13:10 编辑
下面是我的代码,不管怎么弄都没波形输出,有谁能帮我看看怎么回事吗?这里面TIM2可以不管,就用了TIM4.原来用的的TIM4_CH1, PB6输出的,现在改到TIM4_CH3, PB8输出,一点反应也没有。
:[mw_shl_code=c,true]
#include "TIM_PWM_Config.h"
void TIM4_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//NVIC_Configuration(1);
/* GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//open SYSCFG clock for EXTI input
/*GPIOB Configuration: TIM4 channel 1 and 2 as alternate function push-pull */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);
//EXTI input configure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void Tim2_Slave_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//NVIC_Configuration(8);
TIM_TimeBaseStructure.TIM_Period= 40000;
TIM_TimeBaseStructure.TIM_Prescaler= 0; //ê±ÖóÔ¤·ÖÆμ
TIM_TimeBaseStructure.TIM_ClockDivision=0; //2éÑù·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM2,TIM_TS_ITR3);//TIMx internal trigger connection±í
TIM_InternalClockConfig(TIM2);
//TIM2->SMCR |= 0x0007;//é趨′óģ꽣¬¼ÆêyÄ£ê½
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1);
//TIM_UpdateDisableConfig(TIM4,ENABLE);
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Global);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ClearFlag(TIM2,TIM_FLAG_Update); //Çå3yòç3öÖD¶Ï±êÖ¾
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);//¿aÆôê±Öó(¿aÆôoóÿ·¢Ëíò»¸öÂö3壬¶¨ê±Æ÷¼ó1)
}
/*
void Tim3_Slave_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
NVIC_Configuration(9);
TIM_TimeBaseStructure.TIM_Period= XBUF[3] - 1;
TIM_TimeBaseStructure.TIM_Prescaler= 0; //??????
TIM_TimeBaseStructure.TIM_ClockDivision=0; //????
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//????
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM3,TIM_TS_ITR3);//??TIM2?????? ????3 TIM4
TIM_InternalClockConfig(TIM3);
TIM3->SMCR |= 0x0007;//??????????,??????1 ???????
//TIM_UpdateDisableConfig(TIM4,ENABLE);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //????????
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);//??????(??????????,?????)
}*/
/*
* ???:TIM4_Mode_Config
* ?? :??TIM4???PWM?????,??????????
* ?? :?
* ?? :?
* ?? :????
*/
void TIM4_PWM_Init(u16 T)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//u16 CCR1_Val = T/2;
//u16 CCR2_Val = (T+1)/2;
//u16 CCR3_Val = (T+1)/2;
//u16 CCR4_Val = (T+1)/2;
/* TIM4 clock enable */
//TIM4ê±ÖóÔ′μèóú84MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM4);
/* -----------------------------------------------------------------------
TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM1CLK = 36 MHz, Prescaler = 0x0, TIM1 counter clock = 36 MHz
TIM1 ARR Register = 999 => TIM1 Frequency = TIM1 counter clock/(ARR + 1)
TIM1 Frequency = 36 KHz.
TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 50%
TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR)* 100 = 50%
TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR)* 100 = 50%
TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR)* 100 = 50%
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = T-1;//T us
TIM_TimeBaseStructure.TIM_Prescaler = 84-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM4->CR1 &= ((u16)0x03FD);// UDIS enable
//TIM_UpdateDisableConfig(TIM4,DISABLE);
/* PWM1 Mode configuration: Channel1 */
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = T/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);*/
/*
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);*/
TIM_ARRPreloadConfig(TIM4, ENABLE);
//TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
/* TIM1 enable counter */
TIM_Cmd(TIM4, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}[/mw_shl_code]
下面是main函数里面的相关调用:
TIM4_GPIO_Config();
Tim2_Slave_Init();
TIM4_PWM_Init(2500);
while(1) {}
不想多说什么.这种错 自己都发现不出来
我就呵呵了
还是从自身找问题吧,
论坛大佬说的对,
走路还不会呢,就想跑.
当心扯着淡
ps:不一定是这个错导致的不输出
一周热门 更多>