库里面有个例子是测量pwm的,可以参考
贴下目前我用的:
void DAC_Config(void)
{
DAC_InitTypeDef DAC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* DAC clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* Fill DAC InitStructure */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits2_0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
/*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
*/
/* DAC Channel2 Init */
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Enable DAC Channel2 */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* Set DAC Channel2 DHR register: DAC_OUT2 = (3.3 * 868) / 4095 ~ 0.7 V */
DAC_SetChannel2Data(DAC_Align_12b_R, 2);
}
/**
* @brief Configures COMP2: DAC channel 2 to COMP2 inverting input
* and COMP2 output to TIM2 IC4.
* @param None
* @retval None
*/
void COMP_Config(void)
{
COMP_InitTypeDef COMP_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/**************************** COMP2 Config **********************************/
/* GPIOB Peripheral clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configuring PB5 in analog mode closes the switch GR6-2 so PB5 is connected
to COMP2 non inverting input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* COMP clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_RIIOSwitchConfig(RI_IOSwitch_GR6_2,ENABLE);
/* COMP2 Init: COMP2 is enabled as soon as inverting input is selected */
/* use DAC2 output as a reference voltage: DAC2 output is connected to COMP2
inverting input */
COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC2;
/* Redirect COMP2 output to TIM2 Input capture 4 */
COMP_InitStructure.COMP_OutputSelect = COMP_OutputSelect_TIM2IC4;
COMP_InitStructure.COMP_Speed = COMP_Speed_Fast;
COMP_Init(&COMP_InitStructure);
}
/**
* @brief Configures TIM2 channel 4 in input capture mode
* @param None
* @retval None
*/
void TIM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/**************************** TIM2 Config ***********************************/
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM2 Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;//fck_cnt = 8M/2 = 4Mhz
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update|TIM_FLAG_CC4);
/* TIM2 Channel4 Input capture Mode configuration */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
/* TIM2 counter is captured at each transition detection: rising or falling edges (both edges) */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 8;//once 4us
TIM_ICInit(TIM2, &TIM_ICInitStructure);
/* TIM2 IRQChannel enable */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // time2 capture
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable capture interrupt */
TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE);
/* Enable the TIM2 counter */
TIM_Cmd(TIM2, ENABLE);
/* Reset the flags */
TIM2->SR = 0;
}
|