使用STM32f103的tim1_ch1输出pwm,我在keil里面仿真时,PA8脚输出的波形是需要的方波波形,但是放到硬件时,我测芯片PA8脚值时,始终是0.25V,是什么问题呢
- void TIM1_config(u32 Cycle)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphclockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOB , ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM1_CH1 PA8
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- TIM_TimeBaseStructure.TIM_Period = Cycle-1;
- TIM_TimeBaseStructure.TIM_Prescaler =71;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- 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 = Cycle/2-1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
-
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
- TIM_SelectOutputtrigger(TIM1, TIM_TRGOSource_Update);
-
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- }
- void TIM4_config(u32 PulseNum)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- TIM_TimeBaseStructure.TIM_Period = PulseNum-1;
- TIM_TimeBaseStructure.TIM_Prescaler =0;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
- TIM_SelectInputTrigger(TIM4, TIM_TS_ITR0);
- //TIM_InternalClockConfig(TIM4);
- TIM4->SMCR|=0x07;
- //TIM_ITRxExternalClockConfig(TIM4, TIM_TS_ITR0);
- //TIM_ARRPreloadConfig(TIM4, ENABLE);
- TIM_ITConfig(TIM4,TIM_IT_Update,DISABLE);
- // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void Pulse_output(u32 Cycle,u32 PulseNum)
- {
- TIM4_config(PulseNum);
- TIM_Cmd(TIM4, ENABLE);
- TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
- TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
- TIM1_config(Cycle);
-
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- }
- void TIM4_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
- TIM_CtrlPWMOutputs(TIM1, DISABLE);
- TIM_Cmd(TIM1, DISABLE);
- TIM_Cmd(TIM4, DISABLE);
- TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE);
-
- }
- }
复制代码
int main(void) { ChipHalInit(); ChipOutHalInit(); DA_Init(); // tmc2660_init(); TIM_Cmd(TIM2,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); Pulse_output(1000,8000); while(1) {} }
|