2018-03-12 08:31 AM
Hi,
I am having a problem using COMP1 on a STM32l496 chip. I am doing a proof of concept on a Nucleo board. I'm using a signal generator to put a 1KHz sine wave on the IN+ pin, and I'm using the DAC as the IN- signal, with the value set to 0. I'm using CubeMX to generate code for this. When I run this setup, I get no changes on the comparator. When I configure the project to use an external pin as IN-, and attach that pin to ground, I get the expected toggling output from the comparator. When I look at the DAC output, by configuring it's output to go to both an on-chip peripheral and external pin and looking at the pin, the output looks fine. Does anyone have any idea what I may be doing wrong? I'm including a code snippet of the COMP1 and DAC setup, and also the lines to show they are running.
// from main()
HAL_COMP_Start(&hcomp1);
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 100);/* COMP1 init function */
static void MX_COMP1_Init(void){hcomp1.Instance = COMP1;
hcomp1.Init.InvertingInput = COMP_INPUT_MINUS_DAC1_CH2; hcomp1.Init.NonInvertingInput = COMP_INPUT_PLUS_IO2; hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED; hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE; hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE; hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED; hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE; hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE; if (HAL_COMP_Init(&hcomp1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }}
/* DAC1 init function */
static void MX_DAC1_Init(void){DAC_ChannelConfTypeDef sConfig;
/**DAC Initialization
*/ hdac1.Instance = DAC1; if (HAL_DAC_Init(&hdac1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }/**DAC channel OUT2 config
*/ sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE; sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }}
Ricardo
Solved! Go to Solution.
2018-04-02 10:41 AM
I finally figured this out. I had the gpio pins of the comparator configured incorrectly. The input pin needs to have the alternate function set so that it's properly internally routed to the comparator, and the output needed to be set to analog. The hint was when I looked at the COMP1 status register, and I could see that it was changing even though the output pin was not. Trivial no doubt, but it was a real blocker till I got it.
Ricardo
2018-04-02 10:41 AM
I finally figured this out. I had the gpio pins of the comparator configured incorrectly. The input pin needs to have the alternate function set so that it's properly internally routed to the comparator, and the output needed to be set to analog. The hint was when I looked at the COMP1 status register, and I could see that it was changing even though the output pin was not. Trivial no doubt, but it was a real blocker till I got it.
Ricardo