库函数捕捉程序如下:
void main(void)
{
TIM1_Channel = TIM1_CHANNEL_1
TIM1_ICPolarity = TIM1_ICPOLARITY_FALLING
TIM1_ICSelection = TIM1_ICSELECTION_DIRECTTI
TIM1_ICPrescaler = TIM1_ICPSC_DIV8
TIM1_ICFilter = 0x0
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0x0);
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Connect LSI to COO pin*/
GPIO_Init(GPIOE, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
CLK_CCOConfig(CLK_OUTPUT_LSI);
CLK_CCOCmd(ENABLE);
/* wait a capture on CC1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Compute LSI clock frequency */
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
/* Insert a break point here */
while (1);
}
在此库函数基础上,改为TIME2产生PWM程序如下,
void init_hsiclk(void)
{
CLK_DeInit(); //寄存器复位
CLK_HSICmd(ENABLE); //内部高速时钟使能
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //内部分频 2M
}
void TIM2_init(void)
{
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 6665);
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC3PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
}
void main(void)
{
//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//主时钟16/16=1MHZ 4分频
init_hsiclk();
GPIO_Init(TIME2PWMOUT_PORT, TIME2PWMOUT_PIN , GPIO_MODE_OUT_PP_LOW_FAST); //设置PA3端口为推挽输出模式
GPIO_Init(TIME1PWMIN_PORT , TIME1PWMIN_PIN, GPIO_MODE_IN_FL_NO_IT);
TIM2_init();
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0x0);
/* Enable CC1 interrupt */
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Connect LSI to COO pin*/
//GPIO_Init(GPIOE, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST); //不使用
//CLK_CCOConfig(CLK_OUTPUT_LSI);
//CLK_CCOCmd(ENABLE);
/* wait a capture on CC1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Compute LSI clock frequency */
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
/* Insert a break point here */
while (1);
} |