Embedded 将16*2键盘和LCD与atmega32接口

Embedded 将16*2键盘和LCD与atmega32接口,embedded,avr,lcd,keypad,atmega16,Embedded,Avr,Lcd,Keypad,Atmega16,我对键盘模块有一个令人失望的问题,当按下它的任何一个键时,它通常会显示LCD模块上按下的键。问题是每当我按下该键时,行停止被扫描,并且如果我的应用程序(例如,系统)接收密码并显示在LCD上,我无法按下任何其他键在LCD上显示。如果我想在LCD上显示列表,并且想在屏幕上翻开另一页继续显示列表,我现在面临的另一个问题是。我怎样才能实现这一点?! 我附加了一个屏幕截图为我的原理图,加上我提供了键盘和液晶显示器都要检查的代码。谢谢你帮我。 我的申请代码: #define F_CPU 8000000UL

我对键盘模块有一个令人失望的问题,当按下它的任何一个键时,它通常会显示LCD模块上按下的键。问题是每当我按下该键时,行停止被扫描,并且如果我的应用程序(例如,系统)接收密码并显示在LCD上,我无法按下任何其他键在LCD上显示。如果我想在LCD上显示列表,并且想在屏幕上翻开另一页继续显示列表,我现在面临的另一个问题是。我怎样才能实现这一点?! 我附加了一个屏幕截图为我的原理图,加上我提供了键盘和液晶显示器都要检查的代码。谢谢你帮我。

我的申请代码:

#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>
#include "LCD.h"
#include "Keypad.h"

int main(void)
{
    /* Replace with your application code */
    uint8_t keypadPress = 0;
    Keypad_vInit();
    LCD_vInit();

    while( !Keypad_u8Scan() )
    {
        keypadPress = Keypad_u8Scan();
        if( keypadPress == '8' )
        {
            LCD_vPrintChar( '8' );
            while( keypadPress == '8' );
        }
    }
}
我的LCD库:

#if defined MODE_4

static void sendFallingEdge( void ); /* This prototype is declared static       to avoid modifying it. */

static void sendFallingEdge( void )
{
    /* Initializing the EN pin of the LCD when detecting a falling edge. */
    /* The following code is the representation of falling edge using the   system clock. */
    PORTB |= ( 1 << EN );
    _delay_ms( 1 );
    PORTB &= ( ~ ( 1 << EN ) );
    _delay_ms( 1 );
}

void LCD_vSendCmd( char cmd )
{
    /* Transferring the first nibble. */
    PORTA &= 0x0F;
    PORTA |= ( cmd & 0xF0 );
    CLR_BIT( PORTB, RS ); /* Transferring instructions data. */
    sendFallingEdge( );

    /* Transferring the second nibble. */
    PORTA &= 0x0F;
    PORTA |= ( cmd << 4 );
    CLR_BIT( PORTB, RS ); /* Transferring instructions data. */
    sendFallingEdge( );
}

void LCD_vInit( void )
{
    DDRA |= 0xF0; /* DDRA |= 0b11110000; */
    DDRB |= 0x0E; /* DDRB |= 0b00001110; */ /* Those three HIGH bits are the RS, RW and EN respectively. */
    CLR_BIT( PORTB, RW ); /* Write mode enabled according to the LCD's datasheet. */

    LCD_vSendCmd( 0x33 );
    _delay_ms( 1 );

    LCD_vSendCmd( 0x32 );
    _delay_ms( 1 );

    LCD_vSendCmd( 0x28 );
    _delay_ms( 1 );

    LCD_vSendCmd( 0x01 );
    _delay_ms( 1 );

    LCD_vSendCmd( 0x0F );
    _delay_ms( 1 );
}

void LCD_vPrintChar( char data )
{
    PORTA &= 0x0F;
    PORTA |= ( data & 0xF0 );
    SET_BIT( PORTB, RS ); /* Transferring display data. */
    sendFallingEdge( );

    PORTA &= 0x0F;
    PORTA |= ( data << 4 ); 
    SET_BIT( PORTB, RS ); /* Transferring display data. */
    sendFallingEdge( );
}

void LCD_vPrintString( char * str )
{
    uint8_t counter;
    for( counter = 0; str[ counter ] != '\0'; counter ++ )
    {
        LCD_vPrintChar( str[ counter ] );
    }
}

void LCD_vPrintNumbers( uint8_t str[ ], uint8_t size )
{
    uint8_t counter;
    for( counter = 0; str[ counter ] < size; counter ++ )
    {
        LCD_vPrintChar( str[ counter ] );
    }
}

