Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************************
- * ECE 362 - Mini-Project C Source File - Fall 2012
- **********************************************************************************
- **********************************************************************************
- *
- * Team ID: 16
- *
- * Project Name: < chess >
- *
- * Team Members:
- * Name: Mark Luzarowski Signature:________________________________
- * Mail: [email protected]
- * Name: Micheal Standiford Signature:________________________________
- * Mail: [email protected]
- * Name: Mark Sears Signature:________________________________
- * Mail: [email protected]
- * Name: That other guy Signature:________________________________
- * Mail: [email protected]
- *
- * Team Positions:
- *
- * Team/Doc Leader: < Mark Sears > Signature: ______________________
- *
- * Software Leader: < James Massengale > Signature: ______________________
- *
- * Interface Leader: < Micheal Standiford > Signature: ______________________
- *
- * Peripheral Leader: < Mark Luzarowski > Signature: ______________________
- *
- *
- * Academic Honesty Statement: In signing above, we hereby certify that we
- * are the individuals who created this HC(S)12 source file and that we have
- * not copied the work of any other student (past or present) while completing
- * it. We understand that if we fail to honor this agreement, we will receive
- * a grade of ZERO and be subject to possible disciplinary action.
- *
- **********************************************************************************
- * Project Purpose
- **********************************************************************************
- * The objective of this Mini-Project is to create a game board. The board itself will
- * function as a "learning board" of sorts that will allow players not familiar with
- * the game to play it easily.
- *
- **********************************************************************************
- * Project-specific Success Criteria
- **********************************************************************************
- * 1. Piece tracking & movement analysis
- *
- * 2. Time & turn tracking
- *
- **********************************************************************************
- * Change Log
- **********************************************************************************
- * Date code started: November 13, 2012
- *
- * Update history:
- *
- * Date: November 13, 2012
- * Name: Mark Luzarowski
- * Update: Added generalized functions for LCD communications.
- * Added initializations skeleton.
- * -------------------------------------------------------------------------------
- */
- #include <hidef.h> // common defines and macros
- #include "derivative.h" // derivative-specific definitions
- #include <mc9s12c32.h> // 9S12 register & port defines
- /* -------------------------------------------------------------------------------
- * Date: December 2nd
- * Name: Mark L
- * Update: Added these functions
- */
- //Dictionary defines
- #define LCD_RS PTT_PTT2
- #define LCD_CLK PTT_PTT4
- #define RPG_A PTT_PTT5
- #define RPG_B PTT_PTT6
- #define RPG_BTN PTT_PTT7
- #define enable PTAD_PTAD7
- #define enable2 PTAD_PTAD6
- #define output PTM_PTM3
- #define output2 PTM_PTM2
- #define output3 PTM_PTM1
- #define output4 PTM_PTM0
- #define output5 PTT_PTT3
- /* Date: December 3rd
- * Name: James Massengale
- * Update: Added these functions
- */
- void currpccnt(void); //counts pieces on the board
- void pickedup(int row, int col, int piece); //calls the correct ledmatrix functions
- void pawnled(int row,int col,int team,int mode);
- void bishopled(int row,int col,int team);
- void rookled(int row,int col, int team);
- void knightled(int row,int col,int team);
- int bcheck(int row,int col); //checks if area is on board (>0 <8)
- void ledcopy(int mode); //matrix compare clear and copy functions
- void queenled(int row,int col,int team);
- int ischeck(int team); //check if team is in check
- int blackcheckflag = 0;
- int whitecheckflag = 0;
- void kingledout(int row,int col,int team);
- void kingled(int row,int col,int team);
- void ledcopy(int mode);
- void checkmatrix(int team);
- void sevenchar(int mode);
- void outinputmsg(void);
- void sci_outline(int mode);
- void sci_datafetch(int mode, int line);
- void outtype(int type);
- void outteam(int team);
- void nowpccnt(void);
- void hpccnt(void);
- void scanline(void);
- //All funtions after main should be initialized here
- char inchar(void);
- void outchar(char);
- void LCD_Delay(int);
- void LCD_Cursor_Position(int);
- void LCD_Send_Byte(char, int);
- void LCD_Display(int Msgtype);
- void bco(char);
- void LCD_Timer(void);
- void LED_Out(void);
- void LED_Convert(void);
- void Chess_Locations(int);
- void LCD_Piece_Select(int);
- void Play_Sound(int);
- void HALL_Input(void);
- void LED_Test(void);
- void outchar(char ch);
- /* -------------------------------------------------------------------------------
- * Date: December 9th
- * Name: Micheal
- * Update: Added these functions for music
- */
- int noteflag = 0;
- int notedur = 25; //120bpm 8th notes
- int notecnt = 0;
- int hello[8][8];
- int hello2[8][8];
- int duty1 = 0;
- int duty2 = 0;
- int period1 = 0;
- int period2 = 0;
- void Play_Music(void);
- void Song_Select(void);
- void Song_Select2(void);
- /* -------------------------------------------------------------------------------
- * Date: December 9th
- * Name: James
- * Update: Added these functions for gameplay
- */
- void setdown(int row, int col, int piece);
- int validateinput(char input);
- void validatepiece(void);
- void validatelocation(void);
- int hallvlatch(void);
- int hallvchess(void);
- int nummoves = 0;
- //void bco(void);
- void addchar(char a);
- char CR = 0x0D;//Return
- char LF = 0x0A; //line feed
- int valcol;
- int valrow;
- //int temppc = 0;
- // James' variables
- //dec 1st
- int nowpc; //how many pieces are currently on the board
- int currpc; //how many pieces are currently in the game (not taken yet)
- int switchflag = 0; //flag that determines whether a switch is being currently made
- int ldata[8][8];
- int ltemp[8][8];
- int hlatch[8][8];
- //dec 3rd
- int sciset = 0;
- int boardset = 0;
- int sciline = 0;
- int boardline = 0;
- int scioff_flag = 0;
- //dec 5th
- int scicountflag = 0;
- int scicount = 0;
- void sciscan(void);
- //dec 6th
- int movenumber = 2;
- int whitescore = 0;
- int blackscore = 0;
- int scoreline = 0;
- int ledoutmode = 0;
- unsigned char MOVEOutData[8];
- int uprow = -1; //variables for keeping track of pieces
- int upcol = -1;
- int downrow;
- int downcol;
- int echoflag = 0;
- int inputonflag = 0;
- int outputonflag = 1;
- int inputflag = -1;
- int pickedupflag = 0;
- int hpc = 0;
- char inputchar = 0x00;
- int turnoverflag = 0;
- int turnactuallyoverflag = 0;
- //Global Declarations Begin Here (mark l.'s stuff)
- //------------------------------------
- //------------------------------------
- //SCI Variables
- int tin = 0; //SCI transmit display buffer IN pointer
- int tout = 0; //SCI transmit display buffer OUT pointer
- int tsize = 100; //SCI size of transmit buffer
- char tbuf[100]; //SCI transmit display buffer
- int tdata; // how much data is in the buffer
- //TIM Variables
- int P1mins = 20; //White player minutes remaining on chess clock
- int P1secs = 0; //White player seconds remaining on chess clock
- int P2mins = 20; //Black player minutes remaining on chess clock
- int P2secs = 0; //Black player seconds remaining on chess clock
- int TimerCounter = 0; //Chess timer 10ms collector
- int timeExpired = 0; //Timer has reached 00:00 for either player
- //LCD Variables
- char LineOffset = 0x00; //In use to calculate position of LCD write cursor
- //RPG Variables
- int RPGRightTurns = 0; //Number of pulses for right detected
- int RPGLeftTurns = 0; //Number of pulses for left detected
- int RPGRight = 0; //Movement to the right detected
- int RPGLeft = 0; //Movement to the left detected
- int prevRPGbutton = 0; //Status of RPG button (previous)
- int newRPGbutton = 0; //Status of RPG button (current)
- //Menu Variables
- int OutOfSplashScreen = 0; //Sees if the game has left the welcome splash
- //0 = Still on splash screen
- //1 = Left the splash screen
- int MenuPosition = 1; //Current Screen of the menu
- //0 = Game Start
- //1 = Edit Timer
- int TimerEditing = 0; //Are we editing the timer?
- //Gameplay (human) Variables
- int PlayerToggle = 0; //The current player toggle
- //0 = White
- //1 = Black
- int TimerRunning = 0; //Sees if the timer is up
- int TimerExpired = 0; //Sees if the timer has expired for either player
- int TimerRefresh = 0; //Refresh the timer on the LCD
- int GameRunning = 0; //Sees if game is currently running
- int HallMatrix[8][8]; //Magnet sensor input matrix
- int ChessMatrix[8][8];//Chess piece memory map
- unsigned char LEDOutData[8]; //Set of LED shiftout variables; one for each row of LEDs.
- int en1 = 0;
- int en2 = 0;
- int RefreshHall = 0;
- //------------------------------------
- //------------------------------------
- //ASCII character constants
- char ASCII_CR = 0x0D; //Return
- char ASCII_LF = 0x0A; //Linefeed
- //LCD specific constants
- char LCD_LINE1 = 0x80; //LCD line #1
- char LCD_LINE2 = 0xC0; //LCD line #2
- char LCD_ON = 0x0F; //LCD turn on instruction
- char LCD_CLEAR = 0x01; //LCD clear instruction
- char LCD_2LINE = 0x38; //LCD use both lines instruction
- char LCD_CURSOR = 0xFE; //LCD cursor move instruction
- /*********************************************************************************
- Initializations
- **********************************************************************************/
- void initializations(void) {
- // Set the PLL speed (bus clock = 24 MHz)
- CLKSEL = CLKSEL & 0x80; // disengage PLL from system
- PLLCTL = PLLCTL | 0x40; // turn on PLL
- SYNR = 0x02; // set PLL multiplier
- REFDV = 0; // set PLL divider
- while (!(CRGFLG & 0x08)){ }
- CLKSEL = CLKSEL | 0x80; // engage PLL
- // Disable watchdog timer (COPCTL register)
- COPCTL = 0x40; // COP off; RTI and COP stopped in BDM-mode
- // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
- SCIBDH = 0x00; // set baud rate to 9600
- SCIBDL = 0x9C; // 24,000,000 / 16 / 156 = 9600 (approx) dec=26
- SCICR1 = 0x00; // $9C = 156
- SCICR2 = 0x0C; // initialize SCI for program-driven operation
- SCICR2 = SCICR2 & 0x7F;
- DDRB = 0x10; // set PB4 for output mode
- PORTB = 0x10; // assert DTR pin on COM port
- //RTI Initialization (2.048ms)
- CRGINT = CRGINT | 0x80; //RTI enabled
- RTICTL = 0x1F; //
- //SPI Initialization (6MHz)
- SPICR1 = 0x52; //SPI control register #1
- //[Bit 7] SPIE = 0 | SPI interrupts disabled
- //[Bit 6] SPE = 1 | SPI enabled
- //[Bit 5] SPTIE = 0 | SPI transmit empty interrupt disabled
- //[Bit 4] MSTR = 1 | Master mode
- //[Bit 3] CPOL = 0 | Active low clock (SCK) - idles low
- //[Bit 2] CPHA = 0 | Data sampled at odd edges of SCK
- //[Bit 1] SSOE = 1 | Slave select output enable
- //[Bit 0] LSBFE = 0 | Data transferred most significant bit first
- SPICR2 = 0x00;
- SPIBR = 0x08; //Baud rate register
- //[Bit 6-4] SPPRx = 000 | Baud rate pre-selection bits
- //[Bit 2-0] SPRx = 001 | Baud rate selection bits
- //SPI rate = BusClock / [(SPPR + 1) * 2^(SPR + 1)]
- //TIM Initialization (10ms)
- TSCR1_TEN = 1; //TIM enable
- TIOS_IOS7 = 1; //Output compare on ch7
- TSCR2_TCRE = 1; //TCNT reset when OC7 occurs
- TSCR2_PR2 = 1; //24MHz / 16 = 1.5MHz
- TSCR2_PR1 = 0;
- TSCR2_PR0 = 0;
- TC7 = 15000; //15000 / 1.5MHz = 10ms
- TIE_C7I = 0; //TIM interrupts disabled
- //PWM Initialization (1470.5Hz)
- PWME = 0x01; //PMW enable
- PWMPOL = 0x01; //PWM polarity select (active high)
- PWMPER0 = 0xFF; //period
- PWMDTY0 = 0x00; //duty
- PWMPRCLK = 0x04; //24MHz/16 = 1.5MHz
- PWMCLK = 0x01; //use clock SA
- PWMSCLA = 2; //1.5MHz/(2*2) = 375KHz
- PWMSCLB = 0; //375KHz/255 = 1470.5Hz final output
- MODRR = 0x01; //Reroute pwm to PT pins
- PWMCAE = 0;
- //Port Initializations
- ATDDIEN = 0xFF;
- DDRT = 0x17; // 00010111 T5, T6, T7 are inputs from the shiny button
- PTT = 0;
- DDRAD = 0xE0;//DDRAD = 0xFF;
- //LCD Initializations
- LCD_CLK = 1;
- LCD_Send_Byte(LCD_ON, 1);
- LCD_Send_Byte(LCD_2LINE, 1);
- LCD_Send_Byte(LCD_CLEAR, 1);
- LCD_Delay(1);
- LCD_Display(0);
- enable = 0;
- asm {
- nop
- }
- }
- /*********************************************************************************
- Main
- **********************************************************************************/
- void main(void) {
- DisableInterrupts;
- initializations();
- EnableInterrupts;
- for(;;) {
- //RPG turned to the right
- if (RPGRight == 1) {
- RPGRight = 0;
- //Not on welcome splash
- if (OutOfSplashScreen == 1) {
- //Not editing the game timer
- if (TimerEditing == 0) {
- MenuPosition++; //Next menu object
- //Scroller cannot pass maximum menu objects
- if (MenuPosition > 3) {
- MenuPosition = 3;
- } else {
- //Display menu object on LCD
- LCD_Display(MenuPosition);
- }
- //Editing the game timer
- } else {
- P1mins++;
- P2mins++;
- //Timer cannout go over 60 minutes
- if (P1mins > 60) {
- P1mins = 60;
- P2mins = 60;
- }
- LCD_Timer();
- }
- }
- }
- //RPG turned to the left
- if (RPGLeft == 1) {
- RPGLeft = 0;
- //Not on welcome splash
- if (OutOfSplashScreen == 1) {
- //Not editing the game timer
- if (TimerEditing == 0) {
- MenuPosition--; //Next menu object
- //Scroller cannot pass maximum menu objects
- if (MenuPosition < 1) {
- MenuPosition = 1;
- } else {
- //Display menu object on LCD
- LCD_Display(MenuPosition);
- }
- //Editing game timer
- } else {
- P1mins--;
- P2mins--;
- //Timer cannout go below 1 minute
- if (P1mins < 1) {
- P1mins = 1;
- P2mins = 1;
- }
- LCD_Timer();
- }
- }
- }
- //RPG button pressed
- if (newRPGbutton == 1) {
- newRPGbutton = 0;
- //Check if on splash
- if (OutOfSplashScreen == 0) {
- OutOfSplashScreen = 1;
- TIE_C7I = 1;
- PWMDTY0 = 0x00;
- Play_Music();
- LCD_Display(MenuPosition);
- } else {
- //Start game, bring up timer
- if (MenuPosition == 1) {
- GameRunning = 1;
- //If timer not running, start it and game initializations
- if (TimerRunning == 0) {
- TimerRunning = 1;
- Chess_Locations(0); //Initialize new game piece locations
- //enable = 1;
- //LED_Test();
- //enable = 0;
- LED_Convert(); //Set up LED backlighting
- enable = 1;
- LED_Out();
- enable = 0;
- SCICR2 = SCICR2 | 0x80;
- //LED_Convert(); //Set up LED backlighting
- //enable = 1;
- //LED_Out();
- //enable = 0;
- //enable2 = 1;
- //HALL_Input();
- //enable2 = 0;
- //If timer is running, toggle player
- } else if (TimerRunning == 1){
- if(turnactuallyoverflag == 1){
- turnactuallyoverflag = 0;
- turnoverflag = 0;
- PlayerToggle = !PlayerToggle;
- movenumber += 1;
- inputflag = -1;
- SCICR2 = SCICR2 | 0x80;
- }
- }
- //Editing the game timer
- } else if (MenuPosition == 2) {
- LCD_Timer();
- //Already brought the screen up
- if (TimerEditing == 1) {
- LCD_Display(MenuPosition);
- TimerEditing = 0;
- } else {
- TimerEditing = 1;
- }
- } else if (MenuPosition == 3) {
- //load game
- }
- }
- }
- //gameflow
- if(GameRunning == 1 && TimerRunning == 1 && inputchar != 0){
- if(validateinput(inputchar) == 0){
- uprow = -1;
- upcol = -1;
- }
- inputonflag = 0;
- inputchar = 0x00;
- if(turnactuallyoverflag == 0){
- SCICR2 = SCICR2 | 0x80;
- }
- }
- /*if(pickedupflag == 0){
- nowpccnt();
- hpccnt();
- currpccnt();
- if(nowpc > currpc){
- puteverythingback();
- } else {
- nowpclatch = nowpc;
- cndiff = currpc - nowpc;
- if(cndiff == 1){
- hallvchess();
- if((ChessMatrix[uprow][upcol] / 10) == PlayerToggle){
- if(sameflag == 0){
- pickedup(uprow,upcol,ChessMatrix[uprow][upcol]);
- pickedupflag = 1;
- }
- LCD_Display(6);
- } else {
- puteverythingback();
- }
- }
- }
- } else if (pickedupflag == 1){
- currpccnt();
- nowpccnt();
- hpccnt();
- if(nowpc < hpc - 1 || nowpc > hpc + 1){
- puteverythingback();
- } else {
- if(nowpc = hpc + 1){ //setting it back down
- hallvlatch();
- if(ldata[nextrow][nextcol] && sameflag == 0){
- setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
- }
- } else if (nowpc = hpc - 1){ //picking another up
- hallvlatch();
- if(ldata[nextrow][nextcol] && sameflag == 0){
- setdown(nextrow,nextcol,ChessMatrix[uprow][upcol]);
- }
- }
- }
- }
- }*/
- if(pickedupflag == 1 && HallMatrix[uprow][upcol] == 0){
- ledoutmode = 1;
- } else {
- ledoutmode = 0;
- }
- //If timer is running and it needs a refresh, refresh it.
- if ((TimerRunning == 1) && (TimerRefresh == 1)) {
- TimerRefresh = 0;
- LCD_Timer();
- }
- if (RefreshHall == 1) {
- enable2 = 1;
- HALL_Input();
- enable2 = 0;
- RefreshHall = 0;
- }
- _FEED_COP(); /* feeds the dog */
- } /* loop forever */
- /* please make sure that you never leave main */
- }
- /*********************************************************************************
- * Name: Chess_Locations
- * Description: Manages vitual piece location map.
- * [->] (global): ChessMatrix: Map of the board.
- * 00 = empty space
- * X1 = pawn
- * X2 = rook
- * X3 = knight
- * X4 = bishop
- * X5 = Queen
- * X6 = King
- * 1X = White piece
- * 2X = Black piece
- * (int): Mode: Mode of map.
- * 0 = Initialization for new game.
- * 1 = Standard
- * [<-] (void): [void]
- *********************************************************************************/
- void Chess_Locations(int Mode) {
- if (Mode == 0) {
- ChessMatrix[0][0] = 0;//2
- ChessMatrix[0][1] = 0;//3
- ChessMatrix[0][2] = 0;//4
- ChessMatrix[0][3] = 5;//5
- ChessMatrix[0][4] = 6;//6
- ChessMatrix[0][5] = 4;//4
- ChessMatrix[0][6] = 0;//3
- ChessMatrix[0][7] = 0;//2
- ChessMatrix[1][0] = 0;//1
- ChessMatrix[1][1] = 0;//1
- ChessMatrix[1][2] = 0;//1
- ChessMatrix[1][3] = 0;//1
- ChessMatrix[1][4] = 1;//1
- ChessMatrix[1][5] = 1;//1
- ChessMatrix[1][6] = 1;//1
- ChessMatrix[1][7] = 0;//1
- ChessMatrix[2][0] = 0;
- ChessMatrix[2][1] = 0;
- ChessMatrix[2][2] = 0;
- ChessMatrix[2][3] = 0;
- ChessMatrix[2][4] = 0;
- ChessMatrix[2][5] = 0;
- ChessMatrix[2][6] = 0;
- ChessMatrix[2][7] = 0;
- ChessMatrix[3][0] = 12;
- ChessMatrix[3][1] = 0;
- ChessMatrix[3][2] = 0;
- ChessMatrix[3][3] = 0;
- ChessMatrix[3][4] = 0;
- ChessMatrix[3][5] = 0;
- ChessMatrix[3][6] = 0;
- ChessMatrix[3][7] = 0;
- ChessMatrix[4][0] = 1;
- ChessMatrix[4][1] = 0;
- ChessMatrix[4][2] = 0;
- ChessMatrix[4][3] = 0;
- ChessMatrix[4][4] = 0;
- ChessMatrix[4][5] = 0;
- ChessMatrix[4][6] = 0;
- ChessMatrix[4][7] = 0;
- ChessMatrix[5][0] = 0;
- ChessMatrix[5][1] = 0;
- ChessMatrix[5][2] = 0;
- ChessMatrix[5][3] = 0;
- ChessMatrix[5][4] = 0;
- ChessMatrix[5][5] = 0;
- ChessMatrix[5][6] = 0;
- ChessMatrix[5][7] = 0;
- ChessMatrix[6][0] = 0;//11
- ChessMatrix[6][1] = 11;//11
- ChessMatrix[6][2] = 11;//11
- ChessMatrix[6][3] = 11;//11
- ChessMatrix[6][4] = 11;//11
- ChessMatrix[6][5] = 0;//11
- ChessMatrix[6][6] = 0;//11
- ChessMatrix[6][7] = 0;//11
- ChessMatrix[7][0] = 0;//12
- ChessMatrix[7][1] = 13;//13
- ChessMatrix[7][2] = 14;//14
- ChessMatrix[7][3] = 12;//16
- ChessMatrix[7][4] = 0;//15
- ChessMatrix[7][5] = 0;//14
- ChessMatrix[7][6] = 0;//13
- ChessMatrix[7][7] = 0;//12
- }
- if (Mode == 1) { //Update locations map
- }
- return;
- }
- /***********************************************************************
- INPUT ANALYSIS
- ***********************************************************************/
- int validateinput(char input){
- if(inputflag == 1){
- switch(input){
- case('A'): uprow = 0;
- break;
- case('B'): uprow = 1;
- break;
- case('C'): uprow = 2;
- break;
- case('D'): uprow = 3;
- break;
- case('E'): uprow = 4;
- break;
- case('F'): uprow = 5;
- break;
- case('G'): uprow = 6;
- break;
- case('H'): uprow = 7;
- break;
- case('X'): inputflag = 0;
- return 0;
- break;
- default: inputflag = 6;
- return 0;
- }
- inputflag = 2;
- } else if(inputflag == 4){
- switch(input){
- case('A'): downrow = 0;
- break;
- case('B'): downrow = 1;
- break;
- case('C'): downrow = 2;
- break;
- case('D'): downrow = 3;
- break;
- case('E'): downrow = 4;
- break;
- case('F'): downrow = 5;
- break;
- case('G'): downrow = 6;
- break;
- case('H'): downrow = 7;
- break;
- case('X'): inputflag = 0;
- return 0;
- break;
- default: inputflag = 7;
- return 0;
- }
- inputflag = 5;
- } else if(inputflag == 2){
- switch(input){
- case('1'): upcol = 0;
- break;
- case('2'): upcol = 1;
- break;
- case('3'): upcol = 2;
- break;
- case('4'): upcol = 3;
- break;
- case('5'): upcol = 4;
- break;
- case('6'): upcol = 5;
- break;
- case('7'): upcol = 6;
- break;
- case('8'): upcol = 7;
- break;
- case('X'): inputflag = 0;
- return 0;
- break;
- default: inputflag = 6;
- return 0;
- }
- validatepiece();
- } else if(inputflag == 5){
- switch(input){
- case('1'): downcol = 0;
- break;
- case('2'): downcol = 1;
- break;
- case('3'): downcol = 2;
- break;
- case('4'): downcol = 3;
- break;
- case('5'): downcol = 4;
- break;
- case('6'): downcol = 5;
- break;
- case('7'): downcol = 6;
- break;
- case('8'): downcol = 7;
- break;
- case('X'): inputflag = 0;
- return 0;
- break;
- default: inputflag = 7;
- return 0;
- }
- validatelocation();
- } else {
- return 0;
- }
- return 1;
- }
- void validatepiece(void){
- int temp = ChessMatrix[uprow][upcol];
- if(temp != 0 && (temp / 10) == PlayerToggle){
- pickedup(uprow,upcol,ChessMatrix[uprow][upcol]);
- pickedupflag = 1;
- } else if((temp / 10) == 1-PlayerToggle) {
- inputflag = 14;
- } else {
- inputflag = 9;
- }
- }
- void validatelocation(void){
- int temp = ldata[downrow][downcol];
- if(temp == 1){
- //temppc = ChessMatrix[downrow][downcol];
- setdown(downrow,downcol,ChessMatrix[uprow][upcol]);
- pickedupflag = 0;
- } else {
- inputflag = 8;
- }
- }
- /***********************************************************************
- BOARD ANALYSIS
- ***********************************************************************/
- int hallvchess(void){
- int i;
- int j;
- int differ = 0;
- int rtemp;
- int ctemp;
- for (i = 0; i < 8; i++) { //Chess row loop
- for (j = 0; j < 8; j++) { //Chess column loop
- if(ChessMatrix[i][j] > 0){
- hlatch[i][j] = 1;
- } else {
- hlatch[i][j] = 0;
- }
- if (hlatch[i][j] == 1 && HallMatrix[i][j] == 0) { //both aren't the same
- differ += 1;
- rtemp = i;
- ctemp = j;
- } else if(hlatch[i][j] != HallMatrix[i][j]){
- differ += 1;
- }
- }
- }
- uprow = rtemp;
- upcol = ctemp;
- hlatch[uprow][upcol] = 0;
- return differ;
- }
- int hallvlatch(){
- int i;
- int j;
- int differ = 0;
- int rtemp;
- int ctemp;
- for (i = 0; i < 8; i++) { //Chess row loop
- for (j = 0; j < 8; j++) { //Chess column loop
- if (hlatch[i][j] != HallMatrix[i][j]) { //both aren't the same
- differ += 1;
- rtemp = i;
- ctemp = j;
- }
- }
- }
- downrow = rtemp;
- downcol = ctemp;
- hlatch[uprow][upcol] = 0;
- return differ;
- }
- //updates currpc
- void currpccnt(void){
- int pcnum = 0;
- int i;
- int j;
- for (i = 0; i < 8; i++) { //Chess column loop
- for (j = 0; j < 8; j++) { //Chess row loop
- if (ChessMatrix[i][j] != 0) { //if piece exists on tile
- pcnum += 1;
- }
- }
- }
- currpc = pcnum;
- }
- //updates nowpc
- void nowpccnt(void){
- int pcnum = 0;
- int i;
- int j;
- for (i = 0; i < 8; i++) { //Hall column loop
- for (j = 0; j < 8; j++) { //Hall row loop
- if (HallMatrix[i][j] != 0) { //if piece exists on tile
- pcnum += 1;
- }
- }
- }
- nowpc = pcnum;
- }
- void hpccnt(void){
- int pcnum = 0;
- int i;
- int j;
- for (i = 0; i < 8; i++) { //Hall column loop
- for (j = 0; j < 8; j++) { //Hall row loop
- if (hlatch[i][j] != 0) { //if piece exists on tile
- pcnum += 1;
- }
- }
- }
- hpc = pcnum;
- }
- //counts how many moves avail
- void movecnt(void){
- int pcnum = 0;
- int i;
- int j;
- for (i = 0; i < 8; i++) { //Hall column loop
- for (j = 0; j < 8; j++) { //Hall row loop
- if (ldata[i][j] != 0) { //if piece exists on tile
- pcnum += 1;
- }
- }
- }
- nummoves = pcnum;
- }
- void puteverythingback(void){
- int i;
- int j;
- int differ = 0;
- int temp = 0;
- int check = 1;
- ledoutmode = 0;
- while(check == 1){
- differ = 0;
- for (i = 0; i < 8; i++) { //Chess row loop
- for (j = 0; j < 8; j++) { //Chess column loop
- if(ChessMatrix[i][j] > 0){
- temp = 1;
- } else {
- temp = 0;
- }
- if (temp != HallMatrix[i][j]) { //both aren't the same
- differ += 1;
- }
- }
- }
- if(differ == 0){
- check = 0;
- } else {
- LCD_Display(6);
- }
- }
- pickedupflag = 0;
- }
- /*********************************************************************************
- * Name: pickedup
- * Description: What is done when one piece is currently picked up
- * [->] (int i, int j): the coordinates of the piece being picked up
- * [<-] (void): [pcnum]
- *********************************************************************************/
- void pickedup(int row, int col, int piece){
- int type = piece % 10; // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
- int team = piece / 10; //0=white 1=black
- ledcopy(6);
- if(type == 1){
- pawnled(row,col,team,0);
- }
- else if(type == 2){
- rookled(row,col,team);
- }
- else if(type == 3){
- knightled(row,col,team);
- }
- else if(type == 4){
- bishopled(row,col,team);
- }
- else if(type == 5){
- queenled(row,col,team);
- }
- else if(type == 6){
- kingled(row,col,team);
- }
- movecnt();
- //ledcopy(5);
- //ledoutmode = 1;
- if(nummoves == 0){
- inputflag = 10;
- pickedupflag = 0;
- } else {
- inputflag += 1;
- }
- }
- void setdown(int row, int col, int piece){
- int epiece = ChessMatrix[row][col];
- int type = epiece % 10; // 1=pawn 2=rook 3=knight 4=bishop 5=queen 6=king
- int team = piece / 10; //0=white 1=black
- int score;
- //if(ischeck(PlayerToggle)){
- // inputflag = 15;
- //} else {
- if(type == 1){
- score = 1;
- }
- else if(type == 2){
- score = 5;
- }
- else if(type == 3){
- score = 3;
- }
- else if(type == 4){
- score = 3;
- }
- else if(type == 5){
- score = 9;
- } else {
- score = 0;
- }
- if(PlayerToggle == 0){
- whitescore += score;
- } else if(PlayerToggle == 1){
- blackscore += score;
- }
- ChessMatrix[downrow][downcol] = piece;
- ChessMatrix[uprow][upcol] = 0;
- ledoutmode = 0;
- turnactuallyoverflag = 1;
- inputflag = 11;
- //movecnt();
- //}
- SCICR2 = SCICR2 | 0x80;
- }
- /***********************************************************************
- LED/ VALID MOVE MATRIX GENERATION
- ***********************************************************************/
- //mode 0 regular //mode 1 only piece captures
- void pawnled(int row,int col,int team,int mode){
- int dir;
- if (team == 1){
- dir = -1;
- } else {
- dir = 1;
- }
- if(mode == 0){
- if(ChessMatrix[row+dir][col] == 0){ //check 1 space
- ldata[row+dir][col] = 1;
- if(team == 1){ //check 2 spaces
- if(row == 6 && ChessMatrix[row+dir+dir][col] == 0){
- ldata[row+dir+dir][col] = 1;
- }
- } else {
- if(row == 1 && ChessMatrix[row+dir+dir][col] == 0){
- ldata[row+dir+dir][col] = 1;
- }
- }
- }
- }
- if(col != 7 && ChessMatrix[row+dir][col+1] != 0 && (ChessMatrix[row+dir][col+1] / 10) != team){ //check right capture
- ldata[row+dir][col+1] = 1;
- }
- if(col != 0 && ChessMatrix[row+dir][col-1] != 0 && (ChessMatrix[row+dir][col-1] / 10) != team){ //check left capture
- ldata[row+dir][col-1] = 1;
- }
- //pick up piece message
- }
- void rookled(int row,int col, int team){
- int cscan = col;
- int rscan = row;
- int dir = 1;
- while(rscan < 7){ //scan up positive row
- if(ChessMatrix[rscan+dir][col] == 0){
- ldata[rscan+dir][col] = 1;
- rscan += dir;
- } else if((ChessMatrix[rscan+dir][col] / 10) != team){
- ldata[rscan+dir][col] = 1;
- rscan = 8;
- } else {
- rscan = 8;
- }
- }
- dir = -1; //reset scan parameters
- rscan = row;
- while(rscan > 0){ //scan up negative row
- if(ChessMatrix[rscan+dir][col] == 0){
- ldata[rscan+dir][col] = 1;
- rscan += dir;
- } else if((ChessMatrix[rscan+dir][col] / 10) != team){
- ldata[rscan+dir][col] = 1;
- rscan = -1;
- } else {
- rscan = -1;
- }
- }
- dir = 1;
- while(cscan < 7){ //scan up positive column
- if(ChessMatrix[row][cscan+dir] == 0){
- ldata[row][cscan+dir] = 1;
- cscan += dir;
- } else if((ChessMatrix[row][cscan+dir] / 10) != team){
- ldata[row][cscan+dir] = 1;
- cscan = 8;
- } else {
- cscan = 8;
- }
- }
- dir = -1; //reset scan parameters
- cscan = col;
- while(cscan > 0){ //
- if(ChessMatrix[row][cscan+dir] == 0){ //scan up negative column
- ldata[row][cscan+dir] = 1;
- cscan += dir;
- } else if((ChessMatrix[row][cscan+dir] / 10) != team){
- ldata[row][cscan+dir] = 1;
- cscan = -1;
- } else {
- cscan = -1;
- }
- }
- }
- void bishopled(int row,int col,int team){
- int cscan = col;
- int rscan = row;
- int cdir = 1;
- int rdir = 1;
- //+ + dir
- while(rscan < 7 && cscan < 7){ //scan up positive row
- if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan += rdir;
- cscan += cdir;
- } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan = 8;
- cscan = 8;
- } else {
- rscan = 8;
- cscan = 8;
- }
- }
- rscan = row;
- cscan = col;
- cdir = -1;
- //- + dir
- while(rscan < 7 && cscan > 0){ //scan up positive row
- if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan += rdir;
- cscan += cdir;
- } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan = 8;
- cscan = -1;
- } else {
- rscan = 8;
- cscan = -1;
- }
- }
- rscan = row;
- cscan = col;
- cdir = 1;
- rdir = -1;
- //+ - dir
- while(rscan > 0 && cscan < 7){ //scan up positive row
- if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan += rdir;
- cscan += cdir;
- } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan = -1;
- cscan = 8;
- } else {
- rscan = -1;
- cscan = 8;
- }
- }
- rscan = row;
- cscan = col;
- cdir = -1;
- //- - dir
- while(rscan > 0 && cscan > 0){ //scan up positive row
- if(ChessMatrix[rscan+rdir][cscan+cdir] == 0){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan += rdir;
- cscan += cdir;
- } else if((ChessMatrix[rscan+rdir][cscan+cdir] / 10) != team){
- ldata[rscan+rdir][cscan+cdir] = 1;
- rscan = -1;
- cscan = -1;
- } else {
- rscan = -1;
- cscan = -1;
- }
- }
- }
- //0 good -1 not
- int bcheck(int row,int col){
- if(row > 7){
- return -1;
- } else if(col > 7){
- return -1;
- } else if(row < 0){
- return -1;
- } else if(col < 0){
- return -1;
- } else {
- return 0;
- }
- }
- void knightled(int row,int col,int team){
- int cchk = col;
- int rchk = row;
- rchk += 2;
- cchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += 1;
- cchk += 2;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += -1;
- cchk += 2;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += 1;
- cchk += -2;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += 2;
- cchk += -1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += -2;
- cchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += -1;
- cchk += -2;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk = col;
- rchk += -2;
- cchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- }
- void queenled(int row,int col,int team){
- bishopled(row,col,team);
- rookled(row,col,team);
- }
- //mode 1 = data to temp
- //mode 2 = temp back to data
- //cleans the one it moved from
- //mode 3 = switch data
- //mode 4 = switch and filter
- //mode 5 (debugging) ldata to chessmatrix
- //mode 6 clear ldata
- void ledcopy(int mode){
- int r;
- int c;
- int temp;
- for (r = 0; r < 8; r++) {
- for (c = 0; c < 8; c++) {
- if(mode == 1){
- ltemp[r][c] = ldata[r][c];
- ldata[r][c] = 0;
- } else if(mode == 2) {
- ldata[r][c] = ltemp[r][c];
- ltemp[r][c] = 0;
- } else if(mode == 3) {
- temp = ltemp[r][c];
- ltemp[r][c] = ldata[r][c];
- ldata[r][c] = temp;
- } else if(mode == 5) {
- if(ldata[r][c] == 1){
- ChessMatrix[r][c] = 27;
- }
- } else if(mode == 6) {
- ldata[r][c] = 0;
- ltemp[r][c] = 0;
- } else {
- temp = ltemp[r][c];
- ltemp[r][c] = ldata[r][c];
- ldata[r][c] = temp;
- if((ltemp[r][c] + ldata[r][c]) == 2){
- ldata[r][c] = 0;
- }
- }
- }
- }
- }
- void kingledout(int row,int col,int team){
- int rchk = row;
- int cchk = col;
- rchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- rchk += -1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- cchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- cchk += -1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- cchk += 1;
- rchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- rchk = row;
- cchk += -1;
- rchk += 1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- rchk = row;
- cchk += 1;
- rchk += -1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- rchk = row;
- cchk += -1;
- rchk += -1;
- if (bcheck(rchk,cchk) == 0 && ChessMatrix[rchk][cchk] == 0){
- ldata[rchk][cchk] = 1;
- } else if(bcheck(rchk,cchk) == 0 && (ChessMatrix[rchk][cchk] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- }
- void checkmatrix(int team){
- int r;
- int c;
- for (r = 0; r < 8; r++) {
- for (c = 0; c < 8; c++) {
- if((ChessMatrix[r][c] / 10) != team){
- if((ChessMatrix[r][c] % 10) == 1){
- pawnled(r,c,1-team,1);
- }
- else if((ChessMatrix[r][c] % 10) == 2){
- rookled(r,c,1-team);
- }
- else if((ChessMatrix[r][c] % 10) == 3){
- knightled(r,c,1-team);
- }
- else if((ChessMatrix[r][c] % 10) == 4){
- bishopled(r,c,1-team);
- }
- else if((ChessMatrix[r][c] % 10) == 5){
- queenled(r,c,1-team);
- }
- else if((ChessMatrix[r][c] % 10) == 6){
- kingledout(r,c,1-team);
- }
- }
- }
- }
- }
- void kingled(int row,int col,int team){
- kingledout(row,col,team); //show possible moves without checking for check
- ledcopy(1); //store in temp matrix
- checkmatrix(team); //create a matrix of enemy moves
- ledcopy(4); //filter the temp matrix with enemy move matrix
- }
- int ischeck(int team){
- int piece;
- int r;
- int c;
- int tempr;
- int tempc;
- if(team == 0){
- piece = 6;
- } else if(team == 1){
- piece = 16;
- }
- for (r = 0; r < 8; r++) {
- for (c = 0; c < 8; c++) {
- if(ChessMatrix[r][c] == piece){
- tempr = r;
- tempc = c;
- }
- }
- }
- ledcopy(6);
- checkmatrix(team);
- if(ldata[tempr][tempc] == 1){
- ledcopy(6);
- return 1;
- } else {
- ledcopy(6);
- return 0;
- }
- }
- /*********************************************************************************
- * SCI Functions
- **********************************************************************************/
- //0='*', 1=' '
- void sevenchar(int mode){
- int i = 0;
- char a;
- if(mode == 1){
- a = '*';
- } else {
- a = ' ';
- }
- for (i = 0; i < 7; i++) {
- addchar(a);
- }
- }
- //2=solid line, 1=whiteblackwhiteblack, 0=blackwhiteblackwhite
- void sci_outline(int mode){
- int i = 0;
- int j = 0;
- if(mode == 2){
- addchar('*');
- for (i = 0; i < 8; i++) {
- sevenchar(1);
- addchar('*');
- }
- addchar(LF);
- addchar(CR);
- } else if(mode == 1){
- addchar('*');
- j = 0;
- for (i = 0; i < 8; i++) {
- j = 1 - j;
- sevenchar(j);
- addchar('*');
- }
- addchar(LF);
- addchar(CR);
- } else if(mode == 0){
- addchar('*');
- j = 1;
- for (i = 0; i < 8; i++) {
- j = 1 - j;
- sevenchar(j);
- addchar('*');
- }
- addchar(LF);
- addchar(CR);
- }
- }
- void addchar(char a){
- tbuf[tin] = a;
- tdata += 1;
- tin += 1;
- tin = tin % tsize;
- }
- void outtype(int type){
- char x;
- //sciset = 1 - sciset;
- if(boardset == 0){
- x = '*';
- } else {
- x = ' ';
- }
- if(type == 1){
- addchar(x);
- addchar('P');
- addchar('a');
- addchar('w');
- addchar('n');
- addchar(x);
- addchar(x);
- } else if(type == 2){
- addchar(x);
- addchar('R');
- addchar('o');
- addchar('o');
- addchar('k');
- addchar(x);
- addchar(x);
- } else if(type == 3){
- addchar(x);
- addchar('K');
- addchar('n');
- addchar('i');
- addchar('g');
- addchar('h');
- addchar('t');
- } else if(type == 4){
- addchar(x);
- addchar('B');
- addchar('i');
- addchar('s');
- addchar('h');
- addchar('o');
- addchar('p');
- } else if(type == 5){
- addchar(x);
- addchar('Q');
- addchar('u');
- addchar('e');
- addchar('e');
- addchar('n');
- addchar(x);
- } else if(type == 6){
- addchar(x);
- addchar('K');
- addchar('i');
- addchar('n');
- addchar('g');
- addchar(x);
- addchar(x);
- }
- addchar('*');
- }
- void outteam(int team){
- char x;
- //boardset = 1 - sciset;
- if(boardset == 1){
- x = '*';
- } else {
- x = ' ';
- }
- if(team == 0){
- addchar(x);
- addchar('W');
- addchar('h');
- addchar('i');
- addchar('t');
- addchar('e');
- addchar(x);
- } else if(team == 1){
- addchar(x);
- addchar('B');
- addchar('l');
- addchar('a');
- addchar('c');
- addchar('k');
- addchar(x);
- }
- addchar('*');
- }
- //0=team, 1=type
- void sci_datafetch(int mode, int line){
- int i = 0;
- if(mode == 0){
- for (i = 0; i < 8; i++) {
- //
- boardset = 1 - boardset;
- //if(HallMatrix[line][i] == 0){
- if(ChessMatrix[line][i] == 0){
- sevenchar(boardset);
- addchar('*');
- } else {
- outteam(ChessMatrix[line][i] / 10);
- //outteam(HallMatrix[line][i] / 10);
- }
- }
- } else {
- for (i = 0; i < 8; i++) {
- //
- boardset = 1 - boardset;
- //if(HallMatrix[line][i] == 0){
- if(ChessMatrix[line][i] == 0){
- sevenchar(1-boardset);
- addchar('*');
- } else {
- outtype(ChessMatrix[line][i] % 10);
- //outtype(HallMatrix[line][i] % 10);
- }
- }
- //boardset = 1 - boardset;
- }
- //addchar('*');
- addchar(LF);
- addchar(CR);
- }
- void sciscan(void){
- int i = 0;
- int j = 0;
- if(scioff_flag > 0){
- if(scioff_flag == 3){
- SCICR2 = SCICR2 & 0x7F;
- //for (j = 0; j < 2; j++) {
- //addchar(LF);
- //addchar(CR);
- //}
- scioff_flag = 0;
- scicountflag = 1;
- turnoverflag = 1;
- }
- else if(scioff_flag == 2){
- scoreline += 1;
- if(scoreline == 1){
- addchar(LF);
- addchar(CR);
- addchar(LF);
- addchar(CR);
- for (i = 0; i < 15; i++) {
- addchar(' ');
- }
- i = 0;
- for (i = 0; i < 34; i++) {
- addchar('*');
- }
- addchar(LF);
- addchar(CR);
- }
- else if(scoreline == 2){
- for (i = 0; i < 15; i++) {
- addchar(' ');
- }
- addchar('*');
- addchar('W');
- addchar('h');
- addchar('i');
- addchar('t');
- addchar('e');
- addchar(' ');
- addchar('S');
- addchar('c');
- addchar('o');
- addchar('r');\
- addchar('e');
- addchar(':');
- addchar(' ');
- if((whitescore / 10) == 0){
- addchar('0');
- addchar(whitescore + 0x30);
- }
- else {
- addchar((whitescore / 10) + 0x30);
- addchar((whitescore % 10) + 0x30);
- }
- addchar(' ');
- addchar(' ');
- addchar('B');
- addchar('l');
- addchar('a');
- addchar('c');
- addchar('k');
- addchar(' ');
- addchar('S');
- addchar('c');
- addchar('o');
- addchar('r');
- addchar('e');
- addchar(':');
- addchar(' ');
- if((blackscore / 10) == 0){
- addchar('0');
- addchar(blackscore + 0x30);
- }
- else {
- addchar((blackscore / 10) + 0x30);
- addchar((blackscore % 10) + 0x30);
- }
- addchar('*');
- addchar(LF);
- addchar(CR);
- }
- else if(scoreline == 3){
- for (i = 0; i < 15; i++) {
- addchar(' ');
- }
- i = 0;
- addchar('*');
- for (i = 0; i < 9; i++) {
- addchar(' ');
- }
- j = movenumber / 2;
- if(movenumber % 2){
- addchar('B');
- addchar('l');
- addchar('a');
- addchar('c');
- addchar('k');
- addchar(' ');
- }
- else {
- addchar('W');
- addchar('h');
- addchar('i');
- addchar('t');
- addchar('e');
- addchar(' ');
- }
- addchar('T');
- addchar('u');
- addchar('r');
- addchar('n');
- addchar(' ');
- if((j / 100) == 0){
- if((j / 10) == 0){
- addchar('0');
- addchar('0');
- addchar(j + 0x30);
- }
- else {
- addchar('0');
- addchar((j / 10) + 0x30);
- addchar((j % 10) + 0x30);
- }
- } else {
- addchar((j / 100) + 0x30);
- addchar(((j % 100) / 10) + 0x30);
- addchar((j % 10) + 0x30);
- }
- i = 0;
- for (i = 0; i < 9; i++) {
- addchar(' ');
- }
- addchar('*');
- addchar(LF);
- addchar(CR);
- }
- else if(scoreline == 4){
- for (i = 0; i < 15; i++) {
- addchar(' ');
- }
- i = 0;
- for (i = 0; i < 34; i++) {
- addchar('*');
- }
- addchar(LF);
- addchar(CR);
- }
- else {
- scoreline = 0;
- scioff_flag += 1;
- //tdata -= 1;
- }
- }
- else {
- addchar('*');
- sci_outline(2);
- scioff_flag += 1;
- }
- }
- else {
- scanline();
- }
- }
- void scanline(void){
- int i = 0;
- addchar('*');
- sciset = 1 - sciset;
- boardset = sciset;
- if(sciline == 0){
- sci_outline(2);
- } else if(sciline == 1){
- sci_outline(boardline % 2);
- } else if(sciline == 2){
- sci_datafetch(0,boardline);
- } else if(sciline == 3){
- sci_datafetch(1,boardline);
- } else if(sciline == 4){
- sci_outline(boardline % 2);
- }
- sciline += 1;
- if(sciline > 4){
- boardline += 1;
- sciline = 0;
- }
- if(boardline > 7){
- scioff_flag = 1;
- boardline = 0;
- }
- }
- void outinputmsg(void){
- if(inputflag == -1){
- addchar(CR);
- addchar(LF);
- addchar('X');
- addchar(' ');
- addchar('t');
- addchar('o');
- addchar(' ');
- addchar('r');
- addchar('e');
- addchar('s');
- addchar('e');
- addchar('t');
- addchar(CR);
- addchar(LF);
- inputflag += 1;
- } else if(inputflag == 0){
- addchar(LF);
- addchar('C');
- addchar('h');
- addchar('o');
- addchar('o');
- addchar('s');
- addchar('e');
- addchar(' ');
- addchar('p');
- addchar('i');
- addchar('e');
- addchar('c');
- addchar('e');
- addchar(' ');
- addchar('t');
- addchar('o');
- addchar(' ');
- addchar('m');
- addchar('o');
- addchar('v');
- addchar('e');
- addchar(' ');
- addchar('b');
- addchar('y');
- addchar(' ');
- addchar('e');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar('i');
- addchar('n');
- addchar('g');
- addchar(' ');
- addchar('l');
- addchar('o');
- addchar('c');
- addchar('a');
- addchar('t');
- addchar('i');
- addchar('o');
- addchar('n');
- addchar('.');
- addchar(LF);
- addchar(CR);
- inputflag += 1;
- } else if(inputflag == 1){
- addchar(CR);
- addchar(LF);
- addchar('E');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar(' ');
- addchar('r');
- addchar('o');
- addchar('w');
- addchar(' ');
- addchar('(');
- addchar('A');
- addchar('B');
- addchar('C');
- addchar('D');
- addchar('E');
- addchar('F');
- addchar('G');
- addchar('H');
- addchar(')');
- addchar(':');
- addchar(LF);
- addchar(CR);
- addchar(' ');
- addchar(' ');
- addchar(' ');
- addchar(CR);
- inputonflag = 1;
- } else if(inputflag == 2){
- addchar(CR);
- addchar(LF);
- addchar('E');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar(' ');
- addchar('c');
- addchar('o');
- addchar('l');
- addchar('u');
- addchar('m');
- addchar('n');
- addchar(' ');
- addchar('(');
- addchar('1');
- addchar('2');
- addchar('3');
- addchar('4');
- addchar('5');
- addchar('6');
- addchar('7');
- addchar('8');
- addchar(')');
- addchar(':');
- addchar(CR);
- addchar(LF);
- inputonflag = 1;
- } else if(inputflag == 3){
- addchar(CR);
- addchar(LF);
- addchar('P');
- addchar('l');
- addchar('a');
- addchar('c');
- addchar('e');
- addchar(' ');
- addchar('y');
- addchar('o');
- addchar('u');
- addchar('r');
- addchar(' ');
- addchar('p');
- addchar('i');
- addchar('e');
- addchar('c');
- addchar('e');
- addchar(' ');
- addchar('a');
- addchar('n');
- addchar('d');
- addchar(' ');
- addchar('e');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar(' ');
- addchar('t');
- addchar('h');
- addchar('e');
- addchar(' ');
- addchar('d');
- addchar('e');
- addchar('s');
- addchar('t');
- addchar('i');
- addchar('n');
- addchar('a');
- addchar('t');
- addchar('i');
- addchar('o');
- addchar('n');
- addchar(' ');
- addchar('c');
- addchar('o');
- addchar('o');
- addchar('r');
- addchar('d');
- addchar('i');
- addchar('n');
- addchar('a');
- addchar('t');
- addchar('e');
- addchar('!');
- addchar(CR);
- addchar(LF);
- inputflag += 1;
- } else if(inputflag == 4){
- addchar(CR);
- addchar(LF);
- addchar('E');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar(' ');
- addchar('r');
- addchar('o');
- addchar('w');
- addchar(' ');
- addchar('(');
- addchar('A');
- addchar('B');
- addchar('C');
- addchar('D');
- addchar('E');
- addchar('F');
- addchar('G');
- addchar('H');
- addchar(')');
- addchar(':');
- addchar(CR);
- addchar(LF);
- inputonflag = 1;
- } else if(inputflag == 5){
- addchar(CR);
- addchar(LF);
- addchar('E');
- addchar('n');
- addchar('t');
- addchar('e');
- addchar('r');
- addchar(' ');
- addchar('c');
- addchar('o');
- addchar('l');
- addchar('u');
- addchar('m');
- addchar('n');
- addchar(' ');
- addchar('(');
- addchar('1');
- addchar('2');
- addchar('3');
- addchar('4');
- addchar('5');
- addchar('6');
- addchar('7');
- addchar('8');
- addchar(')');
- addchar(':');
- addchar(CR);
- addchar(LF);
- inputonflag = 1;
- } else if(inputflag == 6){ //input error
- addchar(CR);
- addchar(LF);
- addchar('I');
- addchar('n');
- addchar('v');
- addchar('a');
- addchar('l');
- addchar('i');
- addchar('d');
- addchar(' ');
- addchar('I');
- addchar('n');
- addchar('p');
- addchar('u');
- addchar('t');
- addchar('.');
- addchar(CR);
- //addchar(LF);
- inputflag = 1;
- } else if(inputflag == 7){ //input error
- addchar(CR);
- addchar(LF);
- addchar('I');
- addchar('n');
- addchar('v');
- addchar('a');
- addchar('l');
- addchar('i');
- addchar('d');
- addchar(' ');
- addchar('I');
- addchar('n');
- addchar('p');
- addchar('u');
- addchar('t');
- addchar('.');
- addchar(CR);
- //addchar(LF);
- inputflag = 4;
- } else if(inputflag == 8){//move error
- addchar(CR);
- addchar(LF);
- addchar(' ');
- addchar('I');
- addchar('n');
- addchar('v');
- addchar('a');
- addchar('l');
- addchar('i');
- addchar('d');
- addchar(' ');
- addchar('M');
- addchar('o');
- addchar('v');
- addchar('e');
- addchar('m');
- addchar('e');
- addchar('n');
- addchar('t');
- addchar('.');
- addchar(CR);
- //addchar(LF);
- inputflag = 4;
- } else if(inputflag == 9){
- addchar(CR);
- addchar(LF);
- addchar('I');
- addchar('n');
- addchar('v');
- addchar('a');
- addchar('l');
- addchar('i');
- addchar('d');
- addchar(' ');
- addchar('P');
- addchar('i');
- addchar('e');
- addchar('c');
- addchar('e');
- addchar('.');
- addchar(CR);
- addchar(LF);
- inputflag = 1;
- } else if(inputflag == 10){
- addchar(CR);
- addchar(LF);
- addchar('N');
- addchar('o');
- addchar(' ');
- addchar('m');
- addchar('o');
- addchar('v');
- addchar('e');
- addchar('s');
- addchar(' ');
- addchar('a');
- addchar('v');
- addchar('a');
- addchar('i');
- addchar('l');
- addchar('a');
- addchar('b');
- addchar('l');
- addchar('e');
- addchar('!');
- addchar(CR);
- addchar(LF);
- inputflag = 0;
- } else if(inputflag == 11){
- addchar(CR);
- addchar(LF);
- addchar('M');
- addchar('o');
- addchar('v');
- addchar('e');
- addchar(' ');
- addchar('c');
- addchar('o');
- addchar('m');
- addchar('e');
- addchar('p');
- addchar('l');
- addchar('e');
- addchar('t');
- addchar('e');
- addchar('.');
- addchar(' ');
- addchar(CR);
- addchar(LF);
- inputflag = 12;
- } else if(inputflag == 14){
- addchar(CR);
- addchar(LF);
- addchar('W');
- addchar('r');
- addchar('o');
- addchar('n');
- addchar('g');
- addchar(' ');
- addchar('T');
- addchar('e');
- addchar('a');
- addchar('m');
- addchar(CR);
- addchar(LF);
- inputflag = 1;
- } else if(inputflag == 15){
- addchar(CR);
- addchar(LF);
- addchar('I');
- addchar('n');
- addchar('v');
- addchar('a');
- addchar('l');
- addchar('i');
- addchar('d');
- addchar(' ');
- addchar('m');
- addchar('o');
- addchar('v');
- addchar('e');
- addchar(' ');
- addchar('y');
- addchar('o');
- addchar('u');
- addchar('r');
- addchar(' ');
- addchar('k');
- addchar('i');
- addchar('n');
- addchar('g');
- addchar(' ');
- addchar('i');
- addchar('s');
- addchar(' ');
- addchar('s');
- addchar('t');
- addchar('i');
- addchar('l');
- addchar('l');
- addchar(' ');
- addchar('i');
- addchar('n');
- addchar(' ');
- addchar('c');
- addchar('h');
- addchar('e');
- addchar('c');
- addchar('k');
- addchar('1');
- addchar(CR);
- addchar(LF);
- inputflag = 0;
- }
- }
- /**********************************************************************************
- * Name: SCI_ISR
- * Description: SCI RTI for character data transfer to radial buffer.
- * [->] (*): (*)
- * [<-] (void): [void]
- **********************************************************************************/
- interrupt 20 void SCI_ISR( void)
- {
- int j;
- SCISR1_TDRE; //Read status flag to ready data register
- if(scioff_flag == 3){
- //SCICR2 = SCICR2 & 0x7F;
- //tin += 1;
- //tin = 0;
- scicountflag = 1;
- for (j = 0; j < 2; j++) {
- addchar(LF);
- addchar(CR);
- }
- scioff_flag = 0;
- turnoverflag = 1;
- }
- if(tdata <= 0){
- tdata = 0;
- tin = tout;
- addchar(' ');
- addchar(CR);
- if(inputonflag == 1){
- SCICR2 = SCICR2 & 0x7F;
- } else {
- if(turnoverflag == 0){
- sciscan();
- } else {
- outinputmsg();
- }
- }
- }
- if(SCISR1 & 0x80){
- SCIDRL = tbuf[tout];
- tout += 1;
- tout = tout % tsize;
- tdata -= 1;
- if(echoflag == 1){
- echoflag = 0;
- SCICR2 = SCICR2 & 0x7F;
- }
- }
- }
- /*********************************************************************************
- * LED Matrix Functions
- **********************************************************************************
- **********************************************************************************
- * Name: LED_Out
- * Description: Shifts out the updated LED matrix data.
- * [->] (global): LEDOutData: LED lighting data, one for each row.
- * [<-] (void): [void]
- *********************************************************************************/
- void LED_Out(void) {
- int i = 0;
- for (i = 0; i < 8; i++) {
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- if(ledoutmode == 0){
- SPIDR = LEDOutData[i]; //Put data into SPISR data register
- } else if(ledoutmode == 1){
- SPIDR = MOVEOutData[i]; //Put data into SPISR data register
- }
- LCD_Delay(0); //Call a delay for shifting out
- //enable daisy chain, disable LCD
- }
- return;
- }
- /*********************************************************************************
- * Name: LED_Test
- * Description: Turns LED blocks on slowly, from left to right.
- * [->] (void): [void]
- * [<-] (void): [void]
- *********************************************************************************/
- void LED_Test(void) {
- int i = 0;
- int j = 0;
- unsigned char a = 0x00; //wait loop variable
- unsigned char b = 0x00; //wait loop variable
- unsigned char c = 0x00; //wait loop variable
- int counter = 0;
- int row = 0; //controls row
- int k = 0;
- for (k = 0; k < 3; k++) {
- for (j = 0; j < 9; j++) {//Loop for refreshing the row for amounts of lights it has
- for (i = 0; i < 8; i++) {//Loop for shiting full board data to daisy chain
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- if (k == 0) {
- if (i == 0) {
- if (j == 0) { //1000 0000
- counter = 0x80;
- } else if (j == 1) {//1100 0000
- counter = 0xC0;
- } else if (j == 2) {//1110 0000
- counter = 0xE0;
- } else if (j == 3) {//1111 0000
- counter = 0xF0;
- } else if (j == 4) {//1111 1000
- counter = 0xF8;
- } else if (j == 5) {//1111 1100
- counter = 0xFC;
- } else if (j == 6) {//1111 1110
- counter = 0xFE;
- } else if (j == 7) {//1111 1111
- counter = 0xFF;
- }
- } else if ((i != 0) && (k == 0)) {
- counter = 0x00;
- }
- if ((k == 0) && (j != 8)) {
- SPIDR = counter;
- }
- } else if (k == 1) {
- if (i == 0) {
- counter = 0xFF;
- } else if (i == 1) {
- if (j == 0) { //1111 1111 FF
- counter = 0x80;
- } else if (j == 1) {//0111 1111 7F
- counter = 0xC0;
- } else if (j == 2) {//0011 1111 3F
- counter = 0xE0;
- } else if (j == 3) {//0001 1111 1F
- counter = 0xF0;
- } else if (j == 4) {//0000 1111 0F
- counter = 0xF8;
- } else if (j == 5) {//0000 0111 07
- counter = 0xFC;
- } else if (j == 6) {//0000 0011 03
- counter = 0xFE;
- } else if (j == 7) {//0000 0001 01
- counter = 0xFF;
- } //else if (j == 8) {
- //row = 2;
- //}
- } else if ((i != 1) && (k == 1)){
- counter = 0x00;
- }
- if ((k == 1) && (j != 8)) {
- SPIDR = counter;
- }
- } else if (k == 2) {
- if ((i == 0) || (i == 1)) {
- counter = 0xFF;
- } else if (i == 2) {
- if (j == 0) { //1111 1111 FF
- counter = 0x80;
- } else if (j == 1) {//0111 1111 7F
- counter = 0xC0;
- } else if (j == 2) {//0011 1111 3F
- counter = 0xE0;
- } else if (j == 3) {//0001 1111 1F
- counter = 0xF0;
- } else if (j == 4) {//0000 1111 0F
- counter = 0xF8;
- } else if (j == 5) {//0000 0111 07
- counter = 0xFC;
- } else if (j == 6) {//0000 0011 03
- counter = 0xFE;
- } else if (j == 7) {//0000 0001 01
- counter = 0xFF;
- }
- } else if ((i != 2) && (k == 2)) {
- counter = 0x00;
- }
- if ((k == 2) && (j != 8)) {
- SPIDR = counter;
- }
- } else if (k == 3) {
- if ((i == 0) || (i == 1) || (i == 2)) {
- counter = 0xFF;
- } else if (i == 3) {
- if (j == 0) { //1111 1111 FF
- counter = 0x80;
- } else if (j == 1) {//0111 1111 7F
- counter = 0xC0;
- } else if (j == 2) {//0011 1111 3F
- counter = 0xE0;
- } else if (j == 3) {//0001 1111 1F
- counter = 0xF0;
- } else if (j == 4) {//0000 1111 0F
- counter = 0xF8;
- } else if (j == 5) {//0000 0111 07
- counter = 0xFC;
- } else if (j == 6) {//0000 0011 03
- counter = 0xFE;
- } else if (j == 7) {//0000 0001 01
- counter = 0xFF;
- }
- } else {
- counter = 0x00;
- }
- if ((k == 3) && (j != 8)) {
- SPIDR = counter;
- }
- }// row if end
- }//i for end
- for (a = 0x00; a < 0xFE; a++) {
- for (b = 0x00; b < 0xFE; b++) {
- for (c = 0x00; c < 0x3F; c++) {
- //do nothing
- }
- }
- }
- }//j for end
- }//k for end
- //while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- //}
- //counter = 0x00;
- }
- /*********************************************************************************
- * Name: LED_Convert
- * Description: Converts chess data to LED data.
- * [->] (global): LEDOutData
- * ChessMatrix
- * [<-] (void): [void]
- *********************************************************************************/
- void LED_Convert(void) {
- int i = 0;
- int j = 0;
- int k = 0;
- int temp = 1;
- int temp2 = 1;
- for (i = 0; i < 8; i++) { //Chess column loop
- LEDOutData[i] = 0; //Clear of former data.
- MOVEOutData[i] = 0;
- for (j = 0; j < 8; j++) { //Chess row loop
- if (HallMatrix[i][j] != 0) { //if piece exists on tile
- /*temp = 1;
- for (k = 0; k < j; k++) { //shifting data calculation
- temp = temp * 2; //binary shift left
- }*/
- temp = 128;
- for (k = 0; k < j; k++) {
- temp = temp / 2;
- }
- LEDOutData[i] += temp;
- }
- if(ldata[i][j] != 0) { //if light exists in ldata
- /*temp2 = 1;
- for (k = 0; k < j; k++) { //shifting data calculation
- temp2 = temp2 * 2;
- }*/
- temp2 = 128;
- for (k = 0; k < j; k++) {
- temp2 = temp2 / 2;
- }
- MOVEOutData[i] += temp2;
- }
- }
- }
- return;
- }
- /*********************************************************************************
- * Hall Sensor Functions
- **********************************************************************************
- **********************************************************************************
- * Name: HALL_Input
- * Description: Collects magnet connection data.
- * [->] (global): datas
- * [<-] (void): [void]
- *********************************************************************************/
- void HALL_Input(void) {
- int i = 0;
- int j = 0;
- char Selector = 0x00;
- int LinVal = 0;
- int Temp[64];
- int MatVal = 0;
- Selector = 0x0B; //Multiplexer data slector set to highest for chip 5 (12)
- for (j = 0; j < 12; j++) {//Multiplexer inputs on chip 5
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = Selector; //Put data into SPISR data register
- LCD_Delay(0);
- //if (j == 1) {
- // LineVal == 0;
- //}
- Temp[LinVal] = output5;
- Selector -= 0x01;
- LinVal++;
- }
- Selector = 0x0C; //Multiplexer data slector set to highest for chip 4 (13)
- for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 4
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = Selector; //Put data into SPISR data register
- LCD_Delay(0);
- Temp[LinVal] = output4;
- Selector -= 0x01;
- LinVal++;
- }
- Selector = 0x0C; //Multiplexer data slector set to highest for chip 3 (13)
- for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 3
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = Selector; //Put data into SPISR data register
- LCD_Delay(0);
- Temp[LinVal] = output3;
- Selector -= 0x01;
- LinVal++;
- }
- Selector = 0x0C; //Multiplexer data slector set to highest for chip 2 (13)
- for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 2
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = Selector; //Put data into SPISR data register
- LCD_Delay(0);
- Temp[LinVal] = output2;
- Selector -= 0x01;
- LinVal++;
- }
- Selector = 0x0C; //Multiplexer data slector set to highest for chip 1 (13)
- for (j = 0; j < 13; j++) {//Multiplexer inputs on chip 1
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = Selector; //Put data into SPISR data register
- LCD_Delay(0);
- Temp[LinVal] = output;
- Selector -= 0x01;
- LinVal++;
- }
- for (i = 0; i < 8; i++) {
- for (j = 0; j < 8; j++) {
- HallMatrix[i][j] = Temp[MatVal];
- MatVal++;
- }
- }
- return;
- }
- /*********************************************************************************
- * LCD Specific Functions
- **********************************************************************************
- **********************************************************************************
- * Name: LCD_Timer
- * Description: Refreshes the chess timer.
- * [->] (global): P1mins: Player 1 minutes remaining
- * P1secs: Player 1 seconds remaining
- * P2mins: Player 2 minutes remaining
- * P2secs: Player 2 seconds remaining
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Timer(void) {
- int t1 = 0;
- int t2 = 0;
- LineOffset = 0x00;
- LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
- LCD_Cursor_Position(1); //"White: "
- LCD_Send_Byte('W',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(':',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- t2 = P1mins % 10; //mins:secs,
- t1 = P1mins / 10;
- LCD_Cursor_Position(1);
- LCD_Send_Byte(t1 + 0x30,0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(t2 + 0x30,0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(':',0);
- t2 = P1secs % 10;
- t1 = P1secs / 10;
- LCD_Cursor_Position(1);
- LCD_Send_Byte(t1 + 0x30,0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(t2 + 0x30,0);
- //----------------------- line two begins
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte('B',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('l',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('k',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(':',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- t2 = P2mins % 10;
- t1 = P2mins / 10;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(t1 + 0x30,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(t2 + 0x30,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(':',0);
- t2 = P2secs % 10;
- t1 = P2secs / 10;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(t1 + 0x30,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(t2 + 0x30,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- return;
- }
- /*********************************************************************************
- * Name: LCD_Delay
- * Description: Creates a delay of ~2ms or ~30cy using ASM NOPs.
- * [->] (int): mode: Operational mode of the function.
- * 0 = Short wait mode (~30cy)
- * 1 = Long wait mode (~25,000cy) = (~2ms)
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Delay(int mode) {
- int i = 0; //Wait loop counter
- //Mode check
- if (mode == 0) {
- mode = 3;
- } else if (mode == 1) {
- mode = 2500;
- } else {
- mode = 250000000;
- }
- //Wait generation loop
- for (i = 0; i <= mode; i++) {
- asm {
- nop
- nop
- nop
- nop
- nop
- nop
- nop
- nop
- nop
- nop
- }
- }
- return;
- }
- /*********************************************************************************
- * Name: LCD_Cursor_Position
- * Description: Moves the LCD Cursor vertically and horizontally.
- * [->] (int): LineNumber: Declares the line number the cursor is to be moved to
- * 1 = Cursor to be moved to line #1
- * 2 = Cursor to be moved to line #2
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Cursor_Position(int LineNumber) {
- LCD_Send_Byte(LCD_CURSOR, 1);
- if (LineNumber == 1) {
- LCD_Send_Byte((LCD_LINE1 + LineOffset), 1); //Instruction mode
- } else {
- LCD_Send_Byte((LCD_LINE2 + LineOffset), 1); //Instruction mode
- }
- LineOffset = LineOffset + 0x01;
- return;
- }
- /*********************************************************************************
- * Name: LCD_Send_Byte
- * Description: Sends character or instruction through respective shift register.
- * [->] (uchar): data: The byte of data to be sent through the shift register
- * (int): mode: Operational mode of the function,
- * 0 = Normal sending mode
- * 1 = Instruction sending mode
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Send_Byte(char data, int mode) {
- if (mode == 1) {
- LCD_RS = 0; //Register select line low
- }
- while (SPISR_SPTEF == 0) {
- //Loop until SPISR data register is empty
- }
- SPIDR = data; //Put data into SPISR data register
- LCD_Delay(0); //Call a 30cy delay so that data can shift out
- LCD_CLK = 0; //this may be some other clock i don't know
- LCD_CLK = 1;
- LCD_Delay(1); //Call a 2ms delay so LCD can respond
- if (mode == 1) {
- LCD_RS = 1; //Register select line high. This is the default.
- }
- return;
- }
- /*********************************************************************************
- * Name: LCD_Piece_Select
- * Description: Displays some text on the LCD to select a kind of piece.
- * [->] (int): MsgType: The pre-rendered message to print to the LCD.
- *
- *
- * X1 = pawn
- * X2 = rook
- * X3 = knight
- * X4 = bishop
- * X5 = Queen
- *
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Piece_Select(int PieceType) {
- LineOffset = 0x00;
- LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
- LCD_Cursor_Position(1);
- LCD_Send_Byte('S',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('l',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('p',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LineOffset = 0x00;
- /* options for pawn to become:
- * X2 = rook
- * X3 = knight
- * X4 = bishop
- * X5 = Queen
- *
- */
- if (PieceType == 2) {
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('R',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('k',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- }
- if (PieceType == 3) {
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('K',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('g',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- }
- if (PieceType == 4) {
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('B',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('p',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- }
- if (PieceType == 5) {
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('Q',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('u',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- }
- }
- /*********************************************************************************
- * Name: LCD_Display
- * Description: Displays some text on the chosen LCD.
- * [->] (int): MsgType: The pre-rendered message to print to the LCD.
- * 0 = "Press the shiny|button to start!"
- * 1 = " --OPTION-- | >Start chess "
- * 2 = " --OPTION-- | Edit timer "
- * 3 = " --OPTION-- | Load Game "
- * 4 = " --OPTION-- |< Save Game "
- * 5 = " --PAUSED-- |Press to resume"
- * [<-] (void): [void]
- *********************************************************************************/
- void LCD_Display(int MsgType) {
- LineOffset = 0x00;
- LCD_Send_Byte(LCD_CLEAR, 1); //Instruction mode
- if (MsgType == 0) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('y',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte('b',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('u',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('!',0);
- } else if (MsgType == 1) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('I',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('N',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(0x7E,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('S',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('s',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- } else if (MsgType == 2) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('I',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('N',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('E',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('d',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('m',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- } else if (MsgType == 3) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('I',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('N',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('L',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('d',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('G',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('m',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- } else if (MsgType == 4) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('I',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('O',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('N',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('-',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(0x7F,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('S',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('v',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('G',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('m',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- } else if (MsgType == 5) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte('P',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('u',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('E',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('v',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('y',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(0x7F,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('T',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('g',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('B',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('a',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('k',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('!',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- } else if (MsgType == 6) {
- LCD_Cursor_Position(1);
- LCD_Send_Byte('S',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('d',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('w',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('n',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(1);
- LCD_Send_Byte(' ',0);
- LineOffset = 0x00;
- LCD_Cursor_Position(2);
- LCD_Send_Byte(0x7F,0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('p',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('i',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('c',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('k',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('.',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('u',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('p',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('o',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('t',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('h',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('e',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('r',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte('.',0);
- LCD_Cursor_Position(2);
- LCD_Send_Byte(' ',0);
- }
- }
- /*********************************************************************************
- * Interrupt Functions
- **********************************************************************************
- **********************************************************************************
- * Name: RTI_ISR
- * Description: Interrupt at 2.048ms intervals. Checks pushbutton states.
- * [->] (*): (*)
- * [<-] (void): [void]
- *********************************************************************************/
- interrupt 7 void RTI_ISR(void)
- {
- CRGFLG = CRGFLG | 0x80; //Enable
- //debug pushbuttons on docking board
- /*if ((TOP_BTN == 0) && (new_TOP_BTN == 0)) {
- new_TOP_BTN = 1;
- }
- if ((BTM_BTN == 0) && (new_BTM_BTN == 0)) {
- new_BTM_BTN = 1;
- }*/
- if((RPG_BTN == 1) && (prevRPGbutton ==0)) {
- newRPGbutton = 1;
- }
- prevRPGbutton = RPG_BTN;
- if(RPG_A == 1) {
- RPGRightTurns++;
- if(RPGLeftTurns > 3) {
- RPGLeft = 1;
- }
- RPGLeftTurns = 0;
- }
- if(RPG_B == 1) {
- RPGLeftTurns++;
- if(RPGRightTurns > 3) {
- RPGRight = 1;
- }
- RPGRightTurns = 0;
- }
- }
- /**********************************************************************************
- * Name: TIM_ISR
- * Description: TIM RTI at 10ms for 100 packets. Updates timer.
- * [->] (*): (*)
- * [<-] (void): [void]
- **********************************************************************************/
- interrupt 15 void TIM_ISR( void)
- {
- TFLG1 = TFLG1 | 0x80; //Enable
- TimerCounter++; //Another 10ms have passed
- if(TimerCounter % 10 == 0){
- LED_Convert(); //Set up LED backlighting
- enable = 1;
- LED_Out();
- enable = 0;
- RefreshHall = 1;
- }
- if (noteflag == 1) {
- notecnt++;
- //if (notecnt % 10 == 1) {
- PWMDTY0 = duty1;
- PWMPER0 = period1;
- //} else if (notecnt % 10 == 0) {
- // PWMDTY0 = duty2;
- // PWMPER0 = period2;
- //}
- if (notecnt == notedur) {
- notecnt = 0;
- noteflag = 0;
- }
- }
- if(TimerRunning == 1){
- if (TimerCounter >= 100) { //100 10ms interrupts accumulated
- //enable2 = 1;
- //HALL_Input();
- //enable2 = 0;
- if (PlayerToggle == 0) { //player is white
- if (P1secs == 0) { //XX:00 is white timer
- P1mins--;
- P1secs = 59;
- if(P1mins <= 0 && P1secs <= 0) { //timer lock at 00:00
- P1mins = 0;
- P1secs = 0;
- TimerExpired = 1;
- TimerRunning = 0;
- TIE_C7I = 0;
- }
- } else { //XX:XX is white timer
- P1secs--;
- }
- } else { //player is black
- if (P2secs == 0) { //XX:00 is black timer
- P2mins--;
- P2secs = 59;
- if(P2mins <= 0 && P2secs <= 0) { //timer lock at 00:00
- P2mins = 0;
- P2secs = 0;
- TimerExpired = 1;
- TimerRunning = 0;
- TIE_C7I = 0;
- }
- } else { //XX:XX is black timer
- P2secs--;
- }
- }
- TimerRefresh = 1;
- TimerCounter = 0;
- }
- }
- /*if(scicountflag == 1){
- scicount += 1;
- if(scicount == 200){
- //10 seconds have passed
- scicount = 0;
- SCICR2 = SCICR2 | 0x80;
- scicountflag = 0;
- }
- }*/
- if(inputonflag == 1 && (SCISR1 & 0x20)){
- inputchar = SCIDRL;
- echoflag = 1;
- tdata = 0;
- tin = tout;
- addchar(inputchar);
- SCICR2 = SCICR2 | 0x80;
- }
- }
- /*********************************************************************************
- * Sound Generation Functions
- **********************************************************************************
- **********************************************************************************
- * Name: Play_Sound
- * Description: Generates sounds based on what is current required.
- * [->] (int): Mode: The type of sound that will be output.
- * 0 = "Error" sound (buzz)
- * 1 = "White's turn" sound (boop beep)
- * 2 = "Black's turn" sound (beep boop)
- * 3 = "Mystery" sound (whoop)
- * [<-] (void): [void]
- *********************************************************************************/
- void Play_Sound(int mode) {
- unsigned char a = 0; //Looping variables
- unsigned char b = 0;
- unsigned char c = 0;
- unsigned char d = 0;
- unsigned char e = 0;
- if (mode == 0) {
- for (a = 0; a <= 4; a++) {
- for (b = 0xFF; b >= 0x01; b--) {
- PWMPER0 = 0xFF;
- for (c = 0x00; c <= 0xFE; c++) {
- PWMDTY0 = c;
- for (d = 0; d < 1; d++) {
- for (e = 0; e < 1; e++) {
- }
- }
- }
- for (c = 0xFF; c >= 0x01; c--) {
- PWMDTY0 = c;
- for (d = 0; d < 1; d++) {
- for (e = 0; e < 1; e++) {
- }
- }
- }
- }
- for (d = 0; d < 1; d++) {
- for (e = 0; e < 100; e++) {
- }
- }
- }
- PWMDTY0 = 0x00;
- } else if (mode == 1) {
- PWMDTY0 = 175;
- for (a = 0; a < 0xFF; a++) {
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- PWMDTY0 = 225;
- for (a = 0; a < 0xFF; a++) {
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- PWMDTY0 = 0x00;
- } else if (mode == 2) {
- PWMPER0 = 0xFF;
- PWMDTY0 = 225;
- for (a = 0; a < 0xFF; a++) {
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- PWMDTY0 = 175;
- for (a = 0; a < 0xFF; a++) {
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- PWMDTY0 = 0x00;
- } else if (mode == 3) {
- for (a = 0; a < 0xFE; a++) {
- PWMDTY0 = a;
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- for (a = 0; a < 0xFE; a++) {
- PWMDTY0 = 0xFE - a;
- for (b = 0; b < 0xFF; b++) {
- for (c = 0; c < 0x0F; c++) {
- //do nothing
- }
- }
- }
- PWMDTY0 = 0;
- }
- return;
- }
- /*********************************************************************************
- * Name: Play_Music
- * Description:
- *********************************************************************************/
- void Play_Music() {
- int div = 1;
- int note = 0;
- int temp = 0;
- int j = 0;
- int i = 0;
- int hey = 0;
- notedur = 25;
- notecnt = 0;
- PWMSCLA = 12;
- PWMDTY0 = 0x00;
- PWMPER0 = 0xFF;
- Song_Select();
- Song_Select2();
- for (i = 0; i < 8; i++) { //song column loop
- for (j = 0; j < 8; j++) { //song row loop
- temp = hello[i][j];
- note = temp % 100;
- div = temp /100;
- div++;
- if (note == 0) { //no note
- duty1 = 0x00;
- noteflag = 1;
- }
- else if (note == 1) { //low C
- period1 = 0xEF / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 2) { //D
- period1 = 0xD5 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 3) { //E
- period1 = 0xBE / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 4) { //F
- period1 = 0xB3 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 5) { //G
- period1 = 0xA0 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 6) { //A
- period1 = 0x8E / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 7) { //B
- period1 = 0x7F / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 8) { //C sharp
- period1 = 0xE6 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 9) { //E flat
- period1 = 0xC9 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 10) { //F sharp
- period1 = 0xA9 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 11) { //A flat
- period1 = 0x97 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- else if (note == 12) { //B flat
- period1 = 0x86 / div;
- duty1 = period1 / 2;
- noteflag = 1;
- }
- temp = hello2[i][j];
- note = temp % 100;
- div = temp /100;
- div++;
- if (note == 0) { //no note
- duty2 = 0x00;
- noteflag = 1;
- }
- else if (note == 1) { //low C
- period2 = 0xEF / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 2) { //D
- period2 = 0xD5 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 3) { //E
- period2 = 0xBE / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 4) { //F
- period2 = 0xB3 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 5) { //G
- period2 = 0xA0 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 6) { //A
- period2 = 0x8E / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 7) { //B
- period2 = 0x7F / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 8) { //C sharp
- period2 = 0xE6 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 9) { //E flat
- period2 = 0xC9 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 10) { //F sharp
- period2 = 0xA9 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 11) { //A flat
- period2 = 0x97 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- else if (note == 12) { //B flat
- period2 = 0x86 / div;
- duty2 = period2 / 2;
- noteflag = 1;
- }
- while (noteflag == 1) { // wait
- }
- }
- }
- PWMSCLA = 2;
- PWMDTY0 = 0x00;
- PWMPER0 = 0xFF;
- }
- /*********************************************************************************
- * Name: Song_Select
- * Description:
- *********************************************************************************/
- void Song_Select(void) {
- hello[0][0] = 104;
- hello[0][1] = 109;
- hello[0][2] = 111;
- hello[0][3] = 111;
- hello[0][4] = 111;
- hello[0][5] = 111;
- hello[0][6] = 112;
- hello[0][7] = 112;
- hello[1][0] = 112;
- hello[1][1] = 112;
- hello[1][2] = 301;
- hello[1][3] = 301;
- hello[1][4] = 111;
- hello[1][5] = 111;
- hello[1][6] = 109;
- hello[1][7] = 109;
- hello[2][0] = 104;
- hello[2][1] = 109;
- hello[2][2] = 111;
- hello[2][3] = 111;
- hello[2][4] = 111;
- hello[2][5] = 111;
- hello[2][6] = 112;
- hello[2][7] = 112;
- hello[3][0] = 112;
- hello[3][1] = 112;
- hello[3][2] = 301;
- hello[3][3] = 301;
- hello[3][4] = 112;
- hello[3][5] = 112;
- hello[3][6] = 112;
- hello[3][7] = 112;
- hello[4][0] = 104;
- hello[4][1] = 109;
- hello[4][2] = 111;
- hello[4][3] = 111;
- hello[4][4] = 111;
- hello[4][5] = 111;
- hello[4][6] = 112;
- hello[4][7] = 112;
- hello[5][0] = 112;
- hello[5][1] = 112;
- hello[5][2] = 301;
- hello[5][3] = 301;
- hello[5][4] = 111;
- hello[5][5] = 111;
- hello[5][6] = 109;
- hello[5][7] = 109;
- hello[6][0] = 104;
- hello[6][1] = 109;
- hello[6][2] = 111;
- hello[6][3] = 111;
- hello[6][4] = 111;
- hello[6][5] = 111;
- hello[6][6] = 112;
- hello[6][7] = 112;
- hello[7][0] = 111;
- hello[7][1] = 111;
- hello[7][2] = 111;
- hello[7][3] = 111;
- hello[7][4] = 109;
- hello[7][5] = 109;
- hello[7][6] = 109;
- hello[7][7] = 109;
- return;
- }
- /*********************************************************************************
- * Name: Song_Select2
- * Description:
- *********************************************************************************/
- void Song_Select2(void) {
- hello2[0][0] = 8;
- hello2[0][1] = 8;
- hello2[0][2] = 8;
- hello2[0][3] = 8;
- hello2[0][4] = 4;
- hello2[0][5] = 4;
- hello2[0][6] = 4;
- hello2[0][7] = 4;
- hello2[1][0] = 1;
- hello2[1][1] = 1;
- hello2[1][2] = 1;
- hello2[1][3] = 1;
- hello2[1][4] = 4;
- hello2[1][5] = 4;
- hello2[1][6] = 4;
- hello2[1][7] = 4;
- hello2[2][0] = 8;
- hello2[2][1] = 8;
- hello2[2][2] = 8;
- hello2[2][3] = 8;
- hello2[2][4] = 4;
- hello2[2][5] = 4;
- hello2[2][6] = 4;
- hello2[2][7] = 4;
- hello2[3][0] = 8;
- hello2[3][1] = 8;
- hello2[3][2] = 8;
- hello2[3][3] = 8;
- hello2[3][4] = 4;
- hello2[3][5] = 4;
- hello2[3][6] = 4;
- hello2[3][7] = 4;
- hello2[4][0] = 1;
- hello2[4][1] = 1;
- hello2[4][2] = 1;
- hello2[4][3] = 1;
- hello2[4][4] = 4;
- hello2[4][5] = 4;
- hello2[4][6] = 4;
- hello2[4][7] = 4;
- hello2[5][0] = 1;
- hello2[5][1] = 1;
- hello2[5][2] = 1;
- hello2[5][3] = 1;
- hello2[5][4] = 4;
- hello2[5][5] = 4;
- hello2[5][6] = 4;
- hello2[5][7] = 4;
- hello2[6][0] = 1;
- hello2[6][1] = 1;
- hello2[6][2] = 1;
- hello2[6][3] = 1;
- hello2[6][4] = 8;
- hello2[6][5] = 8;
- hello2[6][6] = 8;
- hello2[6][7] = 8;
- hello2[7][0] = 11;
- hello2[7][1] = 11;
- hello2[7][2] = 11;
- hello2[7][3] = 11;
- hello2[7][4] = 5;
- hello2[7][5] = 5;
- hello2[7][6] = 5;
- hello2[7][7] = 5;
- return;
- }
- /*********************************************************************************
- * Character I/O Library Routines
- **********************************************************************************
- **********************************************************************************
- * Name: inchar
- * Description: Inputs ASCII character from SCI serial port and returns it.
- * [->] (void): [void]
- * [<-] (char): ASCII value from Teraterm .
- *********************************************************************************/
- char inchar(void) {
- /* receives character from the terminal channel */
- while (!(SCISR1 & 0x20)); /* wait for input */
- return SCIDRL;
- }
- /*********************************************************************************
- * Name: outchar
- * Description: Outputs ASCII character to the SCI serial port.
- * [->] (char): ASCII value.
- * [<-] (void): [void]
- *********************************************************************************/
- void outchar(char ch) {
- /* sends a character to the terminal channel */
- while (!(SCISR1 & 0x80)); /* wait for output buffer empty */
- SCIDRL = ch;
- }
- /**********************************************************************************
- * Name: bco
- * Description: Outputs character data in radial buffer to serial.
- * [->] (*): (*)
- * [<-] (void): [void]
- **********************************************************************************/
- void bco(char input)
- {
- while(((tin + 1) % tsize) == tout) {}
- tbuf[tin] = input;
- tin = (tin + 1) % tsize;
- SCICR2 = SCICR2 | 0x80;
- }
- /*********************************************************************************
- * ECE 362 - Mini-Project C Source File - Fall 2012
- *********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment