#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HSPLL_HS
#include<p18f4550.h>
#include<delays.h>
#define rs LATAbits.LATA0
#define rw LATAbits.LATA1
#define en LATAbits.LATA2
#define lcdport LATB
#define WIREport PORTAbits.RA3
#define ENDport PORTAbits.RA4
#define BUZZERport LATAbits.LATA5
#define LEDport LATD
void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned int i=0;
void lcd_ini()
{
lcdcmd(0x38); //16x2, 5x7matrix LCD.
lcdcmd(0x01); //CLEAR DISPLAY AND DDRAM CONTENT
lcdcmd(0x06); //ENTRY MODE
lcdcmd(0x0C); //DISPLAY ON, CURSOR OFF
}
void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
Delay10KTCYx( 20 );
en=0;
}
void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay10KTCYx( 20 );
en=0;
}
// THE MAIN FUNCTION
void main(void)
{
unsigned char z,i,j;
unsigned char welcome[]= "Are You Ready !!";
unsigned char attemps[3]= {0x01,0x03,0x07};
ADCON1= 0x0f; // make all pins as digital I/O
CMCON= 0x07; // make all comparators as digital I/O
TRISAbits.TRISA0=0; //config RA0 as output pin
TRISAbits.TRISA1=0; // config RA1 as output pin
TRISAbits.TRISA2=0; // config RA2 as output pin
TRISAbits.TRISA3=1; // config RA3 as input pin
TRISAbits.TRISA4=1; // config RA4 as input pin
TRISAbits.TRISA5=0; // config RA5 as output pin
LATA=0x00;
TRISB=0; // Configure Port B as output port
lcdport=0; // intitialize it with zero
TRISD=0; // Configure Port D as output port
LEDport=0; // intialize it with zero
lcd_ini(); // LCD initialization
// *************
for(z=0; z<16; z++) // to display "Are You Ready !!" message
{
lcddata(welcome[z]);
}
for(z=0; z<18; z++) // some delay
{
Delay10KTCYx( 20 );
}
lcdcmd(0x01); // clear LCD
// **************
// now display 3 .. 2.. 1 .. GO
lcdcmd(0x87); // move cursor to position 7
lcddata('3');
BUZZERport = 1; // buzzer ON
for(i=0; i<8; i++) // some delay
{
Delay10KTCYx( 20 );
}
BUZZERport =0; // buzzer OFF
lcdcmd(0x01); // clear LCD
lcdcmd(0x87);
lcddata('2');
BUZZERport = 1; // buzzer ON
for(i=0; i<8; i++) // some delay
{
Delay10KTCYx( 20 );
}
BUZZERport =0; // buzzer OFF
lcdcmd(0x01); // clear LCD
lcdcmd(0x87);
lcddata('1');
BUZZERport = 1; // buzzer ON
for(i=0; i<8; i++) // some delay
{
Delay10KTCYx( 20 );
}
BUZZERport =0; // buzzer OFF
lcdcmd(0x01); // clear LCD
lcdcmd(0x84); // position 4
lcddata('G');
lcddata('O');
lcddata('!');
lcddata('!');
for(i=0; i<8; i++)
{
Delay10KTCYx( 20 );
}
lcdcmd(0x01);
// ************
// to drive the stepper motor ( the start ring )
for(i=0; i<2; i++)
{
LATD= 0XE0; //0111
Delay10KTCYx( 30 );
LATD= 0XD0; //1011
Delay10KTCYx( 30 );
LATD= 0XB0; //1101
Delay10KTCYx( 30 );
LATD= 0X70; //1110
Delay10KTCYx( 30 );
}
LATD= 0X70; //0111
Delay10KTCYx( 30 );
LATD= 0XB0; //1011
Delay10KTCYx( 30 );
LATD= 0XD0; //1101
Delay10KTCYx( 30 );
LATD= 0XE0; //1110
Delay10KTCYx( 30 );
LATD=0X00;
// ************
// NOW start playing
while(1)
{
if(WIREport==1 && j!=3) // if the loop touch the wire and still the player's Attemps are not finished (not 3)
{
BUZZERport=1; // the buzzzer ON
LEDport=attemps[j]; // glow the the first attemp
j++; // then increase the number of attemps
for(z=0; z<10; z++) // some delay
{
Delay10KTCYx( 20 );
}
}
else
BUZZERport=0; // if the wire not touched then turn OFF the buzzer , but the attemp will stay ON
for(z=0; z<3; z++) // some delay
{
Delay10KTCYx( 20 );
}
if(j==3) // if the attemps reatched 3 ! that's mean the ' Game Over' message should appear
{
BUZZERport = 1; // the buzzer ON
for(z=0; z<10; z++) // some delay
{
Delay10KTCYx( 20 );
}
BUZZERport =0; // the buzzer OFF
while(1) // this to show (Game Over) message forever
{
lcdcmd(0x01); // clear LCD
lcdcmd(0x83);
lcddata('G');
lcddata('a');
lcddata('m');
lcddata('e');
lcdcmd(0x88);
lcddata('O');
lcddata('v');
lcddata('e');
lcddata('r');
for(i=0; i<20; i++) // some delay
{
Delay10KTCYx( 20 );
}
}
}
if(ENDport==1) // here if the attemps are not 3, ant the player have reached the END, one of these message will appear
{
if(j==0) // first: if no one of the attemps' LEDs is glowing :)
{
while(1) // then show "Awesome !" message forever
{
lcdcmd(0x01); // clear LCD
lcdcmd(0x84); // first line ,4th position
lcddata('A');
lcddata('W');
lcddata('E');
lcddata('S');
lcddata('O');
lcddata('M');
lcddata('E');
lcddata('!');
for(i=0; i<20; i++) // some delay
{
Delay10KTCYx( 20 );
}
}
}
else if(j==1) // second: if one of the attmps' LEDs is glowing
{
while(1) // then show "GREATE :)" message forever
{
lcdcmd(0x01); // clear LCD
lcdcmd(0x84); // first line and 4th position
lcddata('G');
lcddata('R');
lcddata('E');
lcddata('A');
lcddata('T');
lcdcmd(0x8A);
lcddata(':');
lcddata(')');
for(i=0; i<20; i++) // some delay
{
Delay10KTCYx( 20 );
}
}
}
else if(j==2) // if two of the attemps' LEDs are glowing
{
while(1) // then show "GOOD" message forever
{
lcdcmd(0x01); // clear LCD
lcdcmd(0x84); // firt line and 4th position
lcddata('G');
lcddata('o');
lcddata('o');
lcddata('d');
lcdcmd(0x89);
lcddata('^');
lcddata('^');
for(i=0; i<20; i++) // some delay
{
Delay10KTCYx( 20 );
}
}
}
}
}
}