Lets Assume I put a voltage into A/D port AN2 that the PIC A/D should convert to a level of 753.
With I use this code I get the wrong value:
#define USE_AD2 TRUE
Dim X_Plus as word
X_Plus = ReadAD10(AN2, TRUE)
print X_Plus ; Reads 1 not 753
BUT if I change the last line to:
Print ReadAD10(AN2, TRUE) ;It correctly gives 753.
Why is this?
Last edit: Anobium 2018-12-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) The 18F4550 has a 10-Bit single-ended ADC. Therefore, "true" is unnecessary and should not be used as a parameter with READAD10. "True" is typically used with 12-bit differential ADC's where a 10 -bit result is desired. By using "true" the library assumes a differential ADC and therefore requires the user variable to be an integer.
2) Since the ADC result will never be negative if "true" is NOT used, X_Plus can be a word or an integer. If the "true" parameter IS used then X_Plus needs to be an integer.
3) By default, the a-d.h library defines all ADC channels with "#define USE_ADx TRUE".
Therefore " # Define USE_AD2 TRUE" really does nothing
To optimize code: Define the unused ADC channels to FALSE
My best guess is that #1 above may be causing the the issue is it calls the inappropriate subroutine in the library for single-ended 10-Bit ADC reads . If correcting #1 does not resolve then we will likely need to escalate
Bill
Last edit: William Roth 2018-12-20
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I found the problem but I'm not sure why it is a problem.
I'm using this code to read a 4-resistor touchscreen. As you probably know a touchscreen changes its input and outputs to read the X and Y positions i.e the DIR command is used alot.
I used:
#define X_Plus PORTA.2
to simplify the renaming. But then I had to use the DIR commands to change my direction throughout the code. For some reason GCB didn't like using X_Plus for this.
When I changed X_Plus to PortA.2 everything started working.
Incidently, I'm using a TFT LCD with this touchscreen and #include <glcd.h>. Everytime I compile I get this warning message:</glcd.h>
WARNINGs / ERRORs reported by Great Cow BASIC (if Syntax Error, doubleclick on the errormessage below) <<<
glcd.h (148): Warning: Cannot find C:\GCB@Syn3\GreatCowBasic\include\glcd_nt7108c.h
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The initial issue seems to be caused by setting X_Plus as both a variable and a constant.
This code reproduces the symptoms in the first post:
#define X_PLUS PORTA.2
Dim X_Plus as word
do
X_Plus = ReadAD10(AN4)
Locate 0,0
print X_Plus : Print " "
Locate 0,7
Print ReadAD10(AN4)
Print " "
loop
With the code above the display shows " 0 ................... 451"
Print X_PLUS returns a 0 and Print ReadAD10(AN4) works as expected
X_Plus cannot be both a variable and a constant. One or the other but not both .
When tracking down issue such as this in order to provide good help we need to see the complete code, not just snippits that may not show the of source of the error,
Last edit: William Roth 2018-12-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
True is required, therefore necessary, to force READAD10 to return a 10bit value on specific parts.
The design of READAD10 as follows. When using ReadAD10 ( ANX ) the returned value is the full range of the ADC module. Therefore, the method may return an 8 bit value [0-255], or an 10 bit value [0-1023] or an 12 bit value [0-4095]. This is dependent on the microcontrollers capabilities. For a 10 bit value [0-1023] always to be returned use user_variable = ReadAD10( ANX , TRUE )
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
NO FAULT FOUND. THIS IS A POISSON ROUGE.
I'm using 98.03 with an 18F4550.
Lets Assume I put a voltage into A/D port AN2 that the PIC A/D should convert to a level of 753.
With I use this code I get the wrong value:
#define USE_AD2 TRUE
Dim X_Plus as word
X_Plus = ReadAD10(AN2, TRUE)
print X_Plus ; Reads 1 not 753
BUT if I change the last line to:
Print ReadAD10(AN2, TRUE) ;It correctly gives 753.
Why is this?
Last edit: Anobium 2018-12-21
Not tested my end as my simulator crashed but try:
Dim X_Plus as Integer
1) The 18F4550 has a 10-Bit single-ended ADC. Therefore, "true" is unnecessary and should not be used as a parameter with READAD10. "True" is typically used with 12-bit differential ADC's where a 10 -bit result is desired. By using "true" the library assumes a differential ADC and therefore requires the user variable to be an integer.
2) Since the ADC result will never be negative if "true" is NOT used, X_Plus can be a word or an integer. If the "true" parameter IS used then X_Plus needs to be an integer.
3) By default, the a-d.h library defines all ADC channels with "#define USE_ADx TRUE".
Therefore " # Define USE_AD2 TRUE" really does nothing
To optimize code: Define the unused ADC channels to FALSE
My best guess is that #1 above may be causing the the issue is it calls the inappropriate subroutine in the library for single-ended 10-Bit ADC reads . If correcting #1 does not resolve then we will likely need to escalate
Bill
Last edit: William Roth 2018-12-20
Chris and Bill,
Thanks for the responses.
I tried all the ideas But, unfortunately I still have the same problem.
Any other ideas?
Share you code please.
Works here on real silicon.
I am using the LCD Print routines via the serial to ensure they are working as expected.
I get the same value printed twice.
Last edit: Anobium 2018-12-20
Tested on real silicon. The attachment shows the optimisation.
I get two numbers, the same, lots of them.
Last edit: Anobium 2018-12-20
But, you are using a real LCD....
try this.. what happens?
There may be an issue with the Print handler. Test with the X_Plus as an integer. Three different program runs please. What is the result?
then
then
Last edit: Anobium 2018-12-20
I found the problem but I'm not sure why it is a problem.
I'm using this code to read a 4-resistor touchscreen. As you probably know a touchscreen changes its input and outputs to read the X and Y positions i.e the DIR command is used alot.
I used:
#define X_Plus PORTA.2
to simplify the renaming. But then I had to use the DIR commands to change my direction throughout the code. For some reason GCB didn't like using X_Plus for this.
When I changed X_Plus to PortA.2 everything started working.
Incidently, I'm using a TFT LCD with this touchscreen and #include <glcd.h>. Everytime I compile I get this warning message:</glcd.h>
Oh. OK. So, Dim X_Plus as word was actaully #define X_Plus PORTA.2? That does not make sense - but, no fault found - so this ok.
Upgrade to v0.90.04 just relased to resolve the glcd issue.
Sorry, I cannot reproduce. I have hooked up a real 4-wire LCD - works ok.
What it the difference? I get same values on both lines.
The initial issue seems to be caused by setting X_Plus as both a variable and a constant.
This code reproduces the symptoms in the first post:
With the code above the display shows " 0 ................... 451"
Print X_PLUS returns a 0 and Print ReadAD10(AN4) works as expected
X_Plus cannot be both a variable and a constant. One or the other but not both .
When tracking down issue such as this in order to provide good help we need to see the complete code, not just snippits that may not show the of source of the error,
Last edit: William Roth 2018-12-21
I get this... but, Dim X_Plus as word was in the first post.......
But is that the root cause?
For a test . . . With 18F25K22, I used the (unnecessary) "true" parameter and made the user variable a WORD. Had no issues.
Root cause. Incorrect posting of the issue.
True is required, therefore necessary, to force READAD10 to return a 10bit value on specific parts.
The design of READAD10 as follows. When using ReadAD10 ( ANX ) the returned value is the full range of the ADC module. Therefore, the method may return an 8 bit value [0-255], or an 10 bit value [0-1023] or an 12 bit value [0-4095]. This is dependent on the microcontrollers capabilities. For a 10 bit value [0-1023] always to be returned use user_variable = ReadAD10( ANX , TRUE )