Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Want to check I understand this code

Status
Not open for further replies.

large_ghostman

Well-Known Member
Most Helpful Member
I have gone back to C18 (thanks M chip for new version!!!), but sticking it out with the mplabx, mainly because its alot like the other IDE's I use like Kiel V5. Anyway, still knawing my way through the ADC libs, I want to use the ADC on the 18f4685 in the past with no problems. But I want to try and go it alone and write my own routines. Anyway in the examples by micro chip is the following.

Code:
for(i=0;i<16;i++)
    {
    ConvertADC();
    while(BusyADC());
    ADCResult += (unsigned int) ReadADC();
    }
    ADCResult /= 16;

Just checking I have this right, because I havnt found the info in C18, but in the 8051 compiler i use the bit that says

ADCResult+=(unsigned int) ReadADC();

would mean, it takes the result and keeps adding it together, in this case 16 times, so on second loop it would take the first result and add it to the second, then on the 3rd loop, it would take the result and add it to the total of the previous two results.
is that the same for all C including C18.
sorry I have really made this hard to understand!!!!
what I am trying to ask is does += mean it takes the total and keeps adding the new result to it?

In my head the question is clear but I always make a mess explaining :D.
What I am doing is looking at there example and seeing how I can alter the macro's and function's to my own way. So far what I am doing is just writing raw register bits, for example

Code:
  /* Setup analog functionality and port direction */
    ADCON1bits.VCFG0 =0; //Voltage ref set to AVDD
    ADCON1bits.VCFG1 =0; //Voltage ref set to AVss
    //The following code set's Anologue function on AN0-AN3 0b1011;
    ADCON1bits.PCFG3 =1;
    ADCON1bits.PCFG2 =0;
    ADCON1bits.PCFG1 =1;
    ADCON1bits.PCFG0 =1;
    // The followings sets ANO-AN3 As inputs.
    TRISAbits.RA0 =1;
    TRISAbits.RA1 =1;
    TRISAbits.RA2 =1;
    TRISAbits.RA3 =1;

Once i get everything working that way, I will go about building my macro's and function's, I know its the long way round, but it is helping me to read and properly grasp the datasheet. (hopefully)
 
Yes, they calculate the average of 16 consecutive reads in the hope of getting better accuracy. The sum them up and divide by 16.

Although they forgot this at the beginning:

C:
ADCResult = 0;

Also, I don't like this:

C:
ADCResult /= 16;

They just threw away 4 binary digits, which could've been used to reduce errors in subsequent calculations. At the very least, they should've done better rounding this way:

C:
ADCResult = (ADCResult + 8)/16;

Or, assuming we know better than the compiler:

C:
ADCResult = (ADCResult + 8)>>4;
 
Thanks Mike. North Guy, not sure I understand, but before I ask you to explain, I am going to go away and think about what you put. The first part
ADCResult = 0;
Ok yep agree and understood, but I dont get how they missed 4 bits! But I will try and work out what you mean before I just ask for the answer! My head say's I should get it straight off, so I might just be tired. Or I might be a bit stupid :D, but I will try and work it out first.
I am determined to master this.
I could just keep on using the built in stuff etc, but it's time to stop hiding behind others code and get to grips with this!!
My thinking is, aged around 2 ish I learnt a whole language! so at 13 I should be able to learn another one thats at least written in the one I already know!!
Yep def tired I am babbling, sorry:oops:
 
Let's say we opened an enterprise. You earned 5 pounds and I earned 2. Now we need to split our gains. (5+2)/2 = 3.50. So, each get 3.50.

Now do it in C:

C:
int ADCResult;
int you = 5;
int me = 2;
ADCResult = you + me;
ADCResult /= 2;

What do we get? 3. But must be 3.50. We just threw away a binary digit. Makes sense?
 
LG!! I usually oversample as much as possible..

As a signed int can be 32,768.. you can sample 24 times so your largest number could be 24,576.
This gives you the ability to keep it large so you can divide down..

Most calculations from here on usually have '*' and then '/' so why bother with the /16 at all....
 
First things first, Thank you all very much for the input. Thanks for the pdf Mr T as always a mine of useful info! we have a few days off (got to love Scotland for there School system), the plan was electronics all the way! But having a everything I touch turns to rat poo day, so plan is, once i start having a normal day, I will step through the code with pot attached, and tot up manually.
Then apply the different maths.
There is a problem somewhere because my DMM and pic dont agree on the voltage, but its a linear disagreement??? so something wrong somewhere.
Still not ready to have it explained to me tho :D.
I am not throwing the towel in that easy.
I should be preparing for the 24th feb, I have a School assembly to do :nailbiting:. Its will be a separate thread. What I had planned to do seemed easy in my head, but its turned into a nightmare. Anyway thats for later.
Back to my ADC.

I am going to see if I can get my board working again! for some reason it wont program, keeps telling me to check my config settings. I have a feeling C18 and mplabX dosnt like you to put the config in a different file, like you can with XC8. Also the header is broke on the ICD3 so going to have to use the pk3 for today :(, that also wont program???? so back to sorting this mess out then I can continue :D
 
Let's say we opened an enterprise. You earned 5 pounds and I earned 2. Now we need to split our gains. (5+2)/2 = 3.50. So, each get 3.50.

Now do it in C:

C:
int ADCResult;
int you = 5;
int me = 2;
ADCResult = you + me;
ADCResult /= 2;

What do we get? 3. But must be 3.50. We just threw away a binary digit. Makes sense?
yes that bit makes sense, could the % be used in some way? to check for left overs so to speak
 
could the % be used in some way? to check for left overs so to speak

Maybe, but not really in practice. It all depends what you are doing with the ADC reading. Do you want to display it as voltage, or do you use it to control something. Ian had a good point.. you do not necessarily need to do the division at all.
 
Actually I am not doing anything with it as such, I am trying to write my own routines, and posted an example I was copying, North Guy then pointed out that the way Micro chip had the example, you lost accuracy, which my DMM confirmed. So its a kind of side step issue really. But since it's been mentioned, it's a good time to learn about the if's and when's.
For code testing I am just using a pot, So checking voltage this time.
But having problems using mplab X and C18 3.47
Keeps refusing to go into debug, telling me it cant program memory and check config etc. I am getting around it by doing everything with the discrete options. Pain in the bum! but at least I can sort of get it to debug!
I think Ive fried the ICD3!! which will annoy me, at least they will replace if I have.
 
No I hadnt seen that! I am having a really hard time with mplab x and C18. even the config window wont spit out the config!!! the whole thing is a nightmare :( and I cant find 3.46 complete install GRRRR
 
sorted the config issue! what a load of BIG hairy dangly bits. It turns out that there is a problem in Mplab X that they cant find a fix for! in order to get a config output you have to make sure you highlight, your tool chain in the dashboard window!!!!
What a load of rubbish! it should be enough that you have set all the tool chain settings etc, to then have to have your cursor on the bloody toolchain is rubbish.

How come many other companies have near identical IDE's and yet they work like clocks!! How long have micro chip been working on this? how long before they decide that actually mplab x was a good idea, but they cant make it work!
Time they gave up on net beans
 
What a day! destroyed the ICD3!!! and mums new laptop :O it just suddenly froze, now nothing. It wont even boot into bios, nothing on the screen, you press power button and the HDD light comes on for 2 seconds then nothing, no screen at all!!! jesus one of them everything I touch I break days :( anyone else get them?
Also broke my soldering station
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top