CodeCodeCode

code that works

Dec 9th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 93.57 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. * Name: Micheal Standiford Signature:________________________________
  13. * Name: Mark Sears Signature:________________________________
  14. * Name: That other guy Signature:________________________________
  15. *
  16. * Team Positions:
  17. *
  18. * Team/Doc Leader: < Mark Luzarowski > Signature: ______________________
  19. *
  20. * Software Leader: < Mark Luzarowski > Signature: ______________________
  21. *
  22. * Interface Leader: < Micheal Standiford > Signature: ______________________
  23. *
  24. * Peripheral Leader: < Micheal Standiford > Signature: ______________________
  25. *
  26. *
  27. * Academic Honesty Statement: In signing above, we hereby certify that we
  28. * are the individuals who created this HC(S)12 source file and that we have
  29. * not copied the work of any other student (past or present) while completing
  30. * it. We understand that if we fail to honor this agreement, we will receive
  31. * a grade of ZERO and be subject to possible disciplinary action.
  32. *
  33. **********************************************************************************
  34. * Project Purpose
  35. **********************************************************************************
  36. * The objective of this Mini-Project is to create a game board the can as a multi-
  37. * game base; games planned so far are chess and checkers. The board itself will
  38. * function as a "learning board" of sorts for any game programmed into it.
  39. *
  40. **********************************************************************************
  41. * Project-specific Success Criteria
  42. **********************************************************************************
  43. * 1. Piece tracking & movement analysis
  44. *
  45. * 2. Time & turn tracking
  46. *
  47. * 3. User-side game editing
  48. *
  49. **********************************************************************************
  50. * Change Log
  51. **********************************************************************************
  52. * Date code started: November 13, 2012
  53. *
  54. * Update history:
  55. *
  56. * Date: November 13, 2012
  57. * Name: Mark Luzarowski
  58. * Update: Added generalized functions for LCD communications.
  59. * Added initializations skeleton.
  60. * -------------------------------------------------------------------------------
  61. * Date:
  62. * Name:
  63. * Update:
  64. *
  65. ***********************************************************************************
  66. **********************************************************************************/
  67. #include <hidef.h> // common defines and macros
  68. #include "derivative.h" // derivative-specific definitions
  69. #include <mc9s12c32.h> // 9S12 register & port defines
  70.  
  71. //Dictionary defines
  72. #define LCD_RS PTT_PTT2
  73. #define LCD_CLK PTT_PTT4
  74. #define RPG_A PTT_PTT5
  75. #define RPG_B PTT_PTT6
  76. #define RPG_BTN PTT_PTT7
  77. #define enable PTAD_PTAD7
  78. #define enable2 PTAD_PTAD6
  79. #define output PTM_PTM3
  80. #define output2 PTM_PTM2
  81. #define output3 PTM_PTM1
  82. #define output4 PTM_PTM0
  83. #define output5 PTT_PTT3
  84.  
  85. //All funtions after main should be initialized here
  86. char inchar(void);
  87. void outchar(char);
  88. void LCD_Delay(int);
  89. void LCD_Cursor_Position(int);
  90. void LCD_Send_Byte(char, int);
  91. void LCD_Display(int Msgtype);
  92. void bco(char);
  93. void LCD_Timer(void);
  94. void LED_Out(void);
  95. void LED_Convert(void);
  96. void Chess_Locations(int);
  97. void LCD_Piece_Select(int);
  98. void Play_Sound(int);
  99. void HALL_Input(void);
  100. void LED_Test(void);
  101.  
  102. //James' Functions
  103. //dec 1st
  104. void currpccnt(void);
  105. void pickedup(int row, int col, int piece);
  106. void pawnled(int row,int col,int team,int mode);
  107. void bishopled(int row,int col,int team);
  108. void rookled(int row,int col, int team);
  109. void knightled(int row,int col,int team);
  110. int bcheck(int row,int col);
  111. void ledcopy(int mode);
  112. void queenled(int row,int col,int team);
  113. void ischeck(int team);
  114. int blackcheckflag = 0;
  115. int whitecheckflag = 0;
  116. void kingledout(int row,int col,int team);
  117. void kingled(int row,int col,int team);
  118. void ledcopy(int mode);
  119. void checkmatrix(int team);
  120. //dec 3rd
  121. void scanline(void);\
  122. void movecnt(void);
  123. int nummoves = 0;
  124. //void bco(void);
  125. void addchar(char a);
  126. char CR = 0x0D;//Return
  127. char LF = 0x0A; //line feed
  128. void sevenchar(int mode);
  129. void sci_outline(int mode);
  130. void sci_datafetch(int mode, int line);
  131. void outtype(int type);
  132. void outteam(int team);
  133. void nowpccnt(void);
  134. void hpccnt(void);
  135.  
  136. // James' variables
  137. //dec 1st
  138. int nowpc; //how many pieces are currently on the board
  139. int currpc; //how many pieces are currently in the game (not taken yet)
  140. int switchflag = 0; //flag that determines whether a switch is being currently made
  141. int ldata[8][8];
  142. int ltemp[8][8];
  143. int hlatch[8][8];
  144. //dec 3rd
  145. int sciset = 0;
  146. int boardset = 0;
  147. int sciline = 0;
  148. int boardline = 0;
  149. int scioff_flag = 0;
  150. //dec 5th
  151. int scicountflag = 0;
  152. int scicount = 0;
  153. void sciscan(void);
  154. //dec 6th
  155. int movenumber = 2;
  156. int whitescore = 0;
  157. int blackscore = 0;
  158. int scoreline = 0;
  159. int ledoutmode = 0;
  160. unsigned char MOVEOutData[8];
  161. int uprow; //variables for keeping track of pieces
  162. int upcol;
  163. int downrow;
  164. int downcol;
  165. int nextrow;
  166. int nextcol;
  167. int sameflag;
  168. int pickedupflag = 0;
  169. int hpc = 0;
  170. int cndiff = 0;
  171. int hndiff = 0;
  172. int rtarget;
  173. int ctarget;
  174. int nowpclatch;
  175. void hallvlatch(void);
  176. void hallvchess(void);
  177. void setdown(int row, int col, int piece);
  178. void puteverythingback(void);
  179.  
  180. //Global Declarations Begin Here
  181. //------------------------------------
  182. //------------------------------------
  183. //SCI Variables
  184. int tin = 0; //SCI transmit display buffer IN pointer
  185. int tout = 0; //SCI transmit display buffer OUT pointer
  186. int tsize = 70; //SCI size of transmit buffer
  187. char tbuf[70]; //SCI transmit display buffer
  188. int tdata; // how much data is in the buffer
  189. //TIM Variables
  190. int P1mins = 20; //White player minutes remaining on chess clock
  191. int P1secs = 0; //White player seconds remaining on chess clock
  192. int P2mins = 20; //Black player minutes remaining on chess clock
  193. int P2secs = 0; //Black player seconds remaining on chess clock
  194. int TimerCounter = 0; //Chess timer 10ms collector
  195. int timeExpired = 0; //Timer has reached 00:00 for either player
  196. //LCD Variables
  197. char LineOffset = 0x00; //In use to calculate position of LCD write cursor
  198. //RPG Variables
  199. int RPGRightTurns = 0; //Number of pulses for right detected
  200. int RPGLeftTurns = 0; //Number of pulses for left detected
  201. int RPGRight = 0; //Movement to the right detected
  202. int RPGLeft = 0; //Movement to the left detected
  203. int prevRPGbutton = 0; //Status of RPG button (previous)
  204. int newRPGbutton = 0; //Status of RPG button (current)
  205. //Menu Variables
  206. int OutOfSplashScreen = 0; //Sees if the game has left the welcome splash
  207. //0 = Still on splash screen
  208. //1 = Left the splash screen
  209. int MenuPosition = 1; //Current Screen of the menu
  210. //0 = Game Start
  211. //1 = Edit Timer
  212. int TimerEditing = 0; //Are we editing the timer?
  213. //Gameplay (human) Variables
  214. int PlayerToggle = 0; //The current player toggle
  215. //0 = White
  216. //1 = Black
  217. int TimerRunning = 0; //Sees if the timer is up
  218. int TimerExpired = 0; //Sees if the timer has expired for either player
  219. int TimerRefresh = 0; //Refresh the timer on the LCD
  220. int GameRunning = 0; //Sees if game is currently running
  221.  
  222. int HallMatrix[8][8]; //Magnet sensor input matrix
  223. int ChessMatrix[8][8];//Chess piece memory map
  224. unsigned char LEDOutData[8]; //Set of LED shiftout variables; one for each row of LEDs.
  225. int en1 = 0;
  226. int en2 = 0;
  227. int RefreshHall = 0;
  228.  
  229.  
  230. //------------------------------------
  231. //------------------------------------
  232.  
  233. //ASCII character constants
  234. char ASCII_CR = 0x0D; //Return
  235. char ASCII_LF = 0x0A; //Linefeed
  236.  
  237. //LCD specific constants
  238. char LCD_LINE1 = 0x80; //LCD line #1
  239. char LCD_LINE2 = 0xC0; //LCD line #2
  240. char LCD_ON = 0x0F; //LCD turn on instruction
  241. char LCD_CLEAR = 0x01; //LCD clear instruction
  242. char LCD_2LINE = 0x38; //LCD use both lines instruction
  243. char LCD_CURSOR = 0xFE; //LCD cursor move instruction
  244.  
  245. /*********************************************************************************
  246. Initializations
  247. **********************************************************************************/
  248. void initializations(void) {
  249.  
  250.  
  251. // Set the PLL speed (bus clock = 24 MHz)
  252. CLKSEL = CLKSEL & 0x80; // disengage PLL from system
  253. PLLCTL = PLLCTL | 0x40; // turn on PLL
  254. SYNR = 0x02; // set PLL multiplier
  255. REFDV = 0; // set PLL divider
  256. while (!(CRGFLG & 0x08)){ }
  257. CLKSEL = CLKSEL | 0x80; // engage PLL
  258.  
  259. // Disable watchdog timer (COPCTL register)
  260. COPCTL = 0x40; // COP off; RTI and COP stopped in BDM-mode
  261.  
  262. // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  263. SCIBDH = 0x00; // set baud rate to 9600
  264. SCIBDL = 0x9C; // 24,000,000 / 16 / 156 = 9600 (approx) dec=26
  265. SCICR1 = 0x00; // $9C = 156
  266. SCICR2 = 0x0C; // initialize SCI for program-driven operation
  267. SCICR2 = SCICR2 & 0x7F;
  268. DDRB = 0x10; // set PB4 for output mode
  269. PORTB = 0x10; // assert DTR pin on COM port
  270.  
  271. //RTI Initialization (2.048ms)
  272. CRGINT = CRGINT | 0x80; //RTI enabled
  273. RTICTL = 0x1F; //
  274.  
  275.  
  276. //SPI Initialization (6MHz)
  277. SPICR1 = 0x52; //SPI control register #1
  278. //[Bit 7] SPIE = 0 | SPI interrupts disabled
  279. //[Bit 6] SPE = 1 | SPI enabled
  280. //[Bit 5] SPTIE = 0 | SPI transmit empty interrupt disabled
  281. //[Bit 4] MSTR = 1 | Master mode
  282. //[Bit 3] CPOL = 0 | Active low clock (SCK) - idles low
  283. //[Bit 2] CPHA = 0 | Data sampled at odd edges of SCK
  284. //[Bit 1] SSOE = 1 | Slave select output enable
  285. //[Bit 0] LSBFE = 0 | Data transferred most significant bit first
  286. SPICR2 = 0x00;
  287. SPIBR = 0x08; //Baud rate register
  288. //[Bit 6-4] SPPRx = 000 | Baud rate pre-selection bits
  289. //[Bit 2-0] SPRx = 001 | Baud rate selection bits
  290. //SPI rate = BusClock / [(SPPR + 1) * 2^(SPR + 1)]
  291.  
  292. //TIM Initialization (10ms)
  293. TSCR1_TEN = 1; //TIM enable
  294. TIOS_IOS7 = 1; //Output compare on ch7
  295. TSCR2_TCRE = 1; //TCNT reset when OC7 occurs
  296. TSCR2_PR2 = 1; //24MHz / 16 = 1.5MHz
  297. TSCR2_PR1 = 0;
  298. TSCR2_PR0 = 0;
  299. TC7 = 15000; //15000 / 1.5MHz = 10ms
  300. TIE_C7I = 0; //TIM interrupts disabled
  301.  
  302. //PWM Initialization (1470.5Hz)
  303. PWME = 0x01; //PMW enable
  304. PWMPOL = 0x01; //PWM polarity select (active high)
  305. PWMPER0 = 0xFF; //period
  306. PWMDTY0 = 0x00; //duty
  307. PWMPRCLK = 0x04; //24MHz/16 = 1.5MHz
  308. PWMCLK = 0x01; //use clock SA
  309. PWMSCLA = 2; //1.5MHz/(2*2) = 375KHz
  310. PWMSCLB = 0; //375KHz/255 = 1470.5Hz final output
  311. MODRR = 0x01; //Reroute pwm to PT pins
  312. PWMCAE = 0;
  313.  
  314. //Port Initializations
  315. ATDDIEN = 0xFF;
  316. DDRT = 0x17; // 00010111 T5, T6, T7 are inputs from the shiny button
  317. PTT = 0;
  318. DDRAD = 0xE0;//DDRAD = 0xFF;
  319.  
  320. //LCD Initializations
  321. LCD_CLK = 1;
  322. LCD_Send_Byte(LCD_ON, 1);
  323. LCD_Send_Byte(LCD_2LINE, 1);
  324. LCD_Send_Byte(LCD_CLEAR, 1);
  325. LCD_Delay(1);
  326. LCD_Display(0);
  327.  
  328. enable = 0;
  329. asm {
  330. nop
  331. }
  332. }
  333.  
  334. /*********************************************************************************
  335. Main
  336. **********************************************************************************/
  337. void main(void) {
  338. DisableInterrupts;
  339. initializations();
  340. EnableInterrupts;
  341.  
  342.  
  343.  
  344. for(;;) {
  345. //RPG turned to the right
  346. if (RPGRight == 1) {
  347. RPGRight = 0;
  348.  
  349. //Not on welcome splash
  350. if (OutOfSplashScreen == 1) {
  351. //Not editing the game timer
  352. if (TimerEditing == 0) {
  353. MenuPosition++; //Next menu object
  354.  
  355. //Scroller cannot pass maximum menu objects
  356. if (MenuPosition > 3) {
  357. MenuPosition = 3;
  358. } else {
  359. //Display menu object on LCD
  360. LCD_Display(MenuPosition);
  361. }
  362. //Editing the game timer
  363. } else {
  364. P1mins++;
  365. P2mins++;
  366.  
  367. //Timer cannout go over 60 minutes
  368. if (P1mins > 60) {
  369. P1mins = 60;
  370. P2mins = 60;
  371. }
  372.  
  373. LCD_Timer();
  374. }
  375. }
  376. }
  377.  
  378. //RPG turned to the left
  379. if (RPGLeft == 1) {
  380. RPGLeft = 0;
  381.  
  382. //Not on welcome splash
  383. if (OutOfSplashScreen == 1) {
  384. //Not editing the game timer
  385. if (TimerEditing == 0) {
  386. MenuPosition--; //Next menu object
  387.  
  388. //Scroller cannot pass maximum menu objects
  389. if (MenuPosition < 1) {
  390. MenuPosition = 1;
  391. } else {
  392. //Display menu object on LCD
  393. LCD_Display(MenuPosition);
  394. }
  395. //Editing game timer
  396. } else {
  397. P1mins--;
  398. P2mins--;
  399.  
  400. //Timer cannout go below 1 minute
  401. if (P1mins < 1) {
  402. P1mins = 1;
  403. P2mins = 1;
  404. }
  405.  
  406. LCD_Timer();
  407. }
  408. }
  409. }
  410.  
  411. //RPG button pressed
  412. if (newRPGbutton == 1) {
  413. newRPGbutton = 0;
  414.  
  415. //Check if on splash
  416. if (OutOfSplashScreen == 0) {
  417. OutOfSplashScreen = 1;
  418. LCD_Display(MenuPosition);
  419. } else {
  420. //Start game, bring up timer
  421. if (MenuPosition == 1) {
  422. GameRunning = 1;
  423.  
  424. //If timer not running, start it and game initializations
  425. if (TimerRunning == 0) {
  426. TIE_C7I = 1;
  427. TimerRunning = 1;
  428.  
  429. //Chess_Locations(0); //Initialize new game piece locations
  430. enable = 1;
  431. LED_Test();
  432. enable = 0;
  433. //LED_Convert(); //Set up LED backlighting
  434. //enable = 1;
  435. //LED_Out();
  436. //enable = 0;
  437.  
  438. //SCICR2 = SCICR2 | 0x80;
  439. //LED_Convert(); //Set up LED backlighting
  440. //enable = 1;
  441. //LED_Out();
  442. //enable = 0;
  443. //enable2 = 1;
  444. //HALL_Input();
  445. //enable2 = 0;
  446. //If timer is running, toggle player
  447. } else if (TimerRunning == 1){
  448. PlayerToggle = !PlayerToggle;
  449.  
  450. }
  451. //Editing the game timer
  452. } else if (MenuPosition == 2) {
  453. LCD_Timer();
  454.  
  455. //Already brought the screen up
  456. if (TimerEditing == 1) {
  457. LCD_Display(MenuPosition);
  458. TimerEditing = 0;
  459. } else {
  460. TimerEditing = 1;
  461. }
  462.  
  463. } else if (MenuPosition == 3) {
  464. //load game
  465. }
  466. }
  467. }
  468.  
  469. //gameflow
  470. /* if(GameRunning == 1 && TimerRunning == 1){
  471. if(pickedupflag == 0){
  472. nowpccnt();
  473. hpccnt();
  474. currpccnt();
  475. if(nowpc > currpc){
  476. puteverythingback();
  477. } else {
  478.  
  479. nowpclatch = nowpc;
  480. cndiff = currpc - nowpc;
  481. if(cndiff == 1){
  482. hallvchess();
  483. if((ChessMatrix[uprow][upcol] / 10) == PlayerToggle){
  484. if(sameflag == 0){
  485. pickedup(uprow,upcol,ChessMatrix[uprow][upcol]);
  486. pickedupflag = 1;
  487. }
  488. LCD_Display(6);
  489. } else {
  490. puteverythingback();
  491. }
  492.  
  493.  
  494. }
  495. }
  496.  
  497.  
  498. } else if (pickedupflag == 1){
  499. currpccnt();
  500. nowpccnt();
  501. hpccnt();
  502. if(nowpc < hpc - 1 || nowpc > hpc + 1){
  503. puteverythingback();
  504. } else {
  505. if(nowpc = hpc + 1){ //setting it back down
  506. hallvlatch();
  507. if(ldata[nextrow][nextcol] && sameflag == 0){
  508. setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
  509. }
  510. } else if (nowpc = hpc - 1){ //picking another up
  511. hallvlatch();
  512. if(ldata[nextrow][nextcol] && sameflag == 0){
  513. setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
  514. }
  515. }
  516. }
  517. }
  518. }*/
  519.  
  520.  
  521. //If timer is running and it needs a refresh, refresh it.
  522. if ((TimerRunning == 1) && (TimerRefresh == 1)) {
  523. TimerRefresh = 0;
  524.  
  525. LCD_Timer();
  526. }
  527.  
  528. if (RefreshHall == 1) {
  529. enable2 = 1;
  530. HALL_Input();
  531. enable2 = 0;
  532. RefreshHall = 0;
  533. }
  534.  
  535.  
  536.  
  537. _FEED_COP(); /* feeds the dog */
  538. } /* loop forever */
  539. /* please make sure that you never leave main */
  540. }
  541.  
  542. /*********************************************************************************
  543. * Name: Chess_Locations
  544. * Description: Manages vitual piece location map.
  545. * [->] (global): ChessMatrix: Map of the board.
  546. * 00 = empty space
  547. * X1 = pawn
  548. * X2 = rook
  549. * X3 = knight
  550. * X4 = bishop
  551. * X5 = Queen
  552. * X6 = King
  553. * 1X = White piece
  554. * 2X = Black piece
  555. * (int): Mode: Mode of map.
  556. * 0 = Initialization for new game.
  557. * 1 = Standard
  558. * [<-] (void): [void]
  559. *********************************************************************************/
  560. void Chess_Locations(int Mode) {
  561.  
  562. if (Mode == 0) {
  563. ChessMatrix[0][0] = 2;
  564. ChessMatrix[0][1] = 3;
  565. ChessMatrix[0][2] = 4;
  566. ChessMatrix[0][3] = 5;
  567. ChessMatrix[0][4] = 6;
  568. ChessMatrix[0][5] = 4;
  569. ChessMatrix[0][6] = 3;
  570. ChessMatrix[0][7] = 2;
  571. ChessMatrix[1][0] = 1;
  572. ChessMatrix[1][1] = 1;
  573. ChessMatrix[1][2] = 1;
  574. ChessMatrix[1][3] = 1;
  575. ChessMatrix[1][4] = 1;
  576. ChessMatrix[1][5] = 1;
  577. ChessMatrix[1][6] = 1;
  578. ChessMatrix[1][7] = 1;
  579. ChessMatrix[2][0] = 0;
  580. ChessMatrix[2][1] = 0;
  581. ChessMatrix[2][2] = 0;
  582. ChessMatrix[2][3] = 0;
  583. ChessMatrix[2][4] = 0;
  584. ChessMatrix[2][5] = 0;
  585. ChessMatrix[2][6] = 0;
  586. ChessMatrix[2][7] = 0;
  587. ChessMatrix[3][0] = 0;
  588. ChessMatrix[3][1] = 0;
  589. ChessMatrix[3][2] = 0;
  590. ChessMatrix[3][3] = 0;
  591. ChessMatrix[3][4] = 0;
  592. ChessMatrix[3][5] = 0;
  593. ChessMatrix[3][6] = 0;
  594. ChessMatrix[3][7] = 0;
  595. ChessMatrix[4][0] = 0;
  596. ChessMatrix[4][1] = 0;
  597. ChessMatrix[4][2] = 0;
  598. ChessMatrix[4][3] = 0;
  599. ChessMatrix[4][4] = 0;
  600. ChessMatrix[4][5] = 0;
  601. ChessMatrix[4][6] = 0;
  602. ChessMatrix[4][7] = 0;
  603. ChessMatrix[5][0] = 0;
  604. ChessMatrix[5][1] = 0;
  605. ChessMatrix[5][2] = 0;
  606. ChessMatrix[5][3] = 0;
  607. ChessMatrix[5][4] = 0;
  608. ChessMatrix[5][5] = 0;
  609. ChessMatrix[5][6] = 0;
  610. ChessMatrix[5][7] = 0;
  611. ChessMatrix[6][0] = 11;
  612. ChessMatrix[6][1] = 11;
  613. ChessMatrix[6][2] = 11;
  614. ChessMatrix[6][3] = 11;
  615. ChessMatrix[6][4] = 11;
  616. ChessMatrix[6][5] = 11;
  617. ChessMatrix[6][6] = 11;
  618. ChessMatrix[6][7] = 11;
  619. ChessMatrix[7][0] = 12;
  620. ChessMatrix[7][1] = 13;
  621. ChessMatrix[7][2] = 14;
  622. ChessMatrix[7][3] = 16;
  623. ChessMatrix[7][4] = 15;
  624. ChessMatrix[7][5] = 14;
  625. ChessMatrix[7][6] = 13;
  626. ChessMatrix[7][7] = 12;
  627. }
  628.  
  629. if (Mode == 1) { //Update locations map
  630.  
  631. }
  632. return;
  633. }
  634. /***********************************************************************
  635. BOARD ANALYSIS
  636. ***********************************************************************/
  637. //hall vs chessmatrix
  638. //up = 1 down = 0
  639. //updates hlatch
  640. void hallvchess(void){
  641. int i;
  642. int j;
  643. int differ = 0;
  644. int rtemp;
  645. int ctemp;
  646.  
  647. for (i = 0; i < 8; i++) { //Chess row loop
  648. for (j = 0; j < 8; j++) { //Chess column loop
  649.  
  650. if(ChessMatrix[i][j] > 0){
  651. hlatch[i][j] = 1;
  652. } else {
  653. hlatch[i][j] = 0;
  654. }
  655.  
  656. if (hlatch[i][j] != HallMatrix[i][j]) { //both aren't the same
  657. differ += 1;
  658. rtemp = i;
  659. ctemp = j;
  660. }
  661. }
  662. }
  663. //if(dir == 1){
  664. uprow = rtemp;
  665. upcol = ctemp;
  666. hlatch[uprow][upcol] = 0;
  667. // } else if(dir == 0){
  668. // downrow = rtemp;
  669. // downcol = ctemp;
  670. // }
  671. if(differ == 0){
  672. sameflag = 1;
  673. } else {
  674. sameflag = 0;
  675. }
  676. }
  677.  
  678.  
  679. void hallvlatch(void){
  680. int i;
  681. int j;
  682. int differ = 0;
  683. int rtemp;
  684. int ctemp;
  685.  
  686. for (i = 0; i < 8; i++) { //Chess row loop
  687. for (j = 0; j < 8; j++) { //Chess column loop
  688.  
  689. if (hlatch[i][j] != HallMatrix[i][j]) { //both aren't the same
  690. differ += 1;
  691. rtemp = i;
  692. ctemp = j;
  693. }
  694. }
  695. }
  696. //if(dir == 1){
  697. nextrow = rtemp;
  698. nextcol = ctemp;
  699. hlatch[uprow][upcol] = 0;
  700. // } else if(dir == 0){
  701. // downrow = rtemp;
  702. // downcol = ctemp;
  703. // }
  704. if(differ == 0){
  705. sameflag = 1;
  706. } else {
  707. sameflag = 0;
  708. }
  709. }
  710.  
  711.  
  712. //updates currpc
  713. void currpccnt(void){
  714. int pcnum = 0;
  715. int i;
  716. int j;
  717.  
  718. for (i = 0; i < 8; i++) { //Chess column loop
  719. for (j = 0; j < 8; j++) { //Chess row loop
  720. if (ChessMatrix[i][j] != 0) { //if piece exists on tile
  721. pcnum += 1;
  722. }
  723. }
  724. }
  725.  
  726. currpc = pcnum;
  727.  
  728. }
  729. //updates nowpc
  730. void nowpccnt(void){
  731. int pcnum = 0;
  732. int i;
  733. int j;
  734.  
  735. for (i = 0; i < 8; i++) { //Hall column loop
  736. for (j = 0; j < 8; j++) { //Hall row loop
  737. if (HallMatrix[i][j] != 0) { //if piece exists on tile
  738. pcnum += 1;
  739. }
  740. }
  741. }
  742.  
  743. nowpc = pcnum;
  744.  
  745. }
  746.  
  747. void hpccnt(void){
  748. int pcnum = 0;
  749. int i;
  750. int j;
  751.  
  752. for (i = 0; i < 8; i++) { //Hall column loop
  753. for (j = 0; j < 8; j++) { //Hall row loop
  754. if (hlatch[i][j] != 0) { //if piece exists on tile
  755. pcnum += 1;
  756. }
  757. }
  758. }
  759.  
  760. hpc = pcnum;
  761.  
  762. }
  763.  
  764. //counts how many moves avail
  765. void movecnt(void){
  766. int pcnum = 0;
  767. int i;
  768. int j;
  769.  
  770. for (i = 0; i < 8; i++) { //Hall column loop
  771. for (j = 0; j < 8; j++) { //Hall row loop
  772. if (ldata[i][j] != 0) { //if piece exists on tile
  773. pcnum += 1;
  774. }
  775. }
  776. }
  777.  
  778. nummoves = pcnum;
  779.  
  780. }
  781.  
  782.  
  783. void puteverythingback(void){
  784.  
  785. int i;
  786. int j;
  787. int differ = 0;
  788. int temp = 0;
  789. int check = 1;
  790. ledoutmode = 0;
  791. LCD_Display(6);
  792. while(check == 1){
  793. differ = 0;
  794. for (i = 0; i < 8; i++) { //Chess row loop
  795. for (j = 0; j < 8; j++) { //Chess column loop
  796.  
  797. if(ChessMatrix[i][j] > 0){
  798. temp = 1;
  799. } else {
  800. temp = 0;
  801. }
  802. if (temp != HallMatrix[i][j]) { //both aren't the same
  803. differ += 1;
  804. }
  805. }
  806. }
  807. if(differ == 0){
  808. check = 0;
  809. }
  810.  
  811. }
  812.  
  813. pickedupflag = 0;
  814. }
  815.  
  816.  
  817. /*********************************************************************************
  818. * Name: pickedup
  819. * Description: What is done when one piece is currently picked up
  820. * [->] (int i, int j): the coordinates of the piece being picked up
  821. * [<-] (void): [pcnum]
  822. *********************************************************************************/
  823.  
  824. void pickedup(int row, int col, int piece){
  825. int type = piece % 10; // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
  826. int team = piece / 10; //0=white 1=black
  827.  
  828. if(type == 1){
  829. pawnled(row,col,team,0);
  830. }
  831. else if(type == 2){
  832. rookled(row,col,team);
  833. }
  834. else if(type == 3){
  835. knightled(row,col,team);
  836. }
  837. else if(type == 4){
  838. bishopled(row,col,team);
  839. }
  840. else if(type == 5){
  841. queenled(row,col,team);
  842. }
  843. else if(type == 6){
  844. kingled(row,col,team);
  845. }
  846. //movecnt();
  847. //ledcopy(5);
  848. ledoutmode = 1;
  849. }
  850.  
  851.  
  852. void setdown(int row, int col, int piece){
  853. int epiece = ChessMatrix[row][col];
  854. int type = epiece % 10; // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
  855. int team = piece / 10; //0=white 1=black
  856. int score;
  857.  
  858.  
  859. if(type == 1){
  860. score = 1;
  861. }
  862. else if(type == 2){
  863. score = 5;
  864. }
  865. else if(type == 3){
  866. score = 3;
  867. }
  868. else if(type == 4){
  869. score = 3;
  870. }
  871. else if(type == 5){
  872. score = 9;
  873. } else {
  874. score = 0;
  875. }
  876. if(team == 0){
  877. whitescore += score;
  878. } else if(team == 1){
  879. blackscore += score;
  880. }
  881.  
  882. ChessMatrix[row][col] = piece;
  883. ChessMatrix[uprow][upcol] = 0;
  884. ledoutmode = 0;
  885. currpccnt();
  886. nowpccnt();
  887. if(currpc != nowpc){
  888. puteverythingback();
  889. }
  890. PlayerToggle = !PlayerToggle;
  891. //movecnt();
  892. //ledcopy(5);
  893. }
  894.  
  895.  
  896.  
  897. /***********************************************************************
  898. LED/ VALID MOVE MATRIX GENERATION
  899. ***********************************************************************/
  900. //mode 0 regular //mode 1 only piece captures
  901. void pawnled(int row,int col,int team,int mode){
  902. int dir;
  903. if (team == 1){
  904. dir = -1;
  905. } else {
  906. dir = 1;
  907. }
  908.  
  909. if(mode == 0){
  910. if(ChessMatrix[row+dir][col] == 0){ //check 1 space
  911. ldata[row+dir][col] = 1;
  912. if(team == 1){ //check 2 spaces
  913. if(row == 6 && ChessMatrix[row+dir+dir][col] == 0){
  914. ldata[row+dir+dir][col] = 1;
  915. }
  916. } else {
  917. if(row == 1 && ChessMatrix[row+dir+dir][col] == 0){
  918. ldata[row+dir+dir][col] = 1;
  919. }
  920. }
  921. }
  922. }
  923.  
  924. if(col != 7 && ChessMatrix[row+dir][col+1] != 0 && (ChessMatrix[row+dir][col+1] / 10) != team){ //check right capture
  925. ldata[row+dir][col+1] = 1;
  926.  
  927. }
  928. if(col != 0 && ChessMatrix[row+dir][col-1] != 0 && (ChessMatrix[row+dir][col-1] / 10) != team){ //check left capture
  929. ldata[row+dir][col-1] = 1;
  930.  
  931. }
  932.  
  933. //pick up piece message
  934. }
  935.  
  936. void rookled(int row,int col, int team){
  937. int cscan = col;
  938. int rscan = row;
  939. int dir = 1;
  940.  
  941. while(rscan < 7){ //scan up positive row
  942. if(ChessMatrix[rscan+dir][col] == 0){
  943. ldata[rscan+dir][col] = 1;
  944. rscan += dir;
  945. } else if((ChessMatrix[rscan+dir][col] / 10) != team){
  946. ldata[rscan+dir][col] = 1;
  947. rscan = 8;
  948. } else {
  949. rscan = 8;
  950. }
  951. }
  952.  
  953. dir = -1; //reset scan parameters
  954. rscan = row;
  955.  
  956. while(rscan > 0){ //scan up negative row
  957. if(ChessMatrix[rscan+dir][col] == 0){
  958. ldata[rscan+dir][col] = 1;
  959. rscan += dir;
  960. } else if((ChessMatrix[rscan+dir][col] / 10) != team){
  961. ldata[rscan+dir][col] = 1;
  962. rscan = -1;
  963. } else {
  964. rscan = -1;
  965. }
  966. }
  967.  
  968. dir = 1;
  969.  
  970.  
  971. while(cscan < 7){ //scan up positive column
  972. if(ChessMatrix[row][cscan+dir] == 0){
  973. ldata[row][cscan+dir] = 1;
  974. cscan += dir;
  975. } else if((ChessMatrix[row][cscan+dir] / 10) != team){
  976. ldata[row][cscan+dir] = 1;
  977. cscan = 8;
  978. } else {
  979. cscan = 8;
  980. }
  981. }
  982.  
  983. dir = -1; //reset scan parameters
  984. cscan = col;
  985.  
  986. while(cscan > 0){ //
  987. if(ChessMatrix[row][cscan+dir] == 0){ //scan up negative column
  988. ldata[row][cscan+dir] = 1;
  989. cscan += dir;
  990. } else if((ChessMatrix[row][cscan+dir] / 10) != team){
  991. ldata[row][cscan+dir] = 1;
  992. cscan = -1;
  993. } else {
  994. cscan = -1;
  995. }
  996. }
  997. }
  998.  
  999. void bishopled(int row,int col,int team){
  1000. int cscan = col;
  1001. int rscan = row;
  1002. int cdir = 1;
  1003. int rdir = 1;
  1004.  
  1005. //+ + dir
  1006. while(rscan < 7 && cscan < 7){ //scan up positive row
  1007. if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1008. ldata[rscan+rdir][cscan+cdir] = 1;
  1009. rscan += rdir;
  1010. cscan += cdir;
  1011. } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1012. ldata[rscan+rdir][cscan+cdir] = 1;
  1013. rscan = 8;
  1014. cscan = 8;
  1015. } else {
  1016. rscan = 8;
  1017. cscan = 8;
  1018. }
  1019. }
  1020.  
  1021. rscan = row;
  1022. cscan = col;
  1023. cdir = -1;
  1024.  
  1025. //- + dir
  1026. while(rscan < 7 && cscan > 0){ //scan up positive row
  1027. if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1028. ldata[rscan+rdir][cscan+cdir] = 1;
  1029. rscan += rdir;
  1030. cscan += cdir;
  1031. } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1032. ldata[rscan+rdir][cscan+cdir] = 1;
  1033. rscan = 8;
  1034. cscan = -1;
  1035. } else {
  1036. rscan = 8;
  1037. cscan = -1;
  1038. }
  1039. }
  1040.  
  1041. rscan = row;
  1042. cscan = col;
  1043. cdir = 1;
  1044. rdir = -1;
  1045.  
  1046. //+ - dir
  1047. while(rscan > 0 && cscan < 7){ //scan up positive row
  1048. if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1049. ldata[rscan+rdir][cscan+cdir] = 1;
  1050. rscan += rdir;
  1051. cscan += cdir;
  1052. } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1053. ldata[rscan+rdir][cscan+cdir] = 1;
  1054. rscan = -1;
  1055. cscan = 8;
  1056. } else {
  1057. rscan = -1;
  1058. cscan = 8;
  1059. }
  1060. }
  1061.  
  1062. rscan = row;
  1063. cscan = col;
  1064. cdir = -1;
  1065.  
  1066.  
  1067. //- - dir
  1068. while(rscan > 0 && cscan > 0){ //scan up positive row
  1069. if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
  1070. ldata[rscan+rdir][cscan+cdir] = 1;
  1071. rscan += rdir;
  1072. cscan += cdir;
  1073. } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
  1074. ldata[rscan+rdir][cscan+cdir] = 1;
  1075. rscan = -1;
  1076. cscan = -1;
  1077. } else {
  1078. rscan = -1;
  1079. cscan = -1;
  1080. }
  1081. }
  1082.  
  1083. }
  1084.  
  1085. //0 good -1 not
  1086. int bcheck(int row,int col){
  1087. if(row > 7){
  1088. return -1;
  1089. } else if(col > 7){
  1090. return -1;
  1091. } else if(row < 0){
  1092. return -1;
  1093. } else if(col < 0){
  1094. return -1;
  1095. } else {
  1096. return 0;
  1097. }
  1098. }
  1099.  
  1100. void knightled(int row,int col,int team){
  1101. int cchk = col;
  1102. int rchk = row;
  1103.  
  1104. rchk += 2;
  1105. cchk += 1;
  1106.  
  1107. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1108. ldata[rchk][cchk] = 1;
  1109. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1110. ldata[rchk][cchk] = 1;
  1111. }
  1112.  
  1113. rchk = row;
  1114. cchk = col;
  1115.  
  1116. rchk += 1;
  1117. cchk += 2;
  1118.  
  1119. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1120. ldata[rchk][cchk] = 1;
  1121. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1122. ldata[rchk][cchk] = 1;
  1123. }
  1124.  
  1125. rchk = row;
  1126. cchk = col;
  1127.  
  1128. rchk += -1;
  1129. cchk += 2;
  1130.  
  1131. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1132. ldata[rchk][cchk] = 1;
  1133. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1134. ldata[rchk][cchk] = 1;
  1135. }
  1136.  
  1137. rchk = row;
  1138. cchk = col;
  1139.  
  1140. rchk += 1;
  1141. cchk += -2;
  1142.  
  1143. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1144. ldata[rchk][cchk] = 1;
  1145. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1146. ldata[rchk][cchk] = 1;
  1147. }
  1148.  
  1149. rchk = row;
  1150. cchk = col;
  1151.  
  1152. rchk += 2;
  1153. cchk += -1;
  1154.  
  1155. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1156. ldata[rchk][cchk] = 1;
  1157. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1158. ldata[rchk][cchk] = 1;
  1159. }
  1160.  
  1161. rchk = row;
  1162. cchk = col;
  1163.  
  1164. rchk += -2;
  1165. cchk += 1;
  1166.  
  1167. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1168. ldata[rchk][cchk] = 1;
  1169. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1170. ldata[rchk][cchk] = 1;
  1171. }
  1172.  
  1173. rchk = row;
  1174. cchk = col;
  1175.  
  1176. rchk += -1;
  1177. cchk += -2;
  1178.  
  1179. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1180. ldata[rchk][cchk] = 1;
  1181. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1182. ldata[rchk][cchk] = 1;
  1183. }
  1184.  
  1185. rchk = row;
  1186. cchk = col;
  1187.  
  1188. rchk += -2;
  1189. cchk += 1;
  1190.  
  1191. if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
  1192. ldata[rchk][cchk] = 1;
  1193. } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1194. ldata[rchk][cchk] = 1;
  1195. }
  1196.  
  1197. }
  1198.  
  1199. void queenled(int row,int col,int team){
  1200. bishopled(row,col,team);
  1201. rookled(row,col,team);
  1202. }
  1203.  
  1204. //mode 1 = data to temp
  1205. //mode 2 = temp back to data
  1206. //cleans the one it moved from
  1207. //mode 3 = switch data
  1208. //mode 4 = switch and filter
  1209. //mode 5 (debugging) ldata to chessmatrix
  1210. //mode 6 get rid of all 27
  1211. void ledcopy(int mode){
  1212.  
  1213. int r;
  1214. int c;
  1215. int temp;
  1216.  
  1217. for (r = 0; r < 8; r++) {
  1218. for (c = 0; c < 8; c++) {
  1219. if(mode == 1){
  1220. ltemp[r][c] = ldata[r][c];
  1221. ldata[r][c] = 0;
  1222. } else if(mode == 2) {
  1223. ldata[r][c] = ltemp[r][c];
  1224. ltemp[r][c] = 0;
  1225. } else if(mode == 3) {
  1226. temp = ltemp[r][c];
  1227. ltemp[r][c] = ldata[r][c];
  1228. ldata[r][c] = temp;
  1229. } else if(mode == 5) {
  1230. if(ldata[r][c] == 1){
  1231. ChessMatrix[r][c] = 27;
  1232. }
  1233. } else if(mode == 6) {
  1234. if(ChessMatrix[r][c] == 27){
  1235. ChessMatrix[r][c] = 0;
  1236. }
  1237. } else {
  1238. temp = ltemp[r][c];
  1239. ltemp[r][c] = ldata[r][c];
  1240. ldata[r][c] = temp;
  1241. if((ltemp[r][c] + ldata[r][c]) == 2){
  1242. ldata[r][c] = 0;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248.  
  1249. void kingledout(int row,int col,int team){
  1250. int rchk = row;
  1251. int cchk = col;
  1252.  
  1253. rchk += 1;
  1254.  
  1255. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1256. ldata[rchk][cchk] = 1;
  1257. }
  1258.  
  1259. rchk = row;
  1260. rchk += -1;
  1261.  
  1262. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1263. ldata[rchk][cchk] = 1;
  1264. }
  1265.  
  1266. rchk = row;
  1267. cchk += 1;
  1268.  
  1269. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1270. ldata[rchk][cchk] = 1;
  1271. }
  1272.  
  1273. cchk = col;
  1274. cchk += -1;
  1275.  
  1276. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1277. ldata[rchk][cchk] = 1;
  1278. }
  1279.  
  1280. cchk = col;
  1281. cchk += 1;
  1282. rchk += 1;
  1283.  
  1284. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1285. ldata[rchk][cchk] = 1;
  1286. }
  1287.  
  1288. cchk = col;
  1289. rchk = row;
  1290. cchk += -1;
  1291. rchk += 1;
  1292.  
  1293. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1294. ldata[rchk][cchk] = 1;
  1295. }
  1296.  
  1297. cchk = col;
  1298. rchk = row;
  1299. cchk += 1;
  1300. rchk += -1;
  1301.  
  1302. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1303. ldata[rchk][cchk] = 1;
  1304. }
  1305.  
  1306. cchk = col;
  1307. rchk = row;
  1308. cchk += -1;
  1309. rchk += -1;
  1310.  
  1311. if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
  1312. ldata[rchk][cchk] = 1;
  1313. }
  1314. }
  1315.  
  1316.  
  1317. void checkmatrix(int team){
  1318.  
  1319. int r;
  1320. int c;
  1321.  
  1322. for (r = 0; r < 8; r++) {
  1323. for (c = 0; c < 8; c++) {
  1324. if((ChessMatrix[r][c] / 10) != team){
  1325. if((ChessMatrix[r][c] % 10) == 1){
  1326. pawnled(r,c,1-team,1);
  1327. }
  1328. else if((ChessMatrix[r][c] % 10) == 2){
  1329. rookled(r,c,1-team);
  1330. }
  1331. else if((ChessMatrix[r][c] % 10) == 3){
  1332. knightled(r,c,1-team);
  1333. }
  1334. else if((ChessMatrix[r][c] % 10) == 4){
  1335. bishopled(r,c,1-team);
  1336. }
  1337. else if((ChessMatrix[r][c] % 10) == 5){
  1338. queenled(r,c,1-team);
  1339. }
  1340. else if((ChessMatrix[r][c] % 10) == 6){
  1341. kingledout(r,c,1-team);
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347.  
  1348.  
  1349. void kingled(int row,int col,int team){
  1350. kingledout(row,col,team); //show possible moves without checking for check
  1351. ledcopy(1); //store in temp matrix
  1352. checkmatrix(team); //create a matrix of enemy moves
  1353. ledcopy(4); //filter the temp matrix with enemy move matrix
  1354. }
  1355.  
  1356. void ischeck(int team){
  1357. int piece;
  1358. int r;
  1359. int c;
  1360. int tempr;
  1361. int tempc;
  1362.  
  1363. if(team == 0){
  1364. piece = 6;
  1365. } else if(team == 1){
  1366. piece = 16;
  1367. }
  1368.  
  1369. for (r = 0; r < 8; r++) {
  1370. for (c = 0; c < 8; c++) {
  1371. if(ChessMatrix[r][c] == piece){
  1372. tempr = r;
  1373. tempc = c;
  1374. }
  1375. }
  1376. }
  1377.  
  1378. //checkmatrix(team);
  1379. if(ldata[tempr][tempc] == 1){
  1380. if(team == 0){
  1381. whitecheckflag = 1;
  1382. } else if (team == 1){
  1383. blackcheckflag = 1;
  1384. }
  1385. }
  1386.  
  1387.  
  1388. }
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397. /*********************************************************************************
  1398. * SCI Functions
  1399. **********************************************************************************/
  1400.  
  1401.  
  1402. //0='*', 1=' '
  1403. void sevenchar(int mode){
  1404. int i = 0;
  1405. char a;
  1406. if(mode == 1){
  1407. a = '*';
  1408. } else {
  1409. a = ' ';
  1410. }
  1411. for (i = 0; i < 7; i++) {
  1412. addchar(a);
  1413. }
  1414.  
  1415. }
  1416.  
  1417. //2=solid line, 1=whiteblackwhiteblack, 0=blackwhiteblackwhite
  1418. void sci_outline(int mode){
  1419. int i = 0;
  1420. int j = 0;
  1421.  
  1422. if(mode == 2){
  1423. addchar('*');
  1424. for (i = 0; i < 8; i++) {
  1425. sevenchar(1);
  1426. addchar('*');
  1427. }
  1428. addchar(LF);
  1429. addchar(CR);
  1430. } else if(mode == 1){
  1431. addchar('*');
  1432. j = 0;
  1433. for (i = 0; i < 8; i++) {
  1434. j = 1 - j;
  1435. sevenchar(j);
  1436. addchar('*');
  1437. }
  1438. addchar(LF);
  1439. addchar(CR);
  1440. } else if(mode == 0){
  1441. addchar('*');
  1442. j = 1;
  1443. for (i = 0; i < 8; i++) {
  1444. j = 1 - j;
  1445. sevenchar(j);
  1446. addchar('*');
  1447. }
  1448. addchar(LF);
  1449. addchar(CR);
  1450.  
  1451. }
  1452.  
  1453. }
  1454.  
  1455.  
  1456.  
  1457. void addchar(char a){
  1458. tbuf[tin] = a;
  1459. tdata += 1;
  1460. tin += 1;
  1461. tin = tin % tsize;
  1462. }
  1463.  
  1464. void outtype(int type){
  1465. char x;
  1466. //sciset = 1 - sciset;
  1467. if(boardset == 0){
  1468. x = '*';
  1469. } else {
  1470. x = ' ';
  1471. }
  1472. if(type == 1){
  1473. addchar(x);
  1474. addchar('P');
  1475. addchar('a');
  1476. addchar('w');
  1477. addchar('n');
  1478. addchar(x);
  1479. addchar(x);
  1480. } else if(type == 2){
  1481. addchar(x);
  1482. addchar('R');
  1483. addchar('o');
  1484. addchar('o');
  1485. addchar('k');
  1486. addchar(x);
  1487. addchar(x);
  1488. } else if(type == 3){
  1489. addchar(x);
  1490. addchar('K');
  1491. addchar('n');
  1492. addchar('i');
  1493. addchar('g');
  1494. addchar('h');
  1495. addchar('t');
  1496. } else if(type == 4){
  1497. addchar(x);
  1498. addchar('B');
  1499. addchar('i');
  1500. addchar('s');
  1501. addchar('h');
  1502. addchar('o');
  1503. addchar('p');
  1504. } else if(type == 5){
  1505. addchar(x);
  1506. addchar('Q');
  1507. addchar('u');
  1508. addchar('e');
  1509. addchar('e');
  1510. addchar('n');
  1511. addchar(x);
  1512. } else if(type == 6){
  1513. addchar(x);
  1514. addchar('K');
  1515. addchar('i');
  1516. addchar('n');
  1517. addchar('g');
  1518. addchar(x);
  1519. addchar(x);
  1520. }
  1521. addchar('*');
  1522. }
  1523.  
  1524. void outteam(int team){
  1525. char x;
  1526. //boardset = 1 - sciset;
  1527. if(boardset == 1){
  1528. x = '*';
  1529. } else {
  1530. x = ' ';
  1531. }
  1532. if(team == 0){
  1533. addchar(x);
  1534. addchar('W');
  1535. addchar('h');
  1536. addchar('i');
  1537. addchar('t');
  1538. addchar('e');
  1539. addchar(x);
  1540. } else if(team == 1){
  1541. addchar(x);
  1542. addchar('B');
  1543. addchar('l');
  1544. addchar('a');
  1545. addchar('c');
  1546. addchar('k');
  1547. addchar(x);
  1548. }
  1549. addchar('*');
  1550. }
  1551. //0=team, 1=type
  1552. void sci_datafetch(int mode, int line){
  1553. int i = 0;
  1554.  
  1555.  
  1556. if(mode == 0){
  1557. for (i = 0; i < 8; i++) {
  1558. //if(ChessMatrix[line][i] == 0){
  1559. boardset = 1 - boardset;
  1560. if(HallMatrix[line][i] == 0){
  1561. sevenchar(boardset);
  1562. addchar('*');
  1563. } else {
  1564. //outteam(ChessMatrix[line][i] / 10);
  1565. outteam(HallMatrix[line][i] / 10);
  1566. }
  1567.  
  1568. }
  1569. } else {
  1570. for (i = 0; i < 8; i++) {
  1571. //if(ChessMatrix[line][i] == 0){
  1572. boardset = 1 - boardset;
  1573. if(HallMatrix[line][i] == 0){
  1574. sevenchar(1-boardset);
  1575. addchar('*');
  1576. } else {
  1577. //outtype(ChessMatrix[line][i] % 10);
  1578. outtype(HallMatrix[line][i] % 10);
  1579. }
  1580. }
  1581. //boardset = 1 - boardset;
  1582. }
  1583. //addchar('*');
  1584. addchar(LF);
  1585. addchar(CR);
  1586.  
  1587. }
  1588.  
  1589. void sciscan(void){
  1590. int i = 0;
  1591. int j = 0;
  1592. if(scioff_flag > 0){
  1593. if(scioff_flag == 3){
  1594. SCICR2 = SCICR2 & 0x7F;
  1595. //for (j = 0; j < 2; j++) {
  1596. //addchar(LF);
  1597. //addchar(CR);
  1598. //}
  1599. scioff_flag = 0;
  1600. scicountflag = 1;
  1601. }
  1602. else if(scioff_flag == 2){
  1603. scoreline += 1;
  1604. if(scoreline == 1){
  1605. addchar(LF);
  1606. addchar(CR);
  1607. addchar(LF);
  1608. addchar(CR);
  1609. for (i = 0; i < 15; i++) {
  1610. addchar(' ');
  1611. }
  1612. i = 0;
  1613. for (i = 0; i < 34; i++) {
  1614. addchar('*');
  1615. }
  1616. addchar(LF);
  1617. addchar(CR);
  1618. }
  1619. else if(scoreline == 2){
  1620. for (i = 0; i < 15; i++) {
  1621. addchar(' ');
  1622. }
  1623. addchar('*');
  1624. addchar('W');
  1625. addchar('h');
  1626. addchar('i');
  1627. addchar('t');
  1628. addchar('e');
  1629. addchar(' ');
  1630. addchar('S');
  1631. addchar('c');
  1632. addchar('o');
  1633. addchar('r');\
  1634. addchar('e');
  1635. addchar(':');
  1636. addchar(' ');
  1637. if((whitescore / 10) == 0){
  1638. addchar('0');
  1639. addchar(whitescore + 0x30);
  1640. }
  1641. else {
  1642. addchar((whitescore / 10) + 0x30);
  1643. addchar((whitescore % 10) + 0x30);
  1644. }
  1645. addchar(' ');
  1646. addchar(' ');
  1647. addchar('B');
  1648. addchar('l');
  1649. addchar('a');
  1650. addchar('c');
  1651. addchar('k');
  1652. addchar(' ');
  1653. addchar('S');
  1654. addchar('c');
  1655. addchar('o');
  1656. addchar('r');\
  1657. addchar('e');
  1658. addchar(':');
  1659. addchar(' ');
  1660.  
  1661. if((blackscore / 10) == 0){
  1662. addchar('0');
  1663. addchar(blackscore + 0x30);
  1664. }
  1665. else {
  1666. addchar((blackscore / 10) + 0x30);
  1667. addchar((blackscore % 10) + 0x30);
  1668. }
  1669. addchar('*');
  1670. addchar(LF);
  1671. addchar(CR);
  1672. }
  1673. else if(scoreline == 3){
  1674. for (i = 0; i < 15; i++) {
  1675. addchar(' ');
  1676. }
  1677. i = 0;
  1678.  
  1679. addchar('*');
  1680. for (i = 0; i < 9; i++) {
  1681. addchar(' ');
  1682. }
  1683. j = PlayerToggle + 1;
  1684.  
  1685. if(movenumber % 2){
  1686. addchar('B');
  1687. addchar('l');
  1688. addchar('a');
  1689. addchar('c');
  1690. addchar('k');
  1691. addchar(' ');
  1692. }
  1693. else {
  1694. addchar('W');
  1695. addchar('h');
  1696. addchar('i');
  1697. addchar('t');
  1698. addchar('e');
  1699. addchar(' ');
  1700. }
  1701.  
  1702. addchar('T');
  1703. addchar('u');
  1704. addchar('r');
  1705. addchar('n');
  1706. addchar(' ');
  1707. if((j / 100) == 0){
  1708. if((j / 10) == 0){
  1709. addchar('0');
  1710. addchar('0');
  1711. addchar(j + 0x30);
  1712. }
  1713. else {
  1714. addchar('0');
  1715. addchar((j / 10) + 0x30);
  1716. addchar((j % 10) + 0x30);
  1717. }
  1718. } else {
  1719. addchar((j / 100) + 0x30);
  1720. addchar(((j % 100) / 10) + 0x30);
  1721. addchar((j % 10) + 0x30);
  1722. }
  1723. i = 0;
  1724. for (i = 0; i < 9; i++) {
  1725. addchar(' ');
  1726. }
  1727. addchar('*');
  1728. addchar(LF);
  1729. addchar(CR);
  1730.  
  1731.  
  1732. }
  1733. else if(scoreline == 4){
  1734.  
  1735. for (i = 0; i < 15; i++) {
  1736. addchar(' ');
  1737. }
  1738. i = 0;
  1739. for (i = 0; i < 34; i++) {
  1740. addchar('*');
  1741. }
  1742. addchar(LF);
  1743. addchar(CR);
  1744. }
  1745. else {
  1746. scoreline = 0;
  1747. scioff_flag += 1;
  1748. //tdata -= 1;
  1749. }
  1750. }
  1751. else {
  1752. addchar('*');
  1753. sci_outline(2);
  1754.  
  1755. scioff_flag += 1;
  1756. }
  1757. }
  1758. else {
  1759. scanline();
  1760. }
  1761. }
  1762.  
  1763. void scanline(void){
  1764. int i = 0;
  1765. addchar('*');
  1766.  
  1767. sciset = 1 - sciset;
  1768. boardset = sciset;
  1769.  
  1770. if(sciline == 0){
  1771. sci_outline(2);
  1772. } else if(sciline == 1){
  1773. sci_outline(boardline % 2);
  1774. } else if(sciline == 2){
  1775. sci_datafetch(0,boardline);
  1776. } else if(sciline == 3){
  1777. sci_datafetch(1,boardline);
  1778. } else if(sciline == 4){
  1779. sci_outline(boardline % 2);
  1780. }
  1781.  
  1782. sciline += 1;
  1783. if(sciline > 4){
  1784. boardline += 1;
  1785. sciline = 0;
  1786. }
  1787.  
  1788. if(boardline > 7){
  1789. scioff_flag = 1;
  1790. boardline = 0;
  1791. }
  1792.  
  1793. }
  1794.  
  1795. /**********************************************************************************
  1796. * Name: SCI_ISR
  1797. * Description: SCI RTI for character data transfer to radial buffer.
  1798. * [->] (*): (*)
  1799. * [<-] (void): [void]
  1800. **********************************************************************************/
  1801. interrupt 20 void SCI_ISR( void)
  1802. {
  1803.  
  1804.  
  1805. int j;
  1806.  
  1807. SCISR1_TDRE; //Read status flag to ready data register
  1808.  
  1809. if(scioff_flag == 3){
  1810. scicountflag = 1;
  1811. SCICR2 = SCICR2 & 0x7F;
  1812. //tin += 1;
  1813. //tin = 0;
  1814.  
  1815. for (j = 0; j < 2; j++) {
  1816. addchar(LF);
  1817. addchar(CR);
  1818. }
  1819. scioff_flag = 0;
  1820. }
  1821.  
  1822.  
  1823. if(tdata == 0){
  1824. sciscan();
  1825. }
  1826.  
  1827. if(SCISR1 & 0x80){
  1828. SCIDRL = tbuf[tout];
  1829. tout += 1;
  1830. tout = tout % tsize;
  1831. tdata -= 1;
  1832. }
  1833.  
  1834.  
  1835.  
  1836. }
  1837.  
  1838. /*********************************************************************************
  1839. * LED Matrix Functions
  1840. **********************************************************************************
  1841. **********************************************************************************
  1842. * Name: LED_Out
  1843. * Description: Shifts out the updated LED matrix data.
  1844. * [->] (global): LEDOutData: LED lighting data, one for each row.
  1845. * [<-] (void): [void]
  1846. *********************************************************************************/
  1847. void LED_Out(void) {
  1848. int i = 0;
  1849.  
  1850. for (i = 0; i < 8; i++) {
  1851. while (SPISR_SPTEF == 0) {
  1852. //Loop until SPISR data register is empty
  1853. }
  1854.  
  1855. if(ledoutmode == 0){
  1856. SPIDR = LEDOutData[i]; //Put data into SPISR data register
  1857. } else if(ledoutmode == 1){
  1858. SPIDR = MOVEOutData[i]; //Put data into SPISR data register
  1859. }
  1860. LCD_Delay(0); //Call a delay for shifting out
  1861.  
  1862. //enable daisy chain, disable LCD
  1863. }
  1864. return;
  1865. }
  1866.  
  1867. /*********************************************************************************
  1868. * Name: LED_Test
  1869. * Description: Outs a spiral to test the matrix
  1870. * [->] (global): LEDOutData
  1871. * [<-] (void): [void]
  1872. *********************************************************************************/
  1873. void LED_Test(void) {
  1874. int i = 0;
  1875. int j = 0;
  1876. unsigned char a = 0x00;
  1877. unsigned char b = 0x00;
  1878. unsigned char c = 0x00;
  1879. int counter = 0;
  1880. int row = 0;
  1881. int k = 0;
  1882. //TIE_C7I = 0;
  1883.  
  1884. for (k = 0; k < 3; k++) {
  1885. for (j = 0; j < 9; j++) {//refreshs
  1886. for (i = 0; i < 8; i++) {//rows
  1887.  
  1888. while (SPISR_SPTEF == 0) {
  1889. //Loop until SPISR data register is empty
  1890. }
  1891.  
  1892. if (k == 0) {
  1893. if (i == 0) {
  1894. if (j == 0) { //1000 0000
  1895. counter = 0x80;
  1896. } else if (j == 1) {//1100 0000
  1897. counter = 0xC0;
  1898. } else if (j == 2) {//1110 0000
  1899. counter = 0xE0;
  1900. } else if (j == 3) {//1111 0000
  1901. counter = 0xF0;
  1902. } else if (j == 4) {//1111 1000
  1903. counter = 0xF8;
  1904. } else if (j == 5) {//1111 1100
  1905. counter = 0xFC;
  1906. } else if (j == 6) {//1111 1110
  1907. counter = 0xFE;
  1908. } else if (j == 7) {//1111 1111
  1909. counter = 0xFF;
  1910. } //else if (j == 8) {//++
  1911. //row = 1;
  1912. //}
  1913. } else if ((i != 0) && (k == 0)) {
  1914. counter = 0x00;
  1915. }
  1916.  
  1917. if ((k == 0) && (j != 8)) {
  1918. SPIDR = counter;
  1919. }
  1920.  
  1921. } else if (k == 1) {
  1922. if (i == 0) {
  1923. counter = 0xFF;
  1924. } else if (i == 1) {
  1925.  
  1926. if (j == 0) { //1111 1111 FF
  1927. counter = 0x80;
  1928. } else if (j == 1) {//0111 1111 7F
  1929. counter = 0xC0;
  1930. } else if (j == 2) {//0011 1111 3F
  1931. counter = 0xE0;
  1932. } else if (j == 3) {//0001 1111 1F
  1933. counter = 0xF0;
  1934. } else if (j == 4) {//0000 1111 0F
  1935. counter = 0xF8;
  1936. } else if (j == 5) {//0000 0111 07
  1937. counter = 0xFC;
  1938. } else if (j == 6) {//0000 0011 03
  1939. counter = 0xFE;
  1940. } else if (j == 7) {//0000 0001 01
  1941. counter = 0xFF;
  1942. } //else if (j == 8) {
  1943. //row = 2;
  1944. //}
  1945. } else if ((i != 1) && (k == 1)){
  1946. counter = 0x00;
  1947. }
  1948.  
  1949. if ((k == 1) && (j != 8)) {
  1950. SPIDR = counter;
  1951. }
  1952. } else if (k == 2) {
  1953. if ((i == 0) || (i == 1)) {
  1954. counter = 0xFF;
  1955. } else if (i == 2) {
  1956. if (j == 0) { //1111 1111 FF
  1957. counter = 0x80;
  1958. } else if (j == 1) {//0111 1111 7F
  1959. counter = 0xC0;
  1960. } else if (j == 2) {//0011 1111 3F
  1961. counter = 0xE0;
  1962. } else if (j == 3) {//0001 1111 1F
  1963. counter = 0xF0;
  1964. } else if (j == 4) {//0000 1111 0F
  1965. counter = 0xF8;
  1966. } else if (j == 5) {//0000 0111 07
  1967. counter = 0xFC;
  1968. } else if (j == 6) {//0000 0011 03
  1969. counter = 0xFE;
  1970. } else if (j == 7) {//0000 0001 01
  1971. counter = 0xFF;
  1972. }
  1973. } else if ((i != 2) && (k == 2)) {
  1974. counter = 0x00;
  1975. }
  1976.  
  1977. if ((k == 2) && (j != 8)) {
  1978. SPIDR = counter;
  1979. }
  1980. } else if (k == 3) {
  1981. if ((i == 0) || (i == 1) || (i == 2)) {
  1982. counter = 0xFF;
  1983. } else if (i == 3) {
  1984. if (j == 0) { //1111 1111 FF
  1985. counter = 0x80;
  1986. } else if (j == 1) {//0111 1111 7F
  1987. counter = 0xC0;
  1988. } else if (j == 2) {//0011 1111 3F
  1989. counter = 0xE0;
  1990. } else if (j == 3) {//0001 1111 1F
  1991. counter = 0xF0;
  1992. } else if (j == 4) {//0000 1111 0F
  1993. counter = 0xF8;
  1994. } else if (j == 5) {//0000 0111 07
  1995. counter = 0xFC;
  1996. } else if (j == 6) {//0000 0011 03
  1997. counter = 0xFE;
  1998. } else if (j == 7) {//0000 0001 01
  1999. counter = 0xFF;
  2000. }
  2001. } else {
  2002. counter = 0x00;
  2003. }
  2004.  
  2005. if ((k == 3) && (j != 8)) {
  2006. SPIDR = counter;
  2007. }
  2008. }// row if end
  2009. }//i for end
  2010.  
  2011. for (a = 0x00; a < 0xFE; a++) {
  2012. for (b = 0x00; b < 0xFE; b++) {
  2013. for (c = 0x00; c < 0x3F; c++) {
  2014. //do nothing
  2015. }
  2016. }
  2017. }
  2018. }//j for end
  2019. }//k for end
  2020. //while (SPISR_SPTEF == 0) {
  2021. //Loop until SPISR data register is empty
  2022. //}
  2023.  
  2024. //counter = 0x00;
  2025.  
  2026. }
  2027.  
  2028. /*********************************************************************************
  2029. * Name: LED_Convert
  2030. * Description: Converts chess data to LED data.
  2031. * [->] (global): LEDOutData
  2032. * ChessMatrix
  2033. * [<-] (void): [void]
  2034. *********************************************************************************/
  2035. void LED_Convert(void) {
  2036. int i = 0;
  2037. int j = 0;
  2038. int k = 0;
  2039. int temp = 1;
  2040. int temp2 = 1;
  2041. for (i = 0; i < 8; i++) { //Chess column loop
  2042. LEDOutData[i] = 0; //Clear of former data.
  2043. MOVEOutData[i] = 0;
  2044.  
  2045. for (j = 0; j < 8; j++) { //Chess row loop
  2046. if (ChessMatrix[i][j] != 0) { //if piece exists on tile
  2047. temp = 1;
  2048.  
  2049. for (k = 0; k < j; k++) { //shifting data calculation
  2050. temp = temp * 2;
  2051.  
  2052. }
  2053. LEDOutData[i] += temp;
  2054. }
  2055. if (ldata[i][j] != 0) { //if light exists in ldata
  2056. temp2 = 1;
  2057.  
  2058. for (k = 0; k < j; k++) { //shifting data calculation
  2059. temp2 = temp2 * 2;
  2060.  
  2061. }
  2062. MOVEOutData[i] += temp2;
  2063. }
  2064. }
  2065. }
  2066.  
  2067. return;
  2068. }
  2069.  
  2070. /*********************************************************************************
  2071. * Hall Sensor Functions
  2072. **********************************************************************************
  2073. **********************************************************************************
  2074. * Name: HALL_Input
  2075. * Description: Collects magnet connection data.
  2076. * [->] (global): datas
  2077. * [<-] (void): [void]
  2078. *********************************************************************************/
  2079. void HALL_Input(void) {
  2080. int i = 0;
  2081. int j = 0;
  2082. char Selector = 0x00;
  2083. int LinVal = 0;
  2084. int Temp[64];
  2085. int MatVal = 0;
  2086.  
  2087. Selector = 0x0B; //Multiplexer data slector set to highest for chip 5 (12)
  2088. for (j = 0; j < 12; j++) {//Multiplexer inputs on chip 5
  2089. while (SPISR_SPTEF == 0) {
  2090. //Loop until SPISR data register is empty
  2091. }
  2092.  
  2093. SPIDR = Selector; //Put data into SPISR data register
  2094. LCD_Delay(0);
  2095.  
  2096. //if (j == 1) {
  2097. // LineVal == 0;
  2098. //}
  2099. Temp[LinVal] = output5;
  2100. Selector -= 0x01;
  2101. LinVal++;
  2102. }
  2103.  
  2104.  
  2105. Selector = 0x0C; //Multiplexer data slector set to highest for chip 4 (13)
  2106. for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 4
  2107. while (SPISR_SPTEF == 0) {
  2108. //Loop until SPISR data register is empty
  2109. }
  2110.  
  2111. SPIDR = Selector; //Put data into SPISR data register
  2112. LCD_Delay(0);
  2113.  
  2114. Temp[LinVal] = output4;
  2115. Selector -= 0x01;
  2116. LinVal++;
  2117. }
  2118.  
  2119. Selector = 0x0C; //Multiplexer data slector set to highest for chip 3 (13)
  2120. for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 3
  2121. while (SPISR_SPTEF == 0) {
  2122. //Loop until SPISR data register is empty
  2123. }
  2124.  
  2125. SPIDR = Selector; //Put data into SPISR data register
  2126. LCD_Delay(0);
  2127.  
  2128. Temp[LinVal] = output3;
  2129. Selector -= 0x01;
  2130. LinVal++;
  2131. }
  2132.  
  2133. Selector = 0x0C; //Multiplexer data slector set to highest for chip 2 (13)
  2134. for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 2
  2135. while (SPISR_SPTEF == 0) {
  2136. //Loop until SPISR data register is empty
  2137. }
  2138.  
  2139. SPIDR = Selector; //Put data into SPISR data register
  2140. LCD_Delay(0);
  2141.  
  2142. Temp[LinVal] = output2;
  2143. Selector -= 0x01;
  2144. LinVal++;
  2145. }
  2146.  
  2147. Selector = 0x0C; //Multiplexer data slector set to highest for chip 1 (13)
  2148. for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 1
  2149. while (SPISR_SPTEF == 0) {
  2150. //Loop until SPISR data register is empty
  2151. }
  2152.  
  2153. SPIDR = Selector; //Put data into SPISR data register
  2154. LCD_Delay(0);
  2155.  
  2156. Temp[LinVal] = output;
  2157. Selector -= 0x01;
  2158. LinVal++;
  2159. }
  2160.  
  2161. for (i = 0; i < 8; i++) {
  2162. for (j = 0; j < 8; j++) {
  2163.  
  2164. HallMatrix[i][j] = Temp[MatVal];
  2165. MatVal++;
  2166. }
  2167.  
  2168. }
  2169. return;
  2170. }
  2171.  
  2172. /*********************************************************************************
  2173. * LCD Specific Functions
  2174. **********************************************************************************
  2175. **********************************************************************************
  2176. * Name: LCD_Timer
  2177. * Description: Refreshes the chess timer.
  2178. * [->] (global): P1mins: Player 1 minutes remaining
  2179. * P1secs: Player 1 seconds remaining
  2180. * P2mins: Player 2 minutes remaining
  2181. * P2secs: Player 2 seconds remaining
  2182. * [<-] (void): [void]
  2183. *********************************************************************************/
  2184. void LCD_Timer(void) {
  2185. int t1 = 0;
  2186. int t2 = 0;
  2187.  
  2188. LineOffset = 0x00;
  2189. LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  2190.  
  2191. LCD_Cursor_Position(1); //"White: "
  2192. LCD_Send_Byte('W',0);
  2193. LCD_Cursor_Position(1);
  2194. LCD_Send_Byte('h',0);
  2195. LCD_Cursor_Position(1);
  2196. LCD_Send_Byte('i',0);
  2197. LCD_Cursor_Position(1);
  2198. LCD_Send_Byte('t',0);
  2199. LCD_Cursor_Position(1);
  2200. LCD_Send_Byte('e',0);
  2201. LCD_Cursor_Position(1);
  2202. LCD_Send_Byte(':',0);
  2203. LCD_Cursor_Position(1);
  2204. LCD_Send_Byte(' ',0);
  2205. LCD_Cursor_Position(1);
  2206. LCD_Send_Byte(' ',0);
  2207. LCD_Cursor_Position(1);
  2208. LCD_Send_Byte(' ',0);
  2209. LCD_Cursor_Position(1);
  2210. LCD_Send_Byte(' ',0);
  2211.  
  2212.  
  2213.  
  2214. t2 = P1mins % 10; //mins:secs,
  2215. t1 = P1mins / 10;
  2216.  
  2217. LCD_Cursor_Position(1);
  2218. LCD_Send_Byte(t1 + 0x30,0);
  2219. LCD_Cursor_Position(1);
  2220. LCD_Send_Byte(t2 + 0x30,0);
  2221.  
  2222. LCD_Cursor_Position(1);
  2223. LCD_Send_Byte(':',0);
  2224.  
  2225. t2 = P1secs % 10;
  2226. t1 = P1secs / 10;
  2227.  
  2228. LCD_Cursor_Position(1);
  2229. LCD_Send_Byte(t1 + 0x30,0);
  2230. LCD_Cursor_Position(1);
  2231. LCD_Send_Byte(t2 + 0x30,0);
  2232. //----------------------- line two begins
  2233. LineOffset = 0x00;
  2234. LCD_Cursor_Position(2);
  2235. LCD_Send_Byte('B',0);
  2236. LCD_Cursor_Position(2);
  2237. LCD_Send_Byte('l',0);
  2238. LCD_Cursor_Position(2);
  2239. LCD_Send_Byte('a',0);
  2240. LCD_Cursor_Position(2);
  2241. LCD_Send_Byte('c',0);
  2242. LCD_Cursor_Position(2);
  2243. LCD_Send_Byte('k',0);
  2244. LCD_Cursor_Position(2);
  2245. LCD_Send_Byte(':',0);
  2246. LCD_Cursor_Position(2);
  2247. LCD_Send_Byte(' ',0);
  2248. LCD_Cursor_Position(2);
  2249. LCD_Send_Byte(' ',0);
  2250. LCD_Cursor_Position(2);
  2251. LCD_Send_Byte(' ',0);
  2252. LCD_Cursor_Position(2);
  2253. LCD_Send_Byte(' ',0);
  2254.  
  2255. t2 = P2mins % 10;
  2256. t1 = P2mins / 10;
  2257.  
  2258. LCD_Cursor_Position(2);
  2259. LCD_Send_Byte(t1 + 0x30,0);
  2260. LCD_Cursor_Position(2);
  2261. LCD_Send_Byte(t2 + 0x30,0);
  2262.  
  2263. LCD_Cursor_Position(2);
  2264. LCD_Send_Byte(':',0);
  2265.  
  2266. t2 = P2secs % 10;
  2267. t1 = P2secs / 10;
  2268.  
  2269. LCD_Cursor_Position(2);
  2270. LCD_Send_Byte(t1 + 0x30,0);
  2271. LCD_Cursor_Position(2);
  2272. LCD_Send_Byte(t2 + 0x30,0);
  2273.  
  2274. LCD_Cursor_Position(2);
  2275. LCD_Send_Byte(' ',0);
  2276. return;
  2277. }
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283. /*********************************************************************************
  2284. * Name: LCD_Delay
  2285. * Description: Creates a delay of ~2ms or ~30cy using ASM NOPs.
  2286. * [->] (int): mode: Operational mode of the function.
  2287. * 0 = Short wait mode (~30cy)
  2288. * 1 = Long wait mode (~25,000cy) = (~2ms)
  2289. * [<-] (void): [void]
  2290. *********************************************************************************/
  2291. void LCD_Delay(int mode) {
  2292. int i = 0; //Wait loop counter
  2293.  
  2294. //Mode check
  2295. if (mode == 0) {
  2296. mode = 3;
  2297. } else if (mode == 1) {
  2298. mode = 2500;
  2299. } else {
  2300. mode = 250000000;
  2301.  
  2302. }
  2303.  
  2304. //Wait generation loop
  2305. for (i = 0; i <= mode; i++) {
  2306. asm {
  2307. nop
  2308. nop
  2309. nop
  2310. nop
  2311. nop
  2312. nop
  2313. nop
  2314. nop
  2315. nop
  2316. nop
  2317. }
  2318. }
  2319.  
  2320. return;
  2321. }
  2322.  
  2323. /*********************************************************************************
  2324. * Name: LCD_Cursor_Position
  2325. * Description: Moves the LCD Cursor vertically and horizontally.
  2326. * [->] (int): LineNumber: Declares the line number the cursor is to be moved to
  2327. * 1 = Cursor to be moved to line #1
  2328. * 2 = Cursor to be moved to line #2
  2329. * [<-] (void): [void]
  2330. *********************************************************************************/
  2331. void LCD_Cursor_Position(int LineNumber) {
  2332. LCD_Send_Byte(LCD_CURSOR, 1);
  2333.  
  2334. if (LineNumber == 1) {
  2335. LCD_Send_Byte((LCD_LINE1 + LineOffset), 1); //Instruction mode
  2336. } else {
  2337. LCD_Send_Byte((LCD_LINE2 + LineOffset), 1); //Instruction mode
  2338. }
  2339.  
  2340. LineOffset = LineOffset + 0x01;
  2341.  
  2342. return;
  2343. }
  2344.  
  2345. /*********************************************************************************
  2346. * Name: LCD_Send_Byte
  2347. * Description: Sends character or instruction through respective shift register.
  2348. * [->] (uchar): data: The byte of data to be sent through the shift register
  2349. * (int): mode: Operational mode of the function,
  2350. * 0 = Normal sending mode
  2351. * 1 = Instruction sending mode
  2352. * [<-] (void): [void]
  2353. *********************************************************************************/
  2354. void LCD_Send_Byte(char data, int mode) {
  2355.  
  2356. if (mode == 1) {
  2357. LCD_RS = 0; //Register select line low
  2358. }
  2359.  
  2360. while (SPISR_SPTEF == 0) {
  2361. //Loop until SPISR data register is empty
  2362. }
  2363.  
  2364. SPIDR = data; //Put data into SPISR data register
  2365.  
  2366. LCD_Delay(0); //Call a 30cy delay so that data can shift out
  2367.  
  2368. LCD_CLK = 0; //this may be some other clock i don't know
  2369. LCD_CLK = 1;
  2370.  
  2371. LCD_Delay(1); //Call a 2ms delay so LCD can respond
  2372.  
  2373. if (mode == 1) {
  2374. LCD_RS = 1; //Register select line high. This is the default.
  2375. }
  2376.  
  2377. return;
  2378. }
  2379.  
  2380. /*********************************************************************************
  2381. * Name: LCD_Piece_Select
  2382. * Description: Displays some text on the LCD to select a kind of piece.
  2383. * [->] (int): MsgType: The pre-rendered message to print to the LCD.
  2384. *
  2385. *
  2386. * X1 = pawn
  2387. * X2 = rook
  2388. * X3 = knight
  2389. * X4 = bishop
  2390. * X5 = Queen
  2391. *
  2392. * [<-] (void): [void]
  2393. *********************************************************************************/
  2394. void LCD_Piece_Select(int PieceType) {
  2395. LineOffset = 0x00;
  2396.  
  2397. LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  2398.  
  2399.  
  2400. LCD_Cursor_Position(1);
  2401. LCD_Send_Byte('S',0);
  2402. LCD_Cursor_Position(1);
  2403. LCD_Send_Byte('e',0);
  2404. LCD_Cursor_Position(1);
  2405. LCD_Send_Byte('l',0);
  2406. LCD_Cursor_Position(1);
  2407. LCD_Send_Byte('e',0);
  2408. LCD_Cursor_Position(1);
  2409. LCD_Send_Byte('c',0);
  2410. LCD_Cursor_Position(1);
  2411. LCD_Send_Byte('t',0);
  2412. LCD_Cursor_Position(1);
  2413. LCD_Send_Byte(' ',0);
  2414. LCD_Cursor_Position(1);
  2415. LCD_Send_Byte('p',0);
  2416. LCD_Cursor_Position(1);
  2417. LCD_Send_Byte('i',0);
  2418. LCD_Cursor_Position(1);
  2419. LCD_Send_Byte('e',0);
  2420. LCD_Cursor_Position(1);
  2421. LCD_Send_Byte('c',0);
  2422. LCD_Cursor_Position(1);
  2423. LCD_Send_Byte('e',0);
  2424. LCD_Cursor_Position(1);
  2425. LCD_Send_Byte(' ',0);
  2426. LCD_Cursor_Position(1);
  2427. LCD_Send_Byte(' ',0);
  2428. LCD_Cursor_Position(1);
  2429. LCD_Send_Byte(' ',0);
  2430. LineOffset = 0x00;
  2431.  
  2432. /* options for pawn to become:
  2433. * X2 = rook
  2434. * X3 = knight
  2435. * X4 = bishop
  2436. * X5 = Queen
  2437. *
  2438. */
  2439. if (PieceType == 2) {
  2440. LCD_Cursor_Position(2);
  2441. LCD_Send_Byte(' ',0);
  2442. LCD_Cursor_Position(2);
  2443. LCD_Send_Byte(' ',0);
  2444. LCD_Cursor_Position(2);
  2445. LCD_Send_Byte(' ',0);
  2446. LCD_Cursor_Position(2);
  2447. LCD_Send_Byte(' ',0);
  2448. LCD_Cursor_Position(2);
  2449. LCD_Send_Byte(' ',0);
  2450. LCD_Cursor_Position(2);
  2451. LCD_Send_Byte(' ',0);
  2452. LCD_Cursor_Position(2);
  2453. LCD_Send_Byte('R',0);
  2454. LCD_Cursor_Position(2);
  2455. LCD_Send_Byte('o',0);
  2456. LCD_Cursor_Position(2);
  2457. LCD_Send_Byte('o',0);
  2458. LCD_Cursor_Position(2);
  2459. LCD_Send_Byte('k',0);
  2460. LCD_Cursor_Position(2);
  2461. LCD_Send_Byte(' ',0);
  2462. LCD_Cursor_Position(2);
  2463. LCD_Send_Byte(' ',0);
  2464. LCD_Cursor_Position(2);
  2465. LCD_Send_Byte(' ',0);
  2466. LCD_Cursor_Position(2);
  2467. LCD_Send_Byte(' ',0);
  2468. LCD_Cursor_Position(2);
  2469. LCD_Send_Byte(' ',0);
  2470. LCD_Cursor_Position(2);
  2471. LCD_Send_Byte(' ',0);
  2472. }
  2473. if (PieceType == 3) {
  2474. LCD_Cursor_Position(2);
  2475. LCD_Send_Byte(' ',0);
  2476. LCD_Cursor_Position(2);
  2477. LCD_Send_Byte(' ',0);
  2478. LCD_Cursor_Position(2);
  2479. LCD_Send_Byte(' ',0);
  2480. LCD_Cursor_Position(2);
  2481. LCD_Send_Byte(' ',0);
  2482. LCD_Cursor_Position(2);
  2483. LCD_Send_Byte(' ',0);
  2484. LCD_Cursor_Position(2);
  2485. LCD_Send_Byte(' ',0);
  2486. LCD_Cursor_Position(2);
  2487. LCD_Send_Byte('K',0);
  2488. LCD_Cursor_Position(2);
  2489. LCD_Send_Byte('n',0);
  2490. LCD_Cursor_Position(2);
  2491. LCD_Send_Byte('i',0);
  2492. LCD_Cursor_Position(2);
  2493. LCD_Send_Byte('g',0);
  2494. LCD_Cursor_Position(2);
  2495. LCD_Send_Byte('h',0);
  2496. LCD_Cursor_Position(2);
  2497. LCD_Send_Byte('t',0);
  2498. LCD_Cursor_Position(2);
  2499. LCD_Send_Byte(' ',0);
  2500. LCD_Cursor_Position(2);
  2501. LCD_Send_Byte(' ',0);
  2502. LCD_Cursor_Position(2);
  2503. LCD_Send_Byte(' ',0);
  2504. LCD_Cursor_Position(2);
  2505. LCD_Send_Byte(' ',0);
  2506. }
  2507. if (PieceType == 4) {
  2508. LCD_Cursor_Position(2);
  2509. LCD_Send_Byte(' ',0);
  2510. LCD_Cursor_Position(2);
  2511. LCD_Send_Byte(' ',0);
  2512. LCD_Cursor_Position(2);
  2513. LCD_Send_Byte(' ',0);
  2514. LCD_Cursor_Position(2);
  2515. LCD_Send_Byte(' ',0);
  2516. LCD_Cursor_Position(2);
  2517. LCD_Send_Byte(' ',0);
  2518. LCD_Cursor_Position(2);
  2519. LCD_Send_Byte(' ',0);
  2520. LCD_Cursor_Position(2);
  2521. LCD_Send_Byte('B',0);
  2522. LCD_Cursor_Position(2);
  2523. LCD_Send_Byte('i',0);
  2524. LCD_Cursor_Position(2);
  2525. LCD_Send_Byte('s',0);
  2526. LCD_Cursor_Position(2);
  2527. LCD_Send_Byte('h',0);
  2528. LCD_Cursor_Position(2);
  2529. LCD_Send_Byte('o',0);
  2530. LCD_Cursor_Position(2);
  2531. LCD_Send_Byte('p',0);
  2532. LCD_Cursor_Position(2);
  2533. LCD_Send_Byte(' ',0);
  2534. LCD_Cursor_Position(2);
  2535. LCD_Send_Byte(' ',0);
  2536. LCD_Cursor_Position(2);
  2537. LCD_Send_Byte(' ',0);
  2538. LCD_Cursor_Position(2);
  2539. LCD_Send_Byte(' ',0);
  2540. }
  2541. if (PieceType == 5) {
  2542. LCD_Cursor_Position(2);
  2543. LCD_Send_Byte(' ',0);
  2544. LCD_Cursor_Position(2);
  2545. LCD_Send_Byte(' ',0);
  2546. LCD_Cursor_Position(2);
  2547. LCD_Send_Byte(' ',0);
  2548. LCD_Cursor_Position(2);
  2549. LCD_Send_Byte(' ',0);
  2550. LCD_Cursor_Position(2);
  2551. LCD_Send_Byte(' ',0);
  2552. LCD_Cursor_Position(2);
  2553. LCD_Send_Byte(' ',0);
  2554. LCD_Cursor_Position(2);
  2555. LCD_Send_Byte('Q',0);
  2556. LCD_Cursor_Position(2);
  2557. LCD_Send_Byte('u',0);
  2558. LCD_Cursor_Position(2);
  2559. LCD_Send_Byte('e',0);
  2560. LCD_Cursor_Position(2);
  2561. LCD_Send_Byte('e',0);
  2562. LCD_Cursor_Position(2);
  2563. LCD_Send_Byte('n',0);
  2564. LCD_Cursor_Position(2);
  2565. LCD_Send_Byte(' ',0);
  2566. LCD_Cursor_Position(2);
  2567. LCD_Send_Byte(' ',0);
  2568. LCD_Cursor_Position(2);
  2569. LCD_Send_Byte(' ',0);
  2570. LCD_Cursor_Position(2);
  2571. LCD_Send_Byte(' ',0);
  2572. LCD_Cursor_Position(2);
  2573. LCD_Send_Byte(' ',0);
  2574. }
  2575. }
  2576.  
  2577. /*********************************************************************************
  2578. * Name: LCD_Display
  2579. * Description: Displays some text on the chosen LCD.
  2580. * [->] (int): MsgType: The pre-rendered message to print to the LCD.
  2581. * 0 = "Press the shiny|button to start!"
  2582. * 1 = " --OPTION-- | >Start chess "
  2583. * 2 = " --OPTION-- | Edit timer "
  2584. * 3 = " --OPTION-- | Load Game "
  2585. * 4 = " --OPTION-- |< Save Game "
  2586. * 5 = " --PAUSED-- |Press to resume"
  2587. * [<-] (void): [void]
  2588. *********************************************************************************/
  2589. void LCD_Display(int MsgType) {
  2590. LineOffset = 0x00;
  2591.  
  2592. LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  2593.  
  2594. if (MsgType == 0) {
  2595. LCD_Cursor_Position(1);
  2596. LCD_Send_Byte('P',0);
  2597. LCD_Cursor_Position(1);
  2598. LCD_Send_Byte('r',0);
  2599. LCD_Cursor_Position(1);
  2600. LCD_Send_Byte('e',0);
  2601. LCD_Cursor_Position(1);
  2602. LCD_Send_Byte('s',0);
  2603. LCD_Cursor_Position(1);
  2604. LCD_Send_Byte('s',0);
  2605. LCD_Cursor_Position(1);
  2606. LCD_Send_Byte(' ',0);
  2607. LCD_Cursor_Position(1);
  2608. LCD_Send_Byte('t',0);
  2609. LCD_Cursor_Position(1);
  2610. LCD_Send_Byte('h',0);
  2611. LCD_Cursor_Position(1);
  2612. LCD_Send_Byte('e',0);
  2613. LCD_Cursor_Position(1);
  2614. LCD_Send_Byte(' ',0);
  2615. LCD_Cursor_Position(1);
  2616. LCD_Send_Byte('s',0);
  2617. LCD_Cursor_Position(1);
  2618. LCD_Send_Byte('h',0);
  2619. LCD_Cursor_Position(1);
  2620. LCD_Send_Byte('i',0);
  2621. LCD_Cursor_Position(1);
  2622. LCD_Send_Byte('n',0);
  2623. LCD_Cursor_Position(1);
  2624. LCD_Send_Byte('y',0);
  2625. LineOffset = 0x00;
  2626. LCD_Cursor_Position(2);
  2627. LCD_Send_Byte('b',0);
  2628. LCD_Cursor_Position(2);
  2629. LCD_Send_Byte('u',0);
  2630. LCD_Cursor_Position(2);
  2631. LCD_Send_Byte('t',0);
  2632. LCD_Cursor_Position(2);
  2633. LCD_Send_Byte('t',0);
  2634. LCD_Cursor_Position(2);
  2635. LCD_Send_Byte('o',0);
  2636. LCD_Cursor_Position(2);
  2637. LCD_Send_Byte('n',0);
  2638. LCD_Cursor_Position(2);
  2639. LCD_Send_Byte(' ',0);
  2640. LCD_Cursor_Position(2);
  2641. LCD_Send_Byte('t',0);
  2642. LCD_Cursor_Position(2);
  2643. LCD_Send_Byte('o',0);
  2644. LCD_Cursor_Position(2);
  2645. LCD_Send_Byte(' ',0);
  2646. LCD_Cursor_Position(2);
  2647. LCD_Send_Byte('s',0);
  2648. LCD_Cursor_Position(2);
  2649. LCD_Send_Byte('t',0);
  2650. LCD_Cursor_Position(2);
  2651. LCD_Send_Byte('a',0);
  2652. LCD_Cursor_Position(2);
  2653. LCD_Send_Byte('r',0);
  2654. LCD_Cursor_Position(2);
  2655. LCD_Send_Byte('t',0);
  2656. LCD_Cursor_Position(2);
  2657. LCD_Send_Byte('!',0);
  2658. } else if (MsgType == 1) {
  2659. LCD_Cursor_Position(1);
  2660. LCD_Send_Byte(' ',0);
  2661. LCD_Cursor_Position(1);
  2662. LCD_Send_Byte(' ',0);
  2663. LCD_Cursor_Position(1);
  2664. LCD_Send_Byte(' ',0);
  2665. LCD_Cursor_Position(1);
  2666. LCD_Send_Byte('-',0);
  2667. LCD_Cursor_Position(1);
  2668. LCD_Send_Byte('-',0);
  2669. LCD_Cursor_Position(1);
  2670. LCD_Send_Byte('O',0);
  2671. LCD_Cursor_Position(1);
  2672. LCD_Send_Byte('P',0);
  2673. LCD_Cursor_Position(1);
  2674. LCD_Send_Byte('T',0);
  2675. LCD_Cursor_Position(1);
  2676. LCD_Send_Byte('I',0);
  2677. LCD_Cursor_Position(1);
  2678. LCD_Send_Byte('O',0);
  2679. LCD_Cursor_Position(1);
  2680. LCD_Send_Byte('N',0);
  2681. LCD_Cursor_Position(1);
  2682. LCD_Send_Byte('-',0);
  2683. LCD_Cursor_Position(1);
  2684. LCD_Send_Byte('-',0);
  2685. LineOffset = 0x00;
  2686. LCD_Cursor_Position(2);
  2687. LCD_Send_Byte(' ',0);
  2688. LCD_Cursor_Position(2);
  2689. LCD_Send_Byte(0x7E,0);
  2690. LCD_Cursor_Position(2);
  2691. LCD_Send_Byte('S',0);
  2692. LCD_Cursor_Position(2);
  2693. LCD_Send_Byte('t',0);
  2694. LCD_Cursor_Position(2);
  2695. LCD_Send_Byte('a',0);
  2696. LCD_Cursor_Position(2);
  2697. LCD_Send_Byte('r',0);
  2698. LCD_Cursor_Position(2);
  2699. LCD_Send_Byte('t',0);
  2700. LCD_Cursor_Position(2);
  2701. LCD_Send_Byte(' ',0);
  2702. LCD_Cursor_Position(2);
  2703. LCD_Send_Byte('c',0);
  2704. LCD_Cursor_Position(2);
  2705. LCD_Send_Byte('h',0);
  2706. LCD_Cursor_Position(2);
  2707. LCD_Send_Byte('e',0);
  2708. LCD_Cursor_Position(2);
  2709. LCD_Send_Byte('s',0);
  2710. LCD_Cursor_Position(2);
  2711. LCD_Send_Byte('s',0);
  2712. LCD_Cursor_Position(2);
  2713. LCD_Send_Byte(' ',0);
  2714. LCD_Cursor_Position(2);
  2715. LCD_Send_Byte(' ',0);
  2716. LCD_Cursor_Position(2);
  2717. LCD_Send_Byte(' ',0);
  2718. } else if (MsgType == 2) {
  2719. LCD_Cursor_Position(1);
  2720. LCD_Send_Byte(' ',0);
  2721. LCD_Cursor_Position(1);
  2722. LCD_Send_Byte(' ',0);
  2723. LCD_Cursor_Position(1);
  2724. LCD_Send_Byte(' ',0);
  2725. LCD_Cursor_Position(1);
  2726. LCD_Send_Byte('-',0);
  2727. LCD_Cursor_Position(1);
  2728. LCD_Send_Byte('-',0);
  2729. LCD_Cursor_Position(1);
  2730. LCD_Send_Byte('O',0);
  2731. LCD_Cursor_Position(1);
  2732. LCD_Send_Byte('P',0);
  2733. LCD_Cursor_Position(1);
  2734. LCD_Send_Byte('T',0);
  2735. LCD_Cursor_Position(1);
  2736. LCD_Send_Byte('I',0);
  2737. LCD_Cursor_Position(1);
  2738. LCD_Send_Byte('O',0);
  2739. LCD_Cursor_Position(1);
  2740. LCD_Send_Byte('N',0);
  2741. LCD_Cursor_Position(1);
  2742. LCD_Send_Byte('-',0);
  2743. LCD_Cursor_Position(1);
  2744. LCD_Send_Byte('-',0);
  2745. LineOffset = 0x00;
  2746. LCD_Cursor_Position(2);
  2747. LCD_Send_Byte(' ',0);
  2748. LCD_Cursor_Position(2);
  2749. LCD_Send_Byte(' ',0);
  2750. LCD_Cursor_Position(2);
  2751. LCD_Send_Byte('E',0);
  2752. LCD_Cursor_Position(2);
  2753. LCD_Send_Byte('d',0);
  2754. LCD_Cursor_Position(2);
  2755. LCD_Send_Byte('i',0);
  2756. LCD_Cursor_Position(2);
  2757. LCD_Send_Byte('t',0);
  2758. LCD_Cursor_Position(2);
  2759. LCD_Send_Byte(' ',0);
  2760. LCD_Cursor_Position(2);
  2761. LCD_Send_Byte('T',0);
  2762. LCD_Cursor_Position(2);
  2763. LCD_Send_Byte('i',0);
  2764. LCD_Cursor_Position(2);
  2765. LCD_Send_Byte('m',0);
  2766. LCD_Cursor_Position(2);
  2767. LCD_Send_Byte('e',0);
  2768. LCD_Cursor_Position(2);
  2769. LCD_Send_Byte('r',0);
  2770. LCD_Cursor_Position(2);
  2771. LCD_Send_Byte(' ',0);
  2772. LCD_Cursor_Position(2);
  2773. LCD_Send_Byte(' ',0);
  2774. LCD_Cursor_Position(2);
  2775. LCD_Send_Byte(' ',0);
  2776. LCD_Cursor_Position(2);
  2777. LCD_Send_Byte(' ',0);
  2778. } else if (MsgType == 3) {
  2779. LCD_Cursor_Position(1);
  2780. LCD_Send_Byte(' ',0);
  2781. LCD_Cursor_Position(1);
  2782. LCD_Send_Byte(' ',0);
  2783. LCD_Cursor_Position(1);
  2784. LCD_Send_Byte(' ',0);
  2785. LCD_Cursor_Position(1);
  2786. LCD_Send_Byte('-',0);
  2787. LCD_Cursor_Position(1);
  2788. LCD_Send_Byte('-',0);
  2789. LCD_Cursor_Position(1);
  2790. LCD_Send_Byte('O',0);
  2791. LCD_Cursor_Position(1);
  2792. LCD_Send_Byte('P',0);
  2793. LCD_Cursor_Position(1);
  2794. LCD_Send_Byte('T',0);
  2795. LCD_Cursor_Position(1);
  2796. LCD_Send_Byte('I',0);
  2797. LCD_Cursor_Position(1);
  2798. LCD_Send_Byte('O',0);
  2799. LCD_Cursor_Position(1);
  2800. LCD_Send_Byte('N',0);
  2801. LCD_Cursor_Position(1);
  2802. LCD_Send_Byte('-',0);
  2803. LCD_Cursor_Position(1);
  2804. LCD_Send_Byte('-',0);
  2805. LineOffset = 0x00;
  2806. LCD_Cursor_Position(2);
  2807. LCD_Send_Byte(' ',0);
  2808. LCD_Cursor_Position(2);
  2809. LCD_Send_Byte(' ',0);
  2810. LCD_Cursor_Position(2);
  2811. LCD_Send_Byte('L',0);
  2812. LCD_Cursor_Position(2);
  2813. LCD_Send_Byte('o',0);
  2814. LCD_Cursor_Position(2);
  2815. LCD_Send_Byte('a',0);
  2816. LCD_Cursor_Position(2);
  2817. LCD_Send_Byte('d',0);
  2818. LCD_Cursor_Position(2);
  2819. LCD_Send_Byte(' ',0);
  2820. LCD_Cursor_Position(2);
  2821. LCD_Send_Byte('G',0);
  2822. LCD_Cursor_Position(2);
  2823. LCD_Send_Byte('a',0);
  2824. LCD_Cursor_Position(2);
  2825. LCD_Send_Byte('m',0);
  2826. LCD_Cursor_Position(2);
  2827. LCD_Send_Byte('e',0);
  2828. LCD_Cursor_Position(2);
  2829. LCD_Send_Byte(' ',0);
  2830. LCD_Cursor_Position(2);
  2831. LCD_Send_Byte(' ',0);
  2832. LCD_Cursor_Position(2);
  2833. LCD_Send_Byte(' ',0);
  2834. LCD_Cursor_Position(2);
  2835. LCD_Send_Byte(' ',0);
  2836. LCD_Cursor_Position(2);
  2837. LCD_Send_Byte(' ',0);
  2838. } else if (MsgType == 4) {
  2839. LCD_Cursor_Position(1);
  2840. LCD_Send_Byte(' ',0);
  2841. LCD_Cursor_Position(1);
  2842. LCD_Send_Byte(' ',0);
  2843. LCD_Cursor_Position(1);
  2844. LCD_Send_Byte(' ',0);
  2845. LCD_Cursor_Position(1);
  2846. LCD_Send_Byte('-',0);
  2847. LCD_Cursor_Position(1);
  2848. LCD_Send_Byte('-',0);
  2849. LCD_Cursor_Position(1);
  2850. LCD_Send_Byte('O',0);
  2851. LCD_Cursor_Position(1);
  2852. LCD_Send_Byte('P',0);
  2853. LCD_Cursor_Position(1);
  2854. LCD_Send_Byte('T',0);
  2855. LCD_Cursor_Position(1);
  2856. LCD_Send_Byte('I',0);
  2857. LCD_Cursor_Position(1);
  2858. LCD_Send_Byte('O',0);
  2859. LCD_Cursor_Position(1);
  2860. LCD_Send_Byte('N',0);
  2861. LCD_Cursor_Position(1);
  2862. LCD_Send_Byte('-',0);
  2863. LCD_Cursor_Position(1);
  2864. LCD_Send_Byte('-',0);
  2865. LineOffset = 0x00;
  2866. LCD_Cursor_Position(2);
  2867. LCD_Send_Byte(0x7F,0);
  2868. LCD_Cursor_Position(2);
  2869. LCD_Send_Byte(' ',0);
  2870. LCD_Cursor_Position(2);
  2871. LCD_Send_Byte('S',0);
  2872. LCD_Cursor_Position(2);
  2873. LCD_Send_Byte('a',0);
  2874. LCD_Cursor_Position(2);
  2875. LCD_Send_Byte('v',0);
  2876. LCD_Cursor_Position(2);
  2877. LCD_Send_Byte('e',0);
  2878. LCD_Cursor_Position(2);
  2879. LCD_Send_Byte(' ',0);
  2880. LCD_Cursor_Position(2);
  2881. LCD_Send_Byte('G',0);
  2882. LCD_Cursor_Position(2);
  2883. LCD_Send_Byte('a',0);
  2884. LCD_Cursor_Position(2);
  2885. LCD_Send_Byte('m',0);
  2886. LCD_Cursor_Position(2);
  2887. LCD_Send_Byte('e',0);
  2888. LCD_Cursor_Position(2);
  2889. LCD_Send_Byte(' ',0);
  2890. LCD_Cursor_Position(2);
  2891. LCD_Send_Byte(' ',0);
  2892. LCD_Cursor_Position(2);
  2893. LCD_Send_Byte(' ',0);
  2894. LCD_Cursor_Position(2);
  2895. LCD_Send_Byte(' ',0);
  2896. LCD_Cursor_Position(2);
  2897. LCD_Send_Byte(' ',0);
  2898. } else if (MsgType == 5) {
  2899. LCD_Cursor_Position(1);
  2900. LCD_Send_Byte('P',0);
  2901. LCD_Cursor_Position(1);
  2902. LCD_Send_Byte('u',0);
  2903. LCD_Cursor_Position(1);
  2904. LCD_Send_Byte('t',0);
  2905. LCD_Cursor_Position(1);
  2906. LCD_Send_Byte(' ',0);
  2907. LCD_Cursor_Position(1);
  2908. LCD_Send_Byte('E',0);
  2909. LCD_Cursor_Position(1);
  2910. LCD_Send_Byte('v',0);
  2911. LCD_Cursor_Position(1);
  2912. LCD_Send_Byte('e',0);
  2913. LCD_Cursor_Position(1);
  2914. LCD_Send_Byte('r',0);
  2915. LCD_Cursor_Position(1);
  2916. LCD_Send_Byte('y',0);
  2917. LCD_Cursor_Position(1);
  2918. LCD_Send_Byte(' ',0);
  2919. LCD_Cursor_Position(1);
  2920. LCD_Send_Byte(' ',0);
  2921. LCD_Cursor_Position(1);
  2922. LCD_Send_Byte(' ',0);
  2923. LCD_Cursor_Position(1);
  2924. LCD_Send_Byte(' ',0);
  2925. LineOffset = 0x00;
  2926. LCD_Cursor_Position(2);
  2927. LCD_Send_Byte(0x7F,0);
  2928. LCD_Cursor_Position(2);
  2929. LCD_Send_Byte('T',0);
  2930. LCD_Cursor_Position(2);
  2931. LCD_Send_Byte('h',0);
  2932. LCD_Cursor_Position(2);
  2933. LCD_Send_Byte('i',0);
  2934. LCD_Cursor_Position(2);
  2935. LCD_Send_Byte('n',0);
  2936. LCD_Cursor_Position(2);
  2937. LCD_Send_Byte('g',0);
  2938. LCD_Cursor_Position(2);
  2939. LCD_Send_Byte(' ',0);
  2940. LCD_Cursor_Position(2);
  2941. LCD_Send_Byte('B',0);
  2942. LCD_Cursor_Position(2);
  2943. LCD_Send_Byte('a',0);
  2944. LCD_Cursor_Position(2);
  2945. LCD_Send_Byte('c',0);
  2946. LCD_Cursor_Position(2);
  2947. LCD_Send_Byte('k',0);
  2948. LCD_Cursor_Position(2);
  2949. LCD_Send_Byte('!',0);
  2950. LCD_Cursor_Position(2);
  2951. LCD_Send_Byte(' ',0);
  2952. LCD_Cursor_Position(2);
  2953. LCD_Send_Byte(' ',0);
  2954. LCD_Cursor_Position(2);
  2955. LCD_Send_Byte(' ',0);
  2956. LCD_Cursor_Position(2);
  2957. LCD_Send_Byte(' ',0);
  2958. } else if (MsgType == 6) {
  2959. LCD_Cursor_Position(1);
  2960. LCD_Send_Byte('S',0);
  2961. LCD_Cursor_Position(1);
  2962. LCD_Send_Byte('e',0);
  2963. LCD_Cursor_Position(1);
  2964. LCD_Send_Byte('t',0);
  2965. LCD_Cursor_Position(1);
  2966. LCD_Send_Byte(' ',0);
  2967. LCD_Cursor_Position(1);
  2968. LCD_Send_Byte('d',0);
  2969. LCD_Cursor_Position(1);
  2970. LCD_Send_Byte('o',0);
  2971. LCD_Cursor_Position(1);
  2972. LCD_Send_Byte('w',0);
  2973. LCD_Cursor_Position(1);
  2974. LCD_Send_Byte('n',0);
  2975. LCD_Cursor_Position(1);
  2976. LCD_Send_Byte(' ',0);
  2977. LCD_Cursor_Position(1);
  2978. LCD_Send_Byte('o',0);
  2979. LCD_Cursor_Position(1);
  2980. LCD_Send_Byte('r',0);
  2981. LCD_Cursor_Position(1);
  2982. LCD_Send_Byte(' ',0);
  2983. LCD_Cursor_Position(1);
  2984. LCD_Send_Byte(' ',0);
  2985. LineOffset = 0x00;
  2986. LCD_Cursor_Position(2);
  2987. LCD_Send_Byte(0x7F,0);
  2988. LCD_Cursor_Position(2);
  2989. LCD_Send_Byte('p',0);
  2990. LCD_Cursor_Position(2);
  2991. LCD_Send_Byte('i',0);
  2992. LCD_Cursor_Position(2);
  2993. LCD_Send_Byte('c',0);
  2994. LCD_Cursor_Position(2);
  2995. LCD_Send_Byte('k',0);
  2996. LCD_Cursor_Position(2);
  2997. LCD_Send_Byte('.',0);
  2998. LCD_Cursor_Position(2);
  2999. LCD_Send_Byte('u',0);
  3000. LCD_Cursor_Position(2);
  3001. LCD_Send_Byte('p',0);
  3002. LCD_Cursor_Position(2);
  3003. LCD_Send_Byte(' ',0);
  3004. LCD_Cursor_Position(2);
  3005. LCD_Send_Byte('o',0);
  3006. LCD_Cursor_Position(2);
  3007. LCD_Send_Byte('t',0);
  3008. LCD_Cursor_Position(2);
  3009. LCD_Send_Byte('h',0);
  3010. LCD_Cursor_Position(2);
  3011. LCD_Send_Byte('e',0);
  3012. LCD_Cursor_Position(2);
  3013. LCD_Send_Byte('r',0);
  3014. LCD_Cursor_Position(2);
  3015. LCD_Send_Byte('.',0);
  3016. LCD_Cursor_Position(2);
  3017. LCD_Send_Byte(' ',0);
  3018.  
  3019. }
  3020.  
  3021. }
  3022.  
  3023.  
  3024. /*********************************************************************************
  3025. * Interrupt Functions
  3026. **********************************************************************************
  3027. **********************************************************************************
  3028. * Name: RTI_ISR
  3029. * Description: Interrupt at 2.048ms intervals. Checks pushbutton states.
  3030. * [->] (*): (*)
  3031. * [<-] (void): [void]
  3032. *********************************************************************************/
  3033. interrupt 7 void RTI_ISR(void)
  3034. {
  3035. CRGFLG = CRGFLG | 0x80; //Enable
  3036.  
  3037. //debug pushbuttons on docking board
  3038. /*if ((TOP_BTN == 0) && (new_TOP_BTN == 0)) {
  3039. new_TOP_BTN = 1;
  3040. }
  3041. if ((BTM_BTN == 0) && (new_BTM_BTN == 0)) {
  3042. new_BTM_BTN = 1;
  3043. }*/
  3044.  
  3045. if((RPG_BTN == 1) && (prevRPGbutton ==0)) {
  3046. newRPGbutton = 1;
  3047. }
  3048. prevRPGbutton = RPG_BTN;
  3049.  
  3050. if(RPG_A == 1) {
  3051. RPGRightTurns++;
  3052.  
  3053. if(RPGLeftTurns > 3) {
  3054. RPGLeft = 1;
  3055. }
  3056.  
  3057. RPGLeftTurns = 0;
  3058. }
  3059.  
  3060.  
  3061. if(RPG_B == 1) {
  3062. RPGLeftTurns++;
  3063.  
  3064. if(RPGRightTurns > 3) {
  3065. RPGRight = 1;
  3066. }
  3067.  
  3068. RPGRightTurns = 0;
  3069. }
  3070. }
  3071.  
  3072. /**********************************************************************************
  3073. * Name: TIM_ISR
  3074. * Description: TIM RTI at 10ms for 100 packets. Updates timer.
  3075. * [->] (*): (*)
  3076. * [<-] (void): [void]
  3077. **********************************************************************************/
  3078. interrupt 15 void TIM_ISR( void)
  3079. {
  3080. TFLG1 = TFLG1 | 0x80; //Enable
  3081.  
  3082. TimerCounter++; //Another 10ms have passed
  3083.  
  3084. if (TimerCounter == 100) { //100 10ms interrupts accumulated
  3085. //enable2 = 1;
  3086. //HALL_Input();
  3087. //enable2 = 0;
  3088.  
  3089. RefreshHall = 1;
  3090. if (PlayerToggle == 0) { //player is white
  3091. if (P1secs == 0) { //XX:00 is white timer
  3092. P1mins--;
  3093. P1secs = 59;
  3094.  
  3095. if(P1mins <= 0 && P1secs <= 0) { //timer lock at 00:00
  3096. P1mins = 0;
  3097. P1secs = 0;
  3098. TimerExpired = 1;
  3099. TimerRunning = 0;
  3100. TIE_C7I = 0;
  3101. }
  3102. } else { //XX:XX is white timer
  3103. P1secs--;
  3104. }
  3105. } else { //player is black
  3106. if (P2secs == 0) { //XX:00 is black timer
  3107. P2mins--;
  3108. P2secs = 59;
  3109.  
  3110. if(P2mins <= 0 && P2secs <= 0) { //timer lock at 00:00
  3111. P2mins = 0;
  3112. P2secs = 0;
  3113. TimerExpired = 1;
  3114. TimerRunning = 0;
  3115. TIE_C7I = 0;
  3116. }
  3117. } else { //XX:XX is black timer
  3118. P2secs--;
  3119. }
  3120. }
  3121. TimerRefresh = 1;
  3122. TimerCounter = 0;
  3123. }
  3124.  
  3125. if(scicountflag = 1){
  3126. scicount += 1;
  3127. if(scicount == 200){
  3128. //10 seconds have passed
  3129. scicount = 0;
  3130. SCICR2 = SCICR2 | 0x80;
  3131. scicountflag = 0;
  3132. }
  3133. }
  3134. }
  3135.  
  3136.  
  3137.  
  3138. /*********************************************************************************
  3139. * Sound Generation Functions
  3140. **********************************************************************************
  3141. **********************************************************************************
  3142. * Name: Play_Sound
  3143. * Description: Generates sounds based on what is current required.
  3144. * [->] (int): Mode: The type of sound that will be output.
  3145. * 0 = "Error" sound (buzz)
  3146. * 1 = "White's turn" sound (boop beep)
  3147. * 2 = "Black's turn" sound (beep boop)
  3148. * 3 = "Mystery" sound (whoop)
  3149. * [<-] (void): [void]
  3150. *********************************************************************************/
  3151. void Play_Sound(int mode) {
  3152. unsigned char a = 0; //Looping variables
  3153. unsigned char b = 0;
  3154. unsigned char c = 0;
  3155. unsigned char d = 0;
  3156. unsigned char e = 0;
  3157.  
  3158. if (mode == 0) {
  3159. for (a = 0; a <= 4; a++) {
  3160. for (b = 0xFF; b >= 0x01; b--) {
  3161. PWMPER0 = 0xFF;
  3162.  
  3163. for (c = 0x00; c <= 0xFE; c++) {
  3164. PWMDTY0 = c;
  3165.  
  3166. for (d = 0; d < 1; d++) {
  3167. for (e = 0; e < 1; e++) {
  3168. }
  3169. }
  3170. }
  3171.  
  3172. for (c = 0xFF; c >= 0x01; c--) {
  3173. PWMDTY0 = c;
  3174. for (d = 0; d < 1; d++) {
  3175. for (e = 0; e < 1; e++) {
  3176. }
  3177. }
  3178. }
  3179. }
  3180. for (d = 0; d < 1; d++) {
  3181. for (e = 0; e < 100; e++) {
  3182. }
  3183. }
  3184. }
  3185. PWMDTY0 = 0x00;
  3186.  
  3187. } else if (mode == 1) {
  3188. PWMDTY0 = 175;
  3189.  
  3190.  
  3191. for (a = 0; a < 0xFF; a++) {
  3192. for (b = 0; b < 0xFF; b++) {
  3193. for (c = 0; c < 0x0F; c++) {
  3194. //do nothing
  3195. }
  3196. }
  3197. }
  3198.  
  3199. PWMDTY0 = 225;
  3200.  
  3201. for (a = 0; a < 0xFF; a++) {
  3202. for (b = 0; b < 0xFF; b++) {
  3203. for (c = 0; c < 0x0F; c++) {
  3204. //do nothing
  3205. }
  3206. }
  3207. }
  3208.  
  3209. PWMDTY0 = 0x00;
  3210.  
  3211. } else if (mode == 2) {
  3212. PWMPER0 = 0xFF;
  3213. PWMDTY0 = 225;
  3214.  
  3215.  
  3216. for (a = 0; a < 0xFF; a++) {
  3217. for (b = 0; b < 0xFF; b++) {
  3218. for (c = 0; c < 0x0F; c++) {
  3219. //do nothing
  3220. }
  3221. }
  3222. }
  3223.  
  3224. PWMDTY0 = 175;
  3225.  
  3226. for (a = 0; a < 0xFF; a++) {
  3227. for (b = 0; b < 0xFF; b++) {
  3228. for (c = 0; c < 0x0F; c++) {
  3229. //do nothing
  3230. }
  3231. }
  3232. }
  3233.  
  3234. PWMDTY0 = 0x00;
  3235. } else if (mode == 3) {
  3236. for (a = 0; a < 0xFE; a++) {
  3237. PWMDTY0 = a;
  3238. for (b = 0; b < 0xFF; b++) {
  3239. for (c = 0; c < 0x0F; c++) {
  3240. //do nothing
  3241. }
  3242. }
  3243. }
  3244. for (a = 0; a < 0xFE; a++) {
  3245. PWMDTY0 = 0xFE - a;
  3246.  
  3247. for (b = 0; b < 0xFF; b++) {
  3248. for (c = 0; c < 0x0F; c++) {
  3249. //do nothing
  3250. }
  3251. }
  3252. }
  3253. PWMDTY0 = 0;
  3254. }
  3255.  
  3256. return;
  3257. }
  3258.  
  3259. /*********************************************************************************
  3260. * Character I/O Library Routines
  3261. **********************************************************************************
  3262. **********************************************************************************
  3263. * Name: inchar
  3264. * Description: Inputs ASCII character from SCI serial port and returns it.
  3265. * [->] (void): [void]
  3266. * [<-] (char): ASCII value from Teraterm .
  3267. *********************************************************************************/
  3268. char inchar(void) {
  3269. /* receives character from the terminal channel */
  3270. while (!(SCISR1 & 0x20)); /* wait for input */
  3271. return SCIDRL;
  3272. }
  3273.  
  3274. /*********************************************************************************
  3275. * Name: outchar
  3276. * Description: Outputs ASCII character to the SCI serial port.
  3277. * [->] (char): ASCII value.
  3278. * [<-] (void): [void]
  3279. *********************************************************************************/
  3280. void outchar(char ch) {
  3281. /* sends a character to the terminal channel */
  3282. while (!(SCISR1 & 0x80)); /* wait for output buffer empty */
  3283. SCIDRL = ch;
  3284. }
  3285.  
  3286. /**********************************************************************************
  3287. * Name: bco
  3288. * Description: Outputs character data in radial buffer to serial.
  3289. * [->] (*): (*)
  3290. * [<-] (void): [void]
  3291. **********************************************************************************/
  3292. void bco(char input)
  3293. {
  3294. while(((tin + 1) % tsize) == tout) {}
  3295. tbuf[tin] = input;
  3296. tin = (tin + 1) % tsize;
  3297.  
  3298. SCICR2 = SCICR2 | 0x80;
  3299. }
  3300.  
  3301. /*********************************************************************************
  3302. * ECE 362 - Mini-Project C Source File - Fall 2012
  3303. *********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment