CodeCodeCode

buttonbutton

Dec 3rd, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 35.94 KB | None | 0 0
  1. /*********************************************************************************
  2. * ECE 362 - Mini-Project C Source File - Fall 2012                    
  3. **********************************************************************************
  4. **********************************************************************************
  5. *                                                      
  6. * Team ID: 16
  7. *
  8. * Project Name: < chess >
  9. *
  10. * Team Members:
  11. *    Name: Mark Luzarowski              Signature:________________________________
  12. *    Mail: [email protected]
  13. *    Name: Micheal Standiford           Signature:________________________________
  14. *    Mail: [email protected]    
  15. *    Name: Mark Sears                   Signature:________________________________
  16. *    Mail: [email protected]
  17. *    Name: That other guy               Signature:________________________________
  18. *    Mail: [email protected]    
  19. *
  20. * Team Positions:    
  21. *
  22. *    Team/Doc Leader: < Mark Luzarowski >      Signature: ______________________
  23. *  
  24. *    Software Leader: < Mark Luzarowski >      Signature: ______________________
  25. *
  26. *    Interface Leader: < Micheal Standiford >  Signature: ______________________
  27. *
  28. *    Peripheral Leader: < Micheal Standiford > Signature: ______________________
  29. *
  30. *
  31. * Academic Honesty Statement:  In signing above, we hereby certify that we
  32. * are the individuals who created this HC(S)12 source file and that we have
  33. * not copied the work of any other student (past or present) while completing
  34. * it. We understand that if we fail to honor this agreement, we will receive
  35. * a grade of ZERO and be subject to possible disciplinary action.
  36. *
  37. **********************************************************************************
  38. * Project Purpose
  39. **********************************************************************************
  40. * The objective of this Mini-Project is to create a game board the can as a multi-
  41. * game base; games planned so far are chess and checkers. The board itself will
  42. * function as a "learning board" of sorts for any game programmed into it.
  43. *
  44. **********************************************************************************
  45. * Project-specific Success Criteria
  46. **********************************************************************************
  47. * 1. Piece tracking & movement analysis
  48. *
  49. * 2. Time & turn tracking
  50. *
  51. * 3. User-side game editing
  52. *
  53. **********************************************************************************
  54. * Change Log
  55. **********************************************************************************
  56. *  Date code started: November 13, 2012
  57. *
  58. *  Update history:
  59. *
  60. *  Date: November 13, 2012  
  61. *  Name: Mark Luzarowski  
  62. *  Update: Added generalized functions for LCD communications.
  63. *          Added initializations skeleton.
  64. * -------------------------------------------------------------------------------
  65. *  Date:
  66. *  Name:
  67. *  Update:  
  68. *
  69. ***********************************************************************************
  70. **********************************************************************************/
  71. #include <hidef.h>       // common defines and macros
  72. #include "derivative.h"  // derivative-specific definitions
  73. #include <mc9s12c32.h>   // 9S12 register & port defines
  74.  
  75. //Dictionary defines
  76. #define LCD_RS PTT_PTT2 //NOT FINAL
  77. #define LCD_CLK PTT_PTT4 //NOT FINAL
  78. #define RPG_A PTT_PTT5
  79. #define RPG_B PTT_PTT6
  80. #define shiny_button PTT_PTT7
  81.  
  82. //Debug defines
  83. #define TOP_BTN PTAD_PTAD7 //top pushbutton
  84. #define BTM_BTN PTAD_PTAD6 //bottom pushbutton
  85. //#define SEL_BTN PTAD_PTAD5
  86.  
  87. // All funtions after main should be initialiezed here
  88. char inchar(void);
  89. void outchar(char);
  90. void LCD_Delay(int);
  91. void LCD_Cursor_Position(int);
  92. void LCD_Send_Byte(char, int);
  93. void LCD_Display(int);
  94. void bco(char);
  95. void LCD_Timer(void);
  96. void LED_Out(void);
  97. void LED_Convert(void);
  98. void Chess_Locations(int);
  99.  
  100. //debug
  101. int new_TOP_BTN = 0;
  102. int new_BTM_BTN = 0;
  103. int scroller = 0;
  104. int bcnt = 0;
  105.  
  106. int rightcount = 0;
  107. int leftcount = 0;
  108. int rightturn = 0;  //set true if one tick of right turn detected
  109. int leftturn = 0;   //set true if one tick of left turn detected
  110.  
  111. /*int new_RPGA = 0;
  112. int new_RPGB = 0;
  113. int old_RPGA = 0;
  114. int old_RPGB = 0;
  115. */
  116.  
  117. int new_shiny_button = 0;
  118. int prev_shiny_button = 0;
  119.  
  120. //Global variable declarations                                                     
  121. int tin  = 0;  // SCI transmit display buffer IN pointer
  122. int tout     = 0;  // SCI transmit display buffer OUT pointer
  123. int tsize    = 10; // size of transmit buffer
  124. char tbuf[10];  // SCI transmit display buffer  
  125. char LineOffset = 0x00;
  126. int P1mins = 20;
  127. int P1secs = 0;
  128. int P2mins = 20;
  129. int P2secs = 0;
  130. int PlayerToggle = 0;
  131. int timerup = 0;
  132. int timeExpired = 0;
  133. int HallMatrix[8][8];
  134. int ChessMatrix[8][8];
  135. unsigned char LEDOutData[8];
  136. int en1 = 0;
  137. int en2 = 0;
  138.  
  139. //ASCII character constants
  140. char ASCII_CR = 0x0D; //Return
  141. char ASCII_LF = 0x0A; //Lifefeed  
  142.  
  143. //LCD specific constants
  144. char LCD_LINE1  = 0x80; //LCD line #1
  145. char LCD_LINE2  = 0xC0; //LCD line #2
  146. char LCD_ON     = 0x0F; //LCD turn on instruction
  147. char LCD_CLEAR  = 0x01; //LCD clear instruction
  148. char LCD_2LINE  = 0x38; //LCD use both lines instruction
  149. char LCD_CURSOR = 0xFE; //LCD cursor move instruction
  150.                
  151. /***********************************************************************
  152. Initializations
  153. ***********************************************************************/
  154. void  initializations(void) {
  155.  
  156.      // Set the PLL speed (bus clock = 24 MHz)
  157.      CLKSEL = CLKSEL & 0x80; // disengage PLL from system
  158.      PLLCTL = PLLCTL | 0x40; // turn on PLL
  159.      SYNR = 0x02;            // set PLL multiplier
  160.      REFDV = 0;              // set PLL divider
  161.      while (!(CRGFLG & 0x08)){  }
  162.      CLKSEL = CLKSEL | 0x80; // engage PLL
  163.  
  164.      // Disable watchdog timer (COPCTL register)
  165.      COPCTL = 0x40; // COP off; RTI and COP stopped in BDM-mode
  166.  
  167.      // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  168.      SCIBDH = 0x00; // set baud rate to 9600
  169.      SCIBDL = 0x9C; // 24,000,000 / 16 / 156 = 9600 (approx)  dec=26
  170.      SCICR1 = 0x00; // $9C = 156
  171.      SCICR2 = 0x0C; // initialize SCI for program-driven operation
  172.      DDRB   = 0x10; // set PB4 for output mode
  173.      PORTB  = 0x10; // assert DTR pin on COM port
  174.  
  175.      //RTI Initialization (2.048ms)
  176.      CRGINT = CRGINT | 0x80; //RTI enabled
  177.      RTICTL = 0x1F; //
  178.    
  179.  
  180.      //SPI Initialization (6MHz)
  181.      SPICR1 = 0x52; //SPI control register #1
  182.                     //[Bit 7] SPIE  = 0 | SPI interrupts disabled
  183.                     //[Bit 6] SPE   = 1 | SPI enabled
  184.                     //[Bit 5] SPTIE = 0 | SPI transmit empty interrupt disabled
  185.                     //[Bit 4] MSTR  = 1 | Master mode
  186.                     //[Bit 3] CPOL  = 0 | Active low clock (SCK) - idles low
  187.                     //[Bit 2] CPHA  = 0 | Data sampled at odd edges of SCK
  188.                     //[Bit 1] SSOE  = 1 | Slave select output enable
  189.                     //[Bit 0] LSBFE = 0 | Data transferred most significant bit first
  190.      SPICR2 = 0x00;
  191.      SPIBR  = 0x01; //Baud rate register
  192.                     //[Bit 6-4] SPPRx = 000 | Baud rate pre-selection bits
  193.                     //[Bit 2-0] SPRx  = 001 | Baud rate selection bits
  194.                     //SPI rate = BusClock / [(SPPR + 1) * 2^(SPR + 1)]
  195.  
  196.      //TIM Initialization (10ms)
  197.      TSCR1_TEN = 1; //TIM enable
  198.      TIOS_IOS7 = 1; //Output compare on ch7
  199.      TSCR2_TCRE = 1; //TCNT reset when OC7 occurs
  200.      TSCR2_PR2 = 1; //24MHz / 16 = 1.5MHz
  201.      TSCR2_PR1 = 0;
  202.      TSCR2_PR0 = 0;
  203.      TC7 = 15000; //15000 / 1.5MHz = 10ms
  204.      TIE_C7I = 0; //TIM interrupts disabled
  205.      
  206.      //PMW Initialization
  207.      PWME = 0x01; //PMW enable
  208.      PWMPOL = 0x01; //PWM polarity select (active high)
  209.      PWMPER0 = 0xFF; //period
  210.      PWMDTY0 = 0xFF; //duty
  211.      PWMPRCLK = 0x04; //24MHz/4 = 6MHz
  212.      PWMCLK = 0x01;
  213.      PWMSCLA = 75;
  214.      PWMSCLB = 0;
  215.      MODRR = 0x01;
  216.      PWMCAE = 0;
  217.      
  218.  
  219.      //Port Initializations
  220.      //ATDDIEN = 0xC0; //program PAD7 and PAD6 pins as digital inputs
  221.      ATDDIEN = 0xE0;
  222.      DDRT = 0x1F; // 00011111 T5, T6, T7 are inputs from the shiny button
  223.      PTT = 0;
  224.  
  225.      //LCD Initializations
  226.      LCD_CLK = 1;
  227.      LCD_Send_Byte(LCD_ON, 1);
  228.      LCD_Send_Byte(LCD_2LINE, 1);
  229.      LCD_Send_Byte(LCD_CLEAR, 1);
  230.      LCD_Delay(1);
  231.      LCD_Display(0);                                                   
  232. }
  233.                                        
  234. /***********************************************************************
  235. Main
  236. ***********************************************************************/
  237. void main(void) {
  238.      DisableInterrupts;
  239.     initializations();                                 
  240.     EnableInterrupts;
  241.  
  242.      for(;;) {
  243.           //debug btn code
  244.           if ((BTM_BTN == 1) && (new_BTM_BTN == 1)) {
  245.                scroller = (scroller + 1) % 3;
  246.                LCD_Display(scroller);
  247.                
  248.                Chess_Locations(0);
  249.                LED_Convert();
  250.                
  251.                outchar('b');
  252.                new_BTM_BTN = 0;
  253.           }
  254.  
  255.           if ((TOP_BTN == 1) && (new_TOP_BTN == 1)) {
  256.                scroller = 4;   //lock at GAME TIMER MODE
  257.                TIE_C7I = 1; //enable TIM interrupts
  258.                outchar('t');
  259.                new_TOP_BTN = 0;
  260.           }
  261.          
  262.            if ((new_shiny_button == 1)) {
  263.            outchar('s');
  264.            new_shiny_button = 0;
  265.            
  266.            if(scroller == 4) //if the game is already going
  267.                 PlayerToggle = !PlayerToggle; // toggle the active player
  268.            
  269.            else if(scroller == 3)
  270.            {
  271.             scroller = 1;        
  272.             LCD_Display(scroller);    
  273.            } else if(scroller == 2)
  274.            {
  275.             scroller = 3; //edit start time mode
  276.             LCD_Display(scroller);
  277.            } else if(scroller == 1)
  278.            {
  279.             PlayerToggle = 0;
  280.             scroller = 4; //start the game
  281.             TIE_C7I = 1; //enable TIM interrupts
  282.            }
  283.              
  284.            //enter the game menu
  285.            else if(scroller == 0)
  286.            {
  287.             outchar('0');
  288.             scroller = 1;   //lock at GAME TIMER MODE
  289.             LCD_Display(scroller);  
  290.            }
  291.                                                                    
  292.            }
  293.          
  294.           if (timerup == 1) {
  295.                LCD_Timer();
  296.                timerup = 0;
  297.           }
  298.          
  299.          
  300.         if(rightturn)
  301.         {
  302.             outchar('r');
  303.             rightturn = 0;
  304.             if(scroller != 4)
  305.             {                            
  306.            
  307.            if(scroller == 1)
  308.             scroller = 2;
  309.            else if(scroller ==2)
  310.             scroller = 1;
  311.            
  312.            
  313.              if(scroller == 3)
  314.              {
  315.                 P1mins++;
  316.                 P2mins++;
  317.              }
  318.                
  319.             LCD_Display(scroller);
  320.                
  321.             Chess_Locations(0);
  322.             LED_Convert();
  323.             }
  324.         }
  325.              
  326.         if(leftturn)
  327.         {
  328.             outchar('l');
  329.             leftturn = 0;
  330.            
  331.             if(scroller != 4)
  332.             {  
  333.            if(scroller == 1)
  334.             scroller = 2;
  335.            else if(scroller ==2)
  336.             scroller = 1;
  337.              
  338.              if(scroller == 3)
  339.              {
  340.                 P1mins--;
  341.                 P2mins--;
  342.              }
  343.                
  344.             LCD_Display(scroller);            
  345.             }
  346.         }
  347.        
  348.    
  349.     _FEED_COP(); /* feeds the dog */
  350.   } /* loop forever */
  351.   /* please make sure that you never leave main */
  352. }
  353.  
  354. /*********************************************************************************
  355. * Chess Control Functions
  356. **********************************************************************************
  357. **********************************************************************************
  358. * Name:          Chess_Locations
  359. * Description:   Manages vitual piece location map.
  360. * [->] (global): ChessMatrix: Map of the board.
  361. *                      00 = empty space
  362. *                      X1 = pawn
  363. *                      X2 = rook
  364. *                      X3 = knight
  365. *                      X4 = bishop
  366. *                      X5 = Queen
  367. *                      X6 = King
  368. *                      1X = White piece
  369. *                      2X = Black piece                    
  370. *         (int): Mode: Mode of map.
  371. *                      0 = Initialization for new game.
  372. *                      1 = Standard
  373. * [<-]   (void): [void]
  374. *********************************************************************************/
  375. void Chess_Locations(int Mode) {
  376.  
  377.      if (Mode == 0) {
  378.           ChessMatrix[0][0] = 12;
  379.           ChessMatrix[0][1] = 13;
  380.           ChessMatrix[0][2] = 14;
  381.           ChessMatrix[0][3] = 15;
  382.           ChessMatrix[0][4] = 16;
  383.           ChessMatrix[0][5] = 14;
  384.           ChessMatrix[0][6] = 13;
  385.           ChessMatrix[0][7] = 12;
  386.           ChessMatrix[1][0] = 11;
  387.           ChessMatrix[1][1] = 11;
  388.           ChessMatrix[1][2] = 11;
  389.           ChessMatrix[1][3] = 11;
  390.           ChessMatrix[1][4] = 11;
  391.           ChessMatrix[1][5] = 11;
  392.           ChessMatrix[1][6] = 11;
  393.           ChessMatrix[1][7] = 11;
  394.           ChessMatrix[2][0] = 0;
  395.           ChessMatrix[2][1] = 0;
  396.           ChessMatrix[2][2] = 0;
  397.           ChessMatrix[2][3] = 0;
  398.           ChessMatrix[2][4] = 0;
  399.           ChessMatrix[2][5] = 0;
  400.           ChessMatrix[2][6] = 0;
  401.           ChessMatrix[2][7] = 0;
  402.           ChessMatrix[3][0] = 0;
  403.           ChessMatrix[3][1] = 0;
  404.           ChessMatrix[3][2] = 0;
  405.           ChessMatrix[3][3] = 0;
  406.           ChessMatrix[3][4] = 0;
  407.           ChessMatrix[3][5] = 0;
  408.           ChessMatrix[3][6] = 0;
  409.           ChessMatrix[3][7] = 0;
  410.           ChessMatrix[4][0] = 0;
  411.           ChessMatrix[4][1] = 0;
  412.           ChessMatrix[4][2] = 0;
  413.           ChessMatrix[4][3] = 0;
  414.           ChessMatrix[4][4] = 0;
  415.           ChessMatrix[4][5] = 0;
  416.           ChessMatrix[4][6] = 0;
  417.           ChessMatrix[4][7] = 0;
  418.           ChessMatrix[5][0] = 0;
  419.           ChessMatrix[5][1] = 0;
  420.           ChessMatrix[5][2] = 0;
  421.           ChessMatrix[5][3] = 0;
  422.           ChessMatrix[5][4] = 0;
  423.           ChessMatrix[5][5] = 0;
  424.           ChessMatrix[5][6] = 0;
  425.           ChessMatrix[5][7] = 0;
  426.           ChessMatrix[6][0] = 21;
  427.           ChessMatrix[6][1] = 21;
  428.           ChessMatrix[6][2] = 21;
  429.           ChessMatrix[6][3] = 21;
  430.           ChessMatrix[6][4] = 21;
  431.           ChessMatrix[6][5] = 21;
  432.           ChessMatrix[6][6] = 21;
  433.           ChessMatrix[6][7] = 21;
  434.           ChessMatrix[7][0] = 22;
  435.           ChessMatrix[7][1] = 23;
  436.           ChessMatrix[7][2] = 24;
  437.           ChessMatrix[7][3] = 26;
  438.           ChessMatrix[7][4] = 25;
  439.           ChessMatrix[7][5] = 24;
  440.           ChessMatrix[7][6] = 23;
  441.           ChessMatrix[7][7] = 22;
  442.      }
  443.      
  444.      if (Mode == 1) { //Update locations map
  445.        
  446.      }
  447.      return;    
  448. }
  449.  
  450. /*********************************************************************************
  451. * LED Matrix Functions
  452. **********************************************************************************
  453. **********************************************************************************
  454. * Name:          LED_Out
  455. * Description:   Shifts out the updated LED matrix data.
  456. * [->] (global): datas
  457. * [<-]   (void): [void]
  458. *********************************************************************************/
  459. void LED_Out(void) {
  460.      int i = 0;
  461.      
  462.      for (i = 0; i < 8; i++) {
  463.           while (SPISR_SPTEF == 0) {
  464.                //Loop until SPISR data register is empty
  465.           }
  466.      
  467.           SPIDR = LEDOutData[i]; //Put data into SPISR data register
  468.  
  469.           LCD_Delay(0); //Call a delay for shifting out
  470.          
  471.           //enable daisy chain disable LCD
  472.      }
  473.      return;    
  474. }
  475.  
  476. /*********************************************************************************
  477. * Name:          LED_Convert
  478. * Description:   Converts chess data to LED data.
  479. * [->] (global): datas
  480. * [<-]   (void): [void]
  481. *********************************************************************************/
  482. void LED_Convert(void) {
  483.      int i = 0;
  484.      int j = 0;
  485.      int k = 0;
  486.      int temp = 1;
  487.      
  488.      for (i = 0; i < 8; i++) {  //Chess column loop
  489.           LEDOutData[i] = 0; //Clear of former data.
  490.          
  491.           for (j = 0; j < 8; j++) { //Chess row loop
  492.                if (ChessMatrix[i][j] != 0) { //if piece exists on tile
  493.                     temp = 1;
  494.                
  495.                     for (k = 0; k < j; k++) { //shifting data calculation
  496.                          temp = temp * 2;  
  497.                          
  498.                     }
  499.                     LEDOutData[i] += temp;
  500.                }
  501.           }
  502.      }
  503.      
  504.      return;    
  505. }
  506.  
  507. /*********************************************************************************
  508. * Hall Sensor Functions
  509. **********************************************************************************
  510. **********************************************************************************
  511. * Name:          HALL_Input
  512. * Description:   Collects magnet connection data.
  513. * [->] (global): datas
  514. * [<-]   (void): [void]
  515. *********************************************************************************/
  516. void HALL_Input(void) {
  517.      int i = 0;
  518.      int j = 0;
  519.      
  520.      
  521.      for (i = 0; i < 8; i++) {
  522.           for (j = 0; j < 8; j++) {
  523.                    
  524.                   //write to HallMatrix[][]
  525.           }
  526.          
  527.      }
  528.      return;    
  529. }
  530.  
  531. /*********************************************************************************
  532. * LCD Specific Functions
  533. **********************************************************************************
  534. **********************************************************************************                      
  535. * Name:          LCD_Timer
  536. * Description:   Refreshes the chess timer.
  537. * [->] (global): P1mins: Player 1 minutes remaining
  538. *                P1secs: Player 1 seconds remaining
  539. *                P2mins: Player 2 minutes remaining
  540. *                P2secs: Player 2 seconds remaining
  541. * [<-]   (void): [void]                                
  542. *********************************************************************************/
  543. void LCD_Timer(void) {
  544.      int t1 = 0;
  545.      int t2 = 0;
  546.      
  547.      LineOffset = 0x00;
  548.      LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  549.      
  550.      LCD_Cursor_Position(1);
  551.      LCD_Send_Byte('W',0);
  552.      LCD_Cursor_Position(1);
  553.      LCD_Send_Byte('h',0);
  554.      LCD_Cursor_Position(1);
  555.      LCD_Send_Byte('i',0);
  556.      LCD_Cursor_Position(1);
  557.      LCD_Send_Byte('t',0);
  558.      LCD_Cursor_Position(1);
  559.      LCD_Send_Byte('e',0);
  560.      LCD_Cursor_Position(1);
  561.      LCD_Send_Byte(':',0);
  562.      LCD_Cursor_Position(1);
  563.      LCD_Send_Byte(' ',0);
  564.      LCD_Cursor_Position(1);
  565.      LCD_Send_Byte(' ',0);
  566.      LCD_Cursor_Position(1);
  567.      LCD_Send_Byte(' ',0);
  568.      LCD_Cursor_Position(1);
  569.      LCD_Send_Byte(' ',0);
  570.      
  571.      
  572.      
  573.      t2 = P1mins % 10;
  574.      t1 = P1mins / 10;
  575.      
  576.      LCD_Cursor_Position(1);
  577.      LCD_Send_Byte(t1 + 0x30,0);
  578.      LCD_Cursor_Position(1);
  579.      LCD_Send_Byte(t2 + 0x30,0);
  580.      
  581.      LCD_Cursor_Position(1);
  582.      LCD_Send_Byte(':',0);
  583.      
  584.      t2 = P1secs % 10;
  585.      t1 = P1secs / 10;
  586.      
  587.      LCD_Cursor_Position(1);
  588.      LCD_Send_Byte(t1 + 0x30,0);
  589.      LCD_Cursor_Position(1);
  590.      LCD_Send_Byte(t2 + 0x30,0);
  591.      //-----------------------
  592.      LineOffset = 0x00;
  593.      LCD_Cursor_Position(2);
  594.      LCD_Send_Byte('B',0);
  595.      LCD_Cursor_Position(2);
  596.      LCD_Send_Byte('l',0);
  597.      LCD_Cursor_Position(2);
  598.      LCD_Send_Byte('a',0);
  599.      LCD_Cursor_Position(2);
  600.      LCD_Send_Byte('c',0);
  601.      LCD_Cursor_Position(2);
  602.      LCD_Send_Byte('k',0);
  603.      LCD_Cursor_Position(2);
  604.      LCD_Send_Byte(':',0);
  605.      LCD_Cursor_Position(2);
  606.      LCD_Send_Byte(' ',0);
  607.      LCD_Cursor_Position(2);
  608.      LCD_Send_Byte(' ',0);
  609.      LCD_Cursor_Position(2);
  610.      LCD_Send_Byte(' ',0);
  611.      LCD_Cursor_Position(2);
  612.      LCD_Send_Byte(' ',0);
  613.      
  614.      t2 = P2mins % 10;
  615.      t1 = P2mins / 10;
  616.      
  617.      LCD_Cursor_Position(2);
  618.      LCD_Send_Byte(t1 + 0x30,0);
  619.      LCD_Cursor_Position(2);
  620.      LCD_Send_Byte(t2 + 0x30,0);
  621.      
  622.      LCD_Cursor_Position(2);
  623.      LCD_Send_Byte(':',0);
  624.      
  625.      t2 = P2secs % 10;
  626.      t1 = P2secs / 10;
  627.      
  628.      LCD_Cursor_Position(2);
  629.      LCD_Send_Byte(t1 + 0x30,0);
  630.      LCD_Cursor_Position(2);
  631.      LCD_Send_Byte(t2 + 0x30,0);
  632.      
  633.      LCD_Cursor_Position(2);
  634.      LCD_Send_Byte(' ',0);
  635.      return;    
  636. }
  637.  
  638. /*********************************************************************************                      
  639. * Name:        LCD_Delay
  640. * Description: Creates a delay of ~2ms or ~30cy using ASM NOPs.
  641. * [->]  (int): mode: Operational mode of the function.
  642. *                    0 = Short wait mode (~30cy)
  643. *                    1 = Long wait mode (~25,000cy) = (~2ms)
  644. * [<-] (void): [void]                                  
  645. *********************************************************************************/
  646. void LCD_Delay(int mode) {
  647.      int i = 0; //Wait loop counter
  648.      
  649.      //Mode check
  650.      if (mode == 0) {
  651.           mode = 3;
  652.      } else {          
  653.           mode = 2500;
  654.      }
  655.      
  656.      //Wait generation loop
  657.      for (i = 0; i <= mode; i++) {
  658.           asm {
  659.                nop
  660.                nop
  661.                nop
  662.                nop
  663.                nop
  664.                nop
  665.                nop
  666.                nop
  667.                nop
  668.                nop
  669.           }
  670.      }
  671.      
  672.      return;
  673. }
  674.  
  675. /*********************************************************************************                      
  676. * Name:        LCD_Cursor_Position
  677. * Description: Moves the LCD Cursor vertically and horizontally.
  678. * [->]  (int): LineNumber: Declares the line number the cursor is to be moved to
  679. *                          1 = Cursor to be moved to line #1
  680. *                          2 = Cursor to be moved to line #2
  681. * [<-] (void): [void]                                  
  682. *********************************************************************************/
  683. void LCD_Cursor_Position(int LineNumber) {
  684.      LCD_Send_Byte(LCD_CURSOR, 1);
  685.  
  686.      if (LineNumber == 1) {
  687.           LCD_Send_Byte((LCD_LINE1 + LineOffset), 1); //Instruction mode
  688.      } else {
  689.           LCD_Send_Byte((LCD_LINE2 + LineOffset), 1); //Instruction mode
  690.      }
  691.      
  692.      LineOffset = LineOffset + 0x01;
  693.  
  694.      return;  
  695. }
  696.  
  697. /*********************************************************************************                      
  698. * Name:         LCD_Send_Byte
  699. * Description:  Sends character or instruction through respective shift register.
  700. * [->] (uchar): data: The byte of data to be sent through the shift register
  701. *        (int): mode: Operational mode of the function,
  702. *                     0 = Normal sending mode
  703. *                     1 = Instruction sending mode
  704. * [<-]  (void): [void]                                 
  705. *********************************************************************************/
  706. void LCD_Send_Byte(char data, int mode) {
  707.      
  708.      if (mode == 1) {
  709.           LCD_RS = 0; //Register select line low
  710.      }
  711.      
  712.      while (SPISR_SPTEF == 0) {
  713.           //Loop until SPISR data register is empty
  714.      }
  715.      
  716.      SPIDR = data; //Put data into SPISR data register
  717.      
  718.      LCD_Delay(0); //Call a 30cy delay so that data can shift out
  719.      
  720.      LCD_CLK = 0; //this may be some other clock i don't know
  721.      LCD_CLK = 1;
  722.      
  723.      LCD_Delay(1); //Call a 2ms delay so LCD can respond
  724.      
  725.      if (mode == 1) {
  726.           LCD_RS = 1; //Register select line high. This is the default.  
  727.      }
  728.      
  729.      return;        
  730. }
  731.  
  732. /*********************************************************************************                      
  733. * Name:        LCD_Display
  734. * Description: Displays some text on the chosen LCD.
  735. * [->]  (int): MsgType: The pre-rendered message to print to the LCD.
  736. *                       0 = "Press the shiny|button to start!"
  737. *                       1 = "   --OPTION--   | >Start chess"
  738. *                       2 = "   --OPTION--   |< Edit timer"
  739. *                       3 = "   --PAUSED--   |Press to resume"                    
  740. * [<-] (void): [void]                                  
  741. *********************************************************************************/
  742. void LCD_Display(int MsgType) {
  743.      LineOffset = 0x00;
  744.  
  745.      LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
  746.    
  747.      if (MsgType == 0) {
  748.           LCD_Cursor_Position(1);
  749.           LCD_Send_Byte('P',0);
  750.           LCD_Cursor_Position(1);
  751.           LCD_Send_Byte('r',0);
  752.           LCD_Cursor_Position(1);
  753.           LCD_Send_Byte('e',0);
  754.           LCD_Cursor_Position(1);
  755.           LCD_Send_Byte('s',0);
  756.           LCD_Cursor_Position(1);
  757.           LCD_Send_Byte('s',0);
  758.           LCD_Cursor_Position(1);
  759.           LCD_Send_Byte(' ',0);
  760.           LCD_Cursor_Position(1);
  761.           LCD_Send_Byte('t',0);
  762.           LCD_Cursor_Position(1);
  763.           LCD_Send_Byte('h',0);
  764.           LCD_Cursor_Position(1);
  765.           LCD_Send_Byte('e',0);
  766.           LCD_Cursor_Position(1);
  767.           LCD_Send_Byte(' ',0);
  768.           LCD_Cursor_Position(1);
  769.           LCD_Send_Byte('s',0);
  770.           LCD_Cursor_Position(1);
  771.           LCD_Send_Byte('h',0);
  772.           LCD_Cursor_Position(1);
  773.           LCD_Send_Byte('i',0);
  774.           LCD_Cursor_Position(1);
  775.           LCD_Send_Byte('n',0);
  776.           LCD_Cursor_Position(1);
  777.           LCD_Send_Byte('y',0);
  778.           LineOffset = 0x00;
  779.           LCD_Cursor_Position(2);
  780.           LCD_Send_Byte('b',0);
  781.           LCD_Cursor_Position(2);
  782.           LCD_Send_Byte('u',0);
  783.           LCD_Cursor_Position(2);
  784.           LCD_Send_Byte('t',0);
  785.           LCD_Cursor_Position(2);
  786.           LCD_Send_Byte('t',0);
  787.           LCD_Cursor_Position(2);
  788.           LCD_Send_Byte('o',0);
  789.           LCD_Cursor_Position(2);
  790.           LCD_Send_Byte('n',0);
  791.           LCD_Cursor_Position(2);
  792.           LCD_Send_Byte(' ',0);
  793.           LCD_Cursor_Position(2);
  794.           LCD_Send_Byte('t',0);
  795.           LCD_Cursor_Position(2);
  796.           LCD_Send_Byte('o',0);
  797.           LCD_Cursor_Position(2);
  798.           LCD_Send_Byte(' ',0);
  799.           LCD_Cursor_Position(2);
  800.           LCD_Send_Byte('s',0);
  801.           LCD_Cursor_Position(2);
  802.           LCD_Send_Byte('t',0);
  803.           LCD_Cursor_Position(2);
  804.           LCD_Send_Byte('a',0);
  805.           LCD_Cursor_Position(2);
  806.           LCD_Send_Byte('r',0);
  807.           LCD_Cursor_Position(2);
  808.           LCD_Send_Byte('t',0);
  809.           LCD_Cursor_Position(2);
  810.           LCD_Send_Byte('!',0);
  811.      } else if (MsgType == 1) {
  812.           LCD_Cursor_Position(1);
  813.           LCD_Send_Byte(' ',0);
  814.           LCD_Cursor_Position(1);
  815.           LCD_Send_Byte(' ',0);
  816.           LCD_Cursor_Position(1);
  817.           LCD_Send_Byte(' ',0);
  818.           LCD_Cursor_Position(1);
  819.           LCD_Send_Byte('-',0);
  820.           LCD_Cursor_Position(1);
  821.           LCD_Send_Byte('-',0);
  822.           LCD_Cursor_Position(1);
  823.           LCD_Send_Byte('O',0);
  824.           LCD_Cursor_Position(1);
  825.           LCD_Send_Byte('P',0);
  826.           LCD_Cursor_Position(1);
  827.           LCD_Send_Byte('T',0);
  828.           LCD_Cursor_Position(1);
  829.           LCD_Send_Byte('I',0);
  830.           LCD_Cursor_Position(1);
  831.           LCD_Send_Byte('O',0);
  832.           LCD_Cursor_Position(1);
  833.           LCD_Send_Byte('N',0);
  834.           LCD_Cursor_Position(1);
  835.           LCD_Send_Byte('-',0);
  836.           LCD_Cursor_Position(1);
  837.           LCD_Send_Byte('-',0);
  838.           LineOffset = 0x00;
  839.           LCD_Cursor_Position(2);
  840.           LCD_Send_Byte(' ',0);
  841.           LCD_Cursor_Position(2);
  842.           LCD_Send_Byte(0x7E,0);
  843.           LCD_Cursor_Position(2);
  844.           LCD_Send_Byte('S',0);
  845.           LCD_Cursor_Position(2);
  846.           LCD_Send_Byte('t',0);
  847.           LCD_Cursor_Position(2);
  848.           LCD_Send_Byte('a',0);
  849.           LCD_Cursor_Position(2);
  850.           LCD_Send_Byte('r',0);
  851.           LCD_Cursor_Position(2);
  852.           LCD_Send_Byte('t',0);
  853.           LCD_Cursor_Position(2);
  854.           LCD_Send_Byte(' ',0);
  855.           LCD_Cursor_Position(2);
  856.           LCD_Send_Byte('c',0);
  857.           LCD_Cursor_Position(2);
  858.           LCD_Send_Byte('h',0);
  859.           LCD_Cursor_Position(2);
  860.           LCD_Send_Byte('e',0);
  861.           LCD_Cursor_Position(2);
  862.           LCD_Send_Byte('s',0);
  863.           LCD_Cursor_Position(2);
  864.           LCD_Send_Byte('s',0);
  865.           LCD_Cursor_Position(2);
  866.           LCD_Send_Byte(' ',0);
  867.           LCD_Cursor_Position(2);
  868.           LCD_Send_Byte(' ',0);
  869.           LCD_Cursor_Position(2);
  870.           LCD_Send_Byte(' ',0);
  871.      } else if (MsgType == 2) {
  872.           LCD_Cursor_Position(1);
  873.           LCD_Send_Byte(' ',0);
  874.           LCD_Cursor_Position(1);
  875.           LCD_Send_Byte(' ',0);
  876.           LCD_Cursor_Position(1);
  877.           LCD_Send_Byte(' ',0);
  878.           LCD_Cursor_Position(1);
  879.           LCD_Send_Byte('-',0);
  880.           LCD_Cursor_Position(1);
  881.           LCD_Send_Byte('-',0);
  882.           LCD_Cursor_Position(1);
  883.           LCD_Send_Byte('O',0);
  884.           LCD_Cursor_Position(1);
  885.           LCD_Send_Byte('P',0);
  886.           LCD_Cursor_Position(1);
  887.           LCD_Send_Byte('T',0);
  888.           LCD_Cursor_Position(1);
  889.           LCD_Send_Byte('I',0);
  890.           LCD_Cursor_Position(1);
  891.           LCD_Send_Byte('O',0);
  892.           LCD_Cursor_Position(1);
  893.           LCD_Send_Byte('N',0);
  894.           LCD_Cursor_Position(1);
  895.           LCD_Send_Byte('-',0);
  896.           LCD_Cursor_Position(1);
  897.           LCD_Send_Byte('-',0);
  898.           LineOffset = 0x00;
  899.           LCD_Cursor_Position(2);
  900.           LCD_Send_Byte(0x7F,0);
  901.           LCD_Cursor_Position(2);
  902.           LCD_Send_Byte(' ',0);
  903.           LCD_Cursor_Position(2);
  904.           LCD_Send_Byte('E',0);
  905.           LCD_Cursor_Position(2);
  906.           LCD_Send_Byte('d',0);
  907.           LCD_Cursor_Position(2);
  908.           LCD_Send_Byte('i',0);
  909.           LCD_Cursor_Position(2);
  910.           LCD_Send_Byte('t',0);
  911.           LCD_Cursor_Position(2);
  912.           LCD_Send_Byte(' ',0);
  913.           LCD_Cursor_Position(2);
  914.           LCD_Send_Byte('T',0);
  915.           LCD_Cursor_Position(2);
  916.           LCD_Send_Byte('i',0);
  917.           LCD_Cursor_Position(2);
  918.           LCD_Send_Byte('m',0);
  919.           LCD_Cursor_Position(2);
  920.           LCD_Send_Byte('e',0);
  921.           LCD_Cursor_Position(2);
  922.           LCD_Send_Byte('r',0);
  923.           LCD_Cursor_Position(2);
  924.           LCD_Send_Byte(' ',0);
  925.           LCD_Cursor_Position(2);
  926.           LCD_Send_Byte(' ',0);
  927.           LCD_Cursor_Position(2);
  928.           LCD_Send_Byte(' ',0);
  929.           LCD_Cursor_Position(2);
  930.           LCD_Send_Byte(' ',0);    
  931.      } else if (MsgType == 3) {
  932.           LCD_Timer();  
  933.      }
  934. }
  935.  
  936. /*********************************************************************************
  937. * Interrupt Functions
  938. **********************************************************************************
  939. **********************************************************************************                      
  940. * Name:        RTI_ISR
  941. * Description: Interrupt at 2.048ms intervals.  Checks pushbutton states.
  942. * [->]    (*): (*)
  943. * [<-] (void): [void]                                                                  
  944. *********************************************************************************/
  945. interrupt 7 void RTI_ISR(void)
  946. {  
  947.      CRGFLG = CRGFLG | 0x80;
  948.      
  949.      /*if(PTAD_PTAD7 == 0 && prevpb == 1)        //active low, 1 is unpressed, 0 is pressed
  950.                 leftpb = 1;
  951.        
  952.         if(PTAD_PTAD6 == 0 && prevpb == 1)
  953.                 rghtpb = 1;
  954.  
  955.         prevpb = PTAD_PTAD6 & PTAD_PTAD7;*/
  956.        
  957.      //debug
  958.      if ((TOP_BTN == 0) && (new_TOP_BTN == 0)) {
  959.           new_TOP_BTN = 1;
  960.      
  961.      }
  962.      if ((BTM_BTN == 0) && (new_BTM_BTN == 0)) {
  963.           new_BTM_BTN = 1;
  964.        
  965.      }
  966.      
  967.      if(shiny_button == 1 && prev_shiny_button ==0)
  968.      {
  969.         new_shiny_button = 1;
  970.      }
  971.      prev_shiny_button = shiny_button;
  972.      
  973.      if(RPG_A == 1)
  974.     {
  975.     rightcount++;
  976.     if(leftcount > 3)
  977.             leftturn = 1;
  978.     leftcount = 0;
  979.     }
  980.            
  981.    
  982.     if(RPG_B ==1)
  983.     {
  984.     leftcount++;
  985.     if(rightcount > 3)
  986.             rightturn = 1;
  987.     rightcount = 0;
  988.     }
  989.      
  990.      
  991.      /*if ((RPG_A == 1) && (en2 == 0))
  992.      {
  993.         en1 == 1;
  994.      }
  995.      
  996.      if (RPG_A == 1 && old_RPGB == 0) {
  997.           new_RPGA = 1;          
  998.      }    
  999.                
  1000.      if (RPG_B == 1 && old_RPGA == 0) {
  1001.           new_RPGB = 1;        
  1002.      }                
  1003.      old_RPGA = RPG_A;
  1004.      old_RPGB = RPG_B;
  1005.      */
  1006.      
  1007. }
  1008.  
  1009. /**********************************************************************************                      
  1010. * Name:        TIM_ISR
  1011. * Description: TIM RTI at xms for x
  1012. * [->]    (*): (*)
  1013. * [<-] (void): [void]                                                      
  1014. **********************************************************************************/
  1015. interrupt 15 void TIM_ISR( void)
  1016. {
  1017.     TFLG1 = TFLG1 | 0x80;
  1018.    
  1019.     bcnt++;
  1020.    
  1021.     if (bcnt == 100) {
  1022.          if (PlayerToggle == 0) {
  1023.               if (P1secs == 0) {
  1024.                    P1mins--;
  1025.                    P1secs = 59;
  1026.                    if(P1mins <= 0 && P1secs <= 0) {
  1027.                     P1mins = 0;
  1028.                     P1secs = 0;
  1029.                     timeExpired = 1;
  1030.                    }
  1031.               } else {
  1032.                    P1secs--;
  1033.               }
  1034.          } else {
  1035.               if (P2secs == 0) {
  1036.                    P2mins--;
  1037.                    P2secs = 59;
  1038.                    if(P2mins <= 0 && P2secs <= 0) {
  1039.                     P2mins = 0;
  1040.                     P2secs = 0;
  1041.                     timeExpired = 1;
  1042.                    }
  1043.               } else {
  1044.                    P2secs--;
  1045.               }
  1046.          }
  1047.          timerup = 1;
  1048.          bcnt = 0;
  1049.     }
  1050. }
  1051.  
  1052. /**********************************************************************************                      
  1053. * Name:        SCI_ISR
  1054. * Description: SCI RTI for character data transfer to radial buffer.
  1055. * [->]    (*): (*)
  1056. * [<-] (void): [void]                                                          
  1057. **********************************************************************************/
  1058. interrupt 20 void SCI_ISR( void)
  1059. {
  1060.      SCISR1_TDRE; //Read status flag to ready data register
  1061.    
  1062.      if (tin == tout) {
  1063.           SCICR2 = SCICR2 & 0x7F;
  1064.      } else {
  1065.           SCIDRL = tbuf[tout];
  1066.           tout = (tout + 1) % tsize;
  1067.      }
  1068. }
  1069.  
  1070. /*********************************************************************************
  1071. * Character I/O Library Routines
  1072. **********************************************************************************
  1073. **********************************************************************************
  1074. * Name:        inchar
  1075. * Description: Inputs ASCII character from SCI serial port and returns it.
  1076. * [->] (void): [void]
  1077. * [<-] (char): ASCII value from Teraterm .
  1078. *********************************************************************************/
  1079. char  inchar(void) {
  1080.   /* receives character from the terminal channel */
  1081.         while (!(SCISR1 & 0x20)); /* wait for input */
  1082.     return SCIDRL;
  1083. }
  1084.  
  1085. /*********************************************************************************
  1086. * Name:        outchar
  1087. * Description: Outputs ASCII character to the SCI serial port.
  1088. * [->] (char): ASCII value.
  1089. * [<-] (void): [void]
  1090. *********************************************************************************/
  1091. void outchar(char ch) {
  1092.   /* sends a character to the terminal channel */
  1093.     while (!(SCISR1 & 0x80));  /* wait for output buffer empty */
  1094.     SCIDRL = ch;
  1095. }
  1096.  
  1097. /**********************************************************************************                      
  1098. * Name:        bco
  1099. * Description: Outputs character data in radial buffer to serial.
  1100. * [->]    (*): (*)
  1101. * [<-] (void): [void]                                                          
  1102. **********************************************************************************/
  1103. void bco(char input)
  1104. {
  1105.      while(((tin + 1) % tsize) == tout) {}
  1106.      tbuf[tin] = input;
  1107.      tin = (tin + 1) % tsize;
  1108.    
  1109.      SCICR2 = SCICR2 | 0x80;
  1110. }
  1111.  
  1112. /*********************************************************************************
  1113. * ECE 362 - Mini-Project C Source File - Fall 2012                        
  1114. *********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment