cancel
Showing results for 
Search instead for 
Did you mean: 

measure time for a puls with ADC

oe2
Associate III
Posted on August 29, 2012 at 16:40

I need to measure the averige a signal is low using the ADC.

Is there a way to connet the adc to a timer? I have nott been able to figure that out, so insted I have tried the folowing...

Start systick at 1ms, that counts up a timer variable. (this works fine)

Then I have configured the ADC, I can see on my rt watch that the ADC is running. The values are apporx 0x300 and 0x60 for a low and hig signal.

I have then this litte loop, that check for 5 low signals, and uses the systick timer to measure high and low time's.

 while (Sample_Count != 5)

  {

    old_Sample = Sample;

    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

    //Delay(5);

    Sample = ADC_GetConversionValue(ADC1);

    if (Sample + 100 < old_Sample) 

    {

      ReciveUnitTimeCounter=0; //Reset timer

     

    }

    if (Sample - 100 > old_Sample) 

    {

      ReciveUnitTime += ReciveUnitTimeCounter;

      Sample_Count++;

    }

  }

  ReciveUnitTime = ReciveUnitTime/5;

Problem is that I need to have a the delay after I have checked if conversion ready for it to work. This is strange... I can't understand whay, I need it since I'm wating on the ADC to complete if its not alredy finished...

I guess it would be better to use the ADC start stop a real timer, but I don't know any other way than this. (The +-100 value should be dynamic changed in the end)

#adc-stm32-timer
6 REPLIES 6
Posted on August 29, 2012 at 17:55

Yes, you should be able to trigger the conversion with a timer, and catch the results with DMA for example. Examine the circular DMA buffer at HT/TC interrupt.

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;

May be look at some audio sampling examples.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Reading voltage -ADC&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=901]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FReading%20voltage%20-ADC&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=901

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
oe2
Associate III
Posted on August 30, 2012 at 07:38

Hi clive1,

I think that you missunderstood me. I don't want to sample from the adc at a regular period.

I want to measure the time it takes for the analouge signal to go from one value to another.

For example, it took 1s for the signal to raise from 0x60 to 0x180, and then 0.5s to fall from 0x170 to 0x80.

raptorhal2
Lead
Posted on August 30, 2012 at 15:19

Have you considered using the Analog Watchdog function ? See RM0008 All Densities Reference Manual  section 11.3.7. An interrupt can be generated at which point you can read a timer.

If the measurement accuracy requirement is of the order of a millisecond, periodic sampling in a timer loop will also work.

Cheers, Hal

emalund
Associate III
Posted on August 30, 2012 at 15:26

since you are only interested in two levels, why not use two comparators?

Erik
Posted on August 30, 2012 at 16:38

My angle on this is that if you have a table of tight periodic values you can in fact make measurements of time, rise time, or whatever, and not waste a lot of time baby sitting the ADC.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
oe2
Associate III
Posted on September 04, 2012 at 10:55

I will have a go with the analog watchdog, hopfully that can triger when the signal is out of boundary, both to high and to low.