Hello Every one,
I wrote a code which is A program that manipulates and controls a bit pattern that is sent to an LED display.The bit pattern is controlled by three switches and one button The button acts as a gun shooting out the Least Significant LED of the Altera DE0. *
Two switches must be used to determine four speeds the LEDs rotate to One switch controls the direction of the rotating pattern and should display the number of shots on the seven segment display.
Now my problem is that I have to increment the seven seg every time I press the button under all conditions; killed a duck , missed a duck or any thing.
This is my code below I though it would be good that you have a look at the whole code . My code somehow relates the sev seg count with the speed of the LEDs because when they are really fast and I press the button , it counts like 10 or 20 times per press !!!
Any advice would be appreciated
Many thanks
Code:
/*********************************************************************************
* Duckshoot Program *
* Author: *
* Date: 11th April *
* Description: A program that manipulates and controls a bit pattern that is sent*
* to an LED display.The bit pattern is controled by three switches and one button *
* The button acts as a gun shooting out the Least Significant LED of the DE0. *
* Two switches must be used to determine four speeds the LEDs rotate to*
* One switch controls the direction of the rotating pattern. *
*List of Functions and Sub sections of functions: *
* Main Program *
which includes the code that edits the number of ducks written on the DE0 LEDs, taking into
account penultys
* Functions *
* WRITE LIGHTS *
* LIGHTS *
* SPEED SETTING *
* DIRECTION *
* END OF GAME *
* *
*********************************************************************************/
/* # Include Section*/
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
/* Function Prototype Section
* Prototypes for all functions called by this module, with the exception of USLEEP function whcih does exist in the Standard C Library*/
int main (void) __attribute__ ((weak, alias ("alt_main")));
void lights (unsigned int *sleep, unsigned int *data);
int endofgame (unsigned int *data);
/********************************************************************************/
/*********************************************************************************
*MAIN PROGRAM *
*********************************************************************************/
int alt_main (void)
{
unsigned int sleep;/*Asssigned to USLEEP*/
unsigned int data=0x2aa; /*set initial light pattern and store in lights to be sent to DE0*/
/*Number of Ducks on the DE0 - should be 2AA for alternate */
int count=0,d=0,n=0,x=0;
while(1)
{
alt_u32 sev[10]; /*Array for 0-9 hex*/
sev[0]=0xc0;
sev[1]=0xf9;
sev[2]=0xa4;
sev[3]=0xb0;
sev[4]=0x99;
sev[5]=0x92;
sev[6]=0x82;
sev[7]=0xf8;
sev[8]=0x80;
sev[9]=0x98;
alt_u16 buttons=0x00;
alt_u8 state=0;
/**********************************************************************************
When the button is on it will shootout the LSB led or add a penulty *
duck if there is no duck to shoot. *
*********************************************************************************/
buttons = IORD_ALTERA_AVALON_PIO_DATA(BUTTONS_BASE); /*READ the shoot button*/
buttons = (~buttons & 0x07);
/*Shoot or Relight the Lights*/
if((buttons &0x04)&& !state){ /*if button_2 is pressed*/
count++; /*First display*/
if(count>9){
d++; /*Second display*/
count=0;
}
if(d>9){
n++; /*Third display*/
count=0;
d=0;
}
if(n>9){
x++; /*Forth display*/
n=0;
count=0;
d=0;
}
if(x>9){
x=0;
n=0;
count=0;
d=0;
}
state=1;
}
IOWR_ALTERA_AVALON_PIO_DATA(SEG7_BASE,sev[x]*16777216 + sev[n]*65536 + sev[d]*256 + sev[count]); /*Display the number of shots on the SEV-SEG*/
if(state==1)
{
if(data & 0x01){ /*if the LSB LED is on*/
data=data^0x01; /*Invert it "shoot it"*/
state=0;
/*Control the SEV-SEG */
}
else if(!(data & 0x01)){ /*if LSB LED is not on*/
data=data^0x01; /*Invert it , turn it on*/
state=1;
}
}
IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE,data); /*Display the shots taken on the SEV-SEG*/
usleep(sleep);
lights (&sleep,&data); /*function edits ducks, in which they are displayed on the DE0 in the
sense of their direction and speed.*/
if (endofgame(&data)) /*if testend() returns 1 then program will
end normally*/
return(0); /*return 0 to break out of while loop in order to exit
the program*/
}
return 0;
}
/************************************************************************************
FUNCTIONS
*************************************************************************************/
/*************************************************************************************
* Lights Function *
* Function name : void lights (unsigned int *sleep, unsigned int *data) *
* Returns : return value data (Ducks) *
* Description : This funtction reads the Speed Switches to work out what speed for *
* the program to run at. It runs 1 of 4 speeds *
* This function also reads the direction switch to find out what *
* direction to rotate the LEDS in. Either right or left. *
* This function will then write the results and update the LEDS (Data).*
*************************************************************************************/
void lights (unsigned int *sleep, unsigned int *data){
alt_u16 speedswitch;
unsigned int directionswitch;
int delay1=600000; /*USleep 1 Option */
int delay2=400000; /*USleep 2 Option */
int delay3=100000; /*USleep 3 Option */
int delay4=10000; /*USleep 4 Option */
/****************************************************
** SPEED SETTING **
**test the two speed switches and returns a value **
**to go into USLEEP. **
****************************************************/
speedswitch = IORD_ALTERA_AVALON_PIO_DATA(SWITCHES_BASE); /*Reads the Speed Switch */
speedswitch = (~speedswitch & 0xE0);
if(!(speedswitch&0x40)) /*test if SW6 is on*/
*sleep=delay2;
else if(!(speedswitch&0x80)) /*test if SW7 is on*/
*sleep=delay3;
else if(!(speedswitch&0xc0))/*test if SW6 and SW7 is on*/
*sleep=delay4;
else
*sleep=delay1; /* if no switches are on DEFAULT speed*/
/****************************************************
** DIRECTION AND ROTATE **
**test the direction switch and rotate in the **
**corresponding direction **
****************************************************/
directionswitch = IORD_ALTERA_AVALON_PIO_DATA(SWITCHES_BASE); /*READ Direction Switch*/
directionswitch = (~directionswitch & 0x07);
/*Move Lights - in association with the direction switch.Whether to
make the LEDS rotate LEFT or RIGHT*/
if (directionswitch&0x01) /*if SW0 is on "UP"*/
{
if((*data & 0x01)==1){ /*If LSB (LED) is on*/
*data=*data&0x3FE; /*1111111110*/
*data=*data>>1; /*move all the lights to the right*/
*data=*data|0x200; /*keep on MSB LED "1000000000"*/
}
else
*data=*data>>1; /*move all the lights to the right*/
}
else /*if the switch is in the off position*/
{
if((*data & 0x200)==0x200){ /*If MSB (LED) is off*/
*data=*data&0x1FF; /*111111111*/
*data=*data<<1; /*move all the lights to the left*/
= *data=*data|0x01; /*keep on the LSB LED"0000000001"*/
}
else
*data=*data<<1; /* move lights to the left*/
}
IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE,*data); /*dispaly data on LEDs*/
usleep(*sleep);/*Run the USLEEP time*/
}
/*************************************************************************************
TEST END OF GAME FUNCTION *
* Function name : int endofgame(unsigned int *data) *
* Returns : return value data (Ducks) *
* Description : This function will test for the different options in terms of *
* whether or not you have won the game. *
* The funtion will find out how many ducks are lit, by working out *
* the hex equivalent, and checking it against the if statement. *
* I.E Either 0x00 (0) - No ducks lit, or 0xFF (225) *
*************************************************************************************/
int endofgame (unsigned int *data){
if ((*data == 0x3FF)|(*data ==0)) /*test whether all LEDs are on or off*/
{
if (*data == 0) /* if no LEDS (ducks) are on .*/
{
alt_u32 val0=0xA1C0C886; /* The game will stop and display "dONE" on the SEV-SEG.*/
IOWR_ALTERA_AVALON_PIO_DATA(SEG7_BASE,val0);
return 1;
}
else
/* if all LEDS (ducks) are on .*/
alt_u32 val=0xC7C09286; /* The game will stop and display "LOSE" on the SEV-SEG.*/
IOWR_ALTERA_AVALON_PIO_DATA(SEG7_BASE,val);
return 1;
}
}
return 0;
}