Help! - Very strange results with ShiftIn/ShiftOut
Hi,
I am running a 16F876 at 20MHz. I am reading a channel on an AD converter (Texas Instruments TLC2543) using the shiftin/shiftout commands and then sending the results to a serial port at 38400. The simple code is shown below. The code works fine as long as the Pause 4 is being used. If I remove the Pause 4 or make it lower than 4 ms, the variable 'result' is zero instead of 2544 or what have you. When I check the voltages at my voltage pin (PORTC.3) which is powering the sensor and ad converter, the pin reads 0 volts when the "PAUSE 4" is not used and when the "PAUSE 4" is used it goes to 5 volts. Now that's weird. The reason I'm getting a zero in the result is because the power pin mysteriously goes to zero as if there is a short or something.
I thought it was perhaps the AD converter being slow but I cancelled this theory when I found I can loop through the converter 20 times and take an average and there is no problem as long as I use the "PAUSE 4" before entering the ReadAD subroutine, looping within the subroutine 20 times and then exiting. If I put the "PAUSE 4" after the ReadAD Subroutine I get zeros. The "PAUSE 4" has to be done before the ReadAD Subroutine. It's really bizzare.
If anyone has some insight into this I would sure appreciate it.
Kind Regards,
Eric
clear ' clear the variables
High SensorPower
MainLoop:
Pause 4
ADch = TiltX
gosub ReadAD
serout2 TX, 6, [Dec result, 13, 10]
goto MainLoop
end
'************************************************* *
' read the AD converter
ReadAD:
High SensorPower
LOW ADcs ' select chip
SHIFTOUT sdo,sclk,MSBFIRST,[ADch<<8\12] ' mode, left justify ADch
SHIFTIN sdi,sclk,MSBPRE,[result\12] ' get result, 12 bits
HIGH ADcs ' deselect chip
RETURN
Finally figured out problem
Thanks for all your help and input. What was causing the problem was that there was no delay between when I powered up the AD converter and when I started reading from the AD converter. The AD converter when powered up needs about 4 ms to stabilize before it can be read from. Once it stabilizes, it has no problem reading the the data.
The fixed code reads:
clear ' clear the variables
High SensorPower ' power up the sensors and the AD converter
Pause 4 ' wait for power to stabilize
MainLoop: ' enter main loop
ADch = TiltX
gosub ReadAD
serout2 TX, 6, [Dec result, 13, 10]
goto MainLoop
end
'************************************************* *
' read the AD converter
ReadAD:
LOW ADcs ' select chip
SHIFTOUT sdo,sclk,MSBFIRST,[ADch<<8\12] ' mode, left justify ADch
SHIFTIN sdi,sclk,MSBPRE,[result\12] ' get result, 12 bits
HIGH ADcs ' deselect chip
RETURN