简体   繁体   中英

Creating a function and calling it in main program

I have a case in AVR programming

case (0xe7): keyPressed=".";

during this I want to call a pre-defined function

switch (keyCode)               //generating key characetr to display on LCD
{

case (0xee): keyPressed="1";
            b=1;
            a=a*10+b; 
            i=i++;
            break;
case (0xed): keyPressed="4";
            b=4;
            a=a*10+b; 
            i=i++;
            break;
case (0xeb): keyPressed="7";
            b=7;
            a=a*10+b; 
            i=i++;
            break;
case (0xde): keyPressed="2";
            b=2;
            a=a*10+b; 
            i=i++;
            break;
case (0xdd): keyPressed="5";
            b=5;
            a=a*10+b; 
            i=i++;
            break;
case (0xdb): keyPressed="8";
            b=8;
            a=a*10+b; 
            i=i++;
            break;
case (0xd7): keyPressed="0";
            b=0;
            a=a*10+b; 
            i=i++;
            break;
case (0xbe): keyPressed="3";
            b=3;
            a=a*10+b; 
            i=i++;
            break;
case (0xbd): keyPressed="6";
            b=6;
            a=a*10+b; 
            i=i++;
            break;
case (0xbb): keyPressed="9";
            b=9;
            a=a*10+b; 
            i=i++;
            break;
}

How do I make the function?? and call it during my case in the main program? Please guide me I am new to all this... Help me out please..

Assuming, In your case When ever you press key from avr you get Keycode value for example when you press 1 you get address 1 ,when you press 2 you will get address 2,when you press 3 you will get address 3 and so on when you press 9 you will get address 9.and when you press . you will get address 10. but in c you did not get input like that.used scanf to read from stdin. and repeated loop untill pressing '.'

each keypress you ae holding keypressed and adding that key to the sequenceof keyspressed and no of times keyspressed.

b=whichkeypressed;
a=a*10+b;
i++;

if you press 3,2,1 , and . then a=321 ,b=1 ,i=3

After pressing '.' you are calling predifined function there you are calculating

c=pow(10,i); 
i=3 => c=1000
and d=a/c;
a=321 ,c=1000
d=0.321 

The code which performs operation as above is :

#include<stdio.h>
#include<math.h>
int a,b,i;
double  c,d;


void function()
{
c= pow(10,i);
d=a/c;
}


main()
{

int keyCode;
char keyPressed;

while(1)
{
printf("Enter Keycode: ");
scanf("%d",&keyCode);

switch (keyCode)               //generating key characetr to display on LCD
{

case 1: keyPressed='1';
            b=1;
            a=a*10+b;
            i++;
            break;
case 4: keyPressed='4';
            b=4;
            a=a*10+b;
            i++;
            break;
case 7: keyPressed='7';
            b=7;
            a=a*10+b;
            i++;
            break;
case 2: keyPressed='2';
            b=2;
            a=a*10+b;
            i++;
            break;
case 5: keyPressed='5';
            b=5;
            a=a*10+b;
            i++;
            break;
case 8: keyPressed='8';
            b=8;
            a=a*10+b;
            i++;
            break;
case 0: keyPressed='0';
            b=0;
            a=a*10+b;
            i++;
            break;
case 3: keyPressed='3';
            b=3;
            a=a*10+b;
            i++;
            break;
case 6: keyPressed='6';
            b=6;
            a=a*10+b;
            i++;
            break;
case 9: keyPressed='9';
            b=9;
            a=a*10+b;
            i++;
            break;

case 10: keyPressed='.';
             printf("No of Times Keys Pressed are = %d\n",i);

             function();
                printf("sequence of keys pressed=%d \nlast key pressed=%d\npower value of Keys=%lf\n The decimal valueof given sequence of keys=%6.10lf\n",a,b,c,d);   //use c and d with the values what you required.

                                break;

        }

        if (keyPressed=='.')
        break;

        }
getchar();
}

OUTPUT:

Enter Keycode: 1
Enter Keycode: 2
Enter Keycode: 3
Enter Keycode: 4
Enter Keycode: 5
Enter Keycode: 6
Enter Keycode: 7
Enter Keycode: 8
Enter Keycode: 9
Enter Keycode: 10
No of Times Keys Pressed are = 9
sequence of keys pressed=123456789
last key pressed=9
power value of Keys=1000000000.000000
The decimal valueof given sequence of keys=0.1234567890

For avr you need to make some required changes,at case addresses and next keypressed statement and some additional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM