Forums MPLAB XC8 Project Discussions Ping pong game in C Reply To: Ping pong game in C

#3028

Viga uk
Participant

#define LED1 PORTDbits.RD0
#define LED8 PORTDbits.RD7

void My_ISR(void); // Function prototype for our ISR
void main(void){

unsigned char direction=0; // 0 = right to left, 1 = left to right
unsigned char player1 =0;
unsigned char player2 =0;
unsigned char i;

// Set up ports and bits as required
TRISBbits.RB2 = 1; // PORTB, RB2 as input
TRISBbits.RB0 = 1; // PORTB, RB0 as input
TRISD = 0; // PORTD as output
PORTD = 0;

INTCONbits.INT0IF = 0; // Clear RB0 interrupt flag
INTCONbits.INT0IE = 1; // Enable RB0 as interrupt source
INTCON2bits.INTEDG0 = 0; // Make RB0 negative edge – remember button press high to low
INTCONbits.GIE = 1; // Enable all interrupts globally

INTCON3bits.INT2IF = 0; // Clear RB2 interrupt flag
INTCON3bits.INT2IE = 1; // Enable RB2 as interrupt source
INTCON2bits.INTEDG2 = 0; // Make RB2 negative edge – remember button press high to low
INTCONbits.GIE = 1; // Enable all interrupts globally

while(1){

if(PORTD == 0b00000001){
direction = 0; //right to left
player2 = 0; //player 2 hasnt won just yet
}

if(PORTD == 0b10000000){
direction = 1; //left to right
player1 = 0; //player 1 hasnt won just yet
}
//wait on start of game
if(PORTD > 0){

//if no one has won, delay before moving
if(player1 == 0 && player2 == 0){

for(i = 0; i < 4; i++){
Delay10KTCYx(50);
}
}
}
//MOVEMENT
//if moves right to left
if(direction == 0){

if(PORTD != 0b10000000){
PORTD = PORTD << 1; //move right to left
}
else{

player1 =1; //ball has flown off, player 1 wins
}
}
//if moves left to right
if(direction == 1){

if(PORTD != 0b00000001){
PORTD = PORTD >> 1; //move left to right
}
else{

player2 =1; //ball has flown off, player 2 wins
}
}
//check to see if a player has won
if(player1 == 1){

PORTD = 0;

//flash LED1 to signify win for player 1
for(i = 0; i < 10; i++){
LED1 ^= 1;
Delay10KTCYx(250);
}
//clear the result
PORTD = 0;
player1 = 0;

}

if(player2 ==1){

PORTD = 0;

//flash LED8 to signify win for player 2
for(i = 0;i < 10; i++){
LED8 ^= 1;
Delay10KTCYx(250);
}
//clear the result
PORTD = 0;
player2 = 0;
}
}
}

#pragma code My_HighPriority_Interrupt=0x08
void My_HighPriority_Interrupt(void){

_asm
GOTO My_ISR
_endasm
}
#pragma interrupt My_ISR
void My_ISR(void){

volatile unsigned char player1=0;

volatile unsigned char player2=0;
volatile unsigned char direction=0;

if(INTCON3bits.INT2IF == 1){
// If Button0 has caused interrupt
INTCON3bits.INT2IF = 0; // Clear RB0 interrupt flag
// Turn off LED
player2 = 1; //player 2 wins if none of the conditions before are met
if(PORTD == 0){
PORTD = 0b00000001;
direction = 0; //right to left
player2 = 0; //player 2 hasnt won just yet

}
}
if(INTCONbits.INT0IF == 1){
// If Button0 has caused interrupt
INTCONbits.INT0IF = 0; // Clear RB0 interrupt flag

player1 = 1; //player 1 wins if none of the conditions before are met
if(PORTD == 0){
PORTD = 0b10000000;
direction = 1; //left to right
player1 = 0; //player 1 hasnt won just yet

}
}
}