#include /* * Developer - Samarth Artweaver * Date - 28/5/2012 * * pin configuration - * * p1.0 - switch1 * p1.1 - switch2 * p1.2 - switch3 * p1.3 - switch4 * p1.4 - latch clock * p1.5 - shift clock * p2.0 - switch5 * p2.1 - switch6 * p2.2 - switch7 * * p1.7 - data output * */ void delay(int); void initialize(); void send(int, int); void sendij(int,int); void set_display(); void compute_ball(); void check_game(); void check_game2(); void game_over(int); void startup_screen(); void display(int); int i=3,j=3,level=100; //level = speed int p1[5]={2,3,4}; //player 1 pixels int p2[5]={2,3,4}; //player 2 pixels int ver=0,hor=0,a,b,paus=0; /*ver = 0 => moving left = 1 => moving right *hor = 0 => moving down = 1 => moving up */ int scorea=4,scoreb=4; //initial scores of the players int arr2[7][8]= { {0x00,0x3c,0x24,0x24,0x24,0x24,0x3c,0x00},//0 {0x00,0x10,0x30,0x50,0x10,0x10,0x10,0x7c},//1 {0x00,0x38,0x44,0x04,0x08,0x10,0x7c,0x00},//2 {0x00,0x38,0x04,0x04,0x18,0x04,0x04,0x38},//3 {0x00,0x24,0x24,0x24,0x3c,0x04,0x04,0x04},//4 {0x00,0x10,0x28,0x44,0x44,0x7c,0x44,0x44},//a {0x00,0x38,0x24,0x24,0x38,0x24,0x24,0x38},//b }; void main(void) { initialize(); //the serial communication, interrupts, IO pins, etc startup_screen(); // P I N G P O N G display while(1) { compute_ball(); //compute ball's next state set_display(); //display ball and the players if(j==6) check_game(); //check game for player 1 if(j==1) check_game2(); //check game for player2 } } void startup_screen() { int arr[8][8]= {{0x00,0x3c,0x24,0x24,0x3c,0x20,0x20,0x00},//p {0x00,0x7c,0x10,0x10,0x10,0x10,0x7c,0x00},//i {0x00,0x44,0x64,0x54,0x4c,0x44,0x44,0x00},//n {0x00,0x3c,0x20,0x20,0x2c,0x24,0x3c,0x00},//g {0x00,0x3c,0x24,0x24,0x24,0x24,0x3c,0x00}};//o int seq[8]={0,1,2,3,0,4,2,3}; //PINGPONG int c; for(a=0;a<8;a++) { for(c=0;c<100;c++) { for(b=0;b<8;b++) { send(1<=7) hor=1; if(j==1) ver=0; else if(j>=6) ver=1; } void check_game() { for(a=0;a<3;a++) { if(p1[a]==i) break; } if(a==1) { if(i>0 && hor==0) hor=1; else if(i<7 && hor==1) hor=0; } if(hor==0 && p1[0]==(i+1)) { a=0; if(i>0) hor=1; } if(hor==1 && p1[2]==(i-1)) { a=0; if(i<7) hor=0; } if(a==3) { game_over(1); scorea--; } } void check_game2() { for (a = 0; a < 3; a++) { if (p2[a] == i) break; } if (a == 1) { if (i > 0 && hor == 0) hor = 1; else if (i < 7 && hor == 1) hor = 0; } if(hor==0 && p2[0]==(i+1)) { a=0; if(i>0) hor=1; } if(hor==1 && p2[2]==(i-1)) { a=0; if(i<7) hor=0; } if (a == 3) { game_over(2); scoreb--; } } void game_over(int x) { if(x==1) { if(scorea!=0) display(scorea); else { display(6); } } else { if(scoreb!=0) display(scoreb); else { display(5); } } } void display(int x) { int c; for(c=0;c<300;c++) { for(a=0;a<8;a++) { send(1<0;i--) _delay_cycles(10000); } void initialize() { /* Stop watchdog timer from timing out during initial start-up. */ WDTCTL = WDTPW | WDTHOLD; P1SEL2 = BIT5 + BIT6 + BIT7; P1OUT = 0; /* Port 1 Port Select Register */ P1SEL = BIT5 + BIT6 + BIT7; /* Port 1 Direction Register */ P1DIR = BIT4; /* Port 1 Interrupt Edge Select Register */ P1IES = 0; /* Port 1 Interrupt Flag Register */ P1IFG = 0; /* Port 2 Output Register */ P2OUT = 0; /* Port 2 Direction Register */ P2DIR = 0; /* Port 2 Interrupt Edge Select Register */ P2IES = 0; /* Port 2 Interrupt Flag Register */ P2IFG = 0; BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0; if (CALBC1_16MHZ != 0xFF) { /* Adjust this accordingly to your VCC rise time */ __delay_cycles(100000); /* Follow recommended flow. First, clear all DCOx and MODx bits. Then * apply new RSELx values. Finally, apply new DCOx and MODx bit values. */ DCOCTL = 0x00; BCSCTL1 = CALBC1_16MHZ; /* Set DCO to 16MHz */ DCOCTL = CALDCO_16MHZ; } BCSCTL1 |= XT2OFF + DIVA_0; BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1; UCB0CTL1 |= UCSWRST; UCB0CTL0 = UCCKPH + UCMST + UCMODE_0 + UCSYNC; UCB0CTL1 = UCSSEL_2 + UCSWRST; UCB0CTL1 &= ~UCSWRST; __bis_SR_register(GIE); WDTCTL = WDTPW + WDTHOLD; //interupt buttons P1REN |= BIT0 + BIT1 + BIT2 + BIT3; // P1.3 Enable Pullup/Pulldown P1OUT = BIT0 + BIT1 + BIT2 + BIT3; // P1.3 pullup P1IE |= BIT0 + BIT1 + BIT2 + BIT3; // P1.3 interrupt enabled P1IES |= BIT0 + BIT1 + BIT2 + BIT3; // P1.3 Hi/lo falling edge P1IFG &= ~(BIT0 + BIT1 + BIT2 + BIT3); // P1.3 IFG cleared just in case P2REN |= BIT0 + BIT1 + BIT2; // P1.3 Enable Pullup/Pulldown P2OUT = BIT0 + BIT1 + BIT2; // P1.3 pullup P2IE |= BIT0 + BIT1 + BIT2; // P1.3 interrupt enabled P2IES |= BIT0 + BIT1 + BIT2; // P1.3 Hi/lo falling edge P2IFG &= ~(BIT0 + BIT1 + BIT2); // P1.3 IFG cleared just in case _EINT(); } void send(int i, int j) { P1OUT &= ~BIT4; UCB0TXBUF = i; P1OUT |= BIT4 ; P1OUT &= ~BIT4; UCB0TXBUF = j; P1OUT |= BIT4 ; }