PDA

View Full Version : 16F876 problem... absolutely baffled



khufumen
- 1st December 2004, 04:52
I've got the following code shown below. In order for it to work I need to put a pause in of 8 ms or greater or else the serout2 command outputs 0s. In other words, if the pause is set to 10 ms I get good data but if the pause is set to 5 seconds or there is no pause at all, I get 0s. Does this occur because my control pins for the AD converter are accessing different ports? This issue has been baffling me for some time.

The 16F876 I am using is a surface mount SOIC chip or else I would check it by moving my control lines to different I/O pins.

I hope somebody can give me some insight.

Kind Regards,
Eric

DEFINE OSC 20 ' define oscillator to be 20Mhz
ADCON1 = 7
include "MODEDEFS.bas"

' Power connections

MainPower VAR PORTC.3

' Communication connections
TX VAR PORTA.0

' AD converter connections
sclk VAR PORTC.1 ' SPI clock out from BS2 to AD2543
sdo VAR PORTC.0 ' SPI data out from BS2 to AD2543 sdi
sdi VAR PORTA.5 ' SPI data in from AD2543 sdo to BS2
ADcs VAR PORTA.3 ' AD2543 chip select, active low

' *********************** [ CONSTANTS ] ************************
' Sensor Constants
TiltX Con 2

' [ VARIABLES ]
Result VAR WORD
ADch var Byte

high MainPower
Adch = TiltX

'[ MAIN PROGRAM ]
Main:
pause 8
gosub ReadAD
serout2 TX, 16468, [dec result, 13]
goto Main


' read the AD converter
ReadAD: ' entry point to give result as millivolts
LOW ADcs ' select chip
SHIFTOUT sdo,sclk,MSBFIRST,[ADch<<8\12]
SHIFTIN sdi,sclk,MSBPRE,[result\12] ' get result, 12 bits
HIGH ADcs ' deselect chip
RETURN

mister_e
- 1st December 2004, 06:46
When you remove pause 8, do always got 0s as results?

what about if you use SEROUT instead of SEROUT2?

Since you have a internal USART why not simply use it??? but i figure everything is already place/set on board now...

khufumen
- 1st December 2004, 15:06
I am using the SEROUT2 for a speedier baud rate. If I remove the PAUSE 8, I get nothing but zeros in the serout2. The ADRead subroutine works great on its own and the SEROUT2 works great on its own; however when the two are combined there is a problem. Still can't figure it out.

L_Gaminde
- 11th December 2004, 16:07
Try two things put the pause 8 right before you adc read and second put the pause 8 at the end of your adc read just before return. you could be trying to read the adc too fast.

Look up the adc chip and check the speed of reading or see if there is a way to do multiple reads and if its different than single reads. also check the state that you are leaving the adc in just before the return command you may need a low there and a high to low just before the shiftin command.