1

我有一个 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;
}

我如何制作功能?并在我的情况下在主程序中调用它?请指导我我对这一切都很陌生...请帮帮我..

4

1 回答 1

0

假设,在你的情况下,当你从 avr 按下键时,你会得到 Keycode 值,例如,当你按下 1 时,你会得到地址 1,当你按下 2 时,你会得到地址 2,当你按下 3 时,你会得到地址 3,依此类推你按 9 你会得到地址 9.and 当你按 。你会得到地址 10。但是在 c 中你没有得到这样的输入。使用 scanf 从标准输入读取。并重复循环,直到按下“。”

每次按键您都按住按键并将该键添加到按键序列中,并且没有按键次数。

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,您需要在案例地址和下一个按键语句以及一些额外的地方进行一些必要的更改。

于 2013-08-29T12:48:04.787 回答