ME 218C Final Project
Introduction Mechanical
Hardware
Electrical
Hardware
Software Gems of
Wisdom
Pictures/
Video
Links
Helm Master

Interrupt:
	PUSH
		Copy W to TEMP register
Swap status to be saved into W
Save status to STATUS_TEMP register
ISR
	If we received anything through EUSART, CALL	EUSART_Rx_Int
	If Timer0 overflow flag is set, CALL	Timer0_Int
	POP
		Swap nibbles in STATUS_TEMP register and place result into W
		Move W into STATUS register
		Swap nibbles in W_TEMP and place result in W_TEMP
		Swap nibbles in W_TEMP and place result into W	
	RETFIE

EUSART_Rx_Int:
	Clear flag first
	Read data into appropriate index of receiving buffer
	Increment the index of the data
	Check whether the first 4 bytes of the received API packets are 0x7E, 0x00, 0x08 and 0x81 for error checking.
	If they don’t match, clear data index and received flag.
If not all the packet is received yet, RETURN
	ELSE, API frame is done. Set the receive DONE flag and clear data index.

Timer0_Int:
	Clear interrupt flag
	If 10 overflow has occurred reset the counter and set a flag
	RETURN

Initialize:
	Disable all the analog inputs
 	Setup ADC
	Set to 8 MHz internal clock
	Setup EUSART with 9600 baud rate
	Setup PWM
	Setup SPI
	Setup Switches/buttons
	Setup Timer0 with 1:256 prescaler and interrupt
	Enable Interrupts
	RETURN


InitializeGameStates:
	Clear all flags
	Set up Ping status as waiting for iButton
	Clear Craft Address
	Clear received flag and data index
	Reset Stand Down Timer
	Set up COM_frameid and COM_options
	Reset TEAM, CRAFT#, ACTIVE BASE, STAND DOWN INDICATORS 
	RETURN

Main:
	Initialize
	InitializeGameStates
	Wait for iButton
	Wait for Synch
REPEAT
	If Hard Reset is received, GOTO Main and start over the game
ELSE, CALL CheckTimer0Flag
		CALL	CheckInputs
		CALL	SetupNavRegs
		CALL	APIRx
	END

CheckTimer0Flag:
	If Timer0 overflow flag is not set, RETURN
	ELSE, 
Clear overflow flag and transmit data
		If we’ve received a new stand down command, reset the timer.
		If the Stand Down Timer is zero, 
Indicate LED that we are not standing down
If the game has been started, send a navigation command
ELSE, send no action
RETURN
		ELSE,	
			Transmit no action
			Indicate LED that we are standing down
			Decrement Stand Down Timer
			RETURN
	END


CheckInputs:
Interpret the user inputs to the helm to get water speed, specials, speed, and direction
	Get Speed from the potentiometer
Read upper 8 bits of the data
	Check forward/reverse input from the toggle switch and calibrate the speed data accordingly
	Get Direction from the accelerometer
Do Direction calibration
	Get Specials Inputs from two buttons
	Get Water strength input from potentiometer
	Read upper 8 bits of the data
	If End Game command is received or the water shoot button is not pressed, disable water strength
	RETURN

SetupNavRegs:
Once all ship control parameters are known, setup navigation registers to hold the data for transfer over the Xbee
	Setup Navigation Byte 1 with speed and direction info
Setup Navigation Byte 2 with water strength and specials info
	RETURN

APIRx:
	Check whether an API packet is received
	If not, RETURN
	ELSE,
		Clear received flag
If the message is from the craft, GOTO API_Craft
ELSE If the message is from the admiral, GOTO API_Admiral
		ELSE, 	It's garbage, RETURN

TransmitNoAxn:
Sends a no action command (helm to craft) as defined in Comm. protocol.
	RETURN

TransmitNav:
	CALL	SetAddressToShip
Send NavByte1 and NavByte2 to craft 
	CALL	API_TxPacket
	RETURN	

API_Admiral:
	If the message is from the admiral's address,
		Check if it is Start Game. If yes, GOTO AdmStartgame
Check if it is End Game. If yes, GOTO AdmEndgame
Check if it is Blue Goal. If yes, GOTO AdmBluegoal
Check if it is Red Goal. If yes, GOTO AdmRedgoal
Check if it is Hard Reset. If yes, GOTO AdmHardreset
Check if it is Ping. If yes, GOTO AdmPing
	RETURN

AdmStartgame:
We set Start Game flag so we can play, but first respond with ack to admiral
	RETURN

AdmEndgame:
We set End Game flag, but first respond with ack to admiral
	RETURN

AdmBluegoal:
Indicate that the blue goal is active - send to slave over SPI
Turn off ActiveRed LED
Turn on ActiveBlue LED
	RETURN

AdmRedgoal:
Indicate that the red goal is active - send to slave over SPI
Turn on ActiveRed LED
	Turn off ActiveBlue LED
	RETURN

AdmHardreset:
Set the hard reset flag
	RETURN

AdmPing:
Send back the correct ping data
	RETURN


API_Craft:
;   The only command we really want is stand down, since matched would happen
;   during waiting for synch section of code
	Check if the message is from the craft,
	Check if it is Stand Down message, If not RETURN
	If It's a stand down command, 
