Return INT or UINT16_t ?

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

Hey all....

 

I am writing a function that will be taking readings from an external ADC and placing them in an array where I will be adding the values in teh array together, and then dividing by the size of the array to get an average.

 

The readings are unsigned as teh ADC is reading only positive voltages, so I am thinking I return UINT16_t but I want to make sure.

 

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. Aug 3, 2020 - 07:56 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, uint16_t is what I would use if they are unsigned and between 9 and 16 bits wide.

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

Thanks Cliff!

 

 

Now to write teh function!

 

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

Hmmmmm....  Question again....

 

The ADC is going to be running constantly, and its possible for the ADC input to be zero volts.  >>For the sake of discussion<< the ADC will output 0x0000 in this case so when the function does the divide, it will be divide by zero resulting in the NEG flag being set.  Can I ignore this flag condition?  The datasheet reads:

The Negative Flag N indicates a negative result in an arithmetic or logic operation. See the “Instruction Set
Description” for detailed information.
 

I am guessign I can ignore the flag, but I am thinking on the side of playing safe I should either clear the flag on exit of the function, or maybe read teh Status register on entry, and write it back on exit?

 

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: 1

jgmdesign wrote:
it will be divide by zero resulting in the NEG flag being set

How would be a divide by zero, since you are dividing by the size of the array?

I'll admit its been awhile since I was in school, but IIRC zero divided(or multiplied) by anything is still zero.

but then I suffer from CRS  (Can't Remember Stuff) these days.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Surely you are averaging 5 or 10 or 20 readings or something? So you will be dividing by 5, 10, 20. Sure it may be 0/5 or 0/10 or whatever if the last 5 or 10 readings were all 0 but you aren't DIVIDING by 0 are you?

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

ki0bk wrote:
How would be a divide by zero, since you are dividing by the size of the array?

 

clawson wrote:
whatever if the last 5 or 10 readings were all 0 but you aren't DIVIDING by 0 are you?

 

I wrote:

jgmdesign wrote:
The ADC is going to be running constantly, and its possible for the ADC input to be zero volts.  >>For the sake of discussion<< the ADC will output 0x0000 in this case so when the function does the divide, it will be divide by zero

 

So, lets say teh array is 10 readings

 

ten samples that the ADC reports as 0x000 are loaded into the array,  Add the ten zeroes together, I get a value of zero.  0/10 is....what? 

 

Oh, Wait a sec.....

0/10 is zero...

 

10/0 is not allowed.

 

DOH!!

 

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

You didn't say what the resolution of the ADC was, so I'm going to assume that it is 14 bits since it is an external device and you can get 10-bits on the internal AVR ADC.

So each ADC result can go from 0 to 16383.  If you use uint16_t for your averaging sum, then you can only get four or so results before the sum overflows. 

Use a uint32_t for the summation variable .

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

Jim -

 

Maybe you should get Daniel to help you with the arithmetic?  :=)

 

West Coast Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

Simonetta wrote:

You didn't say what the resolution of the ADC was, so I'm going to assume that it is 14 bits since it is an external device and you can get 10-bits on the internal AVR ADC.

So each ADC result can go from 0 to 16383.  If you use uint16_t for your averaging sum, then you can only get four or so results before the sum overflows. 

Use a uint32_t for the summation variable .

 

12 bits

 

Thanks

 

ka7ehk wrote:
Maybe you should get Daniel to help you with the arithmetic?  :=)

+1 yes

 

East Side 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

Thing about INT is that it is signed and may vary in size on different machines. uint16_t, on the other hand, is unsigned (so it gets you another bit of capacity in unsigned operations) and will always be 16 bits.

 

West Coast Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

 and then dividing by the size of the array to get an average

Note that you don't have to divide at all, in some cases, to get the average effect.  For example, if you say I'm gonna average 6 readings & if the average  is > 27 turn on the pump.

That's the same as saying if the sum>162, turn on the pump, no division needed.  By not dividing, you also have finer resolution (say, use 159 instead of 162).  Faster & better.

 

If you are going to bother with doing a full average, use a weighted exponential type...much simpler, and in some ways, better (since it looks like an RC filter)..

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Aug 3, 2020 - 07:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Note that you don't have to divide at all, in some cases, to get the average effect.  For example, if you say I'm gonna average 6 readings & if the average  is > 27 turn on the pump.

That's the same as saying if the sum>162, turn on the pump, no division needed.  By not dividing, you also have finer resolution (say, use 159 instead of 162).  Faster & better.

 

Thanks,

I am measuring a ramp signal so the If>sum(x) will not work.

 

Ok, I think I have enough to go on for what I need.

 

Thanks all.  Solution marked

 

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

I am measuring a ramp signal so the If>sum(x) will not work.

What does a ramp have to do with it?  You mean you need to display the value? 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:
What does a ramp have to do with it?  You mean you need to display the value? 

 

Yes, Long story.  Thanks.

 

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