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

时间:2013-08-29 12:19:07

标签: c function calculator avr atmega

我在AVR编程中有一个案例

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

在此期间我想调用预定义函数

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;
}

我该如何制作这个功能?在主程序中我的情况下调用它? 请指导我,我对这一切都是新手...... 请帮帮我..

1 个答案:

答案 0 :(得分:0)

假设,在你的情况下当你从avr按键时你得到Keycode值,例如当你按1得到地址1,当你按2你会得到地址2,当你按3你会得到地址3和所以当你按9时,你会得到地址9.当你按下时。你会得到地址10。 但在c中你没有得到像那样的输入。使用scanf从stdin读取。并重复循环,直到按'。'

每个按键都按住按键并将该按键添加到按键序列中,而不按任何按键次数。

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

如果按 3,2,1 ,和。然后 a = 321,b = 1,i = 3

按'后'。 你正在计算你正在计算的预测函数

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

执行上述操作的代码是:

#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();
}

输出:

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

对于avr,您需要进行一些必要的更改,包括案例地址和下一个keypressed语句以及一些其他更改。