I just started GCBasic. I wrote some code to read a Microchip MCP3428 ADC chip over i2c.
Basically my library returns ADC value as an integer value. i2c code works great.
But when I try to scale, it returns a byte value (0 to 255).
But I expect a number 0 to 32767. What am I doing wrong here?
read = MCP342x_convert_oneshot(2, , , err)
HSerPrintStringCRLF str(read)' here 0 to 32767 value is returned as expected
read = scale( read, 0, 32767, 0, 3000)
HSerPrintStringCRLF str(read)' here I see a 0-255 value, not what I expects
My guess, a total guess as I do not the code segment about.
Try adding #option explicit to your USER program not the .h
Then, I am guessing, but read is a byte.... should be a word? read = MCP342x_convert_oneshot(2, , , err) this returns an?
and, read = scale( read, 0, 32767, 0, 3000) this is an Integer or Word? and, you want the returnd range to be between 0 and 3000?
The .h
Lovely clean code. Very nice.
And, check... line 81... missing brace!
And, check .. line 81... can you return an out_err? Hard code an out_err and make sure this is working.
So, a few things to check.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you so much for the quick reply.
The function MCP342x_convert_oneshot(2, , , err) returns an integer
and read is defined as integer.
Acording to the documantation scale function returns an integer.
You are correct, I'm expecting a number from 0 to 3000 after scaling.
But even with #option explicit my serial turminal shows a 0 to 255 number after scaling.
I'm really confused...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What I know:
1. The scale function is working as expected, as are the string methods.
2. Returning 0 and 32767 from MCP342x_convert_oneshot provides the correct results of 0 and 32767 and the PrintVol and PrintCur handle these tests correctly.
Do this.
Line 137: MCP342x_convert_oneshot = FnLSL([word]Value1, 8)+ Value2. Hard code values here that are meaningful values and test. I cannot test as I dont have this sensor. I think the value being returned from MCP342x_convert_oneshot is not what you expect. So, we need to look carefully in this method.
Test and let me know what happens.
Also, ONLY DO THIS ONCE WE HAVE THE VALUE RETURNED CORRECTLY. Consider this. Remember, I do not know this sensor.. so, check my thinking.
'add - the may work better
dim Returned_Value as integer
dim sersor_state as byte
HI2CSend MCP342x_AddrRead
HI2CReceive Value1, MCP342x_convert_oneshot_h
HI2CReceive Value2, MCP342x_convert_oneshot
HI2CReceive Value3, sersor_state
HI2CStop
set config.7 off
if sersor_state = config then
'err = -100 'One-shot Config Error
end if
Anobium
Last edit: Anobium 2019-03-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tested the library function just like you asked. It returns expected values.
Below is what I am seeing right now.
sub printVol()
Dim readv as integer
readv = MCP342x_convert_oneshot(2, , , err)'....here 30V input to ADC
print str(readv)'...............................LCD shows value 32767
print " "
readv = scale(readv, 0, 32767, 0, 30000)'......expected value is 30000
print str(readv)'...............................but LCD shows value 48
end sub
Last edit: iDeeW 2019-03-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I agree, this is very puzzling...!
I tried the below code with StrInteger& it behaves exactly the same.
sub printVol()
dim readv as Integer
readv = MCP342x_convert_oneshot(2, , , err)'....here 30V input to ADC
'if readv < 0 then readv = 0
print StrInteger(readv)'..LCD shows value 32767. which is what I expects
print " "
readv = readv * 30000 / 32767'......expected value is 30000
'readv = scale(readv, 0, 32767, 0, 30000)'...this prints value 48 on LCD
'adc_val = leftpad(StrInteger(readv), 5, "")+ "mV"
print StrInteger(readv)'.........................This prints value 0 on LCD
end sub
Its not the scaling function!
What am I doing wrong?
Thanks,
Line 137 of MCP342x.h. Hard code a value of 16383 MCP342x_convert_oneshot = 16383
Test - what is the result? 15000?
If the above test returns 15000 , then, line 133 is the same file is not returning what you think. So Value3 is not return a config - to proove this add this to line 139. So, to test is value is not equal to config. Do this.
MCP342x_convert_oneshot =32767
Test - what is the result?
Then, I would get the protocol analyser out.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I hardcoded 16383......function returns 16383 but LCD shows 151
I change the else side to return MCP342x_convert_oneshot =32767, but the function still returns 16383 & LCD shows 151.
Edited library file is attached...
I have my Logic Analyser hooked up.... all return values looks good and match with the datasheet.
Post the code - may be we can spot the original condition but there is no cache of code. Each compile is unique and all new ASM is generated... it would be good to figure out the root cause.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi everyboy,
I just started GCBasic. I wrote some code to read a Microchip MCP3428 ADC chip over i2c.
Basically my library returns ADC value as an integer value. i2c code works great.
But when I try to scale, it returns a byte value (0 to 255).
But I expect a number 0 to 32767. What am I doing wrong here?
Thank you
My guess, a total guess as I do not the code segment about.
Try adding #option explicit to your USER program not the .h
Then, I am guessing, but
read
is a byte.... should be a word?read = MCP342x_convert_oneshot(2, , , err)
this returns an?and,
read = scale( read, 0, 32767, 0, 3000)
this is an Integer or Word? and, you want the returnd range to be between 0 and 3000?The .h
Lovely clean code. Very nice.
And, check... line 81... missing brace!
And, check .. line 81... can you return an
out_err
? Hard code an out_err and make sure this is working.So, a few things to check.
Thank you so much for the quick reply.
The function
MCP342x_convert_oneshot(2, , , err)
returns an integerand
read
is defined as integer.Acording to the documantation
scale
function returns an integer.You are correct, I'm expecting a number from 0 to 3000 after scaling.
But even with
#option explicit
my serial turminal shows a 0 to 255 number after scaling.I'm really confused...
You will have to post the complete project. New .h as well.
But, try this comment out the MCP call. Hard code the read value, first as 0 then as 32767, what happens? Is scale returning 0 and 3000?
You will have to post the complete project. New .h as well.
But, try this comment out the MCP call. Hard code the read value, first as 0 then as 32767, what happens? Is scale returning 0 and 3000?
Will not compile. Missing .h = MCP4725.h
I have worked around.
What I know:
1. The scale function is working as expected, as are the string methods.
2. Returning 0 and 32767 from
MCP342x_convert_oneshot
provides the correct results of 0 and 32767 and thePrintVol
andPrintCur
handle these tests correctly.Do this.
Line 137:
MCP342x_convert_oneshot = FnLSL([word]Value1, 8)+ Value2
. Hard code values here that are meaningful values and test. I cannot test as I dont have this sensor. I think the value being returned from MCP342x_convert_oneshot is not what you expect. So, we need to look carefully in this method.Test and let me know what happens.
Also, ONLY DO THIS ONCE WE HAVE THE VALUE RETURNED CORRECTLY. Consider this. Remember, I do not know this sensor.. so, check my thinking.
Anobium
Last edit: Anobium 2019-03-18
I tested the library function just like you asked. It returns expected values.
Below is what I am seeing right now.
Last edit: iDeeW 2019-03-21
Post the complete project. As I get
32767 30000
when I use the following code. I just hard coded the 32767 in your method.Try this code. What value do you get?
Puzzled
Anobium
Last edit: Anobium 2019-03-21
Is this the cause?
StrInteger()
notStr()
I agree, this is very puzzling...!
I tried the below code with
StrInteger
& it behaves exactly the same.Its not the scaling function!
What am I doing wrong?
Thanks,
Last edit: iDeeW 2019-03-21
Do this.
Line 137 of MCP342x.h. Hard code a value of 16383
MCP342x_convert_oneshot = 16383
Test - what is the result? 15000?
If the above test returns 15000 , then, line 133 is the same file is not returning what you think. So Value3 is not return a config - to proove this add this to line 139. So, to test is value is not equal to config. Do this.
MCP342x_convert_oneshot =32767
Test - what is the result?
Then, I would get the protocol analyser out.
I hardcoded 16383......function returns 16383 but LCD shows 151
I change the
else
side to returnMCP342x_convert_oneshot =32767
, but the function still returns 16383 & LCD shows 151.Edited library file is attached...
I have my Logic Analyser hooked up.... all return values looks good and match with the datasheet.
Last edit: iDeeW 2019-03-22
Last edit: iDeeW 2019-03-22
Got an update?
I created a new file and rewrote the whole code (used the same library files) & it started working...yay!
Is it possible the old project file was somehow currupted??
Thanks
Last edit: iDeeW 2019-03-22
Post the code - may be we can spot the original condition but there is no cache of code. Each compile is unique and all new ASM is generated... it would be good to figure out the root cause.
Attached.
Thanks
Last edit: iDeeW 2019-03-23
Cheers.
MCP342x is a ADC and the MCP4725 is a DAC from Microchip.
Once I add all the functionalities, I will definitely share the libraries.
Thanks
I see. So, hence the two libraries. Make a lot more sense now.
Be good to have 'similar' ADC commands like READAD16_MCP32xx. An idea.
Great idea...will do.
Thanks
any progress on making a library for MCP4725?
Last edit: Pic Fan 1975 2021-01-27
@PIC FAN 1975
See https://sourceforge.net/p/gcbasic/discussion/projects%26guides/thread/bd4ae4f713/ for MCP4725.
Took me longer to write up than do the programming. :-)