hi,
could some one help me to make a calculator connected to a DSP? This is for my real time systems project.

the software used to complile is Code Composer Studio. DSP used in TI TMS320C6711.

keypad is 4x4 Matrix X,Y type.

I could not understand how to make the calculator do basic operations such as add subtract etc.

this is the codes written to recognise the key pressed using timer interrupt. but i could not understand how to incorporate function of doing the basic operations of adding subtractin etc.. could someone help me with adding?? i can figure the other operations from there.

#include <stdio.h>
#include "timer_lib.h"  
#include "keypad_lib.h"  


char read_keypad_rowj();      
char read_keypad_rowk();  /* note the students will have to create row2-row4 read routines */
char read_keypad_rowl();      
char read_keypad_rowm();


char read_keypad_rowj()
{
 int y;        /* define local variable */
 char temp;
 unsigned int *InputPort0 = (unsigned int *) 0xA0000000;    /* First Input Port Address */  
 unsigned int *OutputPort1 = (unsigned int *) 0xA0000008;   /* First Output Port Address   */ 

 temp='x';         /* default value for when the key has not been recognised by this function */
 *OutputPort1 = 0x00;      /* clear keypad inputs */
 *OutputPort1= 0x01;      /* energise only row 1 of the keypad matrix */
 y=*InputPort0;           /* read the output of the keypad matrix */ 
 y&=0xF0;           /* mask to reset irrelevant bits to 0 */

 if (y != 0)        /* if one of the lines has been set, check which one it is */
   {
   if (y == 0x10)
     temp = '1';   /* function return value for the key "1" */ 
   if (y == 0x20)
     temp = '2';   /* function return value for the key "2" */ 
   if (y == 0x40)
     temp = '3';    /* function return value for the key "3" */ 
   if (y == 0x80)
     temp = 'F';    /* function return value for the key "F" */ 
   //printf("%c\n",temp);   
   }   

 return(temp);
}


char read_keypad_rowk()
{
 int y;        /* define local variable */
 char temp;
 unsigned int *InputPort0 = (unsigned int *) 0xA0000000;    /* First Input Port Address */  
 unsigned int *OutputPort1 = (unsigned int *) 0xA0000008;   /* First Output Port Address   */ 

 temp='x';         /* default value for when the key has not been recognised by this function */
 *OutputPort1 = 0x00;      /* clear keypad inputs */
 *OutputPort1= 0x02;      /* energise only row 2 of the keypad matrix */
 y=*InputPort0;           /* read the output of the keypad matrix */ 
 y&=0xF0;           /* mask to reset irrelevant bits to 0 */

 if (y != 0)        /* if one of the lines has been set, check which one it is */
   {
   if (y == 0x10)
     temp = '4';   /* function return value for the key "4" */ 
   if (y == 0x20)
     temp = '5';   /* function return value for the key "5" */  
   if (y == 0x40)
     temp = '6'; /* function return value for the key "6" */ 
   if (y == 0x80)
     temp = 'E';/* function return value for the key "E" */ 
   //printf("%c\n",temp);   
   }   

 return(temp);
}


char read_keypad_rowl()
{
 int y;        /* define local variable */
 char temp;
 unsigned int *InputPort0 = (unsigned int *) 0xA0000000;    /* First Input Port Address */  
 unsigned int *OutputPort1 = (unsigned int *) 0xA0000008;   /* First Output Port Address   */ 

 temp='x';         /* default value for when the key has not been recognised by this function */
 *OutputPort1 = 0x00;      /* clear keypad inputs */
 *OutputPort1= 0x04;      /* energise only row 3 of the keypad matrix */
 y=*InputPort0;           /* read the output of the keypad matrix */ 
 y&=0xF0;           /* mask to reset irrelevant bits to 0 */

 if (y != 0)        /* if one of the lines has been set, check which one it is */
   {
   if (y == 0x10)
     temp = '7';   /* function return value for the key "7" */ 
   if (y == 0x20)
     temp = '8';   /* function return value for the key "8" */ 
   if (y == 0x40)
     temp = '9';    /* function return value for the key "9" */ 
   if (y == 0x80)
     temp = 'D';    /* function return value for the key "D" */ 
   //printf("%c\n",temp);   
   }   

 return(temp);
}


