本帖最后由 锐利 于 2014-6-6 17:34 编辑
想有PA8产生500K方波,外部告诉晶振25M 为什么没有波形输出
程序如下:
int main(void)
{
SystemInit();
tiM1_PWM_Init(); //
while(1)
{
}
}
void TIM1_PWM_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器3时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //Timer1部分重映射 TIM1_CH2->PA8
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形 GPIOA.8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM1_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = 999; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =50; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_Pulse = 120;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 OC1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM3
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
问大神一个问题 我有个类似的代码 在stm32103系列中可以使用 没有TIME_Init这一说 波形可以实现 输出 到了stm322xx波形不能输出 代码和这个类似 我不知道怎么去设置TIM_Init ,下面是放在一个platform.c中的代码
void OUTGPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
OUTGPIO_CLK_INIT(OUTGPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = OUTGPIO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(OUTGPIO_PORT, &GPIO_InitStructure);
}
void TIM_Config(uint16_t Channel2Pulse)//, uint16_t Channel3Pulse
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM4 clock enable */
TIME4_CLK_INIT(TIME4_CLK, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999; //1KHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM2Ô¤·ÖƵÉèÖÃ:1MHZ,APB1·ÖƵϵÊý2£¬ÊäÈëµ½TIM3ʱÖÓΪ36MHzx2 = 72MHz
TIM_PrescalerConfig(TIM4,50, TIM_PSCReloadMode_Immediate);
/* Channel 2 and 3 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
// TIM_OC3Init(TIM2, &TIM_OCInitStructure);
//ʹÄÜTIM2¶¨Ê±¼ÆÊýÆ÷
TIM_Cmd(TIM4, ENABLE);
//ʹÄÜTIM2 PWMÊä³öģʽ
TIM_CtrlPWMOutputs(TIM4, ENABLE);
printf("maoxian ");
}
我在main中直接引用的 硬件部分宏定义了 也声明了 按照你的说法 我是不是也没有开启时钟
谢谢
加上这一句就OK了,造福一下后人······
一周热门 更多>