void LCD_vClrScreen( void )
{
    LCD_vSendCmd( CLR_SCRN );
}

void LCD_vMoveCursor( char row, char column )
{
    char cmd;
    if( row == 1 )
    {
        cmd = STARTROW0 + column - 1;
        LCD_vSendCmd( cmd );
    }
    else if( row == 2 )
    {
        cmd = STARTROW1 + column - 1;
        LCD_vSendCmd( cmd );
    }
}

#endif
我的键盘库:

#include <avr/io.h>
#include "std_macros.h"

void Keypad_vInit( void )
{
    DDRC = 0x0F; 
    CLR_BIT( SFIOR, PUD ); 
    PORTC = 0xFF;          
}
unsigned char Keypad_u8Scan( void )
{
    unsigned char row, column, scan, buttonPressed = 0;
    unsigned char KP[ 4 ][ 4 ] = { { '7', '8', '9', '/' },
                                   { '4', '5', '6', '*' },
                                   { '1', '2', '3', '-' },
                                   { ' ', '0', '=', '+' }           
                                 };
    for( row = 0; row < 4; row ++ )
    {
        PORTC |= 0x0F;
        CLR_BIT( PORTC, row );
        for( column = 4; column < 8; column ++ )
        {
            scan = READ_BIT( PINC, column );
            if( scan == 0 )
            {
                buttonPressed = KP[ row ][ column - 4 ];
            }
        }
    }
    return buttonPressed;
}
最后,我的标准宏:

#define SET_BIT( REGISTER, BIT_NUM ) ( REGISTER = REGISTER | ( 1 << BIT_NUM ) )
#define CLR_BIT( REGISTER, BIT_NUM ) ( REGISTER = REGISTER & ( ~( 1 << BIT_NUM ) ) )

当前,while循环在未按下键时循环:

while( !Keypad_u8Scan() )
它应该永远循环

后面的一行:

keypadPress = Keypad_u8Scan();
正在获得按键,并且一旦while循环被修复,将接收到多个按键,因此可以处理页面按钮并显示不同的页面。

您的while循环将尽快终止!键盘扫描为false,然后main终止。当按下除8以外的任何键时,都会发生这种情况,因为只有当键为8时,才等待释放键

在任何大循环调度的嵌入式系统中,主循环通常不应该终止-外循环应该是不确定的

假设键盘和LCD功能正常工作,且更易于扩展,则以下调度循环将正常工作-添加其他按键关闭事件处理程序只需向交换机添加新的case块即可:

一个更好的结构可能是使用一个单独的函数来等待按键关闭事件,如下所示:

uint8_t getKey()
{
    uint8_t key = 0 ;

    // Wait for key release if pressed on entry
    while( Keypad_u8Scan() != 0 )
    {
        // do nothing          
    } 

    // Wait for new key press
    do
    {
        key = Keypad_u8Scan();

    } while( key == 0 ) ;

    return key ;
}
这样,您的主循环就会变得简单得多:

for(;;) // forever
{
    keypadPress = getKey() ;

    // Process key
    switch( keypadPress )
    {
        case '8' :
        {
            LCD_vPrintChar( '8' );
        }
        break ;

        default :
        {
            // any other key
        }
    }
}

你可以简化这个问题——文本不够清晰。我建议您将其结构为1您需要发生的事情2实际发生的事情,并将其限制为外部可观察到的行为-例如,如果按8,会发生什么?如果按1会发生什么?如果您知道LCD和键盘功能可以工作,您可能可以省略所有代码,只显示主屏幕。包括原理图可能会导致一些人认为这是一个硬件问题,因此离题或过于宽泛,可能会投票结束-这不是真的必要。@Clifford我同意你的观点,但有时当你无助时,你会做任何事情来寻求帮助。很抱歉让您头痛,感谢您的帮助和建议我理解当你不知道问题在哪里时,提供一切都不是不合理的。我没有真正的问题;我更担心的是,一个原本合理的问题在得到解决的时候会被混淆,从而吸引到反对票。现在修复它还不算太晚,问题和答案都可以编辑。但是,你应该避免从根本上改变一个问题,如果以前的答案是正确的,那么它会使现有的答案变得毫无意义。@Clifford我感谢你的帮助,正如我所说的,我感谢你和你的建议,祝你过得愉快:你很好,你的解决方案对我帮助很大。谢谢你我非常感激:
for(;;) // forever
{
    keypadPress = getKey() ;

    // Process key
    switch( keypadPress )
    {
        case '8' :
        {
            LCD_vPrintChar( '8' );
        }
        break ;

        default :
        {
            // any other key
        }
    }
}