This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123 Launchpad periodic ADC sampling



Hi All !

I'm using TivaC Launchpad with latest CCS and TivaWare.

I want to sample 3 ADC inputs with a 5-6kHz sample rate ( oversampling 3 lower freqency -66Hz-  sine waves ).

I need advice on what is the best solution to do this given the below requirements >

1. I need  to sample the signals periodically, let's say every 0.5sec - not really important value, no need to do it more frequently -

2. When sampling I need to oversample the signals. My plan > oversample 6.6kHz for duration of 15ms ( which is the period for the 66Hz )

   This would produce 100samples / channel = 300 samples at 6.6kHz rate in this 15ms time frame.

So to sum it up > I should have 300samples every 0.5sec in memory. The data would be analyzed also only every 0.5sec worst case.

I know the approach of running a timer interrupt at oversample frequency, but I don't want to mess up other functions the board is doing in the main code which such a frequent interrupt as it is doing fine now.

Is it a mode to run the ADC continuously in the background and the DMA to collect the data in memory, overwriting it in every cycle?

I want to access that buffer only every 0.5sec or even slower.

Is it possible to setup the ADC to take samples exactly at a given freq (6.6kHz ) ?

What are my other possibilities ? 

I don't want a complete working code , just ideas.

Thanks !

  • Hello Attila,

    You can use a timer trigger to sample the ADC. Setup the timer for a 0.5 sec time period and use the Timer's Trigger control register to trigger the ADC.

    Regards
    Amit
  • Hi Amit !

    Thanks, but that's the easiest part for me..The thing I really don't know is > Once I triggered the ADC ( every 0.5s ), how to "force it" to collect 100 values from each of the 3 channels at given sampling rate ( ~6kHz ) ? The core of the problem is that once I need the data, I need a precise measurement - it's not enough for me to take a few samples - , it doesn't matter that I need it only every 0.5s.
    Hope I made my issue more clear. Thanks again for your quick reply.

    Regards,
    Attila
  • Hi,

    Attila, to have best advice from forum, would be better to tell us what do you intend to do with those samples - i.e. make rms measurement of respective voltages or analyze the frequency content of those waveform or else what?

    Take into account that sampling a waveform at a time interval equal to the signal period gives you same result, no matter how many samples you take. You will get random starting value, depending on the starting of sample command.

    So, for rms measurement (as you mention the word "accuracy"), it would be better to take an integral number of samples per period, as this is a requirement derived from definition of rms. 

    To sample three channels at once, use a sample sequencer; it will be started by a timer and the three channels will be converted at 1us apart; if needed extra precision this can be corrected, but the correction will be somewhere at the 5th or 6th figure.

    Your turn now to tell us more. 

  • Hi !

    Yes, I think I have to reveal more :) 

    So, I have to measure the output (phase voltage ) of a 3 phase BLDC motor working as a generator at 1000RPM ( motor testing ). The 3 signals from motor are rectified individually and attenuated to 0-3.3V, So I have one positive and one negative half of the sine wave, but of course the negative half inverted. For now I'm only looking for peak values which can be different at + and - half of the wave. So I have to catch only one complete period, because next time I measure the motor will produce new waves.....But....because I'm in NO control of the motor, I don't know if it's running or not, or anything else..so I have to check it periodically (0,5sec). This is for user to see if there is some fluctuation in produced voltage, or false wiring...etc...it's a common testing method.

    When I'm measuring though, I have to measure that 1 cycle quite precisely. I can't do the peak measurement electronically ( peak filter ), because the user can possibly request some other data extraction from those waves..so I have to have them fully present at the ADC pins. The measured peaks will be compared to catalog data , so that's why I have to be precise.

    Summary : I have to have a full rectified sine wave ( 1 period ) , which will include 2 peaks ( 1 from + and 1 from - peak ). I have to find those peaks in ADC data, , average them and display. I can't lower the sampling rate because then I can miss the peaks significantly , and get false data.  

    Ok, maybe I have to take samples for  slightly more time than a full period, but not much..just to be sure that both peaks are there in the data.

    Regards, 

    Attila

  • Hello Attila,

    The trigger will be generated at 6KHz. Once the sampling starts, you can uDMA to move out the data to SRAM.

    Regards
    Amit
  • Attila Kalinka said:
    I can't do the peak measurement electronically ( peak filter ), because the user can possibly request some other data extraction from those waves..

    May I suggest a means by which (I believe) you can achieve both?  

    • Peak captured & stored via electronics - a "classic" analog peak detector can achieve this
    • Extraction of all other ADC Data

    An independent analog peak detector - installed upon 1 or all 3 BLDC motor phases - should enable your desired peak detection and may prove more accurate than the MCU's discrete, time-slot series of measurements.  (this due to the fact that the peak detector "charges to peak value" - which effectively rescues it from your "MCU based" discrete, time-slot, measurement limitation.)

    That independent method should NOT impact your MCU methods - so I believe your rejection of such "Peak Detection" methods is premature...

    And staff/I rarely ride our bikes (downtown Chi) as we value life/limb - and our LED head/tail lights are far less noticeable than driver's PHONES!

  • There is a flexible architecture that will do that with minimal overhead.

    Rather than collecting large amounts of data and bulk processing them later, collect the data as it is generated and run it through a filter. Net processing is equivalent but memory requirements are lower and results are available continuously.

    For your peak detect you could use a leaky peak detect.

    The architecture is ideal f iir filters and I'd actually recommend using one of those in front of the peak detect.

    Robert
  • Hi Amit !

    But what the advantage is of that approach ? If I have a 6kHz interrupt, then moving a collected 3 words of data with uDMA each time makes no sense. I can fill a buffer manually quite fast - it's only 3 words - without the uDMA overhead. Is there a possibility to define a hardware buffer of 300 words and have the interrupt only when it is filled ? I've done it on STM32 micro quite easily, but I don't know if there is a similar approach with Tiva...

    I have more measurement routines running already, including a I/O pin interrupt , timer interrupts, UART... so I'm a bit afraid that a 6KHz interrupt rate would interfere with other working parts of the program. Especially when I don't really need it to be running all the time.

    I'm asking this after hours of research, but the problem is that my requirements are quite unique, so I could not find a convincing example. 

    Maybe I'm beating a dead horse here and it would go fine .. I just wanted to have an alternative approach if it doesn't. Also , if I need a similiar thing in the future but with much higher sample rate, the interrupt approach won't do it ( collecting a relatively small number of samples but with a high sample rate ).

    Thanks for being patient with me :)

  • Hi Robert !

    It's actually a very good idea, because peak detection is possible without having all the data all the time available..and it makes the interrupt routine also quite fast. You just have to compare a few words and refresh the old ones...
    Though this will work for me (6kHz ) , but it won't work if the interrupt rate has to be too fast for the micro to handle. That's why I'm looking for alternative approach. As I mentioned in discussion with Amit, there is a solution on STM32 micros, where you can define a sample rate, a buffer size, and setup the DMA to collect data from the ADC into this buffer. There are no interrupts involved, it's just one interrupt when a buffer is filled, but even that interrupt is only doing restarting the DMA transfer. I'm looking for similarly low CPU load solution.
    But anyway , thanks for the idea.

    Regards,
    Attila
  • Hi cb1- !

    Yes, you are right..and maybe I will go that way at the end...but as I explained the user can ask from me some more complex things to measure. If I can include it in the software now, that I have all the things at my disposal, would make my life much easier later. If all the data is there, I just have to write the routines for processing them. That approach kept away of trouble quite a few times :)

    Off> You have very nice bike tracks near Cleveland ...it's not that far for a cycling fan :) I have spent there a month recently. BTW landing in Chi is an AWESOME view.
  • Attila Kalinka said:
    landing in Chi is an AWESOME view.

    You are correct, Sir!   That said - as small twin owner - ANY landing is awesome!  (especially into swirling, cross-wind)

    Like you - we (often) use that (other's) MCU - it's curious that you stray - especially in light of the limitations (here) which you note.

    I'll add one other (critical) point in support of my earlier suggestion to employ an, "external/apart from MCU" (peak/null signal detector).   Being independent - this method escapes any/all MCU glitches/hangs - thus this method is often favored w/in Medical, Defense & Regulatory communities.   Robustness IS a Big Deal - often (always) trumps "MCU as Do All/Be All!"   (And - there's little to prevent you from feeding the detector's output to the MCU for processing and/or storage)

  • I'm running at 10ksps on each of 12 channels each has iir filtering and most have additional scaling. I don't see an issue with load.

    You should be able to use RMA to set up your scheduling.

    One question that arises is why peak detect each phase independently? You could bridge them in hardware. Depending on the shape of the waveform you may be able to eliminate peak detect altogether. Unless you are measuring different loads per phase.

    Robert
  • Bonjour Robert,

    I'm (often) told to use my "Inside" voice.   (even when outside)   May I suggest that you switch from your procurement/adjustment coat (RMA) to your engineer/lab coat. (DMA)

    As to independent peak measurement:

    • sometimes BLDC motors will "favor" (or disfavor) one phase over another.   (this may result from improper hall sensor locating and/or unmatched hall sensors and/or variation in the hall sensor magnet)
    • BLDC Controller - due to layout or component variation w/in the power-stage - may (again) favor/disfavor a phase
    • even more disturbing - these variations may shift based upon motor's RPM and loading.

    Now this poster is evaluating his motor when it is driven by another - thus it's in, "Generator Mode."    (which effectively nulls my (neat) listing (above) - but I'll leave it here in hopes that sometime, somewhere this may prove of value to some...)    (note to self: read the original post & the post to which you respond!)  Duh...

  • Hello Attila.

    Yes. it is possible. The DMA can transfer upto 1024 samples once configured. so in this case it is as simple as configure the DMA to transfer 300 elements of data from the ADC to SRAM when the ADC starts sampling and at the end generate an interrupt.

    Regards
    Amit
  • TOS! TLA Overload Syndrome!

    And for those who didn't follow my unfortunate ambiguity. RMA = Rate Monotonic Analysis.

    Robert
  • Robert Adsett72 said:
    my unfortunate ambiguity. RMA = Rate Monotonic Analysis.

    And for those w/(perhaps) steadier hand on the tiller - and sails (still) clipped to the mast.   RMA = Return Merchandise Authorization.  

    More inspired - though not (yet) standard: "Robert's Management Academy" and "Respondez Mon Ami."   (or was that RSVP?)

  • Hi Amit !

    Thanks. That's the best hint so far. I will definitely try it out tomorrow. Just on more thing and I'm good  > Do I still need both inetrrupts ?

    One running @6kHz to represent the sample rate, and another which would be triggered by DMA at the end of the 300 word transfer ?

    Is it possible to set up any sample rate without running an interrupt  for that purpose ?

    @Robert >  BLDC motors can have different phase outputs even in generator mode, due to minimal winding an mechanical differences...and also 

    if this difference is too big, it's a best indicator that the motor is damaged and/or has a factory assembly issue...etc.. 

  • Hello Attila,

    You do not need the two interrupts. The simplest method would be to start a timer to trigger the ADC at 6KHz. When the DMA is completed, the interrupt handler can be used to switch off the timer to prevent any further sampling by the ADC.

    Regards
    Amit
  • Thanks , Amit !

    Now I will try some things suggested here to get it work.

    I think I've got the big picture, but it can fail in details as usually :)

    I'll report back if I have more questions or the final solution ( fingers crossed ).

    Regards, 

    Attila

  • As my firm is also w/in BLDC Motor/Control field I trust the following may be of some interest:

    • As you're measuring the MUT as a "generator" much of the electrical noise normally produced by a BLDC motor is greatly reduced
    • We find it wise to "clamp" the peak output voltage which MUT may produce to levels w/in MCU's ADC spec.   (Techs are known to "never" misadjust motor speed controls [on some planets])   Operated at "proper speeds" this clamping will NEVER be imposed - it is a safety, only.
    • Might it prove wise to confine your code to (just) that required for this ADC-µDMA project?   In this way - unanticipated consequences from (other) program operations are eliminated.   In addition - this (focused) code build will enable you to discover an "ADC-µDMA Baseline."    Later - as you selectively (one by one) patch in your additional code - you may then "Measure against your baseline" and thus (really) identify impacting code elements.   (if any)
    • In my firm's development we often perform simultaneous such ADC measurements via "two" (or more) MCUs both as a "sanity check" and as a judge of MCU (real-world, real-application) performance.    One of our, "Measure MCUs is a far faster M7" which provides insight into the (possible) benefits of far higher ADC rates.
    • Our usual choice of ADC "exercise/test input source" is a 2nd MCU - DAC equipped - which may be programmed to output most any waveform you require.   Small, noise-free and far more repeatable than, "MUT as the test source!"    Once the ADC-µDMA is test/verified - and your confidence high - the MUT may be spun up & measured.   (and your chance of proper measurements is greatly increased via this (necessary) ground-work.)

    BTW - we've biked 1/2 way to Cleveland!   (or maybe I dreamed that... ...as we're blister, sunburn, and tire-track free...)

  • Hello Attila

    Sure. Glad to help with the details.

    Regards
    Amit
  • @cb1

    Yes, all of your points are valid. In my case, I'm stuck with an existing design - a test box already doing some other stuff- , so I have limitations implementing these

    things..but yeah, if I could start from scratch...

    My solution is now working. I used simple voltage divider and an opamp rectifier circuit to condition the incoming signals. 

    @Amit  > Now the ADC is giving me nice results at 6.6kHz, with hardware averaging. I skipped the DMA solution, because I found it fast enough to do the transfer in software. So now I use a timer triggered ADC, and a software buffer that I fill up gradually in each interrupt until I reach a desired number of samples.

    There is though remained one "black magic" thing for me , for that I could not found any working code anywhere : It is not absolutely necessary now, but I would like to know how to adjust ADC sample rate.

    I know the  "SysCtlADCSpeedSet" issue, but the newest post was back in 2013, so is there a working solution now for Tiva Launchpad?

    Thanks both of you for your great help !

  • Hello Attila

    Only by using the timer trigger can the ADC Speed be controlled.

    Regards
    Amit
  • Hi Amit !

    So if I understand correctly :

    1. It is not possible to adjust the sampling speed ( always runs at 1Msample )
    2. It is not possible to adjust the sample and hold time

    I can trigger the ADC to start conversion of let's say 8 samples with a sequencer at a reasonable rate ( in my case 6.6KHz ) , but I can't control the speed and/or the sample&hold BETWEEN these steps ?

    I just want clarify if I'm correct, for the sake of my future projects with this board. Current solution is working OK. Thanks

  • Hello Attila

    On the TM4C123 it is possible to adjust the sampling rate as 1M, 500K, 250K and 125K, but it is not possible to adjust the sample and hold time.

    The sampling rate would mean that if a trigger is applied the new sampling rate is effectively the value specified.

    Regards
    Amit