cancel
Showing results for 
Search instead for 
Did you mean: 

Help Required: Comparator Interrupt based Time measurement

WaseemASheikh
Associate II

Hello everyone, this is my first post on ST after starting to use the STM32F072RB on the Nucleo Board. It has been only 3 months since I started working with ST parts, so pardon my missing knowledge and misconceptions. This personal project has been stressing me out too much now, so reaching out to all the amazing brains out here for input, help and support. I am new to STM32, ARM Cortex, STM IDE, and STMCube. Please be nice.. 🙂

So, I am trying to configure a hardware connection between STM32F072 and a chip that works with parasitic power supply allowing power and communication through a single wire. It is similar to OWS but hacky version of it.

Anyway, I have been able to start the transmission to the slave and I am able to get the response through the hardware and I can see that on the oscilloscope as shown in the image below:

WaseemASheikh_0-1712248567829.png

The blue signal is the power line that shows transmission pulses and the small noise like reception pulses. This response is fed into the hardware that also includes the comparator and the pink/purple signal is the response on PA0, that is the forwarded output of COMP1 to the external pin.

WaseemASheikh_1-1712249221122.png

 


The comparator interrupts are enabled after the transmission is done, before the slave starts to respond. I expect to get 8 bits of data as this is the reset command, and I am getting that on the scope.

WaseemASheikh_2-1712249326692.png

 

I am not really a professional firmware developer or programmer so I struggle whenever there is complicated code involved. The problem I have here is, I am not able to configure the timer such that it stores and resets the value between the pulses. I have a variable in the code say 'response_interval' which I need to store the time value in microseconds between the two consecutive pulses, I have logically written the conversion part of these intervals to binary values (only need to change the comparison numbers maybe).

Long story short, any step by step help to start the timer once the first pulse is received, store the value in a variable, reset the time count and start again, the values of the time could be stored in an array or converted into binary in real time - (which should not be a problem I hope, fingers crossed).

7 REPLIES 7
AScha.3
Chief II

Hi,

for me : information missing : which sensor ?  Signal looks "digital" , so why using comparator ?

+

A timer can be set to capture pulse length by itself - but first need more info, whats needed here.

If you feel a post has answered your question, please click "Accept as Solution".

It is not a sensor. It is an EEPROM.

The signal back from the slave is a very small transition of 100mV, I do not know a way of digitally isolating a high from low with 100mV of difference, so to 'amplify' it was fed into the comparator to get a strong/nice(r) signal.

 

Also, I do know the logic, and the implementation of the idea. I am not able to put them in a code. I guess I am not able to understand the timer configuration/trigger and time value storage.

WaseemASheikh_0-1712253008907.png

need to capture and store t ^ in a variable, and then continue until the number of bits are done.

EEPROM  - type ?

+

timing pulse/gap for "1" and "0" ?

If you feel a post has answered your question, please click "Accept as Solution".

No datasheet available for the EEPROM.

the pulse/gap for 1 is 8-10us, for 0 is 15-18us

So basically at so small signal your idea to use comparator is good.

You could also set comparator to both inputs external and connect (-IN) to DAC output, to adjust the switching level very fine. 

To use a timer, to capture a pulse, connect comparator output to TIMxCH1 input, to have the signal at the timer capture channel. 

Then set this timer to a useful timing for your job here, maybe 10MHz clock to get 100ns steps .

I use this mode (at 1MHz, 1us steps), for receiving IR remote signals, this working perfect (400us/600us/1200us pulse/gap timings).

 

If you feel a post has answered your question, please click "Accept as Solution".

@AScha.3 wrote:

To use a timer, to capture a pulse, connect comparator output to TIMxCH1 input, to have the signal at the timer capture channel. 

Then set this timer to a useful timing for your job here, maybe 10MHz clock to get 100ns steps .


Funny thing is, I know this, and I am trying to do this, and I am not able to do this. LOL.

Yes, not super easy...i show you : i use tim5 here, but up to you, to use another :

AScha3_0-1712259315204.png

AScha3_1-1712259366917.png

I have 96MHz clock, so you have to adjust prescaler : (tim core clk) / (xx-1) -> 0,1us (at 48MHz : psc 4 -> about 0,1us);

counter period : set what maximum time could be, more is not possible, so counter limits by itself to this time.

input filter: to suppress short spikes .

To start setup in main :

	HAL_TIM_IC_Start_IT(&htim5, TIM_CHANNEL_2);	// capture in start

 and in INT check pulses and shift your pulses together :

void TIM5_IRQHandler(void)
{
  /* USER CODE BEGIN TIM5_IRQn 0 */
	static uint16_t pnr, cmdnr, IRpuls[15],IRcmd[5];

	uint16_t val=TIM5->CCR2;	// get puls in microsec. (3000us max. timer ARR)

	if(val>1800)  pnr=0;		// 2ms puls found -> start sequence
	IRpuls[pnr++]= val;		// store puls us
	if(pnr>12)			// Sony IR hat 12 bit + start
	{
		pnr=0;			// reset seq.
		IRcmd[cmdnr]=0;		// preset 00
		for(uint16_t aa=1;aa<13;aa++)
		{
			if(IRpuls[aa]>400 )IRcmd[cmdnr] <<= 1;		// > 400us ? shift in 0  (exact: 600us)
			if(IRpuls[aa]>1000)IRcmd[cmdnr] |= 0x01;	// > 1000us ? shift in 1 (exact: 1200us)
		}

		cmdnr++;										// for next
.....

here for my IR receiving of Sony SIRCS code , 12 bits ...)

If you feel a post has answered your question, please click "Accept as Solution".