ADC object works in one section of program, but not another section of same program???
jcfjr
Posts: 74
in Propeller 1
The following code displays a value of q = 357, verifying my ADC is working.
Yet when I use the same command in another method of the same program
I receive q = 0. I am at a total loss why MCP.in works one place in my program, but doesn't elsewhere. Can someone provide some clues where to look for the problem.
OBJ fMath : "Float32" PST : "Parallax Serial Terminal" 'used for comments to debug LcdSerial : "FullDuplexSerialPlus" MCP3202 : "MCP3202" PUB Main | localindex pst.Start(115_200) 'Start Parallax Serial Terminal fMath.start 'Start Floating point Math InitDisplay 'Zero out display MCP3202.start(dataio, clk, cs, %0011) waitcnt(80000 + cnt) ================================================ q := MCP3202.in(0) PST.Str(String(pst#NL," q = ")) PST.dec(q) ================================================ repeat localIndex from 0 to 2 'Initialize encoder status oldState[localIndex] := ina[pinA[localindex]..pinB[localIndex]] oldencstat := INA[18..23]
Yet when I use the same command in another method of the same program
PUB CalcSWR | a,b,c,d,e,f,g,h,j,k,m,x,z,pwrmw,top,bot,ADCfwdin,ADCrefin PST.Str(String(pst#NL)) PST.Str(String(pst#NL," CalcSWR ")) 'Tstart r := 0 r2 := 0 repeat times 'sample forward and reflected voltages times ========================================================== q := MCP3202.in(0) PST.Str(String(pst#NL," q = ")) PST.dec(q) =========================================================== if q < floor 'check to see if you still have signal, if not, nstep := 0 return 'break sampling
I receive q = 0. I am at a total loss why MCP.in works one place in my program, but doesn't elsewhere. Can someone provide some clues where to look for the problem.
Comments
Mike
Jim
I second Jon's question.
My guess is the MCP3202 object is controlling the I/O pins with Spin.
You want to make sure the same cog which initializes the object is the cog to call the object.
Edit: If you attach an archive of your code, we could examine it to see if the cog issue is the problem.
Here's a screen grab of what to select after you've compiled the program.
You can attach the zip file in a similar way you attached the Spin file. This will let us see the complete program and run with our own hardware if desired.
My program does nothing to pull CS line high. But the attached demo does nothing to pull CS high, and it works. I can load this file into same setup on Prop, and it works. Unfortunately I know nothing about PASM, and MCP3202 is written with PASM, so I can't tell you if it is pulling CS high, but I assume so since the attached program works
Sorry Duane. Here is archive of program.
I did take look at the PASM, and comments in program appear to handle this.
What do you get from your initial ADC reading?
I'd suggest adding a test loop just to make sure the ADC is behaving as expected.
Use some code like the code above to make sure output from the ADC is what you expect. When you use a loop like this does the output behave as expected?
Inserted your code, and yes, it repeatedly showed a value.
Looping doesn't seem to be a problem, see my comment with code to CJMJ above. I can load this program into my setup, and it works all day in a loop.
Here's a link to discussion of the issue. There's a lot of good advice in the replies.
You might need a voltage following buffer of some sort.
I scanned thru your file and found quite creative indention.
Unlike C in spin the horizontal position is very important for the compiler.
So my guess is that your formatting of the source does things you do not intend!
First put a empty line above any PRI/PUB/VAR stuff like that.
Next REALLY be careful to put statements into EXACTLY the same column it belongs following its previous one in the same indention.
Else you shoot yourself in the foot.
Enjoy!
Mike
I second Mike's advice.
That sounds very frustrating.
One possible troubleshooting technique is to make the smallest program possible which recreates the problem.
I'll try to take a look at your code some more when I have time.
I think you're reading the ADC too fast
I added your pause, and it had no effect. I can read this chip this fast in a little demo program I worked up attached.
If not...
In Process() it looks like Tune is being used without being initialized. Is that right?
Might want to set it to 1 first or just call Calcswr() directly in your main() routine.
I'm also wondering if you're running out of cogs, I can't tell for sure...
One possible troubleshooting technique is to make the smallest program possible which recreates the problem.
I have tried most of today trying to "break" the small demo file MCP3202demojcfbreak.spin . I have set it up similiar to the structure of my already broken AutotunerIIR9.spin, but cannot break it. Can see results that show 3202 working as expected in attached document. It works as one would expect.
I have also diagnosed the actions in my none working AutotunerIIR9, and attached both of these files. In AutotunerIIR9, the MCP3202 reads correctly after start up, reads correctly in the first method call, but reads 0 after that. I have found that if you don't start the MCP3202, you will get a 0 reading. I am wondering if the cog for the 3202 is some how getting clobbered between the first two calls and the rest. Is there a way to query how many cogs are running?
Actually, I guess you could just put an LED on the chip-select pin to try to see if it's toggling.
Maybe you'd need an oscilloscope or multimeter to say for sure, if the loop is very fast...
It looks like the variable "count" is incremented in every assembly loop.
I think I'd add this to the driver Spin:
It should be changing very often if the driver is running....
Can you capture the data pins and the input at the same time and compare the data with what you're seeing.
Another alternative is to switch 3202 drivers. There's a bunch of different versions. Some which run entirely in Spin. I know JonnyMac has written a couple different versions himself. If you have trouble finding these, I'm sure I have copies on my hard drive.
You have this in hover:
This is forcing your I/O pin for the 3202 low and giving you zero readings...
The analog input always has noise on the line that the 3202 correctly measures the first time after 3202 is called. So it should at least measure this value which is around 317.