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 Luzarowski > Signature: ______________________
- *
- * Software Leader: < Mark Luzarowski > Signature: ______________________
- *
- * Interface Leader: < Micheal Standiford > Signature: ______________________
- *
- * Peripheral Leader: < Micheal Standiford > 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 can as a multi-
- * game base; games planned so far are chess and checkers. The board itself will
- * function as a "learning board" of sorts for any game programmed into it.
- *
- **********************************************************************************
- * Project-specific Success Criteria
- **********************************************************************************
- * 1. Piece tracking & movement analysis
- *
- * 2. Time & turn tracking
- *
- * 3. User-side game editing
- *
- **********************************************************************************
- * 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.
- * -------------------------------------------------------------------------------
- * Date:
- * Name:
- * Update:
- *
- ***********************************************************************************
- **********************************************************************************/
- #include <hidef.h> // common defines and macros
- #include "derivative.h" // derivative-specific definitions
- #include <mc9s12c32.h> // 9S12 register & port defines
- //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
- //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);
- //James' Functions
- //dec 1st
- void currpccnt(void);
- void pickedup(int row, int col, int piece);
- 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);
- void ledcopy(int mode);
- void queenled(int row,int col,int team);
- void ischeck(int team);
- 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);
- //dec 3rd
- void scanline(void);\
- void movecnt(void);
- int nummoves = 0;
- //void bco(void);
- void addchar(char a);
- char CR = 0x0D;//Return
- char LF = 0x0A; //line feed
- void sevenchar(int mode);
- 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);
- // 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; //variables for keeping track of pieces
- int upcol;
- int downrow;
- int downcol;
- int nextrow;
- int nextcol;
- int sameflag;
- int pickedupflag = 0;
- int hpc = 0;
- int cndiff = 0;
- int hndiff = 0;
- int rtarget;
- int ctarget;
- int nowpclatch;
- void hallvlatch(void);
- void hallvchess(void);
- void setdown(int row, int col, int piece);
- void puteverythingback(void);
- //Global Declarations Begin Here
- //------------------------------------
- //------------------------------------
- //SCI Variables
- int tin = 0; //SCI transmit display buffer IN pointer
- int tout = 0; //SCI transmit display buffer OUT pointer
- int tsize = 70; //SCI size of transmit buffer
- char tbuf[70]; //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;
- 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) {
- TIE_C7I = 1;
- 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){
- PlayerToggle = !PlayerToggle;
- }
- //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){
- 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 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] = 2;
- ChessMatrix[0][1] = 3;
- ChessMatrix[0][2] = 4;
- ChessMatrix[0][3] = 5;
- ChessMatrix[0][4] = 6;
- ChessMatrix[0][5] = 4;
- ChessMatrix[0][6] = 3;
- ChessMatrix[0][7] = 2;
- ChessMatrix[1][0] = 1;
- ChessMatrix[1][1] = 1;
- ChessMatrix[1][2] = 1;
- ChessMatrix[1][3] = 1;
- ChessMatrix[1][4] = 1;
- ChessMatrix[1][5] = 1;
- ChessMatrix[1][6] = 1;
- ChessMatrix[1][7] = 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] = 0;
- 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] = 0;
- 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] = 11;
- ChessMatrix[6][1] = 11;
- ChessMatrix[6][2] = 11;
- ChessMatrix[6][3] = 11;
- ChessMatrix[6][4] = 11;
- ChessMatrix[6][5] = 11;
- ChessMatrix[6][6] = 11;
- ChessMatrix[6][7] = 11;
- ChessMatrix[7][0] = 12;
- ChessMatrix[7][1] = 13;
- ChessMatrix[7][2] = 14;
- ChessMatrix[7][3] = 16;
- ChessMatrix[7][4] = 15;
- ChessMatrix[7][5] = 14;
- ChessMatrix[7][6] = 13;
- ChessMatrix[7][7] = 12;
- }
- if (Mode == 1) { //Update locations map
- }
- return;
- }
- /***********************************************************************
- BOARD ANALYSIS
- ***********************************************************************/
- //hall vs chessmatrix
- //up = 1 down = 0
- //updates hlatch
- void 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] != HallMatrix[i][j]) { //both aren't the same
- differ += 1;
- rtemp = i;
- ctemp = j;
- }
- }
- }
- //if(dir == 1){
- uprow = rtemp;
- upcol = ctemp;
- hlatch[uprow][upcol] = 0;
- // } else if(dir == 0){
- // downrow = rtemp;
- // downcol = ctemp;
- // }
- if(differ == 0){
- sameflag = 1;
- } else {
- sameflag = 0;
- }
- }
- void hallvlatch(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 (hlatch[i][j] != HallMatrix[i][j]) { //both aren't the same
- differ += 1;
- rtemp = i;
- ctemp = j;
- }
- }
- }
- //if(dir == 1){
- nextrow = rtemp;
- nextcol = ctemp;
- hlatch[uprow][upcol] = 0;
- // } else if(dir == 0){
- // downrow = rtemp;
- // downcol = ctemp;
- // }
- if(differ == 0){
- sameflag = 1;
- } else {
- sameflag = 0;
- }
- }
- //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;
- LCD_Display(6);
- 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;
- }
- }
- 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
- 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;
- }
- 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(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(team == 0){
- whitescore += score;
- } else if(team == 1){
- blackscore += score;
- }
- ChessMatrix[row][col] = piece;
- ChessMatrix[uprow][upcol] = 0;
- ledoutmode = 0;
- currpccnt();
- nowpccnt();
- if(currpc != nowpc){
- puteverythingback();
- }
- PlayerToggle = !PlayerToggle;
- //movecnt();
- //ledcopy(5);
- }
- /***********************************************************************
- 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 get rid of all 27
- 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) {
- if(ChessMatrix[r][c] == 27){
- ChessMatrix[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] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- rchk = row;
- rchk += -1;
- 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] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- cchk += -1;
- 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] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- rchk = row;
- cchk += -1;
- rchk += 1;
- 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] / 10) != team){
- ldata[rchk][cchk] = 1;
- }
- cchk = col;
- rchk = row;
- cchk += -1;
- rchk += -1;
- 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
- }
- void 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;
- }
- }
- }
- //checkmatrix(team);
- if(ldata[tempr][tempc] == 1){
- if(team == 0){
- whitecheckflag = 1;
- } else if (team == 1){
- blackcheckflag = 1;
- }
- }
- }
- /*********************************************************************************
- * 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++) {
- //if(ChessMatrix[line][i] == 0){
- boardset = 1 - boardset;
- if(HallMatrix[line][i] == 0){
- sevenchar(boardset);
- addchar('*');
- } else {
- //outteam(ChessMatrix[line][i] / 10);
- outteam(HallMatrix[line][i] / 10);
- }
- }
- } else {
- for (i = 0; i < 8; i++) {
- //if(ChessMatrix[line][i] == 0){
- boardset = 1 - boardset;
- if(HallMatrix[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;
- }
- 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 = PlayerToggle + 1;
- 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;
- }
- }
- /**********************************************************************************
- * 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){
- scicountflag = 1;
- SCICR2 = SCICR2 & 0x7F;
- //tin += 1;
- //tin = 0;
- for (j = 0; j < 2; j++) {
- addchar(LF);
- addchar(CR);
- }
- scioff_flag = 0;
- }
- if(tdata == 0){
- sciscan();
- }
- if(SCISR1 & 0x80){
- SCIDRL = tbuf[tout];
- tout += 1;
- tout = tout % tsize;
- tdata -= 1;
- }
- }
- /*********************************************************************************
- * 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: Outs a spiral to test the matrix
- * [->] (global): LEDOutData
- * [<-] (void): [void]
- *********************************************************************************/
- void LED_Test(void) {
- int i = 0;
- int j = 0;
- unsigned char a = 0x00;
- unsigned char b = 0x00;
- unsigned char c = 0x00;
- int counter = 0;
- int row = 0;
- int k = 0;
- //TIE_C7I = 0;
- for (k = 0; k < 3; k++) {
- for (j = 0; j < 9; j++) {//refreshs
- for (i = 0; i < 8; i++) {//rows
- 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 (j == 8) {//++
- //row = 1;
- //}
- } 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 (ChessMatrix[i][j] != 0) { //if piece exists on tile
- temp = 1;
- for (k = 0; k < j; k++) { //shifting data calculation
- 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;
- }
- 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 == 100) { //100 10ms interrupts accumulated
- //enable2 = 1;
- //HALL_Input();
- //enable2 = 0;
- RefreshHall = 1;
- 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;
- }
- }
- }
- /*********************************************************************************
- * 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;
- }
- /*********************************************************************************
- * 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