简体   繁体   English

创建一个函数并在主程序中调用它

[英]Creating a function and calling it in main program

I have a case in AVR programming 我有一个AVR编程案例

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 . 假设,在您的情况下,每当您从avr按下按键时,您都会获得键码值,例如,当您按下1时,您将获得地址1;当您按下2时,您将获得地址2;当您按下3时,您将获得地址3;依此类推。按9您将获得地址9。按时。 you will get address 10. but in c you did not get input like that.used scanf to read from stdin. 您将获得地址10。但是在c中,您没有得到诸如that.usd的输入,以便从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 . 如果按3,2,1和。 then a=321 ,b=1 ,i=3 然后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. 对于avr,您需要进行一些必要的更改,包括案例地址和下一个按键声明以及其他一些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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