PDA

View Full Version : Help with 2 way communication



jessey
- 21st April 2007, 09:44
Hello,

I wrote some very basic code to communicate back and forth between a 16F688 & 18F452 and would like to get some feed back on it or any suggestions for an alternate way of doing it, if its within my comprehension. I'm using one wire connected between pin 11 of the 688 and pin 7 of the 452, both pic's in/out pins have a 10k pull-downs and also a ground wire connected between the power supplies of the 2 circuits.

The code I wrote works as follows:
When its time to turn on or off a pump the code enters a WHILE - WEND where I change the Communications_Pin to an output and send a + signal to the 18F for 100 ms then I set it back to an input and pause for 50 ms for a confirmation back from the 18F that the signal was received. This repeats until the 18F replies. When the 18f replies then that gets us out of the WHILE - WEND and we enter another WHILE - WEND and remain there for the time that the 18F sends its + confirmation signal back that it received our request.

Then when the 18F's + confirmation signal times out, we set the Communications_Pin to an output again and send a + signal back for 1500 ms to turn on the pump and/or 2500 ms to turn off the pump. While receiving, the 18F's code sits in a WHILE - WEND with a counter and ends up with a number that's used in IF - THEN's to decide what to do. Everything seems to work good with no problems. I'm using the INTRC_OSC_NOCLKOUT on the 16F and OSC 4 on the 18F. Will temperature become a problem with this set up, I can't seem to get anything out of the data sheets for this one concern I have?

Thanks
jessey

16F code:


TRISA.2 = 1 'pin is input before entering while - wend

WHILE Communications_Pin = 0'18F sends + to get us out of this loop
Wait_Time2 = Wait_Time2 + 1'sound an alarm if we don't get a + back
IF Wait_Time2 >= 80 THEN'setting C to 0 allows alarm prints to show
GOSUB Set_PORTA5_To_Output:GOSUB Sound_Alarm:C=0
ENDIF
TRISA.2 = 0 ' set Communications_Pin output to enable send to 18F
Communications_Pin=1'+ signal is now being sent to 18F controller
GOSUB Pause_100 ' send the + signal for 1/10 of a second
TRISA.2 = 1 'set pin input to receive a confirmation back from 18F
GOSUB Show_Waiting_And_Or_Error_Print
PAUSE 50
WEND'we get out of this WHILE-WEND loop after receiving confirmation
LCDOut $fe, 1, "Confirmation Has"
LCDOut $fe, $c0, " Been Received "
'WHILE-WEND for the 18F Confirmation
WHILE Communications_Pin = 1 : WEND ' 18f sends + back for 500ms here
TRISA.2 = 0' set Communications_Pin to an output to reply
Communications_Pin = 1'+ signal is now being sent to 18F controller
IF Moisture <= Alarm_Set_Point THEN Pump=On_:PAUSE 1500
IF Probes=0 THEN Probes=1:Pump=Off_:PAUSE 2500
Communications_Pin = 0'stop the 18F from counting any more...
Confirmation = Was_Confirmed'prevents ending up back here next pass
TRISA.2 = 1 ' Communications_Pin is set to input before returning


18F code:



Receive_1st_Incoming_Pulse:
LCDOut $fe, 1,"Received Request"
LCDOut $fe, $c0, "Snd Confirmation"
WHILE Communications_Pin = 1 : WEND'wait here until 688 is finished sending +
TRISA.5 = 0 ' make Communications_Pin an output to send confirmation to 688
PAUSE 10
Communications_Pin = 1' sending confirmation now
Pulse_Length = Is_Not_Counted
PAUSE 500 ' send confirmation for 1/2 second to be sure its received
TRISA.5 = 1 ' make Communications_Pin an input again to receive 2nd pulse

Receive_2nd_Incoming_Pulse_Loop:
LCDOut $fe, 1, " Receive Pulse "
LCDOut $fe, $c0, "CountPulseLength"
WHILE Communications_Pin = 1'wait here until finished receiving pulse from 688
Time_In_Loop = Time_In_Loop + 1
LCDOut $fe, 1, " Receiving Now "
LCDOut $fe, $c0, "PulseLength =",DEC Time_In_Loop'Count the length of pulse
Pulse_Length = Is_Counted : PAUSE 100
WEND
IF Pulse_Length = Is_Counted THEN Use_Count_To_Decide_What_To_Do
PAUSE 100
GOTO Receive_2nd_Incoming_Pulse_Loop 'stay here until incoming pulse is received

Use_Count_To_Decide_What_To_Do:
IF Time_In_Loop <= 18 THEN Pump = Is_Turned_On
IF Time_In_Loop >= 20 THEN Pump = Is_Turned_Off
Time_In_Loop=0
RETURN

Darrel Taylor
- 22nd April 2007, 03:32
Will temperature become a problem with this set up,

I wouldn't think so. You've got a pretty big margin of error with that routine.

Looking at Figures 15-37 thru 15-40 of the 16F688 datasheet, it looks like there's only a +/- 3% change in freq across the -40 to +125°C range.

So the 1500ms will be 1455-1545ms, and 2500ms will be 2425-2575ms.

Since the receive loop is timed by a 100ms pause, the count will only change by +/-1, across the whole temperature range.

I think I would add a minimum though. If it only gets a count of 1 or 2 then it's just spurious noise and should be rejected.

HTH,

jessey
- 22nd April 2007, 09:11
Hi Darrel,

Thanks for clearing that up for me. I'll have to spend more time deciphering the data sheets.

Thanks Again
jessey