AVR 4X3键盘编程

时间:2017-03-21 14:47:42

标签: avr keypad

以下是与ATmega连接的4X3键盘的avr代码。我最后只能在显示屏上看到“#”。我无法弄清楚我在哪里犯了错误,非常感谢任何帮助。

#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz clock speed
#endif

#define D0 eS_PORTB0
#define D1 eS_PORTB1
#define D2 eS_PORTB2
#define D3 eS_PORTB3
#define D4 eS_PORTB4
#define D5 eS_PORTB5
#define D6 eS_PORTB6
#define D7 eS_PORTB7
#define RS eS_PORTD5
#define EN eS_PORTD7
#define RW eS_PORTD6
// #define KEYPAD PORTC  //KEYPAD IS ATTACHED ON PORTC

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"
uint8_t key,keypressed;


   void main()
   {
       DDRB = 0xFF;
       DDRD = 0xFF;
       int i;
       Lcd8_Init();
       DDRC|= 0XF0;
       PORTC|= 0X0F;
       _delay_ms(5000);
       while(1)
        {
            for(i=0;i<10;i++){

            if (PINC!=0b11111111)//in any of column pins goes high execute the loop
            {

            if (PINC==0b00010001)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("1");//if row1 and column1 is high show “1”

            }
            if (PINC==0b00010010)
            {

                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("4");// if row1 and column2 is high show “4”

            }
            if (PINC==0b00010100)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("7");// if row1 and column3 is high show “7”

            }
            if (PINC==0b00011000)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("*");//if row1 and column4 is high show “*”


            }

            if (PINC==0b00100001)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("2");// if row2 and column1 is high show “2”

            }
            if (PINC==0b00100010)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("5");// if row2 and column2 is high show “5”

            }
            if (PINC==0b00100100)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("8");// if row2 and column3 is high show “8”

            }
            if (PINC==0b00101000)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("0");// if row2 and column4 is high show “0”

            }

            if (PINC==0b01000001)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("3");

            }
            if (PINC==0b01000010)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("6");

            }
            if (PINC==0b01000100)
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("9");

            }
            if  (PINC==0b01001000);
            {
                Lcd8_Set_Cursor(0,0);
                Lcd8_Write_String("#");


            }
        }


        else PINC == 0b11111111;
        return 0;
        }

    }

}

所做的更改和我认为的代码有点可以

#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz clock speed
#endif

#define D0 eS_PORTB0
#define D1 eS_PORTB1
#define D2 eS_PORTB2
#define D3 eS_PORTB3
#define D4 eS_PORTB4
#define D5 eS_PORTB5
#define D6 eS_PORTB6
#define D7 eS_PORTB7
#define RS eS_PORTD5
#define EN eS_PORTD7
#define RW eS_PORTD6
// #define KEYPAD PORTC  //KEYPAD IS ATTACHED ON PORTC

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"

void col_init(void)
{
    DDRD = 0X0F;
    PORTD = 0X70;
    _delay_ms(5);
}

void row_init(void)
{
    DDRD = 0X78;
    PORTD = 0X87;
    _delay_ms(5);
}

unsigned char read_key(void)
{
    unsigned char value;
    col_init();
    value = 0;

    if(!(PINC & 0X10))
    {
        value = 0X01;
    }
    else if(!(PINC & 0X20))
    {
        value = 0X02;
    }
    else if(!(PINC & 0X40))
    {
        value = 0X03;
    }

    row_init();

    if(!(PINC & 0X01))
    {
        value += 0X00;
    }
    else if(!(PINC & 0X02))
    {
        value += 0X03;
    }
    else if(!(PINC & 0X04))
    {
        value += 0X06;
    }
    else if(!(PINC & 0X08))
    {
        value += 0X00;
    }

    _delay_ms(50);
    return value;

}

int main (void)
{
    unsigned char keypressed;
    DDRB = 0xFF;
    DDRD = 0xFF;
    Lcd8_Init();
    keypressed = 0X00;
    col_init;
    while(1)
    {
        if(!(PINC == 0X70))
        {
            keypressed = read_key();
        }
            Lcd8_Set_Cursor(1,1);
            Lcd8_Write_String(keypressed);
    }
}

1 个答案:

答案 0 :(得分:1)

您应该发布原理图。这里很难帮到你。 我注意到了:

1)
DDRC | = 0XF0;
PORTC | = 0X0F;

您只在端口C上设置4个引脚作为输出。从你的if()条件判断并且你有一个4x3键盘(7个引脚),这可能是错误的。

2)for循环应该做什么?

3)我觉得你搞砸了你的牙套:

    }
    else PINC == 0b11111111;
    return 0;
    }

可以这样写:

    } else { 
        PINC == 0b11111111;
    }
    return 0;
    }

这可能不是你想要的。

建议:
将代码简化为最小的示例。尝试检测一个按钮。重新考虑你的代码。