I'm trying to write the code for a game that generates random numbers 10 times, in which the player pushes a pushbutton depending on the LED that lights up. At the end of the game, the program is supposed to return the times the player generated the correct and incorrect input. After this, the game restarts by a slide switch.
I've gotten everything to work, however, I can't configure the counter (variables 'correct' and 'wrong') to restart after 10 turns. If I don't set 'correct' and 'wrong' to 0 (as the code is right now), the count just keeps increasing as it should. However, I'm struggling to come up with where to put the reset counters in a way that I don't get output that always returns zero, but restarts after 10 turns. Please help!
Thanks!
#include <c8051_SDCC.h>
#include <stdio.h>
#include <stdlib.h>
void Port_Init(void); // Initialize ports for input and output
void Timer_Init(void); // Initialize Timer 0
void Interrupt_Init(void); //Initialize interrupts
void Timer0_ISR(void) __interrupt 1;
unsigned char random_function(void);
void LED_light(void);
void PB_result(void);
__sbit __at 0xB6 LED0; // LED0, associated with Port 3, Pin 6
__sbit __at 0xB5 LED1; // LED1, associated with Port 3, Pin 5
__sbit __at 0xB3 BILED1; // BILED0, associated with Port 3, Pin 3
__sbit __at 0xB4 BILED2; // BILED1, associated with Port 3, Pin 4
__sbit __at 0xB7 BZZR; // Buzzer, associated with Port 3, Pin 7
__sbit __at 0xA0 SS; // Slide switch, associated with Port 2, Pin 0
__sbit __at 0xB0 PB0; // Push button 1, associated with Port 3, Pin 0
__sbit __at 0xB1 PB1; // Push button 2, associated with Port 3, Pin 1
unsigned char Counts = 0;
unsigned int count = 0;
unsigned char compare_random[2] = {9,9};
unsigned char correct = 0;
unsigned char wrong = 0;
unsigned char random = 0;
void main(void)
{
Sys_Init(); // System Initialization
Port_Init(); // Initialize ports 2 and 3
Interrupt_Init();
Timer_Init(); // Initialize Timer 0
putchar(' ');
printf("Start\r\n");
while (1) /* the following loop prints the number of overflows that occur while the pushbutton is pressed, the BILED is lit while the button is pressed */
{
if ((!SS) && (Counts < 10))
{
random_function();
printf("Random number is %d\r\n", compare_random[0]);
LED_light();
printf("LED here \r\n");
PB_result();
printf("Push Button here1 \r\n");
Counts++;
printf("Counts is %d \r\n", Counts);
}
else if ((SS) && (Counts < 10))
{
TR0 = 0;
printf("Flip slide switch to on position to resume.\r");
}
else if ((SS) && (Counts >= 10))
{
printf("\r\nFlip slide switch to on position to restart.");
TR0 = 0;
Counts = 0;
}
else if ((!SS) && (Counts >= 10))
{
if (Counts == 10)
{
printf("You pushed the buttons correctly %d times and incorrectly %d times.\r", correct, wrong);
}
printf("Flip switch to off position to restart.\r");
TR0 = 0;
}
}
}
void Port_Init(void)
{
// Port 3
P3MDOUT |= 0x78; // set Port 3 output pins to push-pull mode
P3MDOUT &= 0xFC; // set Port 3 input pins to open drain mode
P3 |= ~0xFC; // set Port 3 input pins to high impedance state
// Port 2
P2MDOUT &= 0xFE; //set Port 2 input pins to open drain mode
P2 |= ~0xFE; //set Port 2 input pins to high impedance state
// adding the output bit for LED1
}
void Interrupt_Init(void)
{
IE |= 0x02; // enable Timer0 Interrupt request (by masking)
EA = 1; // enable global interrupts (by sbit)
}
void Timer_Init(void)
{
CKCON |= 0x08; // Timer0 uses SYSCLK as source
TMOD &= 0xF0; // clear the 4 least significant bits
TMOD |= 0x01; // Timer0 in mode 1
TR0 = 0; // Stop Timer0
TMR0 = 0; // Clear high & low byte of T0
}
void Timer0_ISR(void) __interrupt 1
{
TF0 = 0; // Timer 0 interrupt flag
count++; // Increment global variable 'Counts'
}
/*return a random integer number between 0, 1, and 2*/
unsigned char random_function(void)
{
random = rand()%3;
compare_random[1] = random;
while (compare_random[0] == compare_random[1])
{
compare_random[1] = rand()%3;
}
compare_random[0] = compare_random[1];
return compare_random[1];
}
void LED_light(void)
{
if (compare_random[1] == 0)
{
count = 0;
TR0 = 1;
while (count < 337)
{
LED0 = 0;
LED1 = 1; //wait 1 second
}
LED0 = 1;
LED1 = 1;
}
else if (compare_random[1] == 1)
{
count = 0;
TR0 = 1;
while (count < 337)
{
LED0 = 1;
LED1 = 0; //wait 1 second
}
LED0 = 1;
LED1 = 1;
}
else if (compare_random[1] == 2)
{
count = 0;
TR0 = 1;
while (count < 337)
{
LED0 = 0;
LED1 = 0; //wait 1 second
}
LED0 = 1;
LED1 = 1;
}
}
void PB_result(void)
{
if ((!PB0) && (PB1))
{
if (compare_random[1] == 0)
{
//correct = 0;
correct++;
BILED1 = 0;
BILED2 = 1;
}
else if (compare_random[1] != 0)
{
//wrong = 0;
wrong++;
BILED1 = 1;
BILED2 = 0;
}
}
else if ((PB0) && (!PB1))
{
if (compare_random[1] == 1)
{
//correct = 0;
correct++;
BILED1 = 0;
BILED2 = 1;
}
else if (compare_random[1] != 1)
{
//wrong = 0;
wrong++;
BILED1 = 1;
BILED2 = 0;
}
}
else if ((!PB0) && (!PB1))
{
if (compare_random[1] == 2)
{
//correct = 0;
correct++;
BILED1 = 0;
BILED2 = 1;
}
else if (compare_random[1] != 2)
{
//wrong = 0;
wrong++;
BILED1 = 1;
BILED2 = 0;
}
}
else if ((PB0) && (PB1))
{
//wrong = 0;
wrong++;
BILED1 = 1;
BILED2 = 0;
}
}
Aucun commentaire:
Enregistrer un commentaire