View previous topic :: View next topic |
Author |
Message |
jeremiah
Joined: 20 Jul 2010 Posts: 1371
|
|
Posted: Wed Apr 15, 2020 4:09 pm |
|
|
Marttyn wrote: |
But don't understand the last example... why you multiply by 32000?
And can you show some more examples? or is there any good resource to find examples like this? Would like to learn to think "that way"
Thanks! |
Ttelmah's example wanted to display an ADC value from 0.00 to 5.00v. One way to do that is to use integer values 0 to 500. For a 10bit ADC each of those bits is worth 500/1024 = 0.48828125 per bit. That's not an integer so Ttelmah approximated it by multiplying by a big number and then dividing by a bigger number. The key there is Ttelmah divided by 65536, which is a 16bit shift (thus no actual division...much faster). To figure out what to multiply by take
0.48828125 * 65536 = ~32000.
So then Ttelmah new that if he multiplied by 32000 and then did a 16bit shift, it would be about the same as multiplying by 0.48828125 |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9426 Location: Greensville,Ontario
|
|
Posted: Thu Apr 16, 2020 4:37 am |
|
|
hmm. it might be nice to have a 'scaled integers and other tricks' sticky. I'm sure everyone has clever ways to get PICs to run better. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Thu Apr 16, 2020 6:02 am |
|
|
and (of course), I wouldn't do a 16bit shift. Instead use a union,
containing the int32, and two int16 values. Do the multiply with the
int32 value, and then just use the upper int16 value for the result.
An 'optimisation' sticky might be quite a good idea. |
|
 |
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Thu Apr 16, 2020 2:57 pm |
|
|
Sorry trying to give you work Ttelmah
Jay idea is great!
Can you write the:
Quote: | An 'optimisation' sticky might be quite a good idea. |
All of us will benefit from such a topic
And if someone have some idea in the subject can add his post.
Best wishes
Joe |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9426 Location: Greensville,Ontario
|
|
Posted: Thu Apr 16, 2020 5:49 pm |
|
|
I figure Mr. T. needs to get some of his wisdom 'downloaded' before his grey ( gray sp?) cells explode.
I remember my Grade 13 high school chem teacher saying, I'd forget 90% of what he taught BUT that I should remember what BOOK the knowledge is in...
THAT has stuck with me but, um, where are the databooks today ?
I did locate my copy of Basic, better and faster. LOTS of neat 'tricks', though 40 years old some still work today.......
hmm, anyone know what a wirewrap gun is for ?? |
|
 |
Marttyn
Joined: 06 Mar 2015 Posts: 28 Location: Spain
|
|
Posted: Fri Apr 17, 2020 1:24 am |
|
|
I'm sure there should be some information out there already.
But I could buy a beer for Ttelmah (or other) for such a sticky topic.  |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Fri Apr 17, 2020 1:38 am |
|
|
OK. Lets talk privately, and each come up with some ideas and try to
structure some general 'optimisation ideas', which can then be assembled.
A further thought.
kda406, has assembled a nice little post about 'How to use the CRC module'.
Talking to him privately, the suggestion was raised that this is a 'How too',
rather than complete code, and as such it'd be really nice if there was
a 'how too' section to the forum.
Now the thoughts raised here about using integer maths fall into the
same category.
So (don't know if they will answer given Covid-19), I'm going to suggest
to CCS that they might consider a new 'How to' section to the forum.
We can then use this for things like this CRC guide, and perhaps some
guides to using scaled integer maths etc.. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Tue Apr 21, 2020 3:28 am |
|
|
I've put a link to this, and a new post about scaled integer maths into the
new 'Best of' forum. Hopefully of use.  |
|
 |
|