Help with 2 way communication
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:
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:
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