Which data type?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking for a little guidance....or a lot of guidance.....

 

I want to make an averaging filter for a 12bit ADC.  The 12 bits are unsigned.

 

I was thinking of a 16 element array and a FIFO operating scheme, where as each ADC reading is loaded into the array, the code then adds up all the elements in teh array, and then divides by 16 to get an average. 

 

Simple enough right?

 

Heres my headscratcher.  If the ADC maxes out to 0xFFF and I fill teh array 16 times with 0xFFF, the result is a 17 bit number(or 5 nibbles).  - 0x15FEA to be exact.

 

So, what data type do I use for storing the result of the addition?  I checked here:

 

https://www.tutorialspoint.com/c...

 

And teh unsigned integer seems to fit the hole, but for GCC, based on what I read here:

 

https://www.gnu.org/software/gnu...

 

Should I be using uint32_t ?

 

I am guessing YES, but I would like a second...or third..etc opinion(s)

 

JIm

 

This topic has a solution.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Mon. Nov 30, 2020 - 10:27 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

uint16_t is fine.   int16_t is not fine.

 

After all,  your ADC gives 0 .. 4095.

So even if you have 16 samples that are each equal to 4095,  you only get 65520 which is in range of 0 .. 65535

 

There is no harm in using uint32_t or int32_t.   They will both be "safe".   Just uses more resources than necessary.

 

David.

Last Edited: Mon. Nov 30, 2020 - 10:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
o even if you have 16 samples that are each equal to 4095,  you only get 65520 which is in range of 0 .. 65535

Oh for F*** sake!  (insert head smack here!)

 

jgmdesign wrote:
If the ADC maxes out to 0xFFF and I fill teh array 16 times with 0xFFF, the result is a 17 bit number(or 5 nibbles).  - 0x15FEA to be exact.

 

What a dummy I am!  When I did the multiplication on teh Windows programmers calculator I forgot I was using the calculator in HEX mode so '16' is not '16' in hex.  '16' is 10 in hex.  I was mixing decimal and hexadecimal.....blush

 

Thanks David!

 

Time for tea....it's been a helluva day!

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, I would keep a count of how many valid samples you have, as a simple sum/16 would include the zero values, until you have collected at least 16 samples. Either calc the average as (sum/num_samples) or just don't calculate an average until you have captured at least 16 samples; I assume you'll reach 16 pretty quickly.

 

If you're collecting samples at full speed, you may not be averaging over a long enough time period to give a meaningful result. Perhaps only store every 10th/100th/whatever sample, or sample every n milliseconds.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course in newer chip like the XmegaE5 the chip is supposed to do that for you but I chicken out and used 16 samples average.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
Of course in newer chip like the XmegaE5 the chip is supposed to do that for you but I chicken out and used 16 samples average.

 

I used a Mega168....oops!

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user