PDA

View Full Version : Simple Networking problems



ABaylog
05-31-2005, 09:05 AM
·I'm using 5 BS2's to create a RS-485 Network. The network seems to be behaving properly but we are having problems with our switch signals being recognized by the Stamps. We are trying to set up a simple call system for our client in which there are 3 signals types being transmitted. Alert and Reset from the slaves to the Master; Acknowledge from the Master to the Slaves. If all buttons are pressed in proper order (alert, acknowledge, reset) the system seems to work well. However if buttons are pressed out of sync then the system "locks up" and nothing works unless the system is restarted.

·If anyone could look at the code and see if they can help our design team out it would be greatly appriciated. I am wondering if there·may be·an error with the way the BUTTON command is used.

Attached are circuit diagrams for the Master and a Slave as well as the code for each.



'------------Master Program----------------------
'------------------------------------------------
'Declare Constants
serDir CON 0 'Pin 0 is for Serial Transmission
serIO CON 1 'Pin 1 controls the transmitter 0=listen 1=talk
Multiplex_A CON 3 'First pin of the multiplexer
Multiplex_B CON 4 'Second Pin
Multiplex_C CON 5 'Third
Multiplex_D CON 6 'Fourth
MultiplexClock CON 7 'On/Off switch for multiplexer
MultiplexToggle CON 8 'Switch between Multiplexers 0 = A, 1 = B
FlipFlopClock CON 9 'Clock Pulse for flip flops
ClearButton CON 12 'Clears the Button State
AcknowledgeButton CON 13 'Push Button Input
n2400 CON 16780 'Baud Rate
tilde CON "~" 'Start of transmission Character
Room1ID CON "A"
Room2ID CON "B"
Room3ID CON "C"
Room4ID CON "D"
'Room5ID CON "E" 'Not included in our design
'Room6ID CON "F" 'Not included in our design
'Room7ID CON "G" 'Not included in our design
'Room8ID CON "H" 'Not included in our design
'------------------------------------------------
'Declare INPUTS and OUTPUTS
INPUT AcknowledgeButton 'Input Button
OUTPUT Multiplex_A
OUTPUT Multiplex_B
OUTPUT Multiplex_C
OUTPUT Multiplex_D
OUTPUT MultiplexClock
OUTPUT MultiplexToggle
OUTPUT ClearButton
OUTPUT FlipFlopClock

'-------------------------------------------------
'Declare Variables
RoomAddress VAR Byte
AcknowledgeFlag VAR Bit
Room1Flag VAR Bit
Room2Flag VAR Bit
Room3Flag VAR Bit
Room4Flag VAR Bit
'Room5Flag VAR Bit
'Room6Flag VAR Bit
'Room7Flag VAR Bit
'Room8Flag VAR Bit
skeet VAR Word
DataBit VAR skeet.BIT0
AlertBit VAR skeet.BIT1
EmergencyBit VAR skeet.BIT2
AcknowledgeBit VAR skeet.BIT3
ResetBit VAR skeet.BIT4
buttonworkspace VAR Byte
LightDisplay VAR Word
index VAR Byte
'--------------------------------------------------

'--------------MAIN--------------------------------

DEBUG "Top of the Program",CR,CR
HIGH ClearButton
LOW MultiplexToggle
HIGH FlipFlopClock
Start:

FOR RoomAddress = "A" TO "B"
skeet=%10000000
HIGH serDir:
DEBUG "Room Address = ", RoomAddress,CR,"Skeet out = ",BIN skeet,CR
SEROUT serIO, n2400, [tilde, RoomAddress, skeet, "."]
LOW serDir
SERIN serIO, n2400, 2000, Next_Room, [WAIT (tilde, RoomAddress), skeet]
PAUSE 20
DEBUG "Skeet in = ", BIN skeet,CR
IF DataBit = 1 THEN
DEBUG "Data Bit = 1",CR
GOTO Set_Room_Flag:
ELSEIF AcknowledgeFlag = 1 THEN
DEBUG "Acknowledge Flag = 1",CR
GOTO Local_Subroutine:
ELSE
GOTO Next_Room:
ENDIF
Next_Room:
PAUSE 20
DEBUG CR,"Next Room:",CR
NEXT
GOTO Start:
'----------End of Main Program-----------------

