unsigned long initialDartScore = 501, dartScore = 0;
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
void init(void) ()
{
trisb=0b00000000;
portb=0b00000000;
Keypad_Init; //Initialize Keypad on portc
Lcd_Init; // Initialize LCD on PORTB
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 1, "501");
Lcd_Out(2, 1, "Enter Score:");
return;
}
char newValue; //Define newValue
unsigned short kp=0;
int numb=0;
int enter=0;
int input(void)
{
numb=0;
do
{
kp = Keypad_Key_Click();
if (kp==1) //if keypad button one is pressed then display 1
{ // on LCD.
lcd_out(2,16,"1");
numb=1; // data transfer to numb int
}
if (kp==2)
{
lcd_out(2,16,"2");
numb=2;
}
if (kp==3)
{
lcd_out(2,16,"3");
numb=3;
}
if (kp==5)
{
lcd_out(2,16,"4");
numb=4;
}
if (kp==6)
{
lcd_out(2,16,"5");
numb=5;
}
if (kp==7)
{
lcd_out(2,16,"6");
numb=6;
}
if (kp==9)
{
lcd_out(2,16,"7");
numb=7;
}
if (kp==10)
{
lcd_out(2,16,"8");
numb=8;
}
if (kp==11)
{
lcd_out(2,16,"9");
numb=9;
}
if (kp==14)
{
lcd_out(2,16,"0");
numb=0;
}
{
lcd_out(2,16," ");
}
if (kp==15) // # key ENTER KEY
{
enter=1;
}
}
while (enter==0);
enter=0;
return;
}
char HUNDREDS[4];
char TENS[4];
char UNITS[4];
char RESULT[4];
char keypadPort at PORTD; //create char array to store keypad data
int x=0; //create int to store data
int y=0; //create int to store data
int a=0; //create int to store data
int b=0; //create int to store data
void main(void)
{
init(); //goto initialize function to set up ports, etc
while(1)
{
////////////////////////////////////////////////////////////////////////////////
/////////////////////////Hundreds I/P///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
input(); // jump to keypad input function ( save address in the stack)
x=numb; // return from function (after getting address from the stack)
if(x>1) // value limiter to prevent angles over 180 degs
{
x=1;
}
IntToStr(x, HUNDREDS); //int to string convertor for LCD
Lcd_out(2, 11, HUNDREDS); // HUNDREDS O/P to LCD
/////////////////////////Tens I/P///////////////////////////////////////////////
input(); // jump to keypad input function ( save address in the stack)
y=numb; // return from function (after getting address from the stack)
IntToStr(y, TENS); //int to string convertor for LCD
Lcd_out(2, 12, TENS); // TENS O/P to LCD
////////////////////////////////////////////////////////////////////////////////
/////////////////////////Units I/P//////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
input();
a=numb;
IntToStr(a, UNITS); //int to string convertor for LCD
Lcd_out(2, 13, UNITS);
///////////////////////////Maths to add up HUNDS, TENS and UNITS////////////////
x=x*100;
y=y*10;
b=x+y+a;
if(b>180) // value limiter to prevent scores over 180 points
{
b=180;
}
IntToStr(b, RESULT); //int to string convertor for LCD
Lcd_out(2, 11, RESULT); //points O/P to LCD
while(1);
{
//dartScore gets value from key press (0...9)
//after new value is entered for dartScore a flag named newValue is set
if(newValue) {
if(dartScore >= initialDartScore) dartScore -= initialDartScore;
//Convert dartScore to string using IntToStr() and display on LCD
//Clear newValue flag
newValue = 0;
}
}
}