char read_keypad_rowm()
{
 int y;        /* define local variable */
 char temp;
 unsigned int *InputPort0 = (unsigned int *) 0xA0000000;    /* First Input Port Address */  
 unsigned int *OutputPort1 = (unsigned int *) 0xA0000008;   /* First Output Port Address   */ 

 temp='x';         /* default value for when the key has not been recognised by this function */
 *OutputPort1 = 0x00;      /* clear keypad inputs */
 *OutputPort1= 0x08;      /* energise only row 4 of the keypad matrix */
 y=*InputPort0;           /* read the output of the keypad matrix */ 
 y&=0xF0;           /* mask to reset irrelevant bits to 0 */

 if (y != 0)        /* if one of the lines has been set, check which one it is */
   {
   if (y == 0x10)
     temp = 'A';   /* function return value for the key "A" */ 
   if (y == 0x20)
     temp = '0';   /* function return value for the key "0" */ 
   if (y == 0x40)
     temp = 'B';    /* function return value for the key "B" */ 
   if (y == 0x80)
     temp = 'C';    /* function return value for the key "C" */ 
   //printf("%c\n",temp);   
   }   

 return(temp);
}

Recommended Answers

All 5 Replies

I guess you use 0 to 9 for 0 to 9, then say A for Addition, B for suBtraction etc.

Do you also have somewhere to display the answer?

we have the display too.. i just dont know how we make the thing "add" when we press the A button. how do we program it to add when we press the A button?? thats what i was hopin to find out.

Well you read in '1' and '2' and say that's 12
Then press the 'A' key, and note that you want to perform an addition
Then read '3' and '4' and get 34
Then press say 'E' to get an equals, perform the addition of 12 and 34, display the answer.

you have an interrupt service routine of some sort.

when you get an interrupt, process the row that had the keypress event by calling the row function according to the particular interrupt. or call all four row functions in sequence if the interrupt is just generic to the keyboard and not row-specific. the return value of the row function will be the character pressed, if one was pressed, or will be an 'x' if no character was pressed on that row.

otherwise you would expect the first character pressed to be a number -- keep a running total of the first value to be used by storing the value of this number.

recall: if a nine is pressed, the value is 9. but if a three is pressed immediately afterward, the value of the previous nine becomes 90, and the three is 3, and combine for the value of 93 ...

if (when) the character input represents a math operation (addition, subtraction, etc.), hold the final total value of the previous number(s) pressed, and store the "fact" that you will be performing the requested math operation after the next value is finalized.

repeat the process for the next set of numerics, to get the second value.

once "E" is returned from the keyboard interrupt, process the final result in this manner

if (operation == 'A')
    result = value1 + value2;
else if (operation == 'B')
    result = value 1 - values2;
else if (operation == 'C')
    result = value 1 * value2;

etcetera.


Once you get this working, you will have to figure out how to handle multiple and/or unexpected keypresses. What is the range of allowable inputs? How will you handle negative numbers? There can be more than one way to do it and be correct.

Take for instance, if the user presses "9" "A" "E"...

Is that an error, because they didnt enter a second value? Or will it be nine plus an implied zero equals nine?

How about the keypress sequence of "A" "C" "E"? Is that an error? Or is it merely an implied zero plus implied zero multiplied by implied zero.

How about just "E" ? what is the result of that? What if the user attempts a divide by zero?

What do you do in the result of an error, however defined? ignore it? warn? restart? exit?

Stuff to think about. I suspect you have a fair amount of coding to get busy with now.

hey thanks u guys.. i can see i got a hard task,, haha..!! thanks a lot.. ive got an idea now.. will build on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.