将4x4键盘矩阵与Atmega32连接

时间:2015-02-10 12:41:54

标签: c atmega keypad

我正在尝试将Atmega32微控制器与16x2 LCD和4x4键盘矩阵连接。我正在用Proteus模拟并使用WinAVR编译器。

LCD部件没问题(我已对其进行了全面测试)。但是,键盘代码没有像我预期的那样运行。 每当我按下某个键时,键盘行的扫描将无限期地停止

此外,液晶显示屏上不显示该键。请帮我找到错误。

以下是Proteus中绘制的电路原理图以及代码。我没有在这里包含LCD代码,因为我知道这部分工作得很好。

电路:

enter image description here

代码:

#include <avr/io.h>
#include<util/delay.h>



//Keypad Information
#define R0 0
#define R1 1
#define R2 2
#define R3 3
#define C0 4
#define C1 5
#define C2 6
#define C3 7

#define keypadPORT PORTA
#define keypadPIN PINA
#define keypadDDR DDRA


//Keypad functions and global variables
char getkey();
int keypadRow[] = {R0, R1, R2, R3}; //rows of the keypad
int keypadCol[] = {C0, C1, C2, C3};//columnd



int main()
{
   char key_pressed;

   keypadDDR |= (1<<R0)|(1<<R1)|(1<<R2)|(1<<R3);//set upper part of keypad port as output
                                                //this will be required for scanning the rows
   keypadDDR &= ~((1<<C0)|(1<<C1)|(1<<C2)|(1<<C3));//set lower part of keypad port as input.This is
                                                   //the part of the keypad port where the rows are connected.

   LCD_init(); //initialize LCD

   while(1)
   {
      key_pressed = getkey();
      switch(key_pressed)
      {
        case('A'):
            break;//do nothing if no key is pressed
        default:
            send_char(key_pressed);//send the key pressed to LCD
      }
   }     
   return 0;
}



char getkey()
{  
   int i, j;
   for(i = 0; i < 4; i++)
   {
    keypadPORT = 0x00;
    keypadPORT |= (1 << keypadRow[i]);//send a high to a particular row of the keypad

    for(j = 0; j < 4; j++)
    {
        if(bit_is_set(keypadPIN,keypadCol[j]))//check if key is pressed
        {
            while(bit_is_set(keypadPIN,keypadCol[j]));  //wait for key to be released
            switch(i)
            {
                case(0):
                {
                    if (j == 0) return '7';
                    else if (j == 1) return '8';
                    else if (j == 2) return '9';
                    else if (j == 3) return '/';
                    break;
                }
                case(1):
                {
                    if (j == 0) return '4';
                    else if (j == 1) return '5';
                    else if (j == 2) return '6';
                    else if (j == 3) return '*';
                       break;
                }
                case(2):
                {
                    if (j == 0) return '1';
                    else if (j == 1) return '2';
                    else if (j == 2) return '3';
                    else if (j == 3) return '-';
                       break;
                }
                case(3):
                {
                    if (j == 0) return '?';
                    else if (j == 1) return '0';
                    else if (j == 2) return '=';
                    else if (j == 3) return '+';
                       break;
                }
            }
        }
    }
   }
    return 'A';//Return 'A' if no key is pressed.
}

1 个答案:

答案 0 :(得分:0)

如果,只要按下某个键,&#34;键盘行的扫描将无限期停止&#34;并且&#34;键没有显示在LCD&#34;上,然后我会查看任何潜在的无限循环,例如以下行:

while(bit_is_set(keypadPIN,keypadCol[j]));

我不知道当你释放钥匙时柱子会消失到地面还是保持高位但是你应该能够通过用以下代码替换那条线来检查这种行为:

send_char('<');
while(bit_is_set(keypadPIN,keypadCol[j]));
send_char('>');

如果您确实陷入无限循环,显示屏应显示<