Changing the default ADC timing

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Changing the default ADC timing

Post by Steve »

As promised, here's a quick "how to" for those of you wanting to speed up the ADC:

The code for performing ADC conversion is actually contained in the appropriate FCD file for each chip (these can be found in the same directory as the FlowCode EXE). This is because different chips may use different registers for the ADC functions.

For example, the 16F88.FCD file has the following towards the end of the "code" section:

Code: Select all

ADCCapture="char ta, tb, cnt;\nadcon1 = 0x00;\nansel=0x7F;\nta = trisa;\ntb = trisb;\n#if (%a == 0)\n  trisa = trisa | 0x01;\n#endif\n#if (%a == 1)\n  trisa = trisa | 0x02;\n#endif\n#if (%a == 2)\n  trisa = trisa | 0x04;\n#endif\n#if (%a == 3)\n  trisa = trisa | 0x08;\n#endif\n#if (%a == 4)\n  trisa = trisa | 0x10;\n#endif\n#if (%a == 5)\n  trisb = trisb | 0x40;\n#endif\n#if (%a == 6)\n  trisb = trisb | 0x80;\n#endif\nadcon0 = 0x81 | (%a << 3);\ncnt = 0;\nwhile (cnt < 220) cnt++;\nadcon0 = adcon0 | 0x04;\nwhile (adcon0 & 0x04);\ntrisa = ta;\ntrisb = tb;\nansel = 0x00;\n"
This doesn't look very nice as it is, but replacing the "\n" with carriage returns gives:

Code: Select all

	char ta, tb, cnt;
	adcon1 = 0x00;
	ansel=0x7F;
	ta = trisa;
	tb = trisb;
	#if (%a == 0)
	  trisa = trisa | 0x01;
	#endif
	#if (%a == 1)
	  trisa = trisa | 0x02;
	#endif
	#if (%a == 2)
	  trisa = trisa | 0x04;
	#endif
	#if (%a == 3)
	  trisa = trisa | 0x08;
	#endif
	#if (%a == 4)
	  trisa = trisa | 0x10;
	#endif
	#if (%a == 5)
	  trisb = trisb | 0x40;
	#endif
	#if (%a == 6)
	  trisb = trisb | 0x80;
	#endif
	adcon0 = 0x81 | (%a << 3);
	cnt = 0;
	while (cnt < 220) cnt++;
	adcon0 = adcon0 | 0x04;
	while (adcon0 & 0x04);
	trisa = ta;
	trisb = tb;
	ansel = 0x00;

If you want faster acquisition timing, simply replace the "220" in the while statement with a smaller number.

Remember to edit the appropriate chip's FCD file (you will probably need to make it non-read only first). And I'd also suggest that you keep a copy of the original FCD file just in case things go wrong.

I hope this info is of help - especially to you, Jim!

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Steve:

Many thanks for this suggestion (I added a little more under the v3 topic). This seems very useful.

Jim

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Here's a couple of suggestions for making it a bit more customisable (as per Jim's requests in another thread). I've not tested them myself, but they should work:

Suggestion 1 - Use a FlowCode variable

All FlowCode variables are accessible from embedded C code, including most of that stored in the FCD file. Therefore, you could replace the "220" value above with a variable name. Remember to "color" it appropriately. Here's an example:

In the FCD file, replace the "220" value with the variable name "FCV_MY_AQC".

In your FlowCode program, add a variable called "MY_AQC" and set it to an appropriate value. Now, whenever your program performs an A/D conversion, the value stored in the FlowCode variable "MY_AQC" will be used.

Note that if your program uses the ADC component and this variable is not set, your program will fail to compile and you will get an error. [Remeber to use the batch-file trick to see the compilation errors before they disappear off the screen].

Of course, this approach puts the onus on you to make sure you are using an appropriate acquisition value.

Suggestion 2 - Use a variable defined within the FCD file

A neater solution (that should prevent compiler errors) could be to use the other sections of the FCD file to create a "behind-the-scenes" variable for this function. Such a variable should be defined in the "Directives" section of the FCD file. Append the following to it (within the quotes):

Code: Select all

char MX_ACQ_TIME;\n
Now, in the "Initialise" section, add a default value like this (again, within the quotes):

Code: Select all

MX_ACQ_TIME = 220;\n
Finally, change the "ADCCapture" code so that the "220" is replaced by "MX_ACQ_TIME".

Now, whenever you wish to manipulate the acquisition times within FlowCode, add a "C" icon with code similar to the following before your "SampleADC" macro:

Code: Select all

MX_ACQ_TIME = 50;
Let me know if either of these approaches work for you.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

Steve:

I think the former would work best for me.

So, if I understand this (after I modify the FCD file) I simply assign a value to this variable (1 - 255) before each A/D macro call in my code where I want the setup time to change (if I don't want it to change I can just assign one value to the variable at the beginning of my code)? Is it that easy? This just keeps getting better and better!

What do you mean by "color it appropriately"?

Jim

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi Jim,

Yes, it is as simple as that. But do remember that you will *always* need to declare this variable in any future FlowCode programs you create using this chip (if you indend to use the ADC). In addition, you will *always* need to set it to an appropriate default value.

My second suggestion may be better because it doesn't rely on you remembering to create a variable, or to give it an appropriate value. Basically, the ADC component should work exactly as it does at the moment by default. If you need the faster acquisition, then simply set that MX_ACQ_TIME variable within its own C icon.

I'd suggest the second appropach because if you use the first suggestion, I can forsee a situation where you forget that you need to create the "MY_AQC" FlowCode variable and your program fails to compile.

BTW, a FlowCode "colors" its variables by adding a "FCV_" prefix to them when it generates the C code. It does this so that FlowCode variables do not interfere with any C variables, statements, functions, etc. The C compiler then "colors" these variables by additionally appending a single underscore prefix.

Thus, if you have a created a variable "MYVAR" within FlowCode, you need to refer to it as "FCV_MYVAR" in any C code you use and as "_FCV_MYVAR" in any ASM code.

Post Reply