'----------Set Room Flags--------------------------
Set_Room_Flag:
PAUSE 20
IF RoomAddress = "A" THEN
Room1Flag = 1
' DEBUG "Room 1 Flag is set",CR
GOTO Check_Bits:
ELSEIF RoomAddress = "B" THEN
' DEBUG "Room 2 Flag is set",CR
Room2Flag = 1
GOTO Check_Bits:
ELSEIF RoomAddress = "C" THEN
' DEBUG "Room 3 Flag is set",CR
Room3Flag = 1
GOTO Check_Bits:
ELSEIF RoomAddress = "D" THEN
' DEBUG "Room 4 Flag is set",CR
Room4Flag = 1
GOTO Check_Bits:
ELSE
' DEBUG "DataBit = 1 but error setting Room Flag",CR
GOTO Next_Room:
ENDIF
'----------------------------------------------------
'----------Check Bits--------------------------------
Check_Bits:
DEBUG "Check Bits = ", BIN skeet, CR
IF AlertBit = 1 THEN
DEBUG "Alert Bit = 1, Acknowledge Flag Set",CR
AcknowledgeFlag = 1
GOTO Alert_LED:
ELSEIF ResetBit = 1 THEN
DEBUG "Reset Bit = 1, Acknowledge Flag Clear",CR
AcknowledgeFlag = 0
GOTO Reset_LED:
ELSE
' DEBUG "Data Bit Set but Alert Bit and Reset Bit were not",CR
' DEBUG "Going to the Next Room",CR
GOTO Next_Room:
ENDIF
'------------------------------------------------------

'-----------Set Multiplexer Code for Alert-------------
Alert_LED:
IF Room1Flag = 1 THEN
HIGH MultiplexClock
HIGH Multiplex_D
HIGH Multiplex_C
LOW Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 1: Alert",CR, "Multiplexer Pins = 1101",CR
PAUSE 50
GOTO LED_Clock:
ELSEIF Room2Flag = 1 THEN
HIGH MultiplexClock
HIGH Multiplex_D
LOW Multiplex_C
LOW Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 2: Alert",CR, "Multiplexer Pins = 1100",CR
PAUSE 50
GOTO LED_Clock
ELSEIF Room3Flag = 1 THEN
HIGH MultiplexClock
LOW Multiplex_D
HIGH Multiplex_C
LOW Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 3: Alert",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock
ELSE
HIGH MultiplexClock
LOW Multiplex_D
LOW Multiplex_C
LOW Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 4: Alert",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock
ENDIF
'---------------------------------------------------------------

'------------Set Code for Reset---------------------------------
Reset_LED:
IF Room1Flag = 1 THEN
Room1Flag = 0
HIGH MultiplexClock
HIGH Multiplex_D
HIGH Multiplex_C
HIGH Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 1: Reset",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock:
ELSEIF Room2Flag = 1 THEN
Room2Flag = 0
HIGH MultiplexClock
HIGH Multiplex_D
LOW Multiplex_C
HIGH Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 2: Reset",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock
ELSEIF Room3Flag = 1 THEN
Room3Flag = 0
HIGH MultiplexClock
LOW Multiplex_D
HIGH Multiplex_C
HIGH Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 3: Reset",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock
ELSE
Room4Flag = 0
HIGH MultiplexClock
LOW Multiplex_D
LOW Multiplex_C
HIGH Multiplex_B
HIGH Multiplex_A
' DEBUG "Room 4: Reset",CR, "Multiplexer Pins = ", BIN LightDisplay,CR
PAUSE 50
GOTO LED_Clock
ENDIF
'---------------------------------------------------------------
'-------------Turn on the Lights--------------------------------
LED_Clock:
LOW MultiplexClock
PAUSE 50
LOW FlipFlopClock
HIGH FlipFlopClock
PAUSE 50
HIGH MultiplexClock
DEBUG "Multiplexer has been clocked, so LEDs should turn on or off",CR
GOTO Local_Subroutine
'----------------------------------------------------------------
'---Check The State Of Acknowledge Button------------------------
Local_Subroutine:
DEBUG "Check the Button State",CR
BUTTON AcknowledgeButton, 0, 255, 0, buttonworkspace, 1, Room_1_Ack:
DEBUG "Button NOT Pressed",CR
GOTO Next_Room:
Room_1_Ack:
IF Room1Flag = 1 THEN
Room1Flag = 0
AcknowledgeBit = 1
LOW ClearButton
HIGH ClearButton
HIGH MultiplexClock
HIGH Multiplex_D
HIGH Multiplex_C
LOW Multiplex_B
LOW Multiplex_A
' DEBUG "Room 1: Acknowledge",CR, "Multiplexer Code = ", BIN LightDisplay,CR
PAUSE 50
LOW MultiplexClock
PAUSE 50
LOW FlipFlopClock
HIGH FlipFlopClock
PAUSE 50
HIGH MultiplexClock
DEBUG "Sending out Skeet = ", BIN skeet, CR, "To Room ", Room1ID,CR
HIGH serDir:
SEROUT serIO, n2400, [tilde, Room1ID, skeet]
LOW serDir
SERIN serIO, n2400, 2000, Room_2_Ack, [WAIT (tilde, Room1ID)]
DEBUG "Acknowledge Recieved",CR,CR
PAUSE 50
AcknowledgeBit = 0
GOTO Room_2_Ack:
ENDIF

Room_2_Ack:
IF Room2Flag = 1 THEN
Room2Flag = 0
AcknowledgeBit = 1
LOW ClearButton
HIGH ClearButton
HIGH MultiplexClock
HIGH Multiplex_D
LOW Multiplex_C
LOW Multiplex_B
LOW Multiplex_A
' DEBUG "Room 1: Acknowledge",CR, "Multiplexer Code = ", BIN LightDisplay,CR
PAUSE 50
LOW MultiplexClock
PAUSE 50
LOW FlipFlopClock
HIGH FlipFlopClock
PAUSE 50
HIGH MultiplexClock
DEBUG "Sending out Skeet = ", BIN skeet, CR, "To Room ", Room2ID,CR
HIGH serDir
SEROUT serIO, n2400, [tilde, Room2ID, skeet]
LOW serDir
SERIN serIO, n2400, 2000, Error, [WAIT (tilde, Room2ID)]
DEBUG "Acknowledge Recieved",CR,CR
Error:
PAUSE 50
AcknowledgeBit = 0
ENDIF
GOTO Next_Room:
'Room_3_Ack:
'IF Room3Flag = 1 THEN
' Room3Flag = 0
' AcknowledgeBit = 1
' LOW ClearButton
' HIGH ClearButton
' HIGH MultiplexClock
' LOW Multiplex_D
' HIGH Multiplex_C
' LOW Multiplex_B
' LOW Multiplex_A
' DEBUG "Room 3: Acknowledge",CR, "Multiplexer Code = ", BIN LightDisplay,CR
' PAUSE 50
' LOW MultiplexClock
' PAUSE 50
' LOW FlipFlopClock
' HIGH FlipFlopClock
' LOW FlipFlopClock
' PAUSE 50
' HIGH MultiplexClock
' DEBUG "Sending out Skeet = ", BIN skeet, CR, "To Room ", Room3ID,CR
' HIGH serDir:
' SEROUT serIO, n2400, [tilde, Room3ID, skeet,"."]
' LOW serDir
' SERIN serIO, n2400, 5000, Room_4_Ack, [WAIT (tilde, Room3ID)]
' DEBUG "Acknowledge Recieved",CR,CR
' PAUSE 50
' AcknowledgeBit = 0
'ENDIF
'Room_4_Ack:
'IF Room4Flag = 1 THEN
' Room4Flag = 0
' AcknowledgeBit = 1
' LOW ClearButton
' HIGH ClearButton
' HIGH MultiplexClock
' LOW Multiplex_D
' LOW Multiplex_C
' LOW Multiplex_B
' LOW Multiplex_A
' DEBUG "Room 4: Acknowledge",CR, "Multiplexer Code = ", BIN LightDisplay,CR
' PAUSE 50
' LOW MultiplexClock
' PAUSE 50
' LOW FlipFlopClock
' HIGH FlipFlopClock
' LOW FlipFlopClock
' PAUSE 50
' HIGH MultiplexClock
' DEBUG "Sending out Skeet = ", BIN skeet, CR, "To Room ", Room4ID,CR
' HIGH serDir:
' SEROUT serIO, n2400, [tilde, Room4ID, skeet,"."]
' LOW serDir
' SERIN serIO, n2400, 20000, Next_Room, [WAIT (tilde, Room4ID)]
' DEBUG "Acknowledge Recieved",CR,CR
' PAUSE 50
' AcknowledgeBit =

·

' Slave Program
'Declare Constants-----------------------------------------
serDir CON 0
serIO CON 1
EmergencyLED CON 4
AcknowledgeLED CON 5
AlertLED CON 8
ResetButton CON 11
ClearReset CON 12
ClearAlert CON 13
AlertButton CON 14
myID CON "A"
n2400 CON 16780
tilde CON "~"
'Declare Inputs and Outputs--------------------------------
INPUT AlertButton
INPUT ResetButton
OUTPUT AlertLED
OUTPUT AcknowledgeLED
OUTPUT ClearAlert
OUTPUT ClearReset
'Declare Variables------------------------------------------
alertworkspace VAR Byte
resetworkspace VAR Byte
emergencyworkspace VAR Byte
skeet VAR Word
DataBit VAR skeet.BIT0
AlertBit VAR skeet.BIT1
EmergencyBit VAR skeet.BIT2
AcknowledgeBit VAR skeet.BIT3
ResetBit VAR skeet.BIT4
'------------------------Main Program---------------------

HIGH ClearAlert
HIGH ClearReset
skeet = %10000000
alertworkspace = 0
resetworkspace = 0
Main:

DEBUG "Waiting for skeet from master",CR,CR
LOW serDir
SERIN serIO, n2400, 1000, Main, [WAIT (tilde, myID), skeet]
PAUSE 50
DEBUG "Skeet IN = ", BIN skeet,CR
IF AcknowledgeBit=1 THEN
DEBUG "Acknowledge Bit = 1",CR
HIGH AcknowledgeLED
HIGH serDir
SEROUT serIO, n2400, [tilde, myID, "."]
PAUSE 50
LOW serDir
GOTO Main
ELSE
DEBUG "Acknowledge Bit = 0",CR
GOTO Reset_Check:
ENDIF
Send_Data:
DEBUG "Skeet OUT = ", BIN skeet, CR
HIGH serDir
SEROUT serIO, n2400, [tilde, myID, skeet, "."]
LOW serDir
GOTO Main

'-----------------------Subroutine------------------------
Reset_Check:
BUTTON ResetButton, 0, 255, 0, resetworkspace, 1, Reset_On:
DEBUG "Reset Button was NOT pushed",CR
PAUSE 20
Alert_Check:
BUTTON AlertButton, 0, 255, 0, alertworkspace, 1, Alert_On:
DEBUG "Alert Button was NOT pushed",CR
PAUSE 20
IF AlertBit = 0 AND ResetBit = 0 THEN
GOTO No_Data:
ELSE
GOTO Send_Data:
ENDIF

Reset_On:
DEBUG "Reset Button WAS Pressed",CR
LOW AcknowledgeLED
LOW AlertLED
ResetBit = 1
DataBit = 1
LOW ClearReset
HIGH ClearReset
GOTO Alert_Check:
Alert_On:
DEBUG "Alert Button WAS Pressed",CR
HIGH AlertLED
DataBit = 1
AlertBit = 1
LOW ClearAlert
HIGH ClearAlert

Priority_Check:
IF ResetBit = 1 AND AlertBit = 1 THEN
DEBUG "Reset and Alert were both ACTIVE, Reset =1, Alert =0",CR
LOW AlertLED
ResetBit = 1
AlertBit = 0
DataBit = 1
ENDIF
GOTO Send_Data:
No_Data:
DEBUG "No buttons pressed", CR,CR
DataBit = 0
GOTO Send_Data:

Jon Williams
05-31-2005, 06:47 PM
BUTTON is designed to be used in a small, tight loop and can be very tricky.· On top of that, it only lets you look at one input at a time.· Since most of my programs need to look at multiple inputs, I do it with a small subroutine like this:

Scan_Buttons:
· btns = %1111···················' assume pressed
· FOR idx = 1 TO 10
··· btns = btns & ~INA···········' update button state
··· PAUSE 5······················' debounce delay
· NEXT
· RETURN

This routine assumes that we have four active-low buttons (remove the ~ for active-high) buttons on P0 - P3.·· It starts by assuming the buttons are pressed, then loops through ANDing the current inputs with the button state.· If a button ever bounces (gets released) that bit will be cleared; in the end this routine ensures that the button(s) are pressed and held for the entire duration of the routine.

A common request lately is to "one-shot" the buttons, that is, you only want to know when a button transitions from off-to-on.· Here's a pretty neat trick from our pal, Dr. Tracy Allen:

Scan_Buttons:
· btns = %1111·················· ' assume pressed
· FOR idx = 1 TO 10
··· btns = btns & ~INA·········· ' update button state
··· PAUSE 5····················· ' debounce delay
· NEXT
· xBtns = btns·^ oBtns & bnts··· ' report 0-to-1
· oBtns = btns·················· ' save old state
· RETURN

With this, a "1" in xBtns indicates that the button transitioned from 0-to-1 between scans.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

ABaylog
06-01-2005, 05:50 AM
The·following change was recommended:

Scan_Buttons:
· btns = %1111·················· ' assume pressed
· FOR idx = 1 TO 10
··· btns = btns & ~INA·········· ' update button state
··· PAUSE 5····················· ' debounce delay
· NEXT
· RETURN

I tried to implement this into my program and there is no response to a button being pressed. The buttons are ran thru a latch so that once they are pressed they should remain in the ON-state until the program recognizes them and resets them. So making sure that the switch is in the desired state shouldn't be a problem. I am using just two active low switches on P14 and P15 and have been trying variations of the sample code you sent me but am still unable to get it ironed out. I changed the INA part to IND to take care of the different pin use.

Thanks for your help so far and hopefully I am just implementing it incorrectly. If anyone has any further comments or suggestions I would be willing to try about anything to get this project working.
http://forums.parallax.com/attachment.php?attachmentid=73936

Post Edited (ABaylog) : 5/31/2005 10:55:02 PM GMT

Jon Williams
06-01-2005, 07:52 AM
If the buttons are run through a latch then one can assume they're hardwared "debounced" and all you have to do is read the input bits when you want to check the status. Am I missing something? Neither BUTTON or my debounce routine are required. It's as simple as:

· IF (SomeBtn = TargetState) THEN
··· ' do something
· ENDIF

On another topic, if you've not committed to your hardware design you can certainly simplify your master circuit and code by using two 74HC595 shift registers instead of the 'LS154 and the 'LS175s that you've got in your current design.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax