cancel
Showing results for 
Search instead for 
Did you mean: 

Connecting COMP and DAC

theory
Associate
Posted on June 01, 2015 at 22:00

Hello,

I working with STM32F3, and my goal is to feed in an external voltage into COMP2 (non-inverting input) compared against another constant analog voltage (inverting input), in order to produce a calibrated DAC output (which varies based on the external voltage into COMP2). I have attempted to configure the comparator and DAC as follows; however, I am rather confused regarding how to connect the two peripherals together. For example, does TIM4 input capture (

COMP_Output_TIM4IC1)

automatically connect with TIM4 TRGO (

DAC_Trigger_T4_TRGO

)? Help and guidance on how to approach this would be much appreciated! Thank you.

void dac_calibration_init()
{
COMP_InitTypeDef COMP_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
//COMP2 pin initialization
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//COMP2 configuration
COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_IO;
COMP_InitStructure.COMP_Output = COMP_Output_TIM4IC1;
COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;
COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_Medium;
COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;
COMP_Init(COMP_Selection_COMP2, COMP_InitStructure);
COMP_Cmd(COMP_Selection_COMP2, ENABLE);
//DAC1 Channel2 pin initialization
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//DAC1 Channel2 configuration
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
//DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC1, DAC_Channel_2, DAC_InitStructure);
DAC_Cmd(DAC1, DAC_Channel_2, ENABLE);
}
void timer4_init () 
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_SetCouter(TIM4, 0);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 1; 
TIM_TimeBaseStructure.TIM_Period = 0x0000; 
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1 ;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_Cmd(TIM4, ENABLE);
}

0 REPLIES 0