CodeCodeCode

ECE 362 Light-up Chessboard mini project version A

Dec 10th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 127.68 KB | None | 0 0
  1. /*********************************************************************************
  2. * ECE 362 - Mini-Project C Source File - Fall 2012
  3. **********************************************************************************
  4. **********************************************************************************
  5. *
  6. * Team ID: 16
  7. *
  8. * Project Name: < chess >
  9. *
  10. * Team Members:
  11. *    Name: Mark Luzarowski              Signature:________________________________
  12. *    Mail: [email protected]
  13. *    Name: Micheal Standiford           Signature:________________________________
  14. *    Mail: [email protected]
  15. *    Name: Mark Sears                   Signature:________________________________
  16. *    Mail: [email protected]
  17. *    Name: That other guy               Signature:________________________________
  18. *    Mail: [email protected]
  19. *
  20. * Team Positions:
  21. *
  22. *    Team/Doc Leader: < Mark Sears >           Signature: ______________________
  23. *
  24. *    Software Leader: < James Massengale >     Signature: ______________________
  25. *
  26. *    Interface Leader: < Micheal Standiford >  Signature: ______________________
  27. *
  28. *    Peripheral Leader: < Mark Luzarowski >    Signature: ______________________
  29. *
  30. *
  31. * Academic Honesty Statement:  In signing above, we hereby certify that we
  32. * are the individuals who created this HC(S)12 source file and that we have
  33. * not copied the work of any other student (past or present) while completing
  34. * it. We understand that if we fail to honor this agreement, we will receive
  35. * a grade of ZERO and be subject to possible disciplinary action.
  36. *
  37. **********************************************************************************
  38. * Project Purpose
  39. **********************************************************************************
  40. * The objective of this Mini-Project is to create a game board. The board itself will
  41. * function as a "learning board" of sorts that will allow players not familiar with
  42. * the game to play it easily.
  43. *
  44. **********************************************************************************
  45. * Project-specific Success Criteria
  46. **********************************************************************************
  47. * 1. Piece tracking & movement analysis
  48. *
  49. * 2. Time & turn tracking
  50. *
  51. **********************************************************************************
  52. * Change Log
  53. **********************************************************************************
  54. *  Date code started: November 13, 2012
  55. *
  56. *  Update history:
  57. *
  58. *  Date: November 13, 2012
  59. *  Name: Mark Luzarowski
  60. *  Update: Added generalized functions for LCD communications.
  61. *          Added initializations skeleton.
  62. * -------------------------------------------------------------------------------
  63. */
  64. #include <hidef.h>       // common defines and macros
  65. #include "derivative.h"  // derivative-specific definitions
  66. #include <mc9s12c32.h>   // 9S12 register & port defines
  67.  
  68. /* -------------------------------------------------------------------------------
  69. *  Date: December 2nd
  70. *  Name: Mark L
  71. *  Update: Added these functions
  72. */
  73. //Dictionary defines
  74. #define LCD_RS PTT_PTT2
  75. #define LCD_CLK PTT_PTT4
  76. #define RPG_A PTT_PTT5
  77. #define RPG_B PTT_PTT6
  78. #define RPG_BTN PTT_PTT7
  79. #define enable PTAD_PTAD7
  80. #define enable2 PTAD_PTAD6
  81. #define output PTM_PTM3
  82. #define output2 PTM_PTM2
  83. #define output3 PTM_PTM1
  84. #define output4 PTM_PTM0
  85. #define output5 PTT_PTT3
  86.  
  87. /*  Date: December 3rd
  88. *  Name: James Massengale
  89. *  Update: Added these functions
  90. */
  91. void currpccnt(void); //counts pieces on the board
  92. void pickedup(int row, int col, int piece); //calls the correct ledmatrix functions
  93. void pawnled(int row,int col,int team,int mode);
  94. void bishopled(int row,int col,int team);
  95. void rookled(int row,int col, int team);
  96. void knightled(int row,int col,int team);
  97. int bcheck(int row,int col); //checks if area is on board (>0 <8)
  98. void ledcopy(int mode); //matrix compare clear and copy functions
  99. void queenled(int row,int col,int team);
  100. int ischeck(int team); //check if team is in check
  101. int blackcheckflag = 0;
  102. int whitecheckflag = 0;
  103. void kingledout(int row,int col,int team);
  104. void kingled(int row,int col,int team);
  105. void ledcopy(int mode);
  106. void checkmatrix(int team);
  107. void sevenchar(int mode);
  108. void outinputmsg(void);
  109. void sci_outline(int mode);
  110. void sci_datafetch(int mode, int line);
  111. void outtype(int type);
  112. void outteam(int team);
  113. void nowpccnt(void);
  114. void hpccnt(void);
  115. void scanline(void);
  116.  
  117. //All funtions after main should be initialized here
  118. char inchar(void);
  119. void outchar(char);
  120. void LCD_Delay(int);
  121. void LCD_Cursor_Position(int);
  122. void LCD_Send_Byte(char, int);
  123. void LCD_Display(int Msgtype);
  124. void bco(char);
  125. void LCD_Timer(void);
  126. void LED_Out(void);
  127. void LED_Convert(void);
  128. void Chess_Locations(int);
  129. void LCD_Piece_Select(int);
  130. void Play_Sound(int);
  131. void HALL_Input(void);
  132. void LED_Test(void);
  133. void outchar(char ch);
  134.  
  135. /* -------------------------------------------------------------------------------
  136. *  Date: December 9th
  137. *  Name: Micheal
  138. *  Update: Added these functions for music
  139. */
  140. int noteflag = 0;
  141. int notedur = 25;  //120bpm 8th notes
  142. int notecnt = 0;
  143. int hello[8][8];
  144. int hello2[8][8];
  145. int duty1 = 0;
  146. int duty2 = 0;
  147. int period1 = 0;
  148. int period2 = 0;
  149. void Play_Music(void);
  150. void Song_Select(void);
  151. void Song_Select2(void);
  152.  
  153. /* -------------------------------------------------------------------------------
  154. *  Date: December 9th
  155. *  Name: James
  156. *  Update: Added these functions for gameplay
  157. */
  158. void setdown(int row, int col, int piece);
  159. int validateinput(char input);
  160. void validatepiece(void);
  161. void validatelocation(void);
  162. int hallvlatch(void);
  163. int hallvchess(void);
  164.  
  165.  
  166. int nummoves = 0;
  167. //void bco(void);
  168. void addchar(char a);
  169. char CR = 0x0D;//Return                  
  170. char LF = 0x0A; //line feed  
  171.  
  172.  
  173. int valcol;
  174. int valrow;
  175.  
  176. //int temppc = 0;
  177.  
  178. // James' variables
  179. //dec 1st
  180. int nowpc; //how many pieces are currently on the board
  181. int currpc; //how many pieces are currently in the game (not taken yet)
  182. int switchflag = 0; //flag that determines whether a switch is being currently made
  183. int ldata[8][8];
  184. int ltemp[8][8];
  185. int hlatch[8][8];
  186. //dec 3rd
  187. int sciset = 0;
  188. int boardset = 0;
  189. int sciline = 0;
  190. int boardline = 0;
  191. int scioff_flag = 0;
  192. //dec 5th
  193. int scicountflag = 0;
  194. int scicount = 0;
  195. void sciscan(void);
  196. //dec 6th
  197. int movenumber = 2;
  198. int whitescore = 0;
  199. int blackscore = 0;
  200. int scoreline = 0;
  201. int ledoutmode = 0;
  202. unsigned char MOVEOutData[8];
  203. int uprow = -1; //variables for keeping track of pieces
  204. int upcol = -1;
  205. int downrow;
  206. int downcol;
  207. int echoflag = 0;
  208. int inputonflag = 0;
  209. int outputonflag = 1;
  210. int inputflag = -1;
  211. int pickedupflag = 0;
  212. int hpc = 0;
  213. char inputchar = 0x00;
  214. int turnoverflag = 0;
  215. int turnactuallyoverflag = 0;
  216.  
  217.  
  218. //Global Declarations Begin Here (mark l.'s stuff)
  219. //------------------------------------
  220. //------------------------------------
  221. //SCI Variables
  222. int tin    = 0;  //SCI transmit display buffer IN pointer
  223. int tout   = 0;  //SCI transmit display buffer OUT pointer
  224. int tsize = 100; //SCI size of transmit buffer
  225. char tbuf[100];   //SCI transmit display buffer
  226. int tdata;  // how much data is in the buffer
  227. //TIM Variables
  228. int P1mins = 20; //White player minutes remaining on chess clock
  229. int P1secs = 0;  //White player seconds remaining on chess clock
  230. int P2mins = 20; //Black player minutes remaining on chess clock
  231. int P2secs = 0;  //Black player seconds remaining on chess clock
  232. int TimerCounter = 0; //Chess timer 10ms collector
  233. int timeExpired  = 0; //Timer has reached 00:00 for either player
  234. //LCD Variables
  235. char LineOffset = 0x00; //In use to calculate position of LCD write cursor
  236. //RPG Variables
  237. int RPGRightTurns = 0; //Number of pulses for right detected
  238. int RPGLeftTurns  = 0; //Number of pulses for left detected
  239. int RPGRight      = 0; //Movement to the right detected
  240. int RPGLeft       = 0; //Movement to the left detected
  241. int prevRPGbutton = 0; //Status of RPG button (previous)
  242. int newRPGbutton  = 0; //Status of RPG button (current)
  243. //Menu Variables
  244. int OutOfSplashScreen = 0; //Sees if the game has left the welcome splash
  245.                            //0 = Still on splash screen
  246.                            //1 = Left the splash screen
  247. int MenuPosition = 1; //Current Screen of the menu
  248.                       //0 = Game Start
  249.                       //1 = Edit Timer
  250. int TimerEditing = 0; //Are we editing the timer?
  251. //Gameplay (human) Variables
  252. int PlayerToggle = 0; //The current player toggle
  253.                       //0 = White
  254.                       //1 = Black
  255. int TimerRunning = 0; //Sees if the timer is up
  256. int TimerExpired = 0; //Sees if the timer has expired for either player
  257. int TimerRefresh = 0; //Refresh the timer on the LCD
  258. int GameRunning  = 0; //Sees if game is currently running
  259.  
  260. int HallMatrix[8][8]; //Magnet sensor input matrix
  261. int ChessMatrix[8][8];//Chess piece memory map
  262. unsigned char LEDOutData[8]; //Set of LED shiftout variables; one for each row of LEDs.
  263. int en1 = 0;
  264. int en2 = 0;
  265. int RefreshHall = 0;
  266.  
  267.  
  268. //------------------------------------
  269. //------------------------------------
  270.  
  271. //ASCII character constants
  272. char ASCII_CR = 0x0D; //Return
  273. char ASCII_LF = 0x0A; //Linefeed
  274.  
  275. //LCD specific constants
  276. char LCD_LINE1  = 0x80; //LCD line #1
  277. char LCD_LINE2  = 0xC0; //LCD line #2
  278. char LCD_ON     = 0x0F; //LCD turn on instruction
  279. char LCD_CLEAR  = 0x01; //LCD clear instruction
  280. char LCD_2LINE  = 0x38; //LCD use both lines instruction
  281. char LCD_CURSOR = 0xFE; //LCD cursor move instruction
  282.  
  283. /*********************************************************************************
  284. Initializations
  285. **********************************************************************************/
  286. void  initializations(void) {
  287.  
  288.  
  289.      // Set the PLL speed (bus clock = 24 MHz)
  290.      CLKSEL = CLKSEL & 0x80; // disengage PLL from system
  291.      PLLCTL = PLLCTL | 0x40; // turn on PLL
  292.      SYNR = 0x02;            // set PLL multiplier
  293.      REFDV = 0;              // set PLL divider
  294.      while (!(CRGFLG & 0x08)){  }
  295.      CLKSEL = CLKSEL | 0x80; // engage PLL
  296.  
  297.      // Disable watchdog timer (COPCTL register)
  298.      COPCTL = 0x40; // COP off; RTI and COP stopped in BDM-mode
  299.  
  300.      // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  301.      SCIBDH = 0x00; // set baud rate to 9600
  302.      SCIBDL = 0x9C; // 24,000,000 / 16 / 156 = 9600 (approx)  dec=26
  303.      SCICR1 = 0x00; // $9C = 156
  304.      SCICR2 = 0x0C; // initialize SCI for program-driven operation
  305.      SCICR2 = SCICR2 & 0x7F;
  306.      DDRB   = 0x10; // set PB4 for output mode
  307.      PORTB  = 0x10; // assert DTR pin on COM port
  308.  
  309.      //RTI Initialization (2.048ms)
  310.      CRGINT = CRGINT | 0x80; //RTI enabled
  311.      RTICTL = 0x1F; //
  312.  
  313.  
  314.      //SPI Initialization (6MHz)
  315.      SPICR1 = 0x52; //SPI control register #1
  316.                     //[Bit 7] SPIE  = 0 | SPI interrupts disabled
  317.                     //[Bit 6] SPE   = 1 | SPI enabled
  318.                     //[Bit 5] SPTIE = 0 | SPI transmit empty interrupt disabled
  319.                     //[Bit 4] MSTR  = 1 | Master mode
  320.                     //[Bit 3] CPOL  = 0 | Active low clock (SCK) - idles low
  321.                     //[Bit 2] CPHA  = 0 | Data sampled at odd edges of SCK
  322.                     //[Bit 1] SSOE  = 1 | Slave select output enable
  323.                     //[Bit 0] LSBFE = 0 | Data transferred most significant bit first
  324.      SPICR2 = 0x00;
  325.      SPIBR  = 0x08; //Baud rate register
  326.                     //[Bit 6-4] SPPRx = 000 | Baud rate pre-selection bits
  327.                     //[Bit 2-0] SPRx  = 001 | Baud rate selection bits
  328.                     //SPI rate = BusClock / [(SPPR + 1) * 2^(SPR + 1)]
  329.  
  330.      //TIM Initialization (10ms)
  331.      TSCR1_TEN = 1;  //TIM enable
  332.      TIOS_IOS7 = 1;  //Output compare on ch7
  333.      TSCR2_TCRE = 1; //TCNT reset when OC7 occurs
  334.      TSCR2_PR2 = 1;  //24MHz / 16 = 1.5MHz
  335.      TSCR2_PR1 = 0;
  336.      TSCR2_PR0 = 0;
  337.      TC7 = 15000;    //15000 / 1.5MHz = 10ms
  338.      TIE_C7I = 0;    //TIM interrupts disabled
  339.  
  340.      //PWM Initialization (1470.5Hz)
  341.      PWME = 0x01;               //PMW enable
  342.      PWMPOL = 0x01;     //PWM polarity select (active high)
  343.      PWMPER0 = 0xFF;    //period
  344.      PWMDTY0 = 0x00;    //duty
  345.      PWMPRCLK = 0x04;   //24MHz/16 = 1.5MHz
  346.      PWMCLK = 0x01;             //use clock SA
  347.      PWMSCLA = 2;               //1.5MHz/(2*2) = 375KHz
  348.      PWMSCLB = 0;               //375KHz/255 = 1470.5Hz final output
  349.      MODRR = 0x01;              //Reroute pwm to PT pins
  350.      PWMCAE = 0;
  351.  
  352.      //Port Initializations
  353.      ATDDIEN = 0xFF;
  354.      DDRT = 0x17; // 00010111 T5, T6, T7 are inputs from the shiny button
  355.      PTT = 0;
  356.      DDRAD = 0xE0;//DDRAD = 0xFF;
  357.  
  358.      //LCD Initializations
  359.      LCD_CLK = 1;
  360.      LCD_Send_Byte(LCD_ON, 1);
  361.      LCD_Send_Byte(LCD_2LINE, 1);
  362.      LCD_Send_Byte(LCD_CLEAR, 1);
  363.      LCD_Delay(1);
  364.      LCD_Display(0);
  365.  
  366.      enable = 0;
  367.      asm {
  368.          nop
  369.      }
  370. }
  371.  
  372. /*********************************************************************************
  373. Main
  374. **********************************************************************************/
  375. void main(void) {
  376.      DisableInterrupts;
  377.         initializations();
  378.         EnableInterrupts;
  379.  
  380.    
  381.    
  382.      for(;;) {
  383.           //RPG turned to the right
  384.           if (RPGRight == 1) {
  385.                RPGRight = 0;
  386.  
  387.                //Not on welcome splash
  388.                if (OutOfSplashScreen == 1) {
  389.                     //Not editing the game timer
  390.                     if (TimerEditing == 0) {
  391.                          MenuPosition++; //Next menu object
  392.  
  393.                          //Scroller cannot pass maximum menu objects
  394.                          if (MenuPosition > 3) {
  395.                               MenuPosition = 3;
  396.                          } else {
  397.                               //Display menu object on LCD
  398.                               LCD_Display(MenuPosition);
  399.                          }
  400.                     //Editing the game timer
  401.                     } else {
  402.                          P1mins++;
  403.                          P2mins++;
  404.  
  405.                          //Timer cannout go over 60 minutes
  406.                          if (P1mins > 60) {
  407.                               P1mins = 60;
  408.                               P2mins = 60;
  409.                          }
  410.  
  411.                          LCD_Timer();
  412.                     }
  413.                }
  414.           }
  415.    
  416.           //RPG turned to the left
  417.           if (RPGLeft == 1) {
  418.                RPGLeft = 0;
  419.  
  420.                //Not on welcome splash
  421.                if (OutOfSplashScreen == 1) {
  422.                     //Not editing the game timer
  423.                     if (TimerEditing == 0) {
  424.                          MenuPosition--; //Next menu object
  425.  
  426.                          //Scroller cannot pass maximum menu objects
  427.                          if (MenuPosition < 1) {
  428.                               MenuPosition = 1;
  429.                          } else {
  430.                               //Display menu object on LCD
  431.                               LCD_Display(MenuPosition);
  432.                          }
  433.                     //Editing game timer
  434.                     } else {
  435.                          P1mins--;
  436.                          P2mins--;
  437.  
  438.                          //Timer cannout go below 1 minute
  439.                          if (P1mins < 1) {
  440.                               P1mins = 1;
  441.                               P2mins = 1;
  442.                          }
  443.  
  444.                          LCD_Timer();
  445.                     }
  446.                }
  447.           }
  448.    
  449.           //RPG button pressed
  450.           if (newRPGbutton == 1) {
  451.                newRPGbutton = 0;
  452.  
  453.                //Check if on splash
  454.                if (OutOfSplashScreen == 0) {
  455.                     OutOfSplashScreen = 1;
  456.                 TIE_C7I = 1;
  457.                     PWMDTY0 = 0x00;
  458.                     Play_Music();
  459.                     LCD_Display(MenuPosition);
  460.                    
  461.                } else {
  462.                     //Start game, bring up timer
  463.                     if (MenuPosition == 1) {
  464.                          GameRunning = 1;
  465.  
  466.                          //If timer not running, start it and game initializations
  467.                          if (TimerRunning == 0) {
  468.                              
  469.                               TimerRunning = 1;
  470.                              
  471.                               Chess_Locations(0); //Initialize new game piece locations
  472.                               //enable = 1;
  473.                               //LED_Test();
  474.                               //enable = 0;
  475.                               LED_Convert(); //Set up LED backlighting
  476.                               enable = 1;
  477.                               LED_Out();
  478.                               enable = 0;
  479.                              
  480.                               SCICR2 = SCICR2 | 0x80;
  481.                               //LED_Convert(); //Set up LED backlighting
  482.                               //enable = 1;
  483.                               //LED_Out();
  484.                               //enable = 0;
  485.                               //enable2 = 1;
  486.                               //HALL_Input();
  487.                               //enable2 = 0;
  488.                          //If timer is running, toggle player
  489.                          } else if (TimerRunning == 1){
  490.                               if(turnactuallyoverflag == 1){
  491.                                   turnactuallyoverflag = 0;
  492.                                   turnoverflag = 0;
  493.                                 PlayerToggle = !PlayerToggle;
  494.                                 movenumber += 1;
  495.                                 inputflag = -1;
  496.                                 SCICR2 = SCICR2 | 0x80;
  497.                               }
  498.                          }
  499.                     //Editing the game timer
  500.                     } else if (MenuPosition == 2) {
  501.                          LCD_Timer();
  502.  
  503.                          //Already brought the screen up
  504.                          if (TimerEditing == 1) {
  505.                               LCD_Display(MenuPosition);
  506.                               TimerEditing = 0;
  507.                          } else {
  508.                               TimerEditing = 1;
  509.                          }
  510.  
  511.                     } else if (MenuPosition == 3) {
  512.                          //load game
  513.                     }
  514.                }
  515.           }
  516.        
  517.           //gameflow
  518.           if(GameRunning == 1 && TimerRunning == 1 && inputchar != 0){
  519.               if(validateinput(inputchar) == 0){
  520.                   uprow = -1;
  521.                   upcol = -1;
  522.               }
  523.              
  524.          
  525.              
  526.          
  527.               inputonflag = 0;
  528.               inputchar = 0x00;
  529.               if(turnactuallyoverflag == 0){
  530.                   SCICR2 = SCICR2 | 0x80;
  531.               }
  532.           }
  533.          
  534.             /*if(pickedupflag == 0){
  535.                 nowpccnt();
  536.                 hpccnt();
  537.                 currpccnt();
  538.                 if(nowpc > currpc){
  539.                     puteverythingback();
  540.                 } else {
  541.                    
  542.                     nowpclatch = nowpc;              
  543.                     cndiff = currpc - nowpc;
  544.                     if(cndiff == 1){
  545.                         hallvchess();
  546.                         if((ChessMatrix[uprow][upcol] / 10) == PlayerToggle){
  547.                             if(sameflag == 0){
  548.                                 pickedup(uprow,upcol,ChessMatrix[uprow][upcol]);
  549.                                 pickedupflag = 1;
  550.                             }
  551.                             LCD_Display(6);
  552.                         } else {
  553.                             puteverythingback();
  554.                         }
  555.                    
  556.                              
  557.                     }
  558.                 }
  559.            
  560.            
  561.             } else if (pickedupflag == 1){
  562.                 currpccnt();
  563.                 nowpccnt();
  564.                 hpccnt();
  565.                 if(nowpc < hpc - 1 || nowpc > hpc + 1){
  566.                     puteverythingback();
  567.                 } else {          
  568.                     if(nowpc = hpc + 1){ //setting it back down
  569.                         hallvlatch();
  570.                         if(ldata[nextrow][nextcol] && sameflag == 0){
  571.                             setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
  572.                         }
  573.                     } else if (nowpc = hpc - 1){ //picking another up
  574.                         hallvlatch();
  575.                         if(ldata[nextrow][nextcol] && sameflag == 0){
  576.                             setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
  577.                         }  
  578.                     }
  579.                 }      
  580.             }
  581.           }*/
  582.    
  583.        
  584.         if(pickedupflag == 1 && HallMatrix[uprow][upcol] == 0){
  585.             ledoutmode = 1;
  586.         } else {
  587.             ledoutmode = 0;
  588.         }
  589.        
  590.           //If timer is running and it needs a refresh, refresh it.
  591.           if ((TimerRunning == 1) && (TimerRefresh == 1)) {
  592.                TimerRefresh = 0;
  593.  
  594.                LCD_Timer();
  595.           }
  596.        
  597.         if (RefreshHall == 1) {
  598.             enable2 = 1;
  599.             HALL_Input();
  600.             enable2 = 0;
  601.             RefreshHall = 0;
  602.         }
  603.        
  604.    
  605.  
  606.     _FEED_COP(); /* feeds the dog */
  607.   } /* loop forever */
  608.   /* please make sure that you never leave main */
  609. }
  610.  
  611. /*********************************************************************************
  612. * Name:          Chess_Locations
  613. * Description:   Manages vitual piece location map.
  614. * [->] (global): ChessMatrix: Map of the board.
  615. *                      00 = empty space
  616. *                      X1 = pawn
  617. *                      X2 = rook
  618. *                      X3 = knight
  619. *                      X4 = bishop
  620. *                      X5 = Queen
  621. *                      X6 = King
  622. *                      1X = White piece
  623. *                      2X = Black piece
  624. *         (int): Mode: Mode of map.
  625. *                      0 = Initialization for new game.
  626. *                      1 = Standard
  627. * [<-]   (void): [void]
  628. *********************************************************************************/
  629. void Chess_Locations(int Mode) {
  630.  
  631.      if (Mode == 0) {
  632.           ChessMatrix[0][0] = 0;//2
  633.           ChessMatrix[0][1] = 0;//3
  634.           ChessMatrix[0][2] = 0;//4
  635.           ChessMatrix[0][3] = 5;//5
  636.           ChessMatrix[0][4] = 6;//6
  637.           ChessMatrix[0][5] = 4;//4
  638.           ChessMatrix[0][6] = 0;//3
  639.           ChessMatrix[0][7] = 0;//2
  640.           ChessMatrix[1][0] = 0;//1
  641.           ChessMatrix[1][1] = 0;//1
  642.           ChessMatrix[1][2] = 0;//1
  643.           ChessMatrix[1][3] = 0;//1
  644.           ChessMatrix[1][4] = 1;//1
  645.           ChessMatrix[1][5] = 1;//1
  646.           ChessMatrix[1][6] = 1;//1
  647.           ChessMatrix[1][7] = 0;//1
  648.           ChessMatrix[2][0] = 0;
  649.           ChessMatrix[2][1] = 0;
  650.           ChessMatrix[2][2] = 0;
  651.           ChessMatrix[2][3] = 0;
  652.           ChessMatrix[2][4] = 0;
  653.           ChessMatrix[2][5] = 0;
  654.           ChessMatrix[2][6] = 0;
  655.           ChessMatrix[2][7] = 0;
  656.           ChessMatrix[3][0] = 12;
  657.           ChessMatrix[3][1] = 0;
  658.           ChessMatrix[3][2] = 0;
  659.           ChessMatrix[3][3] = 0;
  660.           ChessMatrix[3][4] = 0;
  661.           ChessMatrix[3][5] = 0;
  662.           ChessMatrix[3][6] = 0;
  663.           ChessMatrix[3][7] = 0;
  664.           ChessMatrix[4][0] = 1;
  665.           ChessMatrix[4][1] = 0;
  666.           ChessMatrix[4][2] = 0;
  667.           ChessMatrix[4][3] = 0;
  668.           ChessMatrix[4][4] = 0;
  669.           ChessMatrix[4][5] = 0;
  670.           ChessMatrix[4][6] = 0;
  671.           ChessMatrix[4][7] = 0;
  672.           ChessMatrix[5][0] = 0;
  673.           ChessMatrix[5][1] = 0;
  674.           ChessMatrix[5][2] = 0;
  675.           ChessMatrix[5][3] = 0;
  676.           ChessMatrix[5][4] = 0;
  677.           ChessMatrix[5][5] = 0;
  678.           ChessMatrix[5][6] = 0;
  679.           ChessMatrix[5][7] = 0;
  680.           ChessMatrix[6][0] = 0;//11
  681.           ChessMatrix[6][1] = 11;//11
  682.           ChessMatrix[6][2] = 11;//11
  683.           ChessMatrix[6][3] = 11;//11
  684.           ChessMatrix[6][4] = 11;//11
  685.           ChessMatrix[6][5] = 0;//11
  686.           ChessMatrix[6][6] = 0;//11
  687.           ChessMatrix[6][7] = 0;//11
  688.           ChessMatrix[7][0] = 0;//12
  689.           ChessMatrix[7][1] = 13;//13
  690.           ChessMatrix[7][2] = 14;//14
  691.           ChessMatrix[7][3] = 12;//16
  692.           ChessMatrix[7][4] = 0;//15
  693.           ChessMatrix[7][5] = 0;//14
  694.           ChessMatrix[7][6] = 0;//13
  695.           ChessMatrix[7][7] = 0;//12
  696.      }
  697.  
  698.      if (Mode == 1) { //Update locations map
  699.  
  700.      }
  701.      return;
  702. }
  703. /***********************************************************************
  704. INPUT ANALYSIS
  705. ***********************************************************************/
  706. int validateinput(char input){
  707.     if(inputflag == 1){
  708.         switch(input){
  709.             case('A'): uprow = 0;
  710.                      break;
  711.                      
  712.             case('B'): uprow = 1;
  713.                      break;
  714.                      
  715.             case('C'): uprow = 2;
  716.                      break;
  717.                      
  718.             case('D'): uprow = 3;
  719.                      break;
  720.                      
  721.             case('E'): uprow = 4;
  722.                      break;
  723.                      
  724.             case('F'): uprow = 5;
  725.                      break;
  726.                      
  727.             case('G'): uprow = 6;
  728.                      break;
  729.                      
  730.             case('H'): uprow = 7;
  731.                      break;
  732.                      
  733.             case('X'): inputflag = 0;
  734.                      return 0;
  735.                      break;
  736.                      
  737.             default:   inputflag = 6;
  738.                      return 0;
  739.         }
  740.         inputflag = 2;
  741.     } else if(inputflag == 4){
  742.         switch(input){
  743.             case('A'): downrow = 0;
  744.                      break;
  745.                      
  746.             case('B'): downrow = 1;
  747.                      break;
  748.                      
  749.             case('C'): downrow = 2;
  750.                      break;
  751.                      
  752.             case('D'): downrow = 3;
  753.                      break;
  754.                      
  755.             case('E'): downrow = 4;
  756.                      break;
  757.                      
  758.             case('F'): downrow = 5;
  759.                      break;
  760.                      
  761.             case('G'): downrow = 6;
  762.                      break;
  763.                      
  764.             case('H'): downrow = 7;
  765.                      break;
  766.                      
  767.             case('X'): inputflag = 0;
  768.                      return 0;
  769.                      break;
  770.                      
  771.             default:   inputflag = 7;
  772.                      return 0;
  773.         }
  774.         inputflag = 5;
  775.     } else if(inputflag == 2){
  776.         switch(input){
  777.             case('1'): upcol = 0;
  778.                      break;
  779.                      
  780.             case('2'): upcol = 1;
  781.                      break;
  782.                      
  783.             case('3'): upcol = 2;
  784.                      break;
  785.                      
  786.             case('4'): upcol = 3;
  787.                      break;
  788.                      
  789.             case('5'): upcol = 4;
  790.                      break;
  791.                      
  792.             case('6'): upcol = 5;
  793.                      break;
  794.                      
  795.             case('7'): upcol = 6;
  796.                      break;
  797.                      
  798.             case('8'): upcol = 7;
  799.                      break;
  800.                      
  801.             case('X'): inputflag = 0;
  802.                      return 0;
  803.                      break;
  804.                      
  805.             default:   inputflag = 6;
  806.                      return 0;
  807.         }
  808.         validatepiece();
  809.     } else if(inputflag == 5){
  810.         switch(input){
  811.             case('1'): downcol = 0;
  812.                      break;
  813.                      
  814.             case('2'): downcol = 1;
  815.                      break;
  816.                      
  817.             case('3'): downcol = 2;
  818.                      break;
  819.                      
  820.             case('4'): downcol = 3;
  821.                      break;
  822.                      
  823.             case('5'): downcol = 4;
  824.                      break;
  825.                      
  826.             case('6'): downcol = 5;
  827.                      break;
  828.                      
  829.             case('7'): downcol = 6;
  830.                      break;
  831.                      
  832.             case('8'): downcol = 7;
  833.                      break;
  834.                      
  835.             case('X'): inputflag = 0;
  836.                      return 0;
  837.                      break;
  838.                      
  839.             default:   inputflag = 7;
  840.                      return 0;
  841.         }
  842.         validatelocation();
  843.     } else {
  844.         return 0;
  845.     }
  846.    
  847.     return 1;
  848.    
  849. }
  850.  
  851.  
  852. void validatepiece(void){
  853.     int temp = ChessMatrix[uprow][upcol];
  854.    
  855.     if(temp != 0 && (temp / 10) == PlayerToggle){  
  856.         pickedup(uprow,upcol,ChessMatrix[uprow][upcol]);
  857.         pickedupflag = 1;
  858.     } else if((temp / 10) == 1-PlayerToggle) {
  859.            inputflag = 14;
  860.     } else {
  861.         inputflag = 9;
  862.     }
  863.    
  864. }
  865.  
  866. void validatelocation(void){
  867.     int temp = ldata[downrow][downcol];
  868.    
  869.     if(temp == 1){
  870.         //temppc = ChessMatrix[downrow][downcol];
  871.         setdown(downrow,downcol,ChessMatrix[uprow][upcol]);
  872.         pickedupflag = 0;
  873.     } else {
  874.         inputflag = 8;
  875.     }
  876. }
  877.  
  878.  
  879. /***********************************************************************
  880. BOARD ANALYSIS
  881. ***********************************************************************/
  882. int hallvchess(void){
  883.     int i;
  884.     int j;
  885.     int differ = 0;
  886.     int rtemp;
  887.     int ctemp;
  888.    
  889.     for (i = 0; i < 8; i++) {  //Chess row loop
  890.         for (j = 0; j < 8; j++) { //Chess column loop
  891.            
  892.             if(ChessMatrix[i][j] > 0){
  893.                 hlatch[i][j] = 1;
  894.             } else {
  895.                 hlatch[i][j] = 0;
  896.             }
  897.            
  898.             if (hlatch[i][j] == 1 && HallMatrix[i][j] == 0) { //both aren't the same
  899.                 differ += 1;
  900.                 rtemp = i;
  901.                 ctemp = j;
  902.             } else if(hlatch[i][j] != HallMatrix[i][j]){
  903.                 differ += 1;
  904.             }
  905.            
  906.         }      
  907.     }
  908.    
  909.     uprow = rtemp;
  910.     upcol = ctemp;
  911.     hlatch[uprow][upcol] = 0;
  912.    
  913.     return differ;
  914. }
  915.  
  916.  
  917. int hallvlatch(){
  918.     int i;
  919.     int j;
  920.     int differ = 0;
  921.     int rtemp;
  922.     int ctemp;
  923.    
  924.     for (i = 0; i < 8; i++) {  //Chess row loop
  925.         for (j = 0; j < 8; j++) { //Chess column loop
  926.            
  927.             if (hlatch[i][j] != HallMatrix[i][j]) { //both aren't the same
  928.                 differ += 1;
  929.                 rtemp = i;
  930.                 ctemp = j;
  931.             }
  932.         }      
  933.     }
  934.    
  935.     downrow = rtemp;
  936.     downcol = ctemp;
  937.     hlatch[uprow][upcol] = 0;
  938.  
  939.     return differ;
  940. }
  941.  
  942.  
  943. //updates currpc
  944. void currpccnt(void){
  945.     int pcnum = 0;
  946.     int i;
  947.     int j;
  948.    
  949.     for (i = 0; i < 8; i++) {  //Chess column loop
  950.         for (j = 0; j < 8; j++) { //Chess row loop
  951.             if (ChessMatrix[i][j] != 0) { //if piece exists on tile
  952.                 pcnum += 1;
  953.             }
  954.         }      
  955.     }
  956.    
  957. currpc = pcnum;  
  958.    
  959. }
  960. //updates nowpc
  961. void nowpccnt(void){
  962.     int pcnum = 0;
  963.     int i;
  964.     int j;
  965.    
  966.     for (i = 0; i < 8; i++) {  //Hall column loop
  967.         for (j = 0; j < 8; j++) { //Hall row loop
  968.             if (HallMatrix[i][j] != 0) { //if piece exists on tile
  969.                 pcnum += 1;
  970.             }
  971.         }      
  972.     }
  973.    
  974. nowpc = pcnum;  
  975.    
  976. }
  977.  
  978. void hpccnt(void){
  979.     int pcnum = 0;
  980.     int i;
  981.     int j;
  982.    
  983.     for (i = 0; i < 8; i++) {  //Hall column loop
  984.         for (j = 0; j < 8; j++) { //Hall row loop
  985.             if (hlatch[i][j] != 0) { //if piece exists on tile
  986.                 pcnum += 1;
  987.             }
  988.         }      
  989.     }
  990.    
  991. hpc = pcnum;  
  992.    
  993. }
  994.  
  995. //counts how many moves avail
  996. void movecnt(void){
  997.     int pcnum = 0;
  998.     int i;
  999.     int j;
  1000.    
  1001.     for (i = 0; i < 8; i++) {  //Hall column loop
  1002.         for (j = 0; j < 8; j++) { //Hall row loop
  1003.             if (ldata[i][j] != 0) { //if piece exists on tile
  1004.                 pcnum += 1;
  1005.             }
  1006.         }      
  1007.     }
  1008.    
  1009. nummoves = pcnum;  
  1010.    
  1011. }
  1012.  
  1013.  
  1014. void puteverythingback(void){
  1015.    
  1016.     int i;
  1017.     int j;
  1018.     int differ = 0;
  1019.     int temp = 0;
  1020.     int check = 1;
  1021.     ledoutmode = 0;
  1022.    
  1023.     while(check == 1){
  1024.         differ = 0;
  1025.         for (i = 0; i < 8; i++) {  //Chess row loop
  1026.             for (j = 0; j < 8; j++) { //Chess column loop
  1027.            
  1028.                 if(ChessMatrix[i][j] > 0){
  1029.                     temp = 1;
  1030.                 } else {
  1031.                     temp = 0;
  1032.                 }
  1033.                 if (temp != HallMatrix[i][j]) { //both aren't the same
  1034.                     differ += 1;
  1035.                 }
  1036.             }      
  1037.         }
  1038.         if(differ == 0){
  1039.             check = 0;
  1040.         } else {
  1041.             LCD_Display(6);
  1042.         }
  1043.        
  1044.    
  1045.     }
  1046.  
  1047.     pickedupflag = 0;
  1048. }
  1049.  
  1050.              
  1051.  
  1052. /*********************************************************************************
  1053. * Name:        pickedup
  1054. * Description: What is done when one piece is currently picked up
  1055. * [->]  (int i, int j): the coordinates of the piece being picked up
  1056. * [<-] (void): [pcnum]
  1057. *********************************************************************************/
  1058.  
  1059. void pickedup(int row, int col, int piece){
  1060.     int type = piece % 10;  // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
  1061.     int team = piece / 10; //0=white 1=black
  1062.     ledcopy(6);
  1063.     if(type == 1){
  1064.       pawnled(row,col,team,0);
  1065.     }
  1066.     else if(type == 2){
  1067.       rookled(row,col,team);
  1068.     }
  1069.     else if(type == 3){
  1070.       knightled(row,col,team);
  1071.     }
  1072.     else if(type == 4){
  1073.       bishopled(row,col,team);
  1074.     }
  1075.     else if(type == 5){
  1076.       queenled(row,col,team);
  1077.     }
  1078.     else if(type == 6){  
  1079.       kingled(row,col,team);
  1080.     }
  1081.     movecnt();
  1082.     //ledcopy(5);
  1083.     //ledoutmode = 1;
  1084.     if(nummoves == 0){
  1085.        inputflag = 10;
  1086.        pickedupflag = 0;
  1087.     } else {
  1088.           inputflag += 1;
  1089.     }
  1090.    
  1091. }
  1092.  
  1093.  
  1094. void setdown(int row, int col, int piece){
  1095.     int epiece = ChessMatrix[row][col];
  1096.     int type = epiece % 10;  // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
  1097.     int team = piece / 10; //0=white 1=black
  1098.     int score;
  1099.    
  1100.    
  1101.     //if(ischeck(PlayerToggle)){
  1102.     //      inputflag = 15;
  1103.     //} else {
  1104.        
  1105.        
  1106.  
  1107.    
  1108.    
  1109.    
  1110.         if(type == 1){
  1111.             score = 1;
  1112.             }
  1113.             else if(type == 2){
  1114.             score = 5;
  1115.             }
  1116.             else if(type == 3){
  1117.             score = 3;
  1118.             }
  1119.             else if(type == 4){
  1120.             score = 3;
  1121.             }
  1122.             else if(type == 5){
  1123.             score = 9;
  1124.             } else {
  1125.             score = 0;
  1126.             }
  1127.             if(PlayerToggle == 0){
  1128.                 whitescore += score;
  1129.             } else if(PlayerToggle == 1){
  1130.                 blackscore += score;
  1131.             }
  1132.    
  1133.             ChessMatrix[downrow][downcol] = piece;
  1134.             ChessMatrix[uprow][upcol] = 0;
  1135.             ledoutmode = 0;
  1136.             turnactuallyoverflag = 1;
  1137.             inputflag = 11;
  1138.             //movecnt();
  1139.            
  1140.     //}
  1141.     SCICR2 = SCICR2 | 0x80;
  1142. }
  1143.  
  1144.  
  1145.  
  1146. /***********************************************************************
  1147. LED/ VALID MOVE MATRIX GENERATION
  1148. ***********************************************************************/
  1149. //mode 0 regular //mode 1 only piece captures
  1150. void pawnled(int row,int col,int team,int mode){
  1151.     int dir;
  1152.     if (team == 1){
  1153.         dir = -1;
  1154.     } else {
  1155.         dir = 1;
  1156.     }
  1157.    
  1158.     if(mode == 0){
  1159.       if(ChessMatrix[row+dir][col] == 0){ //check 1 space
  1160.           ldata[row+dir][col] = 1;
  1161.           if(team == 1){  //check 2 spaces
  1162.               if(row == 6 && ChessMatrix[row+dir+dir][col] == 0){
  1163.               ldata[row+dir+dir][col] = 1;
  1164.               }
  1165.           } else {
  1166.              if(row == 1 && ChessMatrix[row+dir+dir][col] == 0){
  1167.               ldata[row+dir+dir][col] = 1;
  1168.              }
  1169.           }
  1170.       }
  1171.     }
  1172.    
  1173.     if(col != 7 && ChessMatrix[row+dir][col+1] != 0 && (ChessMatrix[row+dir][col+1] / 10) != team){ //check right capture
  1174.         ldata[row+dir][col+1] = 1;  
  1175.    
  1176.     }
  1177.     if(col != 0 && ChessMatrix[row+dir][col-1] != 0 && (ChessMatrix[row+dir][col-1] / 10) != team){ //check left capture
  1178.         ldata[row+dir][col-1] = 1;  
  1179.    
  1180.     }
  1181.    
  1182.    //pick up piece message
  1183. }
  1184.  
  1185. void rookled(int row,int col, int team){
  1186.   int cscan = col;
  1187.   int rscan = row;
  1188.   int dir = 1;
  1189.  
  1190.   while(rscan < 7){ //scan up positive row
  1191.     if(ChessMatrix[rscan+dir][col] == 0){
  1192.         ldata[rscan+dir][col] = 1;
  1193.         rscan += dir;
  1194.     } else if((ChessMatrix[rscan+dir][col] / 10) != team){
  1195.         ldata[rscan+dir][col] = 1;
  1196.         rscan = 8;
  1197.     } else {
  1198.       rscan = 8;
  1199.     }
  1200.   }
  1201.  
  1202.   dir = -1; //reset scan parameters
  1203.   rscan = row;
  1204.  
  1205.   while(rscan > 0){ //scan up negative row
  1206.     if(ChessMatrix[rscan+dir][col] == 0){
  1207.         ldata[rscan+dir][col] = 1;
  1208.         rscan += dir;
  1209.     } else if((ChessMatrix[rscan+dir][col] / 10) != team){
  1210.         ldata[rscan+dir][col] = 1;
  1211.         rscan = -1;
  1212.     } else {
  1213.       rscan = -1;
  1214.     }
  1215.   }
  1216.  
  1217.   dir = 1;
  1218.  
  1219.  
  1220.   while(cscan < 7){ //scan up positive column
  1221.     if(ChessMatrix[row][cscan+dir] == 0){
  1222.         ldata[row][cscan+dir] = 1;
  1223.         cscan += dir;
  1224.     } else if((ChessMatrix[row][cscan+dir] / 10) != team){
  1225.         ldata[row][cscan+dir] = 1;
  1226.         cscan = 8;
  1227.     } else {
  1228.       cscan = 8;
  1229.     }
  1230.   }
  1231.  
  1232.   dir = -1; //reset scan parameters
  1233.   cscan = col;
  1234.  
  1235.   while(cscan > 0){ //
  1236.     if(ChessMatrix[row][cscan+dir] == 0){ //scan up negative column
  1237.         ldata[row][cscan+dir] = 1;
  1238.         cscan += dir;
  1239.     } else if((ChessMatrix[row][cscan+dir] / 10) != team){
  1240.         ldata[row][cscan+dir] = 1;
  1241.         cscan = -1;
  1242.     } else {
  1243.      cscan = -1;
  1244.     }
  1245.   }
  1246. }
  1247.  
  1248. void bishopled(int row,int col,int team){
  1249.   int cscan = col;
  1250.   int rscan = row;
  1251.   int cdir = 1;
  1252.   int rdir = 1;
  1253.  
  1254.   //+ + dir
  1255.   while(rscan < 7 && cscan < 7){ //scan up positive row
  1256.     if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1257.         ldata[rscan+rdir][cscan+cdir] = 1;
  1258.         rscan += rdir;
  1259.         cscan += cdir;
  1260.     } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1261.         ldata[rscan+rdir][cscan+cdir] = 1;
  1262.         rscan = 8;
  1263.         cscan = 8;
  1264.     } else {
  1265.       rscan = 8;
  1266.       cscan = 8;
  1267.     }
  1268.   }
  1269.  
  1270.   rscan = row;
  1271.   cscan = col;
  1272.   cdir = -1;
  1273.  
  1274.   //- + dir
  1275.   while(rscan < 7 && cscan > 0){ //scan up positive row
  1276.     if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1277.         ldata[rscan+rdir][cscan+cdir] = 1;
  1278.         rscan += rdir;
  1279.         cscan += cdir;
  1280.     } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1281.         ldata[rscan+rdir][cscan+cdir] = 1;
  1282.         rscan = 8;
  1283.         cscan = -1;
  1284.     } else {
  1285.       rscan = 8;
  1286.       cscan = -1;
  1287.     }
  1288.   }
  1289.  
  1290.   rscan = row;
  1291.   cscan = col;
  1292.   cdir = 1;
  1293.   rdir = -1;
  1294.  
  1295.   //+ - dir
  1296.   while(rscan > 0 && cscan < 7){ //scan up positive row
  1297.     if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1298.         ldata[rscan+rdir][cscan+cdir] = 1;
  1299.         rscan += rdir;
  1300.         cscan += cdir;
  1301.     } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1302.         ldata[rscan+rdir][cscan+cdir] = 1;
  1303.         rscan = -1;
  1304.         cscan = 8;
  1305.     } else {
  1306.       rscan = -1;
  1307.       cscan = 8;
  1308.     }
  1309.   }
  1310.  
  1311.   rscan = row;
  1312.   cscan = col;
  1313.   cdir = -1;
  1314.  
  1315.  
  1316.   //- - dir
  1317.   while(rscan > 0 && cscan > 0){ //scan up positive row
  1318.     if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1319.         ldata[rscan+rdir][cscan+cdir] = 1;
  1320.         rscan += rdir;
  1321.         cscan += cdir;
  1322.     } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1323.         ldata[rscan+rdir][cscan+cdir] = 1;
  1324.         rscan = -1;
  1325.         cscan = -1;
  1326.     } else {
  1327.       rscan = -1;
  1328.       cscan = -1;
  1329.     }
  1330.   }  
  1331.  
  1332. }
  1333.  
  1334. //0 good -1 not
  1335. int bcheck(int row,int col){
  1336.   if(row > 7){
  1337.     return -1;
  1338.   } else if(col > 7){
  1339.     return -1;
  1340.   } else if(row < 0){
  1341.     return -1;
  1342.   } else if(col < 0){
  1343.     return -1;
  1344.   } else {
  1345.     return 0;
  1346.   }
  1347. }
  1348.  
  1349. void knightled(int row,int col,int team){
  1350.   int cchk = col;
  1351.   int rchk = row;
  1352.  
  1353.   rchk += 2;
  1354.   cchk += 1;
  1355.  
  1356.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1357.     ldata[rchk][cchk] = 1;
  1358.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1359.     ldata[rchk][cchk] = 1;
  1360.   }
  1361.  
  1362.   rchk = row;
  1363.   cchk = col;
  1364.  
  1365.   rchk += 1;
  1366.   cchk += 2;
  1367.  
  1368.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1369.     ldata[rchk][cchk] = 1;
  1370.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1371.     ldata[rchk][cchk] = 1;
  1372.   }
  1373.  
  1374.   rchk = row;
  1375.   cchk = col;
  1376.  
  1377.   rchk += -1;
  1378.   cchk += 2;
  1379.  
  1380.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1381.     ldata[rchk][cchk] = 1;
  1382.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1383.     ldata[rchk][cchk] = 1;
  1384.   }
  1385.  
  1386.   rchk = row;
  1387.   cchk = col;
  1388.  
  1389.   rchk += 1;
  1390.   cchk += -2;
  1391.  
  1392.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1393.     ldata[rchk][cchk] = 1;
  1394.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1395.     ldata[rchk][cchk] = 1;
  1396.   }
  1397.  
  1398.   rchk = row;
  1399.   cchk = col;
  1400.  
  1401.   rchk += 2;
  1402.   cchk += -1;
  1403.  
  1404.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1405.     ldata[rchk][cchk] = 1;
  1406.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1407.     ldata[rchk][cchk] = 1;
  1408.   }
  1409.  
  1410.   rchk = row;
  1411.   cchk = col;
  1412.  
  1413.   rchk += -2;
  1414.   cchk += 1;
  1415.  
  1416.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1417.     ldata[rchk][cchk] = 1;
  1418.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1419.     ldata[rchk][cchk] = 1;
  1420.   }
  1421.  
  1422.   rchk = row;
  1423.   cchk = col;
  1424.  
  1425.   rchk += -1;
  1426.   cchk += -2;
  1427.  
  1428.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1429.     ldata[rchk][cchk] = 1;
  1430.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1431.     ldata[rchk][cchk] = 1;
  1432.   }    
  1433.  
  1434.   rchk = row;
  1435.   cchk = col;
  1436.  
  1437.   rchk += -2;
  1438.   cchk += 1;
  1439.  
  1440.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1441.     ldata[rchk][cchk] = 1;
  1442.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1443.     ldata[rchk][cchk] = 1;
  1444.   }
  1445.  
  1446. }
  1447.  
  1448. void queenled(int row,int col,int team){
  1449.   bishopled(row,col,team);
  1450.   rookled(row,col,team);
  1451. }
  1452.  
  1453. //mode 1 = data to temp
  1454. //mode 2 = temp back to data
  1455. //cleans the one it moved from
  1456. //mode 3 = switch data
  1457. //mode 4 = switch and filter
  1458. //mode 5 (debugging) ldata to chessmatrix
  1459. //mode 6 clear ldata
  1460. void ledcopy(int mode){
  1461.  
  1462.   int r;
  1463.   int c;
  1464.   int temp;
  1465.  
  1466.   for (r = 0; r < 8; r++) {
  1467.     for (c = 0; c < 8; c++) {
  1468.        if(mode == 1){
  1469.         ltemp[r][c] = ldata[r][c];
  1470.         ldata[r][c] = 0;
  1471.        } else if(mode == 2) {
  1472.         ldata[r][c] = ltemp[r][c];
  1473.         ltemp[r][c] = 0;
  1474.        } else if(mode == 3) {
  1475.         temp = ltemp[r][c];
  1476.         ltemp[r][c] = ldata[r][c];
  1477.         ldata[r][c] = temp;
  1478.        } else if(mode == 5) {
  1479.         if(ldata[r][c] == 1){
  1480.             ChessMatrix[r][c] = 27;
  1481.         }
  1482.        } else if(mode == 6) {
  1483.          ldata[r][c] = 0;
  1484.          ltemp[r][c] = 0;
  1485.        
  1486.        } else {
  1487.         temp = ltemp[r][c];
  1488.         ltemp[r][c] = ldata[r][c];
  1489.         ldata[r][c] = temp;
  1490.         if((ltemp[r][c] + ldata[r][c]) == 2){
  1491.           ldata[r][c] = 0;
  1492.         }
  1493.        }
  1494.     }
  1495.   }
  1496. }
  1497.  
  1498. void kingledout(int row,int col,int team){
  1499.   int rchk = row;
  1500.   int cchk = col;
  1501.  
  1502.   rchk += 1;
  1503.  
  1504.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1505.     ldata[rchk][cchk] = 1;
  1506.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1507.     ldata[rchk][cchk] = 1;
  1508.   }
  1509.  
  1510.   rchk = row;
  1511.   rchk += -1;
  1512.  
  1513.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1514.     ldata[rchk][cchk] = 1;
  1515.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1516.     ldata[rchk][cchk] = 1;
  1517.   }
  1518.  
  1519.   rchk = row;
  1520.   cchk += 1;
  1521.  
  1522.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1523.     ldata[rchk][cchk] = 1;
  1524.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1525.     ldata[rchk][cchk] = 1;
  1526.   }
  1527.  
  1528.   cchk = col;
  1529.   cchk += -1;
  1530.  
  1531.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1532.     ldata[rchk][cchk] = 1;
  1533.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1534.     ldata[rchk][cchk] = 1;
  1535.   }
  1536.  
  1537.   cchk = col;
  1538.   cchk += 1;
  1539.   rchk += 1;
  1540.  
  1541.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1542.     ldata[rchk][cchk] = 1;
  1543.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1544.     ldata[rchk][cchk] = 1;
  1545.   }
  1546.  
  1547.   cchk = col;
  1548.   rchk = row;
  1549.   cchk += -1;
  1550.   rchk += 1;
  1551.  
  1552.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1553.     ldata[rchk][cchk] = 1;
  1554.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1555.     ldata[rchk][cchk] = 1;
  1556.   }
  1557.  
  1558.   cchk = col;
  1559.   rchk = row;
  1560.   cchk += 1;
  1561.   rchk += -1;
  1562.  
  1563.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1564.     ldata[rchk][cchk] = 1;
  1565.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1566.     ldata[rchk][cchk] = 1;
  1567.   }
  1568.  
  1569.   cchk = col;
  1570.   rchk = row;
  1571.   cchk += -1;
  1572.   rchk += -1;
  1573.  
  1574.   if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1575.     ldata[rchk][cchk] = 1;
  1576.   } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1577.     ldata[rchk][cchk] = 1;
  1578.   }
  1579. }
  1580.  
  1581.  
  1582. void checkmatrix(int team){
  1583.  
  1584.   int r;
  1585.   int c;
  1586.  
  1587.   for (r = 0; r < 8; r++) {
  1588.     for (c = 0; c < 8; c++) {
  1589.       if((ChessMatrix[r][c] / 10) != team){
  1590.         if((ChessMatrix[r][c] % 10) == 1){
  1591.           pawnled(r,c,1-team,1);
  1592.         }
  1593.         else if((ChessMatrix[r][c] % 10) == 2){
  1594.           rookled(r,c,1-team);
  1595.         }
  1596.         else if((ChessMatrix[r][c] % 10) == 3){
  1597.           knightled(r,c,1-team);
  1598.         }
  1599.         else if((ChessMatrix[r][c] % 10) == 4){
  1600.           bishopled(r,c,1-team);
  1601.         }
  1602.         else if((ChessMatrix[r][c] % 10) == 5){
  1603.           queenled(r,c,1-team);
  1604.         }
  1605.         else if((ChessMatrix[r][c] % 10) == 6){  
  1606.           kingledout(r,c,1-team);
  1607.         }
  1608.       }
  1609.     }
  1610.   }
  1611. }
  1612.  
  1613.  
  1614. void kingled(int row,int col,int team){
  1615.   kingledout(row,col,team); //show possible moves without checking for check
  1616.   ledcopy(1); //store in temp matrix
  1617.   checkmatrix(team); //create a matrix of enemy moves
  1618.   ledcopy(4); //filter the temp matrix with enemy move matrix  
  1619. }
  1620.  
  1621. int ischeck(int team){
  1622.     int piece;
  1623.     int r;
  1624.     int c;
  1625.     int tempr;
  1626.     int tempc;
  1627.    
  1628.     if(team == 0){
  1629.         piece = 6;
  1630.     } else if(team == 1){
  1631.         piece = 16;
  1632.     }
  1633.  
  1634.     for (r = 0; r < 8; r++) {
  1635.       for (c = 0; c < 8; c++) {
  1636.         if(ChessMatrix[r][c] == piece){
  1637.             tempr = r;
  1638.             tempc = c;
  1639.         }
  1640.       }
  1641.     }
  1642.     ledcopy(6);
  1643.     checkmatrix(team);
  1644.     if(ldata[tempr][tempc] == 1){
  1645.       ledcopy(6);
  1646.       return 1;
  1647.     } else {
  1648.       ledcopy(6);
  1649.       return 0;
  1650.     }
  1651.    
  1652.  
  1653. }
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662. /*********************************************************************************
  1663. * SCI Functions
  1664. **********************************************************************************/
  1665.  
  1666.  
  1667. //0='*', 1=' '
  1668. void sevenchar(int mode){
  1669.   int i = 0;
  1670.   char a;
  1671.   if(mode == 1){
  1672.     a = '*';
  1673.   } else {
  1674.     a = ' ';
  1675.   }
  1676.   for (i = 0; i < 7; i++) {
  1677.     addchar(a);  
  1678.   }
  1679.    
  1680. }
  1681.  
  1682. //2=solid line, 1=whiteblackwhiteblack, 0=blackwhiteblackwhite
  1683. void sci_outline(int mode){
  1684.   int i = 0;
  1685.   int j = 0;
  1686.  
  1687.   if(mode == 2){
  1688.     addchar('*');
  1689.     for (i = 0; i < 8; i++) {
  1690.        sevenchar(1);
  1691.        addchar('*');
  1692.     }
  1693.     addchar(LF);
  1694.     addchar(CR);
  1695.   } else if(mode ==  1){
  1696.     addchar('*');
  1697.     j = 0;
  1698.     for (i = 0; i < 8; i++) {
  1699.       j = 1 - j;
  1700.       sevenchar(j);
  1701.       addchar('*');
  1702.     }
  1703.     addchar(LF);
  1704.     addchar(CR);
  1705.   } else if(mode == 0){
  1706.     addchar('*');
  1707.     j = 1;
  1708.     for (i = 0; i < 8; i++) {
  1709.       j = 1 - j;
  1710.       sevenchar(j);
  1711.       addchar('*');
  1712.     }
  1713.     addchar(LF);
  1714.     addchar(CR);
  1715.    
  1716.   }
  1717.    
  1718. }
  1719.  
  1720.  
  1721.  
  1722. void addchar(char a){
  1723.   tbuf[tin] = a;
  1724.   tdata += 1;
  1725.   tin += 1;
  1726.   tin = tin % tsize;
  1727. }
  1728.  
  1729. void outtype(int type){
  1730.   char x;
  1731.   //sciset = 1 - sciset;
  1732.   if(boardset == 0){
  1733.     x = '*';
  1734.   } else {
  1735.     x = ' ';
  1736.   }
  1737.   if(type == 1){
  1738.     addchar(x);
  1739.     addchar('P');
  1740.     addchar('a');
  1741.     addchar('w');
  1742.     addchar('n');
  1743.     addchar(x);
  1744.     addchar(x);
  1745.   } else if(type == 2){
  1746.     addchar(x);
  1747.     addchar('R');
  1748.     addchar('o');
  1749.     addchar('o');
  1750.     addchar('k');
  1751.     addchar(x);
  1752.     addchar(x);
  1753.   } else if(type == 3){
  1754.     addchar(x);
  1755.     addchar('K');
  1756.     addchar('n');
  1757.     addchar('i');
  1758.     addchar('g');
  1759.     addchar('h');
  1760.     addchar('t');
  1761.   } else if(type == 4){
  1762.     addchar(x);
  1763.     addchar('B');
  1764.     addchar('i');
  1765.     addchar('s');
  1766.     addchar('h');
  1767.     addchar('o');
  1768.     addchar('p');
  1769.   } else if(type == 5){
  1770.     addchar(x);
  1771.     addchar('Q');
  1772.     addchar('u');
  1773.     addchar('e');
  1774.     addchar('e');
  1775.     addchar('n');
  1776.     addchar(x);
  1777.   } else if(type == 6){
  1778.     addchar(x);
  1779.     addchar('K');
  1780.     addchar('i');
  1781.     addchar('n');
  1782.     addchar('g');
  1783.     addchar(x);
  1784.     addchar(x);
  1785.   }
  1786.   addchar('*');
  1787. }
  1788.  
  1789. void outteam(int team){
  1790.   char x;
  1791.   //boardset = 1 - sciset;
  1792.   if(boardset == 1){
  1793.     x = '*';
  1794.   } else {
  1795.     x = ' ';
  1796.   }
  1797.   if(team == 0){
  1798.     addchar(x);
  1799.     addchar('W');
  1800.     addchar('h');
  1801.     addchar('i');
  1802.     addchar('t');
  1803.     addchar('e');
  1804.     addchar(x);
  1805.   } else if(team == 1){
  1806.     addchar(x);
  1807.     addchar('B');
  1808.     addchar('l');
  1809.     addchar('a');
  1810.     addchar('c');
  1811.     addchar('k');
  1812.     addchar(x);
  1813.   }
  1814.   addchar('*');
  1815. }
  1816. //0=team, 1=type
  1817. void sci_datafetch(int mode, int line){
  1818.   int i = 0;
  1819.  
  1820.  
  1821.   if(mode == 0){
  1822.     for (i = 0; i < 8; i++) {
  1823.       //
  1824.       boardset = 1 - boardset;
  1825.       //if(HallMatrix[line][i] == 0){
  1826.       if(ChessMatrix[line][i] == 0){
  1827.           sevenchar(boardset);
  1828.           addchar('*');
  1829.       } else {
  1830.           outteam(ChessMatrix[line][i] / 10);
  1831.           //outteam(HallMatrix[line][i] / 10);    
  1832.       }
  1833.      
  1834.     }
  1835.   } else {
  1836.     for (i = 0; i < 8; i++) {
  1837.       //
  1838.       boardset = 1 - boardset;
  1839.       //if(HallMatrix[line][i] == 0){      
  1840.       if(ChessMatrix[line][i] == 0){
  1841.         sevenchar(1-boardset);
  1842.         addchar('*');
  1843.       } else {
  1844.         outtype(ChessMatrix[line][i] % 10);
  1845.         //outtype(HallMatrix[line][i] % 10);    
  1846.       }
  1847.     }
  1848.     //boardset = 1 - boardset;
  1849.   }
  1850.   //addchar('*');
  1851.   addchar(LF);
  1852.   addchar(CR);
  1853.  
  1854. }
  1855.  
  1856. void sciscan(void){
  1857.   int i = 0;
  1858.   int j = 0;
  1859.   if(scioff_flag > 0){
  1860.     if(scioff_flag == 3){
  1861.       SCICR2 = SCICR2 & 0x7F;
  1862.       //for (j = 0; j < 2; j++) {
  1863.         //addchar(LF);
  1864.         //addchar(CR);
  1865.       //}
  1866.       scioff_flag = 0;
  1867.       scicountflag = 1;
  1868.       turnoverflag = 1;
  1869.     }
  1870.     else if(scioff_flag == 2){
  1871.       scoreline += 1;
  1872.       if(scoreline == 1){
  1873.         addchar(LF);
  1874.         addchar(CR);
  1875.         addchar(LF);
  1876.         addchar(CR);
  1877.         for (i = 0; i < 15; i++) {
  1878.           addchar(' ');
  1879.         }
  1880.         i = 0;
  1881.         for (i = 0; i < 34; i++) {
  1882.           addchar('*');
  1883.         }
  1884.         addchar(LF);
  1885.         addchar(CR);
  1886.       }
  1887.       else if(scoreline == 2){
  1888.         for (i = 0; i < 15; i++) {
  1889.           addchar(' ');
  1890.         }
  1891.         addchar('*');
  1892.         addchar('W');
  1893.         addchar('h');
  1894.         addchar('i');
  1895.         addchar('t');
  1896.         addchar('e');
  1897.         addchar(' ');
  1898.         addchar('S');
  1899.         addchar('c');
  1900.         addchar('o');
  1901.         addchar('r');\
  1902.         addchar('e');
  1903.         addchar(':');
  1904.         addchar(' ');
  1905.         if((whitescore / 10) == 0){
  1906.             addchar('0');
  1907.             addchar(whitescore + 0x30);
  1908.         }
  1909.         else {
  1910.             addchar((whitescore / 10) + 0x30);
  1911.             addchar((whitescore % 10) + 0x30);
  1912.         }
  1913.         addchar(' ');
  1914.         addchar(' ');
  1915.         addchar('B');
  1916.         addchar('l');
  1917.         addchar('a');
  1918.         addchar('c');
  1919.         addchar('k');
  1920.         addchar(' ');
  1921.         addchar('S');
  1922.         addchar('c');
  1923.         addchar('o');
  1924.         addchar('r');
  1925.         addchar('e');
  1926.         addchar(':');
  1927.         addchar(' ');
  1928.        
  1929.         if((blackscore / 10) == 0){
  1930.             addchar('0');
  1931.             addchar(blackscore + 0x30);
  1932.         }
  1933.         else {
  1934.             addchar((blackscore / 10) + 0x30);
  1935.             addchar((blackscore % 10) + 0x30);
  1936.         }
  1937.         addchar('*');
  1938.         addchar(LF);
  1939.         addchar(CR);
  1940.       }
  1941.       else if(scoreline == 3){
  1942.         for (i = 0; i < 15; i++) {
  1943.           addchar(' ');
  1944.         }
  1945.         i = 0;
  1946.        
  1947.         addchar('*');
  1948.         for (i = 0; i < 9; i++) {
  1949.           addchar(' ');
  1950.         }
  1951.         j = movenumber / 2;
  1952.        
  1953.         if(movenumber % 2){
  1954.           addchar('B');
  1955.           addchar('l');
  1956.           addchar('a');
  1957.           addchar('c');
  1958.           addchar('k');
  1959.           addchar(' ');
  1960.         }
  1961.         else {
  1962.           addchar('W');
  1963.           addchar('h');
  1964.           addchar('i');
  1965.           addchar('t');
  1966.           addchar('e');
  1967.           addchar(' ');
  1968.         }
  1969.        
  1970.         addchar('T');
  1971.         addchar('u');
  1972.         addchar('r');
  1973.         addchar('n');
  1974.         addchar(' ');
  1975.         if((j / 100) == 0){
  1976.             if((j / 10) == 0){
  1977.             addchar('0');
  1978.             addchar('0');
  1979.             addchar(j + 0x30);
  1980.           }
  1981.           else {
  1982.             addchar('0');
  1983.             addchar((j / 10) + 0x30);
  1984.             addchar((j % 10) + 0x30);
  1985.           }
  1986.         } else {
  1987.            addchar((j / 100) + 0x30);
  1988.            addchar(((j % 100) / 10) + 0x30);
  1989.            addchar((j % 10) + 0x30);
  1990.         }
  1991.         i = 0;
  1992.         for (i = 0; i < 9; i++) {
  1993.           addchar(' ');
  1994.         }
  1995.         addchar('*');
  1996.         addchar(LF);
  1997.         addchar(CR);
  1998.        
  1999.        
  2000.       }
  2001.       else if(scoreline == 4){
  2002.        
  2003.         for (i = 0; i < 15; i++) {
  2004.           addchar(' ');
  2005.         }
  2006.         i = 0;
  2007.         for (i = 0; i < 34; i++) {
  2008.           addchar('*');
  2009.         }
  2010.         addchar(LF);
  2011.         addchar(CR);
  2012.       }
  2013.       else {
  2014.          scoreline = 0;
  2015.          scioff_flag += 1;
  2016.          //tdata -= 1;
  2017.       }
  2018.     }
  2019.     else {
  2020.       addchar('*');
  2021.       sci_outline(2);
  2022.      
  2023.       scioff_flag += 1;
  2024.     }
  2025.   }
  2026.   else {
  2027.     scanline();
  2028.   }
  2029. }
  2030.  
  2031. void scanline(void){
  2032.   int i = 0;
  2033.   addchar('*');
  2034.  
  2035.   sciset = 1 - sciset;
  2036.   boardset = sciset;
  2037.  
  2038.   if(sciline == 0){
  2039.     sci_outline(2);
  2040.   } else if(sciline == 1){
  2041.     sci_outline(boardline % 2);
  2042.   } else if(sciline == 2){
  2043.     sci_datafetch(0,boardline);
  2044.   } else if(sciline == 3){
  2045.     sci_datafetch(1,boardline);
  2046.   } else if(sciline == 4){
  2047.     sci_outline(boardline % 2);
  2048.   }
  2049.  
  2050.   sciline += 1;
  2051.   if(sciline > 4){
  2052.     boardline += 1;
  2053.     sciline = 0;
  2054.   }
  2055.  
  2056.   if(boardline > 7){
  2057.     scioff_flag = 1;
  2058.     boardline = 0;
  2059.   }
  2060.    
  2061. }
  2062.  
  2063. void outinputmsg(void){
  2064.     if(inputflag == -1){
  2065.               addchar(CR);
  2066.               addchar(LF);
  2067.               addchar('X');
  2068.               addchar(' ');
  2069.               addchar('t');
  2070.               addchar('o');
  2071.               addchar(' ');
  2072.               addchar('r');
  2073.               addchar('e');
  2074.               addchar('s');
  2075.               addchar('e');
  2076.               addchar('t');
  2077.               addchar(CR);
  2078.               addchar(LF);
  2079.               inputflag += 1;
  2080.       } else if(inputflag == 0){
  2081.               addchar(LF);
  2082.               addchar('C');
  2083.               addchar('h');
  2084.               addchar('o');
  2085.               addchar('o');
  2086.               addchar('s');
  2087.               addchar('e');
  2088.               addchar(' ');
  2089.               addchar('p');
  2090.               addchar('i');
  2091.               addchar('e');
  2092.               addchar('c');
  2093.               addchar('e');
  2094.               addchar(' ');
  2095.               addchar('t');
  2096.               addchar('o');
  2097.               addchar(' ');
  2098.               addchar('m');
  2099.               addchar('o');
  2100.               addchar('v');
  2101.               addchar('e');
  2102.               addchar(' ');
  2103.               addchar('b');
  2104.               addchar('y');
  2105.               addchar(' ');
  2106.               addchar('e');
  2107.               addchar('n');
  2108.               addchar('t');
  2109.               addchar('e');
  2110.               addchar('r');
  2111.               addchar('i');
  2112.               addchar('n');
  2113.               addchar('g');
  2114.               addchar(' ');
  2115.               addchar('l');
  2116.               addchar('o');
  2117.               addchar('c');
  2118.               addchar('a');
  2119.               addchar('t');
  2120.               addchar('i');
  2121.               addchar('o');
  2122.               addchar('n');
  2123.               addchar('.');
  2124.               addchar(LF);
  2125.               addchar(CR);
  2126.               inputflag += 1;
  2127.           } else if(inputflag == 1){
  2128.               addchar(CR);
  2129.               addchar(LF);
  2130.               addchar('E');
  2131.               addchar('n');
  2132.               addchar('t');
  2133.               addchar('e');
  2134.               addchar('r');
  2135.               addchar(' ');
  2136.               addchar('r');
  2137.               addchar('o');
  2138.               addchar('w');
  2139.               addchar(' ');
  2140.               addchar('(');
  2141.               addchar('A');
  2142.               addchar('B');
  2143.               addchar('C');
  2144.               addchar('D');
  2145.               addchar('E');
  2146.               addchar('F');
  2147.               addchar('G');
  2148.               addchar('H');
  2149.               addchar(')');
  2150.               addchar(':');
  2151.               addchar(LF);
  2152.               addchar(CR);
  2153.               addchar(' ');
  2154.               addchar(' ');
  2155.               addchar(' ');
  2156.               addchar(CR);
  2157.               inputonflag = 1;
  2158.           } else if(inputflag == 2){
  2159.               addchar(CR);
  2160.               addchar(LF);
  2161.               addchar('E');
  2162.               addchar('n');
  2163.               addchar('t');
  2164.               addchar('e');
  2165.               addchar('r');
  2166.               addchar(' ');
  2167.               addchar('c');
  2168.               addchar('o');
  2169.               addchar('l');
  2170.               addchar('u');
  2171.               addchar('m');
  2172.               addchar('n');
  2173.               addchar(' ');
  2174.               addchar('(');
  2175.               addchar('1');
  2176.               addchar('2');
  2177.               addchar('3');
  2178.               addchar('4');
  2179.               addchar('5');
  2180.               addchar('6');
  2181.               addchar('7');
  2182.               addchar('8');
  2183.               addchar(')');
  2184.               addchar(':');
  2185.               addchar(CR);
  2186.               addchar(LF);
  2187.               inputonflag = 1;
  2188.           } else if(inputflag == 3){
  2189.               addchar(CR);
  2190.               addchar(LF);
  2191.               addchar('P');
  2192.               addchar('l');
  2193.               addchar('a');
  2194.               addchar('c');
  2195.               addchar('e');
  2196.               addchar(' ');
  2197.               addchar('y');
  2198.               addchar('o');
  2199.               addchar('u');
  2200.               addchar('r');
  2201.               addchar(' ');
  2202.               addchar('p');
  2203.               addchar('i');
  2204.               addchar('e');
  2205.               addchar('c');
  2206.               addchar('e');
  2207.               addchar(' ');
  2208.               addchar('a');
  2209.               addchar('n');
  2210.               addchar('d');
  2211.               addchar(' ');
  2212.               addchar('e');
  2213.               addchar('n');
  2214.               addchar('t');
  2215.               addchar('e');
  2216.               addchar('r');
  2217.               addchar(' ');
  2218.               addchar('t');
  2219.               addchar('h');
  2220.               addchar('e');
  2221.               addchar(' ');
  2222.               addchar('d');
  2223.               addchar('e');
  2224.               addchar('s');
  2225.               addchar('t');
  2226.               addchar('i');
  2227.               addchar('n');
  2228.               addchar('a');
  2229.               addchar('t');
  2230.               addchar('i');
  2231.               addchar('o');
  2232.               addchar('n');
  2233.               addchar(' ');
  2234.               addchar('c');
  2235.               addchar('o');
  2236.               addchar('o');
  2237.               addchar('r');
  2238.               addchar('d');
  2239.               addchar('i');
  2240.               addchar('n');
  2241.               addchar('a');
  2242.               addchar('t');
  2243.               addchar('e');
  2244.               addchar('!');
  2245.               addchar(CR);
  2246.               addchar(LF);
  2247.               inputflag += 1;  
  2248.           } else if(inputflag == 4){
  2249.               addchar(CR);
  2250.               addchar(LF);
  2251.               addchar('E');
  2252.               addchar('n');
  2253.               addchar('t');
  2254.               addchar('e');
  2255.               addchar('r');
  2256.               addchar(' ');
  2257.               addchar('r');
  2258.               addchar('o');
  2259.               addchar('w');
  2260.               addchar(' ');
  2261.               addchar('(');
  2262.               addchar('A');
  2263.               addchar('B');
  2264.               addchar('C');
  2265.               addchar('D');
  2266.               addchar('E');
  2267.               addchar('F');
  2268.               addchar('G');
  2269.               addchar('H');
  2270.               addchar(')');
  2271.               addchar(':');
  2272.               addchar(CR);
  2273.               addchar(LF);
  2274.               inputonflag = 1;
  2275.           } else if(inputflag == 5){
  2276.               addchar(CR);
  2277.               addchar(LF);
  2278.               addchar('E');
  2279.               addchar('n');
  2280.               addchar('t');
  2281.               addchar('e');
  2282.               addchar('r');
  2283.               addchar(' ');
  2284.               addchar('c');
  2285.               addchar('o');
  2286.               addchar('l');
  2287.               addchar('u');
  2288.               addchar('m');
  2289.               addchar('n');
  2290.               addchar(' ');
  2291.               addchar('(');
  2292.               addchar('1');
  2293.               addchar('2');
  2294.               addchar('3');
  2295.               addchar('4');
  2296.               addchar('5');
  2297.               addchar('6');
  2298.               addchar('7');
  2299.               addchar('8');
  2300.               addchar(')');
  2301.               addchar(':');
  2302.               addchar(CR);
  2303.               addchar(LF);
  2304.               inputonflag = 1;
  2305.           } else if(inputflag == 6){ //input error
  2306.               addchar(CR);
  2307.               addchar(LF);
  2308.               addchar('I');
  2309.               addchar('n');
  2310.               addchar('v');
  2311.               addchar('a');
  2312.               addchar('l');
  2313.               addchar('i');
  2314.               addchar('d');
  2315.               addchar(' ');
  2316.               addchar('I');
  2317.               addchar('n');
  2318.               addchar('p');
  2319.               addchar('u');
  2320.               addchar('t');
  2321.               addchar('.');
  2322.               addchar(CR);
  2323.               //addchar(LF);
  2324.               inputflag = 1;
  2325.           } else if(inputflag == 7){ //input error
  2326.               addchar(CR);
  2327.               addchar(LF);
  2328.               addchar('I');
  2329.               addchar('n');
  2330.               addchar('v');
  2331.               addchar('a');
  2332.               addchar('l');
  2333.               addchar('i');
  2334.               addchar('d');
  2335.               addchar(' ');
  2336.               addchar('I');
  2337.               addchar('n');
  2338.               addchar('p');
  2339.               addchar('u');
  2340.               addchar('t');
  2341.               addchar('.');
  2342.               addchar(CR);
  2343.               //addchar(LF);
  2344.               inputflag = 4;
  2345.           } else if(inputflag == 8){//move error
  2346.               addchar(CR);
  2347.               addchar(LF);
  2348.               addchar(' ');
  2349.               addchar('I');
  2350.               addchar('n');
  2351.               addchar('v');
  2352.               addchar('a');
  2353.               addchar('l');
  2354.               addchar('i');
  2355.               addchar('d');
  2356.               addchar(' ');
  2357.               addchar('M');
  2358.               addchar('o');
  2359.               addchar('v');
  2360.               addchar('e');
  2361.               addchar('m');
  2362.               addchar('e');
  2363.               addchar('n');
  2364.               addchar('t');
  2365.               addchar('.');
  2366.               addchar(CR);
  2367.               //addchar(LF);
  2368.               inputflag = 4;
  2369.           } else if(inputflag == 9){
  2370.               addchar(CR);
  2371.               addchar(LF);
  2372.               addchar('I');
  2373.               addchar('n');
  2374.               addchar('v');
  2375.               addchar('a');
  2376.               addchar('l');
  2377.               addchar('i');
  2378.               addchar('d');
  2379.               addchar(' ');
  2380.               addchar('P');
  2381.               addchar('i');
  2382.               addchar('e');
  2383.               addchar('c');
  2384.               addchar('e');
  2385.               addchar('.');
  2386.               addchar(CR);
  2387.               addchar(LF);
  2388.               inputflag = 1;
  2389.           } else if(inputflag == 10){
  2390.               addchar(CR);
  2391.               addchar(LF);
  2392.               addchar('N');
  2393.               addchar('o');
  2394.               addchar(' ');
  2395.               addchar('m');
  2396.               addchar('o');
  2397.               addchar('v');
  2398.               addchar('e');
  2399.               addchar('s');
  2400.               addchar(' ');
  2401.               addchar('a');
  2402.               addchar('v');
  2403.               addchar('a');
  2404.               addchar('i');
  2405.               addchar('l');
  2406.               addchar('a');
  2407.               addchar('b');
  2408.               addchar('l');
  2409.               addchar('e');
  2410.               addchar('!');
  2411.               addchar(CR);
  2412.               addchar(LF);
  2413.               inputflag = 0;
  2414.           } else if(inputflag == 11){
  2415.               addchar(CR);
  2416.               addchar(LF);
  2417.               addchar('M');
  2418.               addchar('o');
  2419.               addchar('v');
  2420.               addchar('e');
  2421.               addchar(' ');
  2422.               addchar('c');
  2423.               addchar('o');
  2424.               addchar('m');
  2425.               addchar('e');
  2426.               addchar('p');
  2427.               addchar('l');
  2428.               addchar('e');
  2429.               addchar('t');
  2430.               addchar('e');
  2431.               addchar('.');
  2432.               addchar(' ');
  2433.               addchar(CR);
  2434.               addchar(LF);
  2435.               inputflag = 12;
  2436.           } else if(inputflag == 14){
  2437.               addchar(CR);
  2438.               addchar(LF);
  2439.               addchar('W');
  2440.               addchar('r');
  2441.               addchar('o');
  2442.               addchar('n');
  2443.               addchar('g');
  2444.               addchar(' ');
  2445.               addchar('T');
  2446.               addchar('e');
  2447.               addchar('a');
  2448.               addchar('m');
  2449.               addchar(CR);
  2450.               addchar(LF);
  2451.               inputflag = 1;
  2452.           } else if(inputflag == 15){
  2453.               addchar(CR);
  2454.               addchar(LF);
  2455.               addchar('I');
  2456.               addchar('n');
  2457.               addchar('v');
  2458.               addchar('a');
  2459.               addchar('l');
  2460.               addchar('i');
  2461.               addchar('d');
  2462.               addchar(' ');
  2463.               addchar('m');
  2464.               addchar('o');
  2465.               addchar('v');
  2466.               addchar('e');
  2467.               addchar(' ');
  2468.               addchar('y');
  2469.               addchar('o');
  2470.               addchar('u');
  2471.               addchar('r');
  2472.               addchar(' ');
  2473.               addchar('k');
  2474.               addchar('i');
  2475.               addchar('n');
  2476.               addchar('g');
  2477.               addchar(' ');
  2478.               addchar('i');
  2479.               addchar('s');
  2480.               addchar(' ');
  2481.               addchar('s');
  2482.               addchar('t');
  2483.               addchar('i');
  2484.               addchar('l');
  2485.               addchar('l');
  2486.               addchar(' ');
  2487.               addchar('i');
  2488.               addchar('n');
  2489.               addchar(' ');
  2490.               addchar('c');
  2491.               addchar('h');
  2492.               addchar('e');
  2493.               addchar('c');
  2494.               addchar('k');
  2495.               addchar('1');
  2496.               addchar(CR);
  2497.               addchar(LF);
  2498.               inputflag = 0;
  2499.           }
  2500. }
  2501.  
  2502.  
  2503.  
  2504. /**********************************************************************************
  2505. * Name:        SCI_ISR
  2506. * Description: SCI RTI for character data transfer to radial buffer.
  2507. * [->]    (*): (*)
  2508. * [<-] (void): [void]
  2509. **********************************************************************************/
  2510. interrupt 20 void SCI_ISR( void)
  2511. {
  2512.    
  2513.    
  2514.     int j;
  2515.    
  2516.     SCISR1_TDRE; //Read status flag to ready data register
  2517.    
  2518.     if(scioff_flag == 3){
  2519.      
  2520.       //SCICR2 = SCICR2 & 0x7F;
  2521.       //tin += 1;
  2522.       //tin = 0;
  2523.       scicountflag = 1;
  2524.       for (j = 0; j < 2; j++) {
  2525.         addchar(LF);
  2526.         addchar(CR);
  2527.       }
  2528.       scioff_flag = 0;
  2529.       turnoverflag = 1;
  2530.     }
  2531.    
  2532.    
  2533.     if(tdata <= 0){
  2534.          tdata = 0;
  2535.          tin = tout;
  2536.          addchar(' ');
  2537.          addchar(CR);
  2538.       if(inputonflag == 1){
  2539.           SCICR2 = SCICR2 & 0x7F;
  2540.       } else {
  2541.           if(turnoverflag == 0){
  2542.               sciscan();
  2543.           } else {
  2544.               outinputmsg();
  2545.             }
  2546.       }
  2547.     }
  2548.    
  2549.     if(SCISR1 & 0x80){
  2550.       SCIDRL = tbuf[tout];
  2551.       tout += 1;
  2552.       tout = tout % tsize;
  2553.       tdata -= 1;
  2554.       if(echoflag == 1){
  2555.           echoflag = 0;
  2556.           SCICR2 = SCICR2 & 0x7F;
  2557.       }
  2558.     }
  2559.  
  2560.    
  2561.  
  2562. }
  2563.  
  2564. /*********************************************************************************
  2565. * LED Matrix Functions
  2566. **********************************************************************************
  2567. **********************************************************************************
  2568. * Name:          LED_Out
  2569. * Description:   Shifts out the updated LED matrix data.
  2570. * [->] (global): LEDOutData: LED lighting data, one for each row.
  2571. * [<-]   (void): [void]
  2572. *********************************************************************************/
  2573. void LED_Out(void) {
  2574.      int i = 0;
  2575.  
  2576.      for (i = 0; i < 8; i++) {
  2577.           while (SPISR_SPTEF == 0) {
  2578.                //Loop until SPISR data register is empty
  2579.           }
  2580.  
  2581.           if(ledoutmode == 0){
  2582.             SPIDR = LEDOutData[i]; //Put data into SPISR data register
  2583.           } else if(ledoutmode == 1){
  2584.             SPIDR = MOVEOutData[i]; //Put data into SPISR data register
  2585.           }
  2586.           LCD_Delay(0); //Call a delay for shifting out
  2587.  
  2588.           //enable daisy chain, disable LCD
  2589.      }
  2590.      return;
  2591. }
  2592.  
  2593. /*********************************************************************************
  2594. * Name:          LED_Test
  2595. * Description:   Turns LED blocks on slowly, from left to right.
  2596. * [->]   (void): [void]              
  2597. * [<-]   (void): [void]
  2598. *********************************************************************************/
  2599. void LED_Test(void) {
  2600.     int i = 0;
  2601.     int j = 0;
  2602.     unsigned char a = 0x00; //wait loop variable
  2603.     unsigned char b = 0x00; //wait loop variable
  2604.     unsigned char c = 0x00; //wait loop variable
  2605.     int counter = 0;
  2606.     int row = 0; //controls row
  2607.     int k = 0;
  2608.    
  2609.     for (k = 0; k < 3; k++) {
  2610.         for (j = 0; j < 9; j++) {//Loop for refreshing the row for amounts of lights it has      
  2611.             for (i = 0; i < 8; i++) {//Loop for shiting full board data to daisy chain
  2612.        
  2613.                 while (SPISR_SPTEF == 0) {
  2614.                     //Loop until SPISR data register is empty  
  2615.                 }
  2616.                
  2617.                 if (k == 0) {
  2618.                     if (i == 0) {                      
  2619.                         if (j == 0) {        //1000 0000      
  2620.                             counter = 0x80;
  2621.                         } else if (j == 1) {//1100 0000
  2622.                             counter = 0xC0;
  2623.                         } else if (j == 2) {//1110 0000
  2624.                             counter = 0xE0;
  2625.                         } else if (j == 3) {//1111 0000
  2626.                             counter = 0xF0;
  2627.                         } else if (j == 4) {//1111 1000
  2628.                             counter = 0xF8;
  2629.                         } else if (j == 5) {//1111 1100
  2630.                             counter = 0xFC;
  2631.                         } else if (j == 6) {//1111 1110
  2632.                             counter = 0xFE;
  2633.                         } else if (j == 7) {//1111 1111
  2634.                             counter = 0xFF;
  2635.                         }  
  2636.                     } else if ((i != 0) && (k == 0)) {
  2637.                         counter = 0x00;
  2638.                     }
  2639.                    
  2640.                     if ((k == 0) && (j != 8)) {                      
  2641.                         SPIDR = counter;
  2642.                     }
  2643.                    
  2644.                 } else if (k == 1) {
  2645.                     if (i == 0) {
  2646.                         counter = 0xFF;
  2647.                     } else if (i == 1) {          
  2648.                
  2649.                         if (j == 0) {        //1111 1111 FF          
  2650.                             counter = 0x80;
  2651.                         } else if (j == 1) {//0111 1111 7F
  2652.                             counter = 0xC0;
  2653.                         } else if (j == 2) {//0011 1111 3F
  2654.                             counter = 0xE0;
  2655.                         } else if (j == 3) {//0001 1111 1F
  2656.                             counter = 0xF0;
  2657.                         } else if (j == 4) {//0000 1111 0F
  2658.                             counter = 0xF8;
  2659.                         } else if (j == 5) {//0000 0111 07
  2660.                             counter = 0xFC;
  2661.                         } else if (j == 6) {//0000 0011 03
  2662.                             counter = 0xFE;
  2663.                         } else if (j == 7) {//0000 0001 01
  2664.                             counter = 0xFF;
  2665.                         } //else if (j == 8) {
  2666.                             //row = 2;  
  2667.                         //}  
  2668.                     } else if ((i != 1) && (k == 1)){
  2669.                         counter = 0x00;
  2670.                     }
  2671.                    
  2672.                     if ((k == 1) && (j != 8)) {                      
  2673.                         SPIDR = counter;
  2674.                     }
  2675.                 } else if (k == 2) {
  2676.                     if ((i == 0) || (i == 1)) {
  2677.                         counter = 0xFF;
  2678.                     } else if (i == 2) {          
  2679.                         if (j == 0) {        //1111 1111 FF          
  2680.                             counter = 0x80;
  2681.                         } else if (j == 1) {//0111 1111 7F
  2682.                             counter = 0xC0;
  2683.                         } else if (j == 2) {//0011 1111 3F
  2684.                             counter = 0xE0;
  2685.                         } else if (j == 3) {//0001 1111 1F
  2686.                             counter = 0xF0;
  2687.                         } else if (j == 4) {//0000 1111 0F
  2688.                             counter = 0xF8;
  2689.                         } else if (j == 5) {//0000 0111 07
  2690.                             counter = 0xFC;
  2691.                         } else if (j == 6) {//0000 0011 03
  2692.                             counter = 0xFE;
  2693.                         } else if (j == 7) {//0000 0001 01
  2694.                             counter = 0xFF;
  2695.                         }  
  2696.                     } else if ((i != 2) && (k == 2)) {
  2697.                         counter = 0x00;
  2698.                     }
  2699.                    
  2700.                     if ((k == 2) && (j != 8)) {
  2701.                         SPIDR = counter;
  2702.                     }
  2703.                 } else if (k == 3) {
  2704.                     if ((i == 0) || (i == 1) || (i == 2)) {
  2705.                         counter = 0xFF;
  2706.                     } else if (i == 3) {                          
  2707.                         if (j == 0) {        //1111 1111 FF          
  2708.                             counter = 0x80;
  2709.                         } else if (j == 1) {//0111 1111 7F
  2710.                             counter = 0xC0;
  2711.                         } else if (j == 2) {//0011 1111 3F
  2712.                             counter = 0xE0;
  2713.                         } else if (j == 3) {//0001 1111 1F
  2714.                             counter = 0xF0;
  2715.                         } else if (j == 4) {//0000 1111 0F
  2716.                             counter = 0xF8;
  2717.                         } else if (j == 5) {//0000 0111 07
  2718.                             counter = 0xFC;
  2719.                         } else if (j == 6) {//0000 0011 03
  2720.                             counter = 0xFE;
  2721.                         } else if (j == 7) {//0000 0001 01
  2722.                             counter = 0xFF;
  2723.                         }    
  2724.                     } else {
  2725.                         counter = 0x00;
  2726.                     }
  2727.                    
  2728.                     if ((k == 3) && (j != 8)) {
  2729.                         SPIDR = counter;
  2730.                     }  
  2731.                 }// row if end      
  2732.             }//i for end
  2733.    
  2734.             for (a = 0x00; a < 0xFE; a++) {
  2735.                 for (b = 0x00; b < 0xFE; b++) {
  2736.                     for (c = 0x00; c < 0x3F; c++) {
  2737.                         //do nothing  
  2738.                     }
  2739.                 }
  2740.             }
  2741.         }//j for end
  2742.     }//k for end
  2743.     //while (SPISR_SPTEF == 0) {
  2744.         //Loop until SPISR data register is empty  
  2745.     //}
  2746.    
  2747.     //counter = 0x00;
  2748.    
  2749. }
  2750.  
  2751. /*********************************************************************************
  2752. * Name:          LED_Convert
  2753. * Description:   Converts chess data to LED data.
  2754. * [->] (global): LEDOutData
  2755. *                ChessMatrix
  2756. * [<-]   (void): [void]
  2757. *********************************************************************************/
  2758. void LED_Convert(void) {
  2759.      int i = 0;
  2760.      int j = 0;
  2761.      int k = 0;
  2762.      int temp = 1;
  2763.      int temp2 = 1;
  2764.      for (i = 0; i < 8; i++) {  //Chess column loop
  2765.           LEDOutData[i] = 0; //Clear of former data.
  2766.           MOVEOutData[i] = 0;
  2767.  
  2768.           for (j = 0; j < 8; j++) { //Chess row loop
  2769.                if (HallMatrix[i][j] != 0) { //if piece exists on tile
  2770.                     /*temp = 1;
  2771.  
  2772.                     for (k = 0; k < j; k++) { //shifting data calculation
  2773.                          temp = temp * 2; //binary shift left
  2774.  
  2775.                     }*/
  2776.                     temp = 128;
  2777.                    
  2778.                     for (k = 0; k < j; k++) {
  2779.                         temp = temp / 2;  
  2780.                     }
  2781.                    
  2782.                     LEDOutData[i] += temp;
  2783.                }
  2784.                if(ldata[i][j] != 0) { //if light exists in ldata
  2785.                     /*temp2 = 1;
  2786.  
  2787.                     for (k = 0; k < j; k++) { //shifting data calculation
  2788.                          temp2 = temp2 * 2;
  2789.  
  2790.                     }*/
  2791.                    
  2792.                    temp2 = 128;
  2793.                    
  2794.                     for (k = 0; k < j; k++) {
  2795.                         temp2 = temp2 / 2;  
  2796.                     }
  2797.                    
  2798.                     MOVEOutData[i] += temp2;
  2799.                }
  2800.           }
  2801.      }
  2802.  
  2803.      return;
  2804. }
  2805.  
  2806. /*********************************************************************************
  2807. * Hall Sensor Functions
  2808. **********************************************************************************
  2809. **********************************************************************************
  2810. * Name:          HALL_Input
  2811. * Description:   Collects magnet connection data.
  2812. * [->] (global): datas
  2813. * [<-]   (void): [void]
  2814. *********************************************************************************/
  2815. void HALL_Input(void) {
  2816.      int i = 0;
  2817.      int j = 0;
  2818.      char Selector = 0x00;
  2819.      int LinVal = 0;
  2820.      int Temp[64];  
  2821.         int MatVal = 0;
  2822.        
  2823.         Selector = 0x0B; //Multiplexer data slector set to highest for chip 5 (12)  
  2824.      for (j = 0; j < 12; j++) {//Multiplexer inputs on chip 5      
  2825.            while (SPISR_SPTEF == 0) {
  2826.               //Loop until SPISR data register is empty
  2827.           }
  2828.  
  2829.           SPIDR = Selector; //Put data into SPISR data register
  2830.         LCD_Delay(0);
  2831.  
  2832.         //if (j == 1) {
  2833.         //    LineVal == 0;
  2834.         //}
  2835.           Temp[LinVal] = output5;
  2836.           Selector -= 0x01;
  2837.           LinVal++;
  2838.       }
  2839.            
  2840.            
  2841.      Selector = 0x0C; //Multiplexer data slector set to highest for chip 4 (13)  
  2842.      for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 4      
  2843.            while (SPISR_SPTEF == 0) {
  2844.               //Loop until SPISR data register is empty
  2845.           }
  2846.  
  2847.           SPIDR = Selector; //Put data into SPISR data register
  2848.         LCD_Delay(0);
  2849.  
  2850.           Temp[LinVal] = output4;
  2851.           Selector -= 0x01;
  2852.           LinVal++;
  2853.       }
  2854.      
  2855.      Selector = 0x0C; //Multiplexer data slector set to highest for chip 3 (13)  
  2856.      for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 3      
  2857.            while (SPISR_SPTEF == 0) {
  2858.               //Loop until SPISR data register is empty
  2859.           }
  2860.  
  2861.           SPIDR = Selector; //Put data into SPISR data register
  2862.         LCD_Delay(0);
  2863.  
  2864.           Temp[LinVal] = output3;
  2865.           Selector -= 0x01;
  2866.           LinVal++;
  2867.       }
  2868.  
  2869.      Selector = 0x0C; //Multiplexer data slector set to highest for chip 2 (13)  
  2870.      for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 2      
  2871.            while (SPISR_SPTEF == 0) {
  2872.               //Loop until SPISR data register is empty
  2873.           }
  2874.  
  2875.           SPIDR = Selector; //Put data into SPISR data register
  2876.         LCD_Delay(0);
  2877.  
  2878.           Temp[LinVal] = output2;
  2879.           Selector -= 0x01;
  2880.           LinVal++;
  2881.       }
  2882.  
  2883.      Selector = 0x0C; //Multiplexer data slector set to highest for chip 1 (13)  
  2884.      for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 1      
  2885.            while (SPISR_SPTEF == 0) {
  2886.               //Loop until SPISR data register is empty
  2887.           }
  2888.  
  2889.           SPIDR = Selector; //Put data into SPISR data register
  2890.         LCD_Delay(0);
  2891.  
  2892.           Temp[LinVal] = output;
  2893.           Selector -= 0x01;
  2894.           LinVal++;
  2895.       }
  2896.            
  2897.      for (i = 0; i < 8; i++) {
  2898.           for (j = 0; j < 8; j++) {
  2899.  
  2900.                   HallMatrix[i][j] = Temp[MatVal];
  2901.                   MatVal++;
  2902.           }
  2903.  
  2904.      }
  2905.      return;
  2906. }
  2907.  
  2908. /*********************************************************************************
  2909. * LCD Specific Functions
  2910. **********************************************************************************
  2911. **********************************************************************************
  2912. * Name:          LCD_Timer
  2913. * Description:   Refreshes the chess timer.
  2914. * [->] (global): P1mins: Player 1 minutes remaining
  2915. *                P1secs: Player 1 seconds remaining
  2916. *                P2mins: Player 2 minutes remaining
  2917. *                P2secs: Player 2 seconds remaining
  2918. * [<-]   (void): [void]
  2919. *********************************************************************************/
  2920. void LCD_Timer(void) {
  2921.      int t1 = 0;
  2922.      int t2 = 0;
  2923.  
  2924.      LineOffset = 0x00;
  2925.      LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  2926.  
  2927.      LCD_Cursor_Position(1); //"White:    "
  2928.      LCD_Send_Byte('W',0);
  2929.      LCD_Cursor_Position(1);
  2930.      LCD_Send_Byte('h',0);
  2931.      LCD_Cursor_Position(1);
  2932.      LCD_Send_Byte('i',0);
  2933.      LCD_Cursor_Position(1);
  2934.      LCD_Send_Byte('t',0);
  2935.      LCD_Cursor_Position(1);
  2936.      LCD_Send_Byte('e',0);
  2937.      LCD_Cursor_Position(1);
  2938.      LCD_Send_Byte(':',0);
  2939.      LCD_Cursor_Position(1);
  2940.      LCD_Send_Byte(' ',0);
  2941.      LCD_Cursor_Position(1);
  2942.      LCD_Send_Byte(' ',0);
  2943.      LCD_Cursor_Position(1);
  2944.      LCD_Send_Byte(' ',0);
  2945.      LCD_Cursor_Position(1);
  2946.      LCD_Send_Byte(' ',0);
  2947.  
  2948.  
  2949.  
  2950.      t2 = P1mins % 10; //mins:secs,
  2951.      t1 = P1mins / 10;
  2952.  
  2953.      LCD_Cursor_Position(1);
  2954.      LCD_Send_Byte(t1 + 0x30,0);
  2955.      LCD_Cursor_Position(1);
  2956.      LCD_Send_Byte(t2 + 0x30,0);
  2957.  
  2958.      LCD_Cursor_Position(1);
  2959.      LCD_Send_Byte(':',0);
  2960.  
  2961.      t2 = P1secs % 10;
  2962.      t1 = P1secs / 10;
  2963.  
  2964.      LCD_Cursor_Position(1);
  2965.      LCD_Send_Byte(t1 + 0x30,0);
  2966.      LCD_Cursor_Position(1);
  2967.      LCD_Send_Byte(t2 + 0x30,0);
  2968.      //----------------------- line two begins
  2969.      LineOffset = 0x00;
  2970.      LCD_Cursor_Position(2);
  2971.      LCD_Send_Byte('B',0);
  2972.      LCD_Cursor_Position(2);
  2973.      LCD_Send_Byte('l',0);
  2974.      LCD_Cursor_Position(2);
  2975.      LCD_Send_Byte('a',0);
  2976.      LCD_Cursor_Position(2);
  2977.      LCD_Send_Byte('c',0);
  2978.      LCD_Cursor_Position(2);
  2979.      LCD_Send_Byte('k',0);
  2980.      LCD_Cursor_Position(2);
  2981.      LCD_Send_Byte(':',0);
  2982.      LCD_Cursor_Position(2);
  2983.      LCD_Send_Byte(' ',0);
  2984.      LCD_Cursor_Position(2);
  2985.      LCD_Send_Byte(' ',0);
  2986.      LCD_Cursor_Position(2);
  2987.      LCD_Send_Byte(' ',0);
  2988.      LCD_Cursor_Position(2);
  2989.      LCD_Send_Byte(' ',0);
  2990.  
  2991.      t2 = P2mins % 10;
  2992.      t1 = P2mins / 10;
  2993.  
  2994.      LCD_Cursor_Position(2);
  2995.      LCD_Send_Byte(t1 + 0x30,0);
  2996.      LCD_Cursor_Position(2);
  2997.      LCD_Send_Byte(t2 + 0x30,0);
  2998.  
  2999.      LCD_Cursor_Position(2);
  3000.      LCD_Send_Byte(':',0);
  3001.  
  3002.      t2 = P2secs % 10;
  3003.      t1 = P2secs / 10;
  3004.  
  3005.      LCD_Cursor_Position(2);
  3006.      LCD_Send_Byte(t1 + 0x30,0);
  3007.      LCD_Cursor_Position(2);
  3008.      LCD_Send_Byte(t2 + 0x30,0);
  3009.  
  3010.      LCD_Cursor_Position(2);
  3011.      LCD_Send_Byte(' ',0);
  3012.      return;
  3013. }
  3014.  
  3015.  
  3016.    
  3017.    
  3018.  
  3019. /*********************************************************************************
  3020. * Name:        LCD_Delay
  3021. * Description: Creates a delay of ~2ms or ~30cy using ASM NOPs.
  3022. * [->]  (int): mode: Operational mode of the function.
  3023. *                    0 = Short wait mode (~30cy)
  3024. *                    1 = Long wait mode (~25,000cy) = (~2ms)
  3025. * [<-] (void): [void]
  3026. *********************************************************************************/
  3027. void LCD_Delay(int mode) {
  3028.      int i = 0; //Wait loop counter
  3029.  
  3030.      //Mode check
  3031.      if (mode == 0) {
  3032.           mode = 3;
  3033.      } else if (mode == 1) {
  3034.           mode = 2500;
  3035.      } else {
  3036.          mode = 250000000;  
  3037.        
  3038.      }
  3039.  
  3040.      //Wait generation loop
  3041.      for (i = 0; i <= mode; i++) {
  3042.           asm {
  3043.                nop
  3044.                nop
  3045.                nop
  3046.                nop
  3047.                nop
  3048.                nop
  3049.                nop
  3050.                nop
  3051.                nop
  3052.                nop
  3053.           }
  3054.      }
  3055.  
  3056.      return;
  3057. }
  3058.  
  3059. /*********************************************************************************
  3060. * Name:        LCD_Cursor_Position
  3061. * Description: Moves the LCD Cursor vertically and horizontally.
  3062. * [->]  (int): LineNumber: Declares the line number the cursor is to be moved to
  3063. *                          1 = Cursor to be moved to line #1
  3064. *                          2 = Cursor to be moved to line #2
  3065. * [<-] (void): [void]
  3066. *********************************************************************************/
  3067. void LCD_Cursor_Position(int LineNumber) {
  3068.      LCD_Send_Byte(LCD_CURSOR, 1);
  3069.  
  3070.      if (LineNumber == 1) {
  3071.           LCD_Send_Byte((LCD_LINE1 + LineOffset), 1); //Instruction mode
  3072.      } else {
  3073.           LCD_Send_Byte((LCD_LINE2 + LineOffset), 1); //Instruction mode
  3074.      }
  3075.  
  3076.      LineOffset = LineOffset + 0x01;
  3077.  
  3078.      return;
  3079. }
  3080.  
  3081. /*********************************************************************************
  3082. * Name:         LCD_Send_Byte
  3083. * Description:  Sends character or instruction through respective shift register.
  3084. * [->] (uchar): data: The byte of data to be sent through the shift register
  3085. *        (int): mode: Operational mode of the function,
  3086. *                     0 = Normal sending mode
  3087. *                     1 = Instruction sending mode
  3088. * [<-]  (void): [void]
  3089. *********************************************************************************/
  3090. void LCD_Send_Byte(char data, int mode) {
  3091.  
  3092.      if (mode == 1) {
  3093.           LCD_RS = 0; //Register select line low
  3094.      }
  3095.  
  3096.      while (SPISR_SPTEF == 0) {
  3097.           //Loop until SPISR data register is empty
  3098.      }
  3099.  
  3100.      SPIDR = data; //Put data into SPISR data register
  3101.  
  3102.      LCD_Delay(0); //Call a 30cy delay so that data can shift out
  3103.  
  3104.      LCD_CLK = 0; //this may be some other clock i don't know
  3105.      LCD_CLK = 1;
  3106.  
  3107.      LCD_Delay(1); //Call a 2ms delay so LCD can respond
  3108.  
  3109.      if (mode == 1) {
  3110.           LCD_RS = 1; //Register select line high. This is the default.
  3111.      }
  3112.  
  3113.      return;
  3114. }
  3115.  
  3116. /*********************************************************************************
  3117. * Name:        LCD_Piece_Select
  3118. * Description: Displays some text on the LCD to select a kind of piece.
  3119. * [->]  (int): MsgType: The pre-rendered message to print to the LCD.
  3120. *
  3121. *
  3122. *                      X1 = pawn
  3123. *                      X2 = rook
  3124. *                      X3 = knight
  3125. *                      X4 = bishop
  3126. *                      X5 = Queen
  3127. *
  3128. * [<-] (void): [void]
  3129. *********************************************************************************/
  3130. void LCD_Piece_Select(int PieceType) {
  3131.      LineOffset = 0x00;
  3132.  
  3133.      LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  3134.  
  3135.  
  3136.           LCD_Cursor_Position(1);
  3137.           LCD_Send_Byte('S',0);
  3138.           LCD_Cursor_Position(1);
  3139.           LCD_Send_Byte('e',0);
  3140.           LCD_Cursor_Position(1);
  3141.           LCD_Send_Byte('l',0);
  3142.           LCD_Cursor_Position(1);
  3143.           LCD_Send_Byte('e',0);
  3144.           LCD_Cursor_Position(1);
  3145.           LCD_Send_Byte('c',0);
  3146.           LCD_Cursor_Position(1);
  3147.           LCD_Send_Byte('t',0);
  3148.           LCD_Cursor_Position(1);
  3149.           LCD_Send_Byte(' ',0);
  3150.           LCD_Cursor_Position(1);
  3151.           LCD_Send_Byte('p',0);
  3152.           LCD_Cursor_Position(1);
  3153.           LCD_Send_Byte('i',0);
  3154.           LCD_Cursor_Position(1);
  3155.           LCD_Send_Byte('e',0);
  3156.           LCD_Cursor_Position(1);
  3157.           LCD_Send_Byte('c',0);
  3158.           LCD_Cursor_Position(1);
  3159.           LCD_Send_Byte('e',0);
  3160.           LCD_Cursor_Position(1);
  3161.           LCD_Send_Byte(' ',0);
  3162.           LCD_Cursor_Position(1);
  3163.           LCD_Send_Byte(' ',0);
  3164.           LCD_Cursor_Position(1);
  3165.           LCD_Send_Byte(' ',0);
  3166.           LineOffset = 0x00;
  3167.  
  3168. /*                         options for pawn to become:
  3169. *                      X2 = rook
  3170. *                      X3 = knight
  3171. *                      X4 = bishop
  3172. *                      X5 = Queen
  3173. *
  3174. */
  3175.      if (PieceType == 2) {
  3176.           LCD_Cursor_Position(2);
  3177.           LCD_Send_Byte(' ',0);
  3178.           LCD_Cursor_Position(2);
  3179.           LCD_Send_Byte(' ',0);
  3180.           LCD_Cursor_Position(2);
  3181.           LCD_Send_Byte(' ',0);
  3182.           LCD_Cursor_Position(2);
  3183.           LCD_Send_Byte(' ',0);
  3184.           LCD_Cursor_Position(2);
  3185.           LCD_Send_Byte(' ',0);
  3186.           LCD_Cursor_Position(2);
  3187.           LCD_Send_Byte(' ',0);
  3188.           LCD_Cursor_Position(2);
  3189.           LCD_Send_Byte('R',0);
  3190.           LCD_Cursor_Position(2);
  3191.           LCD_Send_Byte('o',0);
  3192.           LCD_Cursor_Position(2);
  3193.           LCD_Send_Byte('o',0);
  3194.           LCD_Cursor_Position(2);
  3195.           LCD_Send_Byte('k',0);
  3196.           LCD_Cursor_Position(2);
  3197.           LCD_Send_Byte(' ',0);
  3198.           LCD_Cursor_Position(2);
  3199.           LCD_Send_Byte(' ',0);
  3200.           LCD_Cursor_Position(2);
  3201.           LCD_Send_Byte(' ',0);
  3202.           LCD_Cursor_Position(2);
  3203.           LCD_Send_Byte(' ',0);
  3204.           LCD_Cursor_Position(2);
  3205.           LCD_Send_Byte(' ',0);
  3206.           LCD_Cursor_Position(2);
  3207.           LCD_Send_Byte(' ',0);
  3208.      }
  3209.      if (PieceType == 3) {
  3210.           LCD_Cursor_Position(2);
  3211.           LCD_Send_Byte(' ',0);
  3212.           LCD_Cursor_Position(2);
  3213.           LCD_Send_Byte(' ',0);
  3214.           LCD_Cursor_Position(2);
  3215.           LCD_Send_Byte(' ',0);
  3216.           LCD_Cursor_Position(2);
  3217.           LCD_Send_Byte(' ',0);
  3218.           LCD_Cursor_Position(2);
  3219.           LCD_Send_Byte(' ',0);
  3220.           LCD_Cursor_Position(2);
  3221.           LCD_Send_Byte(' ',0);
  3222.           LCD_Cursor_Position(2);
  3223.           LCD_Send_Byte('K',0);
  3224.           LCD_Cursor_Position(2);
  3225.           LCD_Send_Byte('n',0);
  3226.           LCD_Cursor_Position(2);
  3227.           LCD_Send_Byte('i',0);
  3228.           LCD_Cursor_Position(2);
  3229.           LCD_Send_Byte('g',0);
  3230.           LCD_Cursor_Position(2);
  3231.           LCD_Send_Byte('h',0);
  3232.           LCD_Cursor_Position(2);
  3233.           LCD_Send_Byte('t',0);
  3234.           LCD_Cursor_Position(2);
  3235.           LCD_Send_Byte(' ',0);
  3236.           LCD_Cursor_Position(2);
  3237.           LCD_Send_Byte(' ',0);
  3238.           LCD_Cursor_Position(2);
  3239.           LCD_Send_Byte(' ',0);
  3240.           LCD_Cursor_Position(2);
  3241.           LCD_Send_Byte(' ',0);
  3242.      }
  3243.       if (PieceType == 4) {
  3244.           LCD_Cursor_Position(2);
  3245.           LCD_Send_Byte(' ',0);
  3246.           LCD_Cursor_Position(2);
  3247.           LCD_Send_Byte(' ',0);
  3248.           LCD_Cursor_Position(2);
  3249.           LCD_Send_Byte(' ',0);
  3250.           LCD_Cursor_Position(2);
  3251.           LCD_Send_Byte(' ',0);
  3252.           LCD_Cursor_Position(2);
  3253.           LCD_Send_Byte(' ',0);
  3254.           LCD_Cursor_Position(2);
  3255.           LCD_Send_Byte(' ',0);
  3256.           LCD_Cursor_Position(2);
  3257.           LCD_Send_Byte('B',0);
  3258.           LCD_Cursor_Position(2);
  3259.           LCD_Send_Byte('i',0);
  3260.           LCD_Cursor_Position(2);
  3261.           LCD_Send_Byte('s',0);
  3262.           LCD_Cursor_Position(2);
  3263.           LCD_Send_Byte('h',0);
  3264.           LCD_Cursor_Position(2);
  3265.           LCD_Send_Byte('o',0);
  3266.           LCD_Cursor_Position(2);
  3267.           LCD_Send_Byte('p',0);
  3268.           LCD_Cursor_Position(2);
  3269.           LCD_Send_Byte(' ',0);
  3270.           LCD_Cursor_Position(2);
  3271.           LCD_Send_Byte(' ',0);
  3272.           LCD_Cursor_Position(2);
  3273.           LCD_Send_Byte(' ',0);
  3274.           LCD_Cursor_Position(2);
  3275.           LCD_Send_Byte(' ',0);
  3276.       }
  3277.       if (PieceType == 5) {
  3278.           LCD_Cursor_Position(2);
  3279.           LCD_Send_Byte(' ',0);
  3280.           LCD_Cursor_Position(2);
  3281.           LCD_Send_Byte(' ',0);
  3282.           LCD_Cursor_Position(2);
  3283.           LCD_Send_Byte(' ',0);
  3284.           LCD_Cursor_Position(2);
  3285.           LCD_Send_Byte(' ',0);
  3286.           LCD_Cursor_Position(2);
  3287.           LCD_Send_Byte(' ',0);
  3288.           LCD_Cursor_Position(2);
  3289.           LCD_Send_Byte(' ',0);
  3290.           LCD_Cursor_Position(2);
  3291.           LCD_Send_Byte('Q',0);
  3292.           LCD_Cursor_Position(2);
  3293.           LCD_Send_Byte('u',0);
  3294.           LCD_Cursor_Position(2);
  3295.           LCD_Send_Byte('e',0);
  3296.           LCD_Cursor_Position(2);
  3297.           LCD_Send_Byte('e',0);
  3298.           LCD_Cursor_Position(2);
  3299.           LCD_Send_Byte('n',0);
  3300.           LCD_Cursor_Position(2);
  3301.           LCD_Send_Byte(' ',0);
  3302.           LCD_Cursor_Position(2);
  3303.           LCD_Send_Byte(' ',0);
  3304.           LCD_Cursor_Position(2);
  3305.           LCD_Send_Byte(' ',0);
  3306.           LCD_Cursor_Position(2);
  3307.           LCD_Send_Byte(' ',0);
  3308.           LCD_Cursor_Position(2);
  3309.           LCD_Send_Byte(' ',0);
  3310.       }
  3311. }
  3312.  
  3313. /*********************************************************************************
  3314. * Name:        LCD_Display
  3315. * Description: Displays some text on the chosen LCD.
  3316. * [->]  (int): MsgType: The pre-rendered message to print to the LCD.
  3317. *                       0 = "Press the shiny|button to start!"
  3318. *                       1 = "   --OPTION--   | >Start chess  "
  3319. *                       2 = "   --OPTION--   |  Edit timer   "
  3320. *                       3 = "   --OPTION--   |  Load Game    "
  3321. *                       4 = "   --OPTION--   |< Save Game    "
  3322. *                       5 = "   --PAUSED--   |Press to resume"
  3323. * [<-] (void): [void]
  3324. *********************************************************************************/
  3325. void LCD_Display(int MsgType) {
  3326.      LineOffset = 0x00;
  3327.  
  3328.      LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  3329.  
  3330.      if (MsgType == 0) {
  3331.           LCD_Cursor_Position(1);
  3332.           LCD_Send_Byte('P',0);
  3333.           LCD_Cursor_Position(1);
  3334.           LCD_Send_Byte('r',0);
  3335.           LCD_Cursor_Position(1);
  3336.           LCD_Send_Byte('e',0);
  3337.           LCD_Cursor_Position(1);
  3338.           LCD_Send_Byte('s',0);
  3339.           LCD_Cursor_Position(1);
  3340.           LCD_Send_Byte('s',0);
  3341.           LCD_Cursor_Position(1);
  3342.           LCD_Send_Byte(' ',0);
  3343.           LCD_Cursor_Position(1);
  3344.           LCD_Send_Byte('t',0);
  3345.           LCD_Cursor_Position(1);
  3346.           LCD_Send_Byte('h',0);
  3347.           LCD_Cursor_Position(1);
  3348.           LCD_Send_Byte('e',0);
  3349.           LCD_Cursor_Position(1);
  3350.           LCD_Send_Byte(' ',0);
  3351.           LCD_Cursor_Position(1);
  3352.           LCD_Send_Byte('s',0);
  3353.           LCD_Cursor_Position(1);
  3354.           LCD_Send_Byte('h',0);
  3355.           LCD_Cursor_Position(1);
  3356.           LCD_Send_Byte('i',0);
  3357.           LCD_Cursor_Position(1);
  3358.           LCD_Send_Byte('n',0);
  3359.           LCD_Cursor_Position(1);
  3360.           LCD_Send_Byte('y',0);
  3361.           LineOffset = 0x00;
  3362.           LCD_Cursor_Position(2);
  3363.           LCD_Send_Byte('b',0);
  3364.           LCD_Cursor_Position(2);
  3365.           LCD_Send_Byte('u',0);
  3366.           LCD_Cursor_Position(2);
  3367.           LCD_Send_Byte('t',0);
  3368.           LCD_Cursor_Position(2);
  3369.           LCD_Send_Byte('t',0);
  3370.           LCD_Cursor_Position(2);
  3371.           LCD_Send_Byte('o',0);
  3372.           LCD_Cursor_Position(2);
  3373.           LCD_Send_Byte('n',0);
  3374.           LCD_Cursor_Position(2);
  3375.           LCD_Send_Byte(' ',0);
  3376.           LCD_Cursor_Position(2);
  3377.           LCD_Send_Byte('t',0);
  3378.           LCD_Cursor_Position(2);
  3379.           LCD_Send_Byte('o',0);
  3380.           LCD_Cursor_Position(2);
  3381.           LCD_Send_Byte(' ',0);
  3382.           LCD_Cursor_Position(2);
  3383.           LCD_Send_Byte('s',0);
  3384.           LCD_Cursor_Position(2);
  3385.           LCD_Send_Byte('t',0);
  3386.           LCD_Cursor_Position(2);
  3387.           LCD_Send_Byte('a',0);
  3388.           LCD_Cursor_Position(2);
  3389.           LCD_Send_Byte('r',0);
  3390.           LCD_Cursor_Position(2);
  3391.           LCD_Send_Byte('t',0);
  3392.           LCD_Cursor_Position(2);
  3393.           LCD_Send_Byte('!',0);
  3394.      } else if (MsgType == 1) {
  3395.           LCD_Cursor_Position(1);
  3396.           LCD_Send_Byte(' ',0);
  3397.           LCD_Cursor_Position(1);
  3398.           LCD_Send_Byte(' ',0);
  3399.           LCD_Cursor_Position(1);
  3400.           LCD_Send_Byte(' ',0);
  3401.           LCD_Cursor_Position(1);
  3402.           LCD_Send_Byte('-',0);
  3403.           LCD_Cursor_Position(1);
  3404.           LCD_Send_Byte('-',0);
  3405.           LCD_Cursor_Position(1);
  3406.           LCD_Send_Byte('O',0);
  3407.           LCD_Cursor_Position(1);
  3408.           LCD_Send_Byte('P',0);
  3409.           LCD_Cursor_Position(1);
  3410.           LCD_Send_Byte('T',0);
  3411.           LCD_Cursor_Position(1);
  3412.           LCD_Send_Byte('I',0);
  3413.           LCD_Cursor_Position(1);
  3414.           LCD_Send_Byte('O',0);
  3415.           LCD_Cursor_Position(1);
  3416.           LCD_Send_Byte('N',0);
  3417.           LCD_Cursor_Position(1);
  3418.           LCD_Send_Byte('-',0);
  3419.           LCD_Cursor_Position(1);
  3420.           LCD_Send_Byte('-',0);
  3421.           LineOffset = 0x00;
  3422.           LCD_Cursor_Position(2);
  3423.           LCD_Send_Byte(' ',0);
  3424.           LCD_Cursor_Position(2);
  3425.           LCD_Send_Byte(0x7E,0);
  3426.           LCD_Cursor_Position(2);
  3427.           LCD_Send_Byte('S',0);
  3428.           LCD_Cursor_Position(2);
  3429.           LCD_Send_Byte('t',0);
  3430.           LCD_Cursor_Position(2);
  3431.           LCD_Send_Byte('a',0);
  3432.           LCD_Cursor_Position(2);
  3433.           LCD_Send_Byte('r',0);
  3434.           LCD_Cursor_Position(2);
  3435.           LCD_Send_Byte('t',0);
  3436.           LCD_Cursor_Position(2);
  3437.           LCD_Send_Byte(' ',0);
  3438.           LCD_Cursor_Position(2);
  3439.           LCD_Send_Byte('c',0);
  3440.           LCD_Cursor_Position(2);
  3441.           LCD_Send_Byte('h',0);
  3442.           LCD_Cursor_Position(2);
  3443.           LCD_Send_Byte('e',0);
  3444.           LCD_Cursor_Position(2);
  3445.           LCD_Send_Byte('s',0);
  3446.           LCD_Cursor_Position(2);
  3447.           LCD_Send_Byte('s',0);
  3448.           LCD_Cursor_Position(2);
  3449.           LCD_Send_Byte(' ',0);
  3450.           LCD_Cursor_Position(2);
  3451.           LCD_Send_Byte(' ',0);
  3452.           LCD_Cursor_Position(2);
  3453.           LCD_Send_Byte(' ',0);
  3454.      } else if (MsgType == 2) {
  3455.           LCD_Cursor_Position(1);
  3456.           LCD_Send_Byte(' ',0);
  3457.           LCD_Cursor_Position(1);
  3458.           LCD_Send_Byte(' ',0);
  3459.           LCD_Cursor_Position(1);
  3460.           LCD_Send_Byte(' ',0);
  3461.           LCD_Cursor_Position(1);
  3462.           LCD_Send_Byte('-',0);
  3463.           LCD_Cursor_Position(1);
  3464.           LCD_Send_Byte('-',0);
  3465.           LCD_Cursor_Position(1);
  3466.           LCD_Send_Byte('O',0);
  3467.           LCD_Cursor_Position(1);
  3468.           LCD_Send_Byte('P',0);
  3469.           LCD_Cursor_Position(1);
  3470.           LCD_Send_Byte('T',0);
  3471.           LCD_Cursor_Position(1);
  3472.           LCD_Send_Byte('I',0);
  3473.           LCD_Cursor_Position(1);
  3474.           LCD_Send_Byte('O',0);
  3475.           LCD_Cursor_Position(1);
  3476.           LCD_Send_Byte('N',0);
  3477.           LCD_Cursor_Position(1);
  3478.           LCD_Send_Byte('-',0);
  3479.           LCD_Cursor_Position(1);
  3480.           LCD_Send_Byte('-',0);
  3481.           LineOffset = 0x00;
  3482.           LCD_Cursor_Position(2);
  3483.           LCD_Send_Byte(' ',0);
  3484.           LCD_Cursor_Position(2);
  3485.           LCD_Send_Byte(' ',0);
  3486.           LCD_Cursor_Position(2);
  3487.           LCD_Send_Byte('E',0);
  3488.           LCD_Cursor_Position(2);
  3489.           LCD_Send_Byte('d',0);
  3490.           LCD_Cursor_Position(2);
  3491.           LCD_Send_Byte('i',0);
  3492.           LCD_Cursor_Position(2);
  3493.           LCD_Send_Byte('t',0);
  3494.           LCD_Cursor_Position(2);
  3495.           LCD_Send_Byte(' ',0);
  3496.           LCD_Cursor_Position(2);
  3497.           LCD_Send_Byte('T',0);
  3498.           LCD_Cursor_Position(2);
  3499.           LCD_Send_Byte('i',0);
  3500.           LCD_Cursor_Position(2);
  3501.           LCD_Send_Byte('m',0);
  3502.           LCD_Cursor_Position(2);
  3503.           LCD_Send_Byte('e',0);
  3504.           LCD_Cursor_Position(2);
  3505.           LCD_Send_Byte('r',0);
  3506.           LCD_Cursor_Position(2);
  3507.           LCD_Send_Byte(' ',0);
  3508.           LCD_Cursor_Position(2);
  3509.           LCD_Send_Byte(' ',0);
  3510.           LCD_Cursor_Position(2);
  3511.           LCD_Send_Byte(' ',0);
  3512.           LCD_Cursor_Position(2);
  3513.           LCD_Send_Byte(' ',0);
  3514.      } else if (MsgType == 3) {
  3515.           LCD_Cursor_Position(1);
  3516.           LCD_Send_Byte(' ',0);
  3517.           LCD_Cursor_Position(1);
  3518.           LCD_Send_Byte(' ',0);
  3519.           LCD_Cursor_Position(1);
  3520.           LCD_Send_Byte(' ',0);
  3521.           LCD_Cursor_Position(1);
  3522.           LCD_Send_Byte('-',0);
  3523.           LCD_Cursor_Position(1);
  3524.           LCD_Send_Byte('-',0);
  3525.           LCD_Cursor_Position(1);
  3526.           LCD_Send_Byte('O',0);
  3527.           LCD_Cursor_Position(1);
  3528.           LCD_Send_Byte('P',0);
  3529.           LCD_Cursor_Position(1);
  3530.           LCD_Send_Byte('T',0);
  3531.           LCD_Cursor_Position(1);
  3532.           LCD_Send_Byte('I',0);
  3533.           LCD_Cursor_Position(1);
  3534.           LCD_Send_Byte('O',0);
  3535.           LCD_Cursor_Position(1);
  3536.           LCD_Send_Byte('N',0);
  3537.           LCD_Cursor_Position(1);
  3538.           LCD_Send_Byte('-',0);
  3539.           LCD_Cursor_Position(1);
  3540.           LCD_Send_Byte('-',0);
  3541.           LineOffset = 0x00;
  3542.           LCD_Cursor_Position(2);
  3543.           LCD_Send_Byte(' ',0);
  3544.           LCD_Cursor_Position(2);
  3545.           LCD_Send_Byte(' ',0);
  3546.           LCD_Cursor_Position(2);
  3547.           LCD_Send_Byte('L',0);
  3548.           LCD_Cursor_Position(2);
  3549.           LCD_Send_Byte('o',0);
  3550.           LCD_Cursor_Position(2);
  3551.           LCD_Send_Byte('a',0);
  3552.           LCD_Cursor_Position(2);
  3553.           LCD_Send_Byte('d',0);
  3554.           LCD_Cursor_Position(2);
  3555.           LCD_Send_Byte(' ',0);
  3556.           LCD_Cursor_Position(2);
  3557.           LCD_Send_Byte('G',0);
  3558.           LCD_Cursor_Position(2);
  3559.           LCD_Send_Byte('a',0);
  3560.           LCD_Cursor_Position(2);
  3561.           LCD_Send_Byte('m',0);
  3562.           LCD_Cursor_Position(2);
  3563.           LCD_Send_Byte('e',0);
  3564.           LCD_Cursor_Position(2);
  3565.           LCD_Send_Byte(' ',0);
  3566.           LCD_Cursor_Position(2);
  3567.           LCD_Send_Byte(' ',0);
  3568.           LCD_Cursor_Position(2);
  3569.           LCD_Send_Byte(' ',0);
  3570.           LCD_Cursor_Position(2);
  3571.           LCD_Send_Byte(' ',0);
  3572.           LCD_Cursor_Position(2);
  3573.           LCD_Send_Byte(' ',0);
  3574.      } else if (MsgType == 4) {
  3575.           LCD_Cursor_Position(1);
  3576.           LCD_Send_Byte(' ',0);
  3577.           LCD_Cursor_Position(1);
  3578.           LCD_Send_Byte(' ',0);
  3579.           LCD_Cursor_Position(1);
  3580.           LCD_Send_Byte(' ',0);
  3581.           LCD_Cursor_Position(1);
  3582.           LCD_Send_Byte('-',0);
  3583.           LCD_Cursor_Position(1);
  3584.           LCD_Send_Byte('-',0);
  3585.           LCD_Cursor_Position(1);
  3586.           LCD_Send_Byte('O',0);
  3587.           LCD_Cursor_Position(1);
  3588.           LCD_Send_Byte('P',0);
  3589.           LCD_Cursor_Position(1);
  3590.           LCD_Send_Byte('T',0);
  3591.           LCD_Cursor_Position(1);
  3592.           LCD_Send_Byte('I',0);
  3593.           LCD_Cursor_Position(1);
  3594.           LCD_Send_Byte('O',0);
  3595.           LCD_Cursor_Position(1);
  3596.           LCD_Send_Byte('N',0);
  3597.           LCD_Cursor_Position(1);
  3598.           LCD_Send_Byte('-',0);
  3599.           LCD_Cursor_Position(1);
  3600.           LCD_Send_Byte('-',0);
  3601.           LineOffset = 0x00;
  3602.           LCD_Cursor_Position(2);
  3603.           LCD_Send_Byte(0x7F,0);
  3604.           LCD_Cursor_Position(2);
  3605.           LCD_Send_Byte(' ',0);
  3606.           LCD_Cursor_Position(2);
  3607.           LCD_Send_Byte('S',0);
  3608.           LCD_Cursor_Position(2);
  3609.           LCD_Send_Byte('a',0);
  3610.           LCD_Cursor_Position(2);
  3611.           LCD_Send_Byte('v',0);
  3612.           LCD_Cursor_Position(2);
  3613.           LCD_Send_Byte('e',0);
  3614.           LCD_Cursor_Position(2);
  3615.           LCD_Send_Byte(' ',0);
  3616.           LCD_Cursor_Position(2);
  3617.           LCD_Send_Byte('G',0);
  3618.           LCD_Cursor_Position(2);
  3619.           LCD_Send_Byte('a',0);
  3620.           LCD_Cursor_Position(2);
  3621.           LCD_Send_Byte('m',0);
  3622.           LCD_Cursor_Position(2);
  3623.           LCD_Send_Byte('e',0);
  3624.           LCD_Cursor_Position(2);
  3625.           LCD_Send_Byte(' ',0);
  3626.           LCD_Cursor_Position(2);
  3627.           LCD_Send_Byte(' ',0);
  3628.           LCD_Cursor_Position(2);
  3629.           LCD_Send_Byte(' ',0);
  3630.           LCD_Cursor_Position(2);
  3631.           LCD_Send_Byte(' ',0);
  3632.           LCD_Cursor_Position(2);
  3633.           LCD_Send_Byte(' ',0);
  3634.      } else if (MsgType == 5) {
  3635.           LCD_Cursor_Position(1);
  3636.           LCD_Send_Byte('P',0);
  3637.           LCD_Cursor_Position(1);
  3638.           LCD_Send_Byte('u',0);
  3639.           LCD_Cursor_Position(1);
  3640.           LCD_Send_Byte('t',0);
  3641.           LCD_Cursor_Position(1);
  3642.           LCD_Send_Byte(' ',0);
  3643.           LCD_Cursor_Position(1);
  3644.           LCD_Send_Byte('E',0);
  3645.           LCD_Cursor_Position(1);
  3646.           LCD_Send_Byte('v',0);
  3647.           LCD_Cursor_Position(1);
  3648.           LCD_Send_Byte('e',0);
  3649.           LCD_Cursor_Position(1);
  3650.           LCD_Send_Byte('r',0);
  3651.           LCD_Cursor_Position(1);
  3652.           LCD_Send_Byte('y',0);
  3653.           LCD_Cursor_Position(1);
  3654.           LCD_Send_Byte(' ',0);
  3655.           LCD_Cursor_Position(1);
  3656.           LCD_Send_Byte(' ',0);
  3657.           LCD_Cursor_Position(1);
  3658.           LCD_Send_Byte(' ',0);
  3659.           LCD_Cursor_Position(1);
  3660.           LCD_Send_Byte(' ',0);
  3661.           LineOffset = 0x00;
  3662.           LCD_Cursor_Position(2);
  3663.           LCD_Send_Byte(0x7F,0);
  3664.           LCD_Cursor_Position(2);
  3665.           LCD_Send_Byte('T',0);
  3666.           LCD_Cursor_Position(2);
  3667.           LCD_Send_Byte('h',0);
  3668.           LCD_Cursor_Position(2);
  3669.           LCD_Send_Byte('i',0);
  3670.           LCD_Cursor_Position(2);
  3671.           LCD_Send_Byte('n',0);
  3672.           LCD_Cursor_Position(2);
  3673.           LCD_Send_Byte('g',0);
  3674.           LCD_Cursor_Position(2);
  3675.           LCD_Send_Byte(' ',0);
  3676.           LCD_Cursor_Position(2);
  3677.           LCD_Send_Byte('B',0);
  3678.           LCD_Cursor_Position(2);
  3679.           LCD_Send_Byte('a',0);
  3680.           LCD_Cursor_Position(2);
  3681.           LCD_Send_Byte('c',0);
  3682.           LCD_Cursor_Position(2);
  3683.           LCD_Send_Byte('k',0);
  3684.           LCD_Cursor_Position(2);
  3685.           LCD_Send_Byte('!',0);
  3686.           LCD_Cursor_Position(2);
  3687.           LCD_Send_Byte(' ',0);
  3688.           LCD_Cursor_Position(2);
  3689.           LCD_Send_Byte(' ',0);
  3690.           LCD_Cursor_Position(2);
  3691.           LCD_Send_Byte(' ',0);
  3692.           LCD_Cursor_Position(2);
  3693.           LCD_Send_Byte(' ',0);
  3694.      } else if (MsgType == 6) {
  3695.           LCD_Cursor_Position(1);
  3696.           LCD_Send_Byte('S',0);
  3697.           LCD_Cursor_Position(1);
  3698.           LCD_Send_Byte('e',0);
  3699.           LCD_Cursor_Position(1);
  3700.           LCD_Send_Byte('t',0);
  3701.           LCD_Cursor_Position(1);
  3702.           LCD_Send_Byte(' ',0);
  3703.           LCD_Cursor_Position(1);
  3704.           LCD_Send_Byte('d',0);
  3705.           LCD_Cursor_Position(1);
  3706.           LCD_Send_Byte('o',0);
  3707.           LCD_Cursor_Position(1);
  3708.           LCD_Send_Byte('w',0);
  3709.           LCD_Cursor_Position(1);
  3710.           LCD_Send_Byte('n',0);
  3711.           LCD_Cursor_Position(1);
  3712.           LCD_Send_Byte(' ',0);
  3713.           LCD_Cursor_Position(1);
  3714.           LCD_Send_Byte('o',0);
  3715.           LCD_Cursor_Position(1);
  3716.           LCD_Send_Byte('r',0);
  3717.           LCD_Cursor_Position(1);
  3718.           LCD_Send_Byte(' ',0);
  3719.           LCD_Cursor_Position(1);
  3720.           LCD_Send_Byte(' ',0);
  3721.           LineOffset = 0x00;
  3722.           LCD_Cursor_Position(2);
  3723.           LCD_Send_Byte(0x7F,0);
  3724.           LCD_Cursor_Position(2);
  3725.           LCD_Send_Byte('p',0);
  3726.           LCD_Cursor_Position(2);
  3727.           LCD_Send_Byte('i',0);
  3728.           LCD_Cursor_Position(2);
  3729.           LCD_Send_Byte('c',0);
  3730.           LCD_Cursor_Position(2);
  3731.           LCD_Send_Byte('k',0);
  3732.           LCD_Cursor_Position(2);
  3733.           LCD_Send_Byte('.',0);
  3734.           LCD_Cursor_Position(2);
  3735.           LCD_Send_Byte('u',0);
  3736.           LCD_Cursor_Position(2);
  3737.           LCD_Send_Byte('p',0);
  3738.           LCD_Cursor_Position(2);
  3739.           LCD_Send_Byte(' ',0);
  3740.           LCD_Cursor_Position(2);
  3741.           LCD_Send_Byte('o',0);
  3742.           LCD_Cursor_Position(2);
  3743.           LCD_Send_Byte('t',0);
  3744.           LCD_Cursor_Position(2);
  3745.           LCD_Send_Byte('h',0);
  3746.           LCD_Cursor_Position(2);
  3747.           LCD_Send_Byte('e',0);
  3748.           LCD_Cursor_Position(2);
  3749.           LCD_Send_Byte('r',0);
  3750.           LCD_Cursor_Position(2);
  3751.           LCD_Send_Byte('.',0);
  3752.           LCD_Cursor_Position(2);
  3753.           LCD_Send_Byte(' ',0);
  3754.    
  3755.      }
  3756.  
  3757. }
  3758.  
  3759.  
  3760. /*********************************************************************************
  3761. * Interrupt Functions
  3762. **********************************************************************************
  3763. **********************************************************************************
  3764. * Name:        RTI_ISR
  3765. * Description: Interrupt at 2.048ms intervals.  Checks pushbutton states.
  3766. * [->]    (*): (*)
  3767. * [<-] (void): [void]
  3768. *********************************************************************************/
  3769. interrupt 7 void RTI_ISR(void)
  3770. {
  3771.      CRGFLG = CRGFLG | 0x80; //Enable
  3772.  
  3773.      //debug pushbuttons on docking board
  3774.      /*if ((TOP_BTN == 0) && (new_TOP_BTN == 0)) {
  3775.           new_TOP_BTN = 1;
  3776.      }
  3777.      if ((BTM_BTN == 0) && (new_BTM_BTN == 0)) {
  3778.           new_BTM_BTN = 1;
  3779.      }*/
  3780.  
  3781.      if((RPG_BTN == 1) && (prevRPGbutton ==0)) {
  3782.         newRPGbutton = 1;
  3783.      }
  3784.      prevRPGbutton = RPG_BTN;
  3785.  
  3786.      if(RPG_A == 1) {
  3787.                 RPGRightTurns++;
  3788.  
  3789.                 if(RPGLeftTurns > 3) {
  3790.                 RPGLeft = 1;
  3791.                 }
  3792.  
  3793.                 RPGLeftTurns = 0;
  3794.      }
  3795.  
  3796.  
  3797.         if(RPG_B == 1) {
  3798.                 RPGLeftTurns++;
  3799.  
  3800.                 if(RPGRightTurns > 3) {
  3801.                 RPGRight = 1;
  3802.                 }
  3803.  
  3804.                 RPGRightTurns = 0;
  3805.     }
  3806. }
  3807.  
  3808. /**********************************************************************************
  3809. * Name:        TIM_ISR
  3810. * Description: TIM RTI at 10ms for 100 packets.  Updates timer.
  3811. * [->]    (*): (*)
  3812. * [<-] (void): [void]
  3813. **********************************************************************************/
  3814. interrupt 15 void TIM_ISR( void)
  3815. {
  3816.         TFLG1 = TFLG1 | 0x80; //Enable
  3817.  
  3818.         TimerCounter++; //Another 10ms have passed
  3819.        
  3820.        if(TimerCounter % 10 == 0){
  3821.             LED_Convert(); //Set up LED backlighting
  3822.             enable = 1;
  3823.             LED_Out();
  3824.             enable = 0;
  3825.             RefreshHall = 1;
  3826.        }
  3827.        
  3828.      
  3829.      
  3830.        if (noteflag == 1) {
  3831.           notecnt++;
  3832.            //if (notecnt % 10 == 1) {
  3833.                PWMDTY0 = duty1;
  3834.                PWMPER0 = period1;
  3835.            //} else if (notecnt % 10 == 0) {
  3836.            //    PWMDTY0 = duty2;
  3837.            //    PWMPER0 = period2;
  3838.            //}
  3839.          
  3840.           if (notecnt == notedur) {
  3841.               notecnt = 0;
  3842.               noteflag = 0;
  3843.           }
  3844.         }
  3845.      
  3846.       if(TimerRunning == 1){
  3847.         if (TimerCounter >= 100) { //100 10ms interrupts accumulated
  3848.              //enable2 = 1;
  3849.              //HALL_Input();
  3850.              //enable2 = 0;
  3851.  
  3852.          
  3853.          
  3854.              if (PlayerToggle == 0) { //player is white
  3855.                   if (P1secs == 0) { //XX:00 is white timer
  3856.                        P1mins--;
  3857.                        P1secs = 59;
  3858.  
  3859.                        if(P1mins <= 0 && P1secs <= 0) { //timer lock at 00:00
  3860.                                 P1mins = 0;
  3861.                                 P1secs = 0;
  3862.                                 TimerExpired = 1;
  3863.                                 TimerRunning = 0;
  3864.                                 TIE_C7I = 0;
  3865.                        }
  3866.                   } else { //XX:XX is white timer
  3867.                        P1secs--;
  3868.                   }
  3869.              } else { //player is black
  3870.                   if (P2secs == 0) { //XX:00 is black timer
  3871.                        P2mins--;
  3872.                        P2secs = 59;
  3873.  
  3874.                        if(P2mins <= 0 && P2secs <= 0) { //timer lock at 00:00
  3875.                             P2mins = 0;
  3876.                             P2secs = 0;
  3877.                             TimerExpired = 1;
  3878.                             TimerRunning = 0;
  3879.                             TIE_C7I = 0;
  3880.                        }
  3881.                   } else { //XX:XX is black timer
  3882.                        P2secs--;
  3883.                   }
  3884.              }
  3885.              TimerRefresh = 1;
  3886.              TimerCounter = 0;
  3887.         }
  3888.       }
  3889.         /*if(scicountflag == 1){
  3890.           scicount += 1;
  3891.           if(scicount == 200){
  3892.             //10 seconds have passed
  3893.             scicount = 0;
  3894.             SCICR2 = SCICR2 | 0x80;
  3895.             scicountflag = 0;
  3896.           }
  3897.         }*/
  3898.        
  3899.         if(inputonflag == 1 && (SCISR1 & 0x20)){
  3900.             inputchar = SCIDRL;
  3901.             echoflag = 1;
  3902.             tdata = 0;
  3903.             tin = tout;
  3904.             addchar(inputchar);
  3905.             SCICR2 = SCICR2 | 0x80;      
  3906.         }
  3907. }
  3908.  
  3909.  
  3910.  
  3911. /*********************************************************************************
  3912. * Sound Generation Functions
  3913. **********************************************************************************
  3914. **********************************************************************************
  3915. * Name: Play_Sound
  3916. * Description: Generates sounds based on what is current required.
  3917. * [->] (int): Mode: The type of sound that will be output.
  3918. *                   0 = "Error" sound (buzz)
  3919. *                   1 = "White's turn" sound (boop beep)
  3920. *                   2 = "Black's turn" sound (beep boop)
  3921. *                   3 = "Mystery" sound (whoop)
  3922. * [<-] (void): [void]
  3923. *********************************************************************************/
  3924. void Play_Sound(int mode) {
  3925.      unsigned char a = 0; //Looping variables
  3926.      unsigned char b = 0;
  3927.      unsigned char c = 0;
  3928.      unsigned char d = 0;
  3929.      unsigned char e = 0;
  3930.  
  3931.      if (mode == 0) {
  3932.           for (a = 0; a <= 4; a++) {
  3933.                         for (b = 0xFF; b  >= 0x01; b--) {
  3934.                                 PWMPER0 = 0xFF;
  3935.  
  3936.                                 for (c = 0x00; c <= 0xFE; c++) {
  3937.                                         PWMDTY0 = c;
  3938.  
  3939.                                         for (d = 0; d < 1; d++) {
  3940.                                                 for (e = 0; e < 1; e++) {
  3941.                                                 }
  3942.                                         }
  3943.                                 }
  3944.  
  3945.                                 for (c = 0xFF; c >= 0x01; c--) {
  3946.                                         PWMDTY0 = c;
  3947.                                         for (d = 0; d < 1; d++) {
  3948.                                                 for (e = 0; e < 1; e++) {
  3949.                                                 }
  3950.                                         }
  3951.                                 }
  3952.                         }
  3953.                         for (d = 0; d < 1; d++) {
  3954.                                 for (e = 0; e < 100; e++) {
  3955.                                 }
  3956.                         }
  3957.                }
  3958.           PWMDTY0 = 0x00;
  3959.  
  3960.      } else if (mode == 1) {
  3961.           PWMDTY0 = 175;
  3962.  
  3963.  
  3964.           for (a = 0; a < 0xFF; a++) {
  3965.                for (b = 0; b < 0xFF; b++) {
  3966.                     for (c = 0; c < 0x0F; c++) {
  3967.                        //do nothing
  3968.                     }
  3969.                }
  3970.           }
  3971.  
  3972.           PWMDTY0 = 225;
  3973.  
  3974.           for (a = 0; a < 0xFF; a++) {
  3975.                for (b = 0; b < 0xFF; b++) {
  3976.                     for (c = 0; c < 0x0F; c++) {
  3977.                        //do nothing
  3978.                     }
  3979.                }
  3980.           }
  3981.  
  3982.           PWMDTY0 = 0x00;
  3983.  
  3984.      } else if (mode == 2) {
  3985.           PWMPER0 = 0xFF;
  3986.           PWMDTY0 = 225;
  3987.  
  3988.  
  3989.           for (a = 0; a < 0xFF; a++) {
  3990.                for (b = 0; b < 0xFF; b++) {
  3991.                     for (c = 0; c < 0x0F; c++) {
  3992.                        //do nothing
  3993.                     }
  3994.                }
  3995.           }
  3996.  
  3997.           PWMDTY0 = 175;
  3998.  
  3999.           for (a = 0; a < 0xFF; a++) {
  4000.                for (b = 0; b < 0xFF; b++) {
  4001.                     for (c = 0; c < 0x0F; c++) {
  4002.                        //do nothing
  4003.                     }
  4004.                }
  4005.           }
  4006.  
  4007.           PWMDTY0 = 0x00;
  4008.      } else if (mode == 3) {
  4009.           for (a = 0; a < 0xFE; a++) {
  4010.                PWMDTY0 = a;
  4011.                for (b = 0; b < 0xFF; b++) {
  4012.                     for (c = 0; c < 0x0F; c++) {
  4013.                        //do nothing
  4014.                     }
  4015.                }
  4016.           }
  4017.           for (a = 0; a < 0xFE; a++) {
  4018.                PWMDTY0 = 0xFE - a;
  4019.  
  4020.                for (b = 0; b < 0xFF; b++) {
  4021.                     for (c = 0; c < 0x0F; c++) {
  4022.                        //do nothing
  4023.                     }
  4024.                }
  4025.           }
  4026.           PWMDTY0 = 0;
  4027.      }
  4028.  
  4029.      return;
  4030. }
  4031.  
  4032. /*********************************************************************************
  4033. * Name: Play_Music
  4034. * Description:
  4035. *********************************************************************************/
  4036.  
  4037. void Play_Music() {
  4038.      int div = 1;
  4039.      int note = 0;
  4040.      int temp = 0;
  4041.      int j = 0;
  4042.      int i = 0;
  4043.      int hey = 0;
  4044.    
  4045.      notedur = 25;
  4046.      notecnt = 0;
  4047.  
  4048.      PWMSCLA = 12;
  4049.      PWMDTY0 = 0x00;
  4050.      PWMPER0 = 0xFF;
  4051.  
  4052.      Song_Select();
  4053.     Song_Select2();
  4054.  
  4055.      for (i = 0; i < 8; i++) {  //song column loop
  4056.          for (j = 0; j < 8; j++) { //song row loop
  4057.  
  4058.                temp = hello[i][j];
  4059.                note = temp % 100;
  4060.                div = temp /100;
  4061.                div++;
  4062.              
  4063.  
  4064.                if (note == 0) {       //no note
  4065.                     duty1 = 0x00;
  4066.                     noteflag = 1;
  4067.                }
  4068.  
  4069.                else if (note == 1) {  //low C
  4070.                     period1 = 0xEF / div;
  4071.                     duty1 = period1 / 2;
  4072.                     noteflag = 1;
  4073.                }
  4074.  
  4075.                else if (note == 2) {  //D
  4076.                     period1 = 0xD5 / div;
  4077.                     duty1 = period1 / 2;
  4078.                     noteflag = 1;
  4079.                }
  4080.  
  4081.                else if (note == 3) {  //E
  4082.                     period1 = 0xBE / div;
  4083.                     duty1 = period1 / 2;
  4084.                     noteflag = 1;
  4085.                }
  4086.  
  4087.                else if (note == 4) {  //F
  4088.                     period1 = 0xB3 / div;
  4089.                     duty1 = period1 / 2;
  4090.                     noteflag = 1;
  4091.                }
  4092.  
  4093.                else if (note == 5) {  //G
  4094.                     period1 = 0xA0 / div;
  4095.                     duty1 = period1 / 2;
  4096.                     noteflag = 1;
  4097.                }
  4098.  
  4099.                else if (note == 6) {  //A
  4100.                     period1 = 0x8E / div;
  4101.                     duty1 = period1 / 2;
  4102.                     noteflag = 1;
  4103.                }
  4104.  
  4105.                else if (note == 7) {  //B
  4106.                     period1 = 0x7F / div;
  4107.                     duty1 = period1 / 2;
  4108.                     noteflag = 1;
  4109.                }
  4110.  
  4111.                else if (note == 8) {  //C sharp
  4112.                     period1 = 0xE6 / div;
  4113.                     duty1 = period1 / 2;
  4114.                     noteflag = 1;
  4115.                }
  4116.  
  4117.                else if (note == 9) {  //E flat
  4118.                     period1 = 0xC9 / div;
  4119.                     duty1 = period1 / 2;
  4120.                     noteflag = 1;
  4121.                }
  4122.  
  4123.                else if (note == 10) {  //F sharp
  4124.                     period1 = 0xA9 / div;
  4125.                     duty1 = period1 / 2;
  4126.                     noteflag = 1;
  4127.                }
  4128.  
  4129.                else if (note == 11) {  //A flat
  4130.                     period1 = 0x97 / div;
  4131.                     duty1 = period1 / 2;
  4132.                     noteflag = 1;
  4133.                }
  4134.  
  4135.                else if (note == 12) {  //B flat
  4136.                     period1 = 0x86 / div;
  4137.                     duty1 = period1 / 2;
  4138.                     noteflag = 1;
  4139.                }
  4140.            
  4141.             temp = hello2[i][j];
  4142.                note = temp % 100;
  4143.                div = temp /100;
  4144.                div++;
  4145.  
  4146.                if (note == 0) {       //no note
  4147.                     duty2 = 0x00;
  4148.                     noteflag = 1;
  4149.                }
  4150.  
  4151.                else if (note == 1) {  //low C
  4152.                     period2 = 0xEF / div;
  4153.                     duty2 = period2 / 2;
  4154.                     noteflag = 1;
  4155.                }
  4156.  
  4157.                else if (note == 2) {  //D
  4158.                     period2 = 0xD5 / div;
  4159.                     duty2 = period2 / 2;
  4160.                     noteflag = 1;
  4161.                }
  4162.  
  4163.                else if (note == 3) {  //E
  4164.                     period2 = 0xBE / div;
  4165.                     duty2 = period2 / 2;
  4166.                     noteflag = 1;
  4167.                }
  4168.  
  4169.                else if (note == 4) {  //F
  4170.                     period2 = 0xB3 / div;
  4171.                     duty2 = period2 / 2;
  4172.                     noteflag = 1;
  4173.                }
  4174.  
  4175.                else if (note == 5) {  //G
  4176.                     period2 = 0xA0 / div;
  4177.                     duty2 = period2 / 2;
  4178.                     noteflag = 1;
  4179.                }
  4180.  
  4181.                else if (note == 6) {  //A
  4182.                     period2 = 0x8E / div;
  4183.                     duty2 = period2 / 2;
  4184.                     noteflag = 1;
  4185.                }
  4186.  
  4187.                else if (note == 7) {  //B
  4188.                     period2 = 0x7F / div;
  4189.                     duty2 = period2 / 2;
  4190.                     noteflag = 1;
  4191.                }
  4192.  
  4193.                else if (note == 8) {  //C sharp
  4194.                     period2 = 0xE6 / div;
  4195.                     duty2 = period2 / 2;
  4196.                     noteflag = 1;
  4197.                }
  4198.  
  4199.                else if (note == 9) {  //E flat
  4200.                     period2 = 0xC9 / div;
  4201.                     duty2 = period2 / 2;
  4202.                     noteflag = 1;
  4203.                }
  4204.  
  4205.                else if (note == 10) {  //F sharp
  4206.                     period2 = 0xA9 / div;
  4207.                     duty2 = period2 / 2;
  4208.                     noteflag = 1;
  4209.                }
  4210.  
  4211.                else if (note == 11) {  //A flat
  4212.                     period2 = 0x97 / div;
  4213.                     duty2 = period2 / 2;
  4214.                     noteflag = 1;
  4215.                }
  4216.  
  4217.                else if (note == 12) {  //B flat
  4218.                     period2 = 0x86 / div;
  4219.                     duty2 = period2 / 2;
  4220.                     noteflag = 1;
  4221.                }
  4222.            
  4223.             while (noteflag == 1) {   // wait
  4224.                     }
  4225.              
  4226.          }
  4227.      }
  4228.  
  4229.      PWMSCLA = 2;
  4230.      PWMDTY0 = 0x00;
  4231.      PWMPER0 = 0xFF;
  4232. }
  4233.  
  4234. /*********************************************************************************
  4235. * Name: Song_Select
  4236. * Description:
  4237. *********************************************************************************/
  4238. void Song_Select(void) {
  4239.  
  4240.           hello[0][0] = 104;
  4241.           hello[0][1] = 109;
  4242.           hello[0][2] = 111;
  4243.           hello[0][3] = 111;
  4244.           hello[0][4] = 111;
  4245.           hello[0][5] = 111;
  4246.           hello[0][6] = 112;
  4247.           hello[0][7] = 112;
  4248.           hello[1][0] = 112;
  4249.           hello[1][1] = 112;
  4250.           hello[1][2] = 301;
  4251.           hello[1][3] = 301;
  4252.           hello[1][4] = 111;
  4253.           hello[1][5] = 111;
  4254.           hello[1][6] = 109;
  4255.           hello[1][7] = 109;
  4256.           hello[2][0] = 104;
  4257.           hello[2][1] = 109;
  4258.           hello[2][2] = 111;
  4259.           hello[2][3] = 111;
  4260.           hello[2][4] = 111;
  4261.           hello[2][5] = 111;
  4262.           hello[2][6] = 112;
  4263.           hello[2][7] = 112;
  4264.           hello[3][0] = 112;
  4265.           hello[3][1] = 112;
  4266.           hello[3][2] = 301;
  4267.           hello[3][3] = 301;
  4268.           hello[3][4] = 112;
  4269.           hello[3][5] = 112;
  4270.           hello[3][6] = 112;
  4271.           hello[3][7] = 112;
  4272.           hello[4][0] = 104;
  4273.           hello[4][1] = 109;
  4274.           hello[4][2] = 111;
  4275.           hello[4][3] = 111;
  4276.           hello[4][4] = 111;
  4277.           hello[4][5] = 111;
  4278.           hello[4][6] = 112;
  4279.           hello[4][7] = 112;
  4280.           hello[5][0] = 112;
  4281.           hello[5][1] = 112;
  4282.           hello[5][2] = 301;
  4283.           hello[5][3] = 301;
  4284.           hello[5][4] = 111;
  4285.           hello[5][5] = 111;
  4286.           hello[5][6] = 109;
  4287.           hello[5][7] = 109;
  4288.           hello[6][0] = 104;
  4289.           hello[6][1] = 109;
  4290.           hello[6][2] = 111;
  4291.           hello[6][3] = 111;
  4292.           hello[6][4] = 111;
  4293.           hello[6][5] = 111;
  4294.           hello[6][6] = 112;
  4295.           hello[6][7] = 112;
  4296.           hello[7][0] = 111;
  4297.           hello[7][1] = 111;
  4298.           hello[7][2] = 111;
  4299.           hello[7][3] = 111;
  4300.           hello[7][4] = 109;
  4301.           hello[7][5] = 109;
  4302.           hello[7][6] = 109;
  4303.           hello[7][7] = 109;
  4304.      return;
  4305. }
  4306.  
  4307.  
  4308. /*********************************************************************************
  4309. * Name: Song_Select2
  4310. * Description:
  4311. *********************************************************************************/
  4312. void Song_Select2(void) {
  4313.  
  4314.           hello2[0][0] = 8;
  4315.           hello2[0][1] = 8;
  4316.           hello2[0][2] = 8;
  4317.           hello2[0][3] = 8;
  4318.           hello2[0][4] = 4;
  4319.           hello2[0][5] = 4;
  4320.           hello2[0][6] = 4;
  4321.           hello2[0][7] = 4;
  4322.           hello2[1][0] = 1;
  4323.           hello2[1][1] = 1;
  4324.           hello2[1][2] = 1;
  4325.           hello2[1][3] = 1;
  4326.           hello2[1][4] = 4;
  4327.           hello2[1][5] = 4;
  4328.           hello2[1][6] = 4;
  4329.           hello2[1][7] = 4;
  4330.           hello2[2][0] = 8;
  4331.           hello2[2][1] = 8;
  4332.           hello2[2][2] = 8;
  4333.           hello2[2][3] = 8;
  4334.           hello2[2][4] = 4;
  4335.           hello2[2][5] = 4;
  4336.           hello2[2][6] = 4;
  4337.           hello2[2][7] = 4;
  4338.           hello2[3][0] = 8;
  4339.           hello2[3][1] = 8;
  4340.           hello2[3][2] = 8;
  4341.           hello2[3][3] = 8;
  4342.           hello2[3][4] = 4;
  4343.           hello2[3][5] = 4;
  4344.           hello2[3][6] = 4;
  4345.           hello2[3][7] = 4;
  4346.           hello2[4][0] = 1;
  4347.           hello2[4][1] = 1;
  4348.           hello2[4][2] = 1;
  4349.           hello2[4][3] = 1;
  4350.           hello2[4][4] = 4;
  4351.           hello2[4][5] = 4;
  4352.           hello2[4][6] = 4;
  4353.           hello2[4][7] = 4;
  4354.           hello2[5][0] = 1;
  4355.           hello2[5][1] = 1;
  4356.           hello2[5][2] = 1;
  4357.           hello2[5][3] = 1;
  4358.           hello2[5][4] = 4;
  4359.           hello2[5][5] = 4;
  4360.           hello2[5][6] = 4;
  4361.           hello2[5][7] = 4;
  4362.           hello2[6][0] = 1;
  4363.           hello2[6][1] = 1;
  4364.           hello2[6][2] = 1;
  4365.           hello2[6][3] = 1;
  4366.           hello2[6][4] = 8;
  4367.           hello2[6][5] = 8;
  4368.           hello2[6][6] = 8;
  4369.           hello2[6][7] = 8;
  4370.           hello2[7][0] = 11;
  4371.           hello2[7][1] = 11;
  4372.           hello2[7][2] = 11;
  4373.           hello2[7][3] = 11;
  4374.           hello2[7][4] = 5;
  4375.           hello2[7][5] = 5;
  4376.           hello2[7][6] = 5;
  4377.           hello2[7][7] = 5;
  4378.      return;
  4379. }
  4380.  
  4381.  
  4382. /*********************************************************************************
  4383. * Character I/O Library Routines
  4384. **********************************************************************************
  4385. **********************************************************************************
  4386. * Name:        inchar
  4387. * Description: Inputs ASCII character from SCI serial port and returns it.
  4388. * [->] (void): [void]
  4389. * [<-] (char): ASCII value from Teraterm .
  4390. *********************************************************************************/
  4391. char  inchar(void) {
  4392.   /* receives character from the terminal channel */
  4393.         while (!(SCISR1 & 0x20)); /* wait for input */
  4394.     return SCIDRL;
  4395. }
  4396.  
  4397. /*********************************************************************************
  4398. * Name:        outchar
  4399. * Description: Outputs ASCII character to the SCI serial port.
  4400. * [->] (char): ASCII value.
  4401. * [<-] (void): [void]
  4402. *********************************************************************************/
  4403. void outchar(char ch) {
  4404.   /* sends a character to the terminal channel */
  4405.     while (!(SCISR1 & 0x80));  /* wait for output buffer empty */
  4406.     SCIDRL = ch;
  4407. }
  4408.  
  4409. /**********************************************************************************
  4410. * Name:        bco
  4411. * Description: Outputs character data in radial buffer to serial.
  4412. * [->]    (*): (*)
  4413. * [<-] (void): [void]
  4414. **********************************************************************************/
  4415. void bco(char input)
  4416. {
  4417.      while(((tin + 1) % tsize) == tout) {}
  4418.      tbuf[tin] = input;
  4419.      tin = (tin + 1) % tsize;
  4420.  
  4421.      SCICR2 = SCICR2 | 0x80;
  4422. }
  4423.  
  4424. /*********************************************************************************
  4425. * ECE 362 - Mini-Project C Source File - Fall 2012
  4426. *********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment