View Full Version : PLC Analog input channels - crosstalk??
04-15-2007, 03:27 AM
I am developing an application using a StampPLC equipped with a Stamp2 module and a MAX1270 A/D converter.
I have routines that use SHFTIN to read values from Channel 1 (0-5 volts) and Channel 2 (0-5 volts). These exactly mirror those on page 10 of the StampPLC documentation. I am aware of the difference between how the configuration byte is used by the PLC installation of the MAX1270 compared to how the MAX1270 documentation configures that byte, and my configuration of the control byte mirrors the PLC documentation. For example, channel 2 at 0-5 VDC looks like: 11100000
Each channel has a seperate subroutine with a seperate variable for the incoming result. One is used to monitor a sensor, the other will monitor battery voltage.
At the moment, the Stamp PLC has only the rs-232 and power pins in use. all other including the analog channels are not attched to anything (ground included) - floating?
When I comment out the channel 1 subroutine, the channel 2 subrountine returns the expected 0 value. When I comment out the channel 2 subroutine, the channel 1 subroutine returns the expected 0 value.
However, when I run with both the channel 1 and channel 2 subroutines working (not commented out), both show a small "residue" of about 500-600 millivolts (according to the software) after conversion from the ADC.
Is this environmental crosstalk between channels? I could understand if a channel showed a little voltage when ungrounded, but why show none until another channel is read and then show some?
Having seen the "read_tank_level" routine on page 25 of the PLC documenation, I added some pauses (tried 1 to 10) between the HIGHs, LOWs, and SHIFTINs to add to the clock time with no improvement.
Needless to say, I'm no electrical engineer.
04-16-2007, 07:38 PM
I had instances of a small amout of capacitive/resistive coupling between two adjacent channels on another a/d chip.
I put a relatively high value of pull-down (15K to 47K ohms) from each channel's input pin to ground and that cured the problem.
Note that if your incoming signal is a high impedance source, your converted number may be smaller than expected, but it will be correctable.
04-16-2007, 10:49 PM
Thanks for in info. The "Nuts and Volts" article (1/2004) about the PLC indicated it has a 100K ohm resistor and a .1 uF capacitor built into each analog input circuit, but I'll try your suggestion. I have noted that when one or the other circuit is attached, the error on the "floating" pin is substantially reduced. Of course, I can't really reach any conclusions about the size of the error that might be present on the attive pin, but the stamp millivolts readings pretty well agree to my accurate voltmeter, so I don't think the error is at all substantial.
I have actually built in two mathematical corrections - one for that circuit which corrects by a factor of 1.05243 as suggested in the N&V article. In addition, I also have a scaling factor to match the 0-5 VDC input to the 0-4095 bit scale. So now I have 4095 divisions of a 5 volt scale, corrected for the voltage divider circuit. As mentioned, it seems to agree very closely to my voltmeter. I think I've got that much handled.
That 0-5 VDC input is from an ultrasonic sensor. This presents me with a big math problem - a variable in the division of the equasion. I need to compute the depth of a substance on top of a surfce with a known distance. This involves the computation of a sensor voltage range, a millivolts-per-inch value, and a distance based on those values.
The maximum value of the input range will always be 5000 millivolts. The minimun is unknow to me now, but will be something more than 0 volts. I will acquire it on a button press and store this value in eeprom for future use. This value is the distance from the sensor to the flat surface that is the bottom of the depth measurement. The total voltage range of the instrument will be the maximum (5 volts) minus the minimum (?). From this range (maybe 4000 millivolts?) I can compute a millivolts per inch of range since I know the exact height of the sensor (like, for example, 72 inches over the bottom surface). Knowing the factor, all I have to do is take the current sensor input, subtract the minimum, and multiply the result by the millivots-per-inch factor to get the depth of the target from the zero point.
However, I have to do fractional math to get the millivolts-per-inch facor which computes as (scale max - scale min)/distance in inches. in real numbers this might look like (5000 - 2000)/72 or 3000/72. It would never be ouside the range of 0/72 to 5000/72 as logical extremes. I then have to do another division (current reading - minimum)/millivolts per inch to get the distance from target answer. this value would have logical extremes of 5000/69.444 (approx 72) and 0/some positive value (call it 0).
I have found that trying to do stepwise long division or just use integer base 10 math yields an unacceptable answer in tern of the mathematical accuracy of the result.
I know this is a difficult explanation to follow, but if anyone can give me a clue as to maybe how some binary math could be employed, let me know!
Thanks for the help!
04-17-2007, 01:04 AM
Have you had a look at the */ and ** binary arithmetic functions in the stamp help files? I think they will give you the fractional flexibility you need and appear to work well within word-sized variables.
04-17-2007, 09:51 AM
Thanks for that suggestion. I have indeed looked at those functions, and have solved the problem. I had to do some fancy scaling and re-thinking of the order of operations to keep the numbers within the Word 2 byte size, but finally got it done.
Now working on the PC side - probably will use the spreadsheet macro or data logging appliactions.
04-17-2007, 12:12 PM
You have already come up with a solution, but it troubles me that it requires fancy scaling! Maybe the following will help.
The problem is always something like inches * (reading - minvalue)/(maxvalue-minvalue). The ratio on the right is always between 0 (when reading = minvalue) and 1 (when reading = maxvalue). That satifies the conditions for the following algorithm.
N = reading - minvalue
D = maxvalue-minvalue
' F is a word variable
for J=15 to 0 ' 16 bits, binary long division
N=N//D<<1 ' remainder*2
F.bit0(J)=N/D ' next bit
answer = F ** inches
04-19-2007, 03:52 AM
Many Thanks for the division suggestion. I eventually asked the sensor tech rep if, in fact, the analog 0-5 volt sensor output was linear and that was confirmed. In fact, the output is linear over 126 inches, so .039683 volts per inch is the magic number. Rather than trying to recalculate it each time, I will just use it as a constant. By "scaling up" my 0-5 volts I can do the math using a whole number (397) and stay within the Word limit. I will spend some time checking the calibration when the instrument is finished and in place, but I think this will work for my purposes. If I'm off by a bit, I can always fuss with the factor constant
Using the Basic Stamp and the StampPLC for a project has brought back memories of struggling with the assembler programming limitations of early processors like the 8080 and Z-80. In addition, my failing math and non-existant engineering skills have been highligthed. But for me using the Stamp is not only necessary to accomplish the project, but also a lot more fun than putting together jig saw puzzles. All of this is an enjoyable challenge for me.
At this point, I have the PLC sending the appropriate data in the appropriate form back to StampPlot software on the bench, so I'm getting close to moving from prototyping to hardwiring the whole project. Maybe when it's done I'll post a description - its a solar powered ultrasonic snow depth measuring device that communicates with a remote PC via radio link, to be used for some environmental studies related to butterfly habitat restoration.
First I'll go to the manuals and figure our the logic of your solution. It certainly looks more elegant than my crude long division. After that I'm off to solder some parts to a circuit board. Maybe I can get that much right! Heck - I've got until next winter to finish it and calibrate it.