BASED CALCULATOR - SERIAL COMMUNICATION ! I need you help

Status
Not open for further replies.

kingleys

Newbie level 1
Joined
May 22, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,286
I'm a beginer, I start my project based calculator - serial communication in ebook "Advanced PIC Microcontroller
Projects in C
" -Dogan Ibrahim. But I want to improve result.Ex I take " 12 + 22 =34 Ok I don't know how to calc 1.2 '+' or '-' or '*' or '/' with 2.2. I need your help.And code here :
Code:
#define Enter 13
#define Plus '+'
#define Minus '−'
#define Multiply '∗'
#define Divide '/'
//
// This function sends carriage-return and line-feed to USART
//
void Newline()
{
Usart_Write(0x0D); // Send carriage-return
Usart_Write(0x0A); // Send line-feed
}
//
// This function sends a text to USART
//
void Text_To_Usart(unsigned char ∗m)
{
unsigned char i;
i = 0;
while(m[i] != 0)
{ // Send TEXT to USART
Usart_Write(m[i]);
i++;
}
}
//
// Start of MAIN program
//
void main()
{
unsigned char MyKey, i,j,kbd[5],op[12];
unsigned long Calc, Op1, Op2,Key;
unsigned char msg1[] = " CALCULATOR PROGRAM";
unsigned char msg2[] = " Enter First Number: ";
unsigned char msg3[]= "Enter Second Nummber: ";
unsigned char msg4[] = " Enter Operation: ";
unsigned char msg5[] = " Result = ";
//
// Configure the USART
//
Usart_Init(9600); // Baud=9600
//
// Program loop
//
for(;;) // Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Newline(); // Send newline
Newline(); // Send newline
Text_To_Usart(msg1); // Send TEXT
Newline(); // Send newline
Newline(); // Send newline
//
// Get the first number
//
Text_To_Usart(msg2); // Send TEXT to USART
do // Get first number
{
if(Usart_Data_Ready()) // If a character ready
{
MyKey = Usart_Read(); // Get a character
if(MyKey == Enter)break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey - '0';
Op1 = 10∗Op1 + Key; // First number in Op1
}
}while(1);
Newline();
//
// Get the second character
//
Text_To_Usart(msg3); // Send TEXT to USART
do // Get second number
{
if(Usart_Data_Ready())
{
MyKey = Usart_Read(); // Get a character
if(Mykey == Enter)break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey - '0';
Op2 = 10∗Op2 + Key; // Second number in Op2
}
}while(1);
Newline();
//
// Get the operation
//
Text_To_Usart(msg4);
do
{
if(Usart_Data_Ready())
{
MyKey = Usart_Read(); // Get a character
if(MyKey == Enter)break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey;
}
}while(1);
//
// Perform the operation
//
Newline();
switch(Key) // Calculate
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 ∗ Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string
//
// Remove leading blanks
//
j=0;
for(i=0;i<=11;i++)
{
if(op[i] != ' ') // If a blank
{
kbd[j]=op[i];
j++;
}
}
Text_To_Usart(msg5);
for(i=0; i<j;i++)Usart_Write(kbd[i]); // Display result
}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…