Send an ack to the craft and then set Stand Down flag so we can send no actions for 10 seconds
RETURN

SendAckToAddress:
	Send acknowledgement message to the address set up
	RETURN

SPI_Transmit:
Transmits to SPI, no slave select
Wait until data been received (transmit complete)
Set W reg = contents of SSPBUF
	Save the received data in user RAM
	Set W reg = contents of TXDATA
	Move it to SSPBUF to xmit
	RETURN

Indicate_Stand_Down:
Indicate Stand Down through LED
Send the command using SPI_Transmit
RETURN

Indicate_No_Stand_Down:
Indicate No Stand Down through LED
Send the command using SPI_Transmit
RETURN

Check_Team:
	If the iButton reading is odd, set Team as Red
	Else, Team is Blue
	Send the information to slave using SPI	
	RETURN

Indicate_Craft_Num:
	Send Craft number to slave using SPI
	Change Ping data to Craft number 
	RETURN


Waiting_for_iButton:
	Initialize Timer1 as 8MHz and 1:1 prescaler
	Set iButton data pin as digital I/O
	CALL	READ_iButton
CALL	RESET_iButton
CALL	SEND_READ_COMMAND
CALL	READ_iButton_data
	RETURN

READ_iButton:
Check if we received admiral ping command. If yes, CALL AdmPing
Set iButton pin as an output
Set the line low
Wait for 500 usec
Set the pin as an input
Wait for 50 usec
Read the pin
If no iButton found, repeat READ_iButton
	If iButton is found, RETURN

RESET_iButton:
Set the pin as an output
	Set the line low
	Wait for 500 usec
	Set the line high
	Wait for 500 usec
	RETURN

SEND_READ_COMMAND:
Send read ROM command (0x33) by WRITE_1 and WRITE0
RETURN

WRITE_1:
Set the pin as an output
	Set the line low
	Wait for 5 usec
Set the pin as an input
	Wait for 70 usec
	RETURN

WRITE_0:
Set the pin as an output
	Set the line low
	Wait for 60 usec
Set the pin as an input
	Wait for 15 usec
	RETURN

READ_iButton_data:
	REPEAT 24 times because we need to read 24 bits
Set the pin as an output
Set the line low
Wait for 5 usec
Set the pin as an input
Wait for 10 usec
Shift the readings to get it ready for the next bit
		Set the line low
		Read the pin
If high, reading is 1. Save 1
		If low, reading is 0. Save 0
Wait for 60 usec
	END
	RETURN


Waiting_for_Sync:
Update status for ping command to waiting for synch
	Setup to broadcast ibutton data
		CALL	SetAddressToBroadcast
		Send iButton reading
		Wait for a while
	Check if we've received a message.  At this point all we care about
	   receiving are matched from the ship, or ping from the admiral
	If nothing received, go back to broadcast iButton data
	If received,
		Clear received flag
If it's a matched command,
	Save the address of the craft 
Setup ping data to respond
	CALL	Check_Team	
	CALL	Indicate_Craft_Num
			RETURN
ELSE If it’s an admiral ping, CALL AdmPing
	END
	Go back to broadcast iButton data
 
Helm Slave

Initialize:
	Setup internal clock as 8MHz
	Setup SPI
	Setup LEDs as outputs and turn everything off
	Enable Interrupts
	RETURN


Main:
	CALL	Initialize
	REPEAT and wait for the command from Helm Master
		NOP
	FOREVER

Interrupt:
	PUSH
		Copy W to TEMP register
Swap status to be saved into W
Save status to STATUS_TEMP register
ISR
	If we received anything through SPI, CALL	SPI_Int
	POP
		Swap nibbles in STATUS_TEMP register and place result into W
		Move W into STATUS register
		Swap nibbles in W_TEMP and place result in W_TEMP
		Swap nibbles in W_TEMP and place result into W	
	RETFIE


SPI_Int:
;   ISR that receives/transmits from SPI, but primary purpose is to receive
WREG reg = contents of SSPBUF
	Save the received data in user RAM
	W reg = contents of TXDATA
	Process the command
	If the command is Reset (0xff), Goto Initialize
	If the command is LED control, GOTO LEDCtrl
	If the command is Craft number, GOTO CraftNumCtrl

LEDCtrl:
	If the command is about Stand Down,
		If standing down, turn on the stand down LED
		ELSE, turn off the stand down LED
	ELSE If the command is about Active Base,
		If Blue is active,
			Turn on Blue Active Base LED and Turn off Red LED
		ELSE If Red is active,
			Turn on Red Active Base LED and Turn off Blue LED
	ELSE, the command is about Team
		If  Red Team ON, turn on Red Team LED 
		ELSE If Red Team OFF, turn off Red Team LED 
		ELSE If Blue Team ON, turn on Blue Team LED 
		ELSE If Blue Team OFF, turn off Blue Team LED
 	RETURN


CraftNumCtrl:
	Error check if the craft number is only between 1 and 12
	If not,	RETURN

	Decide how to set LED pins from the LCD Table according to the craft number
	Set 8 LED pins according to the value from the table
	Give pulse between writing each bit for clock bit of the shift register
	Pulse the cycle bit of the shift register after all 8 bits have been written
	RETURN