首先谢谢各位的建议与支持,感谢ing,当然也离不开自己的探索!
之前不能得到正确的波形,主要是混淆了PWM频率和信号的采样频率,错误地使用了用DAC做输出时的8K频率,导致滤波电路无法正常工作. 以下的程序我使用TIMER4 channel3作为PWM输出,并使用TIMER2做8K定时去更新PWM占空比,输出了1KHz的正弦波。
/* --------------------------------------------------------------- TIM4 Configuration: Output Compare Inactive Mode: TIM4CLK = 72 MHz, Prescaler = 0, TIM2 counter clock = 72 KHz TIM4 frequency = TIM4CLK/(TIM1_Period + 1) = 70 KHz --------------------------------------------------------------- */ void TIMER4_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1023; TIM_TimeBaseStructure.TIM_Prescaler = 0x00; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Channel = TIM_Channel_3; TIM_OCInitStructure.TIM_Pulse = 512; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
/* TIM4 enable counter */ TIM_Cmd(TIM4, ENABLE);
}
void TIMER2_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
/* --------------------------------------------------------------- TIM2 Configuration: Output Compare Inactive Mode: TIM2CLK = 72 MHz, Prescaler = 1000, TIM2 counter clock = 72 KHz TIM2 frequency = TIM2CLK/(TIM2_Period + 1) = 8KHz --------------------------------------------------------------- */
/* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 8; TIM_TimeBaseStructure.TIM_Prescaler = 1000-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration */ // TIM_PrescalerConfig(TIM2, 1000-1, TIM_PSCReloadMode_Immediate);
/* Output Compare Active Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 8; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
/* Set PC.06, PC.07, PC.08 and PC.09 pins */ GPIO_SetBits(GPIOC, GPIO_Pin_6);
/* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); }
u16 SINDATA[8]= { 512,874,1024,874, 512,149,0,149 };
/******************************************************************************* * Function Name : TIM2_IRQHandler * Description : This function handles TIM2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void TIM2_IRQHandler(void) { static u16 i=0; static u8 j=0;
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { /* Clear TIM2 Capture Compare1 interrupt pending bit*/ TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); } if(i==0) { i = 1; GPIO_ResetBits(GPIOC, GPIO_Pin_6); } else { i = 0; GPIO_SetBits(GPIOC, GPIO_Pin_6); }
// /* Set the Pulse value */ TIM4->CCR3 = SINDATA[j++]; if(j == 8)j = 0; }
|