Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
If you are using C the basic maths will do... add subtract multiply an divide are already available with standard libraries....
Have you any code you have written???
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;
}
}
}
{
lcd_out(2,16," ");
}
Your last while(1); statement causes a hang!! as there is a ; after the statement... Code will sit at this line forever..
In your input routine you have an unconditional piece of code that overwrites all the other conditions..
C:{ lcd_out(2,16," "); }
As a pointer.. I would have a 16 key keypad.. 0~9 D T B and enter.
I would have a key routine that returns a key if one was pressed.
A second routine that would read the values and correct for (D) double (T) treble or (B) bull..
So entering " D 4 enter " would auto deduct 8 from the total, or print "Too much" if total exceeded keeping the score as is..
// Encoder
#include "msp430g2452.h"
#include "intrinsics.h"
// Function prototypes
void bdelay(int j);
char keyread(void);
void main( void )
{
// P1.3,P1.2,P1.1,P1.0 are inputs (Rows)
// P1.7,P1.6,P1.5,P1.4 are outputs (Columns)
// Interrupt happens when any row input goes high - key is pressed
P1DIR = 0xf0; //Configure P1 - lower nibble is an input
P1SEL=0x00; // no special functions for P1 port lines
P1IE=0x0f; // interrupts on lower nibble of P1
P1IES=0x00; // interrupt on rising edge
P1OUT=0xf0;
// Port 2 is used as an output
P2DIR = 0xff; //Configure P2 as output lines
P2SEL=0x00; // no special functions for P2 port lines
P2IE=0x00; // no interrupts for P2 port lines
P2OUT =0x00; // 0 on un-used lines
// Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD;
__enable_interrupt();
while(1)
{
__low_power_mode_0();
}
}
void bdelay (int i)
{
i=i*500;
while(i!=0)i--;
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT1__ISR (void)
{
char row=0x99,col=0x99,key=0x00;
char numbers[]={0x01,0x04,0x07,0x0f,0x02,0x05,0x08,0x00,0x03,0x06,0x09,0x0e,0x0a,0x0b,0x0c,0x0d}; // Array for assigning of buttons
//debounce for 20ms approx
bdelay(20);
// find row - which one is HIGH?
if((P1IN&0x01)==0x01)row=0x00;
else if((P1IN&0x02)==0x02)row=0x01;
else if((P1IN&0x04)==0x04)row=0x02;
else row=0x03;
// find column - write LOW to each Column, in turn and check if Row(s) go LOW
P1OUT=0x7f; // try Column 3 - P1.7
bdelay(2);
if((P1IN&0x0f)==0x00)col=0x03;
else
{P1OUT=0xbf; // try Column 2- P1.6
bdelay(2);
if((P1IN&0x0f)==0x00)col=0x02;
else
{P1OUT=0xdf;// try Column 1- P1.5
bdelay(2);
if((P1IN&0x0f)==0x00)col=0x01;
else
{P1OUT=0xef;// try Column 0- P1.4
bdelay(2);
if((P1IN&0x0f)==0x00);col=0x00;
}}}// Restore output to normal
P1OUT=0xf0;
//If multiple keys are pressed either row or col is still 0x99
//Ignore key(s), completely, if this is the case
if((row!=0x99)&(col!=0x99))
{
//Form new (4-bit) key code from its column and row position
key=row|(col*4);
// Data available (P2.7) HIGH and output key read (P2.3....P2.0)
P2OUT=(numbers[key])|0x80;
// wait until key(s) released
while((P1IN&0x0f)!=0x00)
{
}
// Data Available low( key not pressed), last valid key value shown
P2OUT=numbers[key];
// end of routine
}
P1IFG=0x00;
}