PDA

View Full Version : WHILE WEND and SHIFTIN



Bill Legge
- 14th July 2011, 11:54
I'm using: PBPL on a PIC18F1220 and reading data from a SHT15 temperature/humidity sensor. Two issues:

1. I want the PIC to keep going in the main loop even if the SHT15 is not connected. However, the code includes:

while DataPin = 0 .... wend
.........
while DataPin = 1 .... wend

So, the loop halts if the sensor does not set/clear the DATAPIN. I guess there is a simple way to fix this - but it escapes me at the moment. Any ideas?

2. And a speed issue. All works OK with a 20MHz xtal and 'HS' compile. But with an 8MHz xtal and 'HSPLL' the SHT15 gives bad results. All the other routines work OK at 32Mhz and I have tried DEFINE SHIFT_PAUSEUS 1000; this puts the SHIFT clock at <1KHz and the SHT15 is rated to 50KHz - but no luck. Any ideas?

Regards Bill Legge

sayzer
- 14th July 2011, 12:39
1. When you are in the while loop, have a time out routine.
For example, have your PIC timer module set to the time you need. For example, IF TMR1IF THEN Exitloop. TMR1 is preloaded with your required time.

2. Need to see the code.

mackrackit
- 14th July 2011, 12:43
2.
Do you have
DEFINE OSC 32

mister_e
- 14th July 2011, 19:23
I'm using: PBPL on a PIC18F1220 and reading data from a SHT15 temperature/humidity sensor. Two issues:

1. I want the PIC to keep going in the main loop even if the SHT15 is not connected. However, the code includes:

while DataPin = 0 .... wend
.........
while DataPin = 1 .... wend

So, the loop halts if the sensor does not set/clear the DATAPIN. I guess there is a simple way to fix this - but it escapes me at the moment. Any ideas?
How about a pull-up resistor on DataPin?


2. And a speed issue. All works OK with a 20MHz xtal and 'HS' compile. But with an 8MHz xtal and 'HSPLL' the SHT15 gives bad results. All the other routines work OK at 32Mhz and I have tried DEFINE SHIFT_PAUSEUS 1000; this puts the SHIFT clock at <1KHz and the SHT15 is rated to 50KHz - but no luck. Any ideas?

Regards Bill LeggeNeed to investigate on that, maybe the PAUSEUS accept up to byte value, if so...mmmm... if this PIC allow to switch to the internal OSC, do it before SHIFTIN, and go back to the external after Shiftin.

EDIT: The DEFINE should allow you a range of 0-65,535 uSec... so that's strange...

mister_e
- 14th July 2011, 20:29
I suspect the lenght of the clock pulse not being long enough, the frequency should be ok... but the pulse too short for a 50KHz decvice. Maybe one of those roll-your-own thing... or using the OSC switchover thing would work.

Bill Legge
- 14th July 2011, 23:54
Thanks for all your replies.

1. Mackrackit. Yes, there is a 10k pull-up on the data line.
2. Mr_e. Yes, OSC 32 is in the code.
3. Mr_e. OSC switchover to use a slower internal oscillator - how do you do this?

4. Sayzer. Use a timer interrupt to kill the WHILE. I think this is the best way to go but I'm not clear how to make the interrupt jump over the necessary lines. Can an interrupt direct the execution of the code to a label later in the program? I'd appreciate some advice. I am happy with both PBP and assembler coded interrupts.

The code bit causing the problem is:



' ************************************************** ***************************
' * *
' * READ SHT15. Using default setting of 12 bit humidity,14 bit temperature *
' * Because of the WHILE statemets, this hangs if the SHT15 is not conected *
' * *
' ************************************************** ***************************
ReadSensor:
gosub Initialise_SHT15
gosub Start_Sequence
shiftout DataPin,Clk,1,[SHTCommand\8] ' Send command byte %0000000011 or %000000101
input DataPin ' Wait for acknowledge
low Clk
while DataPin = 1 ' Sensor ACK by making Data = 0
wend
pulsout Clk,10 ' Send acknowledge
while DataPin = 0
wend
while DataPin = 1 ' Wait for conversion to complete
wend
low Clk
shiftin DataPin,Clk,0,[RawData.byte1\8] ' Get the first byte, 8 bits
low DataPin
pulsout Clk,10 ' Send acknowledge
shiftin DataPin,Clk,0,[RawData.byte0\8] ' Get the second byte, 8 bits
low DataPin
pulsout Clk,10 ' Send acknowledge
shiftin DataPin,Clk,0,[CRCsensirion\8] ' Get third byte, 8 bits, CRC
high DataPin
pulsout Clk,10 ' Send acknowledge
input DataPin ' End of Transmission
input Clk
return


If anyone is interested, I'm happy to post all the SHT15 code?

Regards Bill Legge

mister_e
- 15th July 2011, 00:05
check this out
http://www.picbasic.co.uk/forum/showthread.php?t=4093

still, an home made shiftin is well under 20 lines of code.

Bill Legge
- 15th July 2011, 00:17
Mister_e
Thanks for that, I didn't know it was possible.
Another solution to prevent the code hanging has just occured to me.
Replace the WHILEs with fixed delays that are equal to the longest conversion time of the SHT15 sensor. I think this will be OK but it seems a crude solution compared with an interrupt?

Regards Bill Legge

mister_e
- 15th July 2011, 00:23
well if you build your own shiftin routine... you could check the state at each clock pulse... neat too.