Before we began our design, there are some issues that we need to consider:
Our program used only timer1 as the time base. We chose timer1 to ensure accuracy and to get the time base of 1ms. We ran 4 main task timers, 1 for keypad scan, 1 for checking new messages, 1 for transmitter, and another for the receiver.
The keypad scanning timer is run every 30ms, the mail checking timer is run every 100ms, while the transmitter and the receiver timers are run every 2ms.
Besides these timers we also had several other "expiry" timers that exits a certain operation for error protection.
We used an "ack_timer" to exit the process when we don't receive an acknowledgement from the other device for a long time. There was also timeout that ends the receiving process if data transmission gets lost or obstracted. These timers value can be arbitrarily adjusted to fit the environment or situation.
More on Handshaking:
The transmitter and receiver timers each has 3 states. It is the transition between these states that implements the handshaking scheme.
A simple, "ideal", scenario:
Device A wants to send message to device B. A starts by sending an initialize character to B and then turning off the transmitter. At this time, A's receiver is set to be in "wait_for_ack" state. Basically, once 'X' has been sent, A is now waiting for an acknowledgement for B. If everything goes well, B gets the signal and turns off the transmitter to prevent interference. B then reply with an ACK. A now receives the ACK and proceed with sending the message. B is receives the message at the same time. Once sending and receiving of the message is done, the states restart on both sides.
Now, some error handling schemes:
1) If both are transmitting at the same time, neither device will get an acknowledgement, and this is where the ack_timer kicks in. After a specified time, both device will prompt a timeout message and resets the connection.
2) While one device is typing a message, it can still receive message.
3) The device will not send message until receiving is over.
4) If a device is interrupted while receiving message, the timeout timer will take effect and resets the connection after a certain time.
5) If the user is reading a message, the device will not accept new messages to prevent overwrite.
6) Since due to limited memory, only 1 received message and 1 send message will be saved. This means that every new received message, if allowed, will overwrite the existing one.