首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >这是检查是否存储了现有密码的正确方法吗?

这是检查是否存储了现有密码的正确方法吗?
EN

Stack Overflow用户
提问于 2019-10-17 14:20:41
回答 1查看 78关注 0票数 0

我正在用2 ATMega16微控制器创建一个应用程序。

第二个微应该检查密码是否存储在外部EEPROM中,然后将此信息发送给第一个。因此,如果有一个存储的密码用户将被要求登录,否则用户应该设置一个新的密码,并将其发送到第二个微,以保存在外部EEPROM。

代码没有以正确的方式运行;您能帮助我理解发生了什么吗?

注意:所有使用的驱动程序都经过测试,并且所有驱动程序都正常工作。

第一微码

代码语言:javascript
运行
复制
#define PASSWORD_LENGTH 5
#define PASSWORD_ADDRESS 0x0311
#define HMI_READY 0
#define CONTROL_READY 1
#define IS_PASSWORD_EXIST 6
#define PASSWORD_EXISTS 7
#define PASSWORD_NOT_EXISTS 8

#include "lcd.h"
#include "keypad.h"
#include "uart.h"

void HMI_init(void) ;
void HMI_set_new_password(uint8 *a_ptrPassword) ;
void HMI_send_password(uint8 *a_ptrPass) ;

uint8 g_password[PASSWORD_LENGTH] = {0} ;

int main(void)
{
    HMI_init() ;
    UART_sendByte(HMI_READY) ;
    LCD_clearDisplay() ;
    LCD_displayString("Stuck Here : ( ") ; /*<<<<<<<<<<<<<<<<<<<<<*/
    if(UART_receiveByte() == PASSWORD_NOT_EXISTS)
    {
        LCD_sendCommand(CLEAR_DISPLAY) ;
        LCD_displayString("N O ") ;

    }
    else if(UART_receiveByte() == PASSWORD_EXISTS) ;
    {
        LCD_sendCommand(CLEAR_DISPLAY) ;
        LCD_displayString("Y E S ") ;
        while(1)
        {

        }
    }

}




/* Functions Definitions */
void HMI_init(void)
{
    LCD_init() ;
    LCD_sendCommand(CLEAR_DISPLAY) ;
    LCD_sendCommand(CURSOR_OFF) ;
    LCD_displayStringRowCol(0,4,"WELCOME") ;
    LCD_displayStringRowCol(1,1,"TO DOOR LOCKER") ;
    UART_init();
    SREG |=(1<<7) ;
}

void HMI_set_new_password(uint8 *a_ptrPassword)
{
    uint8 i = 0 ;
    uint8 key = 0 ;
    uint8 temp_pass[PASSWORD_LENGTH] = {0} ;
    uint8 confirm_flag = 0 ;

    while(confirm_flag == 0)
    {
        i = 0 ;
        LCD_sendCommand(CLEAR_DISPLAY) ;
        LCD_sendCommand(CURSOR_ON) ;
        LCD_displayString("SET A PASSWORD : ") ;
        LCD_goToRowCol(1,0) ;
        while(i<PASSWORD_LENGTH)
        {
            key = KeyPad_getPresssedKey() ;
            if(key>=0 && key<=9)
            {
                a_ptrPassword[i] = key ;
                LCD_displayCharacter('*') ;
                i++ ;

            }else
            {

            }
            _delay_ms(2000) ;
        }
        LCD_sendCommand(CLEAR_DISPLAY) ;
        LCD_sendCommand(CURSOR_ON) ;
        LCD_displayString("REPEAT PASSWORD : ") ;
        LCD_goToRowCol(1,0) ;
        i = 0 ;
        while(i<PASSWORD_LENGTH)
        {
            key = KeyPad_getPresssedKey() ;
            if(key>=0 && key <=9)
            {
                temp_pass[i] = key ;
                i++ ;
                LCD_displayCharacter('*') ;
            }else
            {

            }
            _delay_ms(2000) ;
        }
        /* compare */
        for(i = 0 ; i<PASSWORD_LENGTH ; i++)
        {
            if(a_ptrPassword[i] != temp_pass[i])
            {
                confirm_flag = 0 ;
                break ;
            }else{
                confirm_flag = 1 ;
            }
        }

        if(confirm_flag == 1)
        {
            LCD_sendCommand(CLEAR_DISPLAY) ;
            LCD_displayString("CONFIRMED") ;
            _delay_ms(2000) ;
        }else if(confirm_flag == 0 )
        {
            LCD_sendCommand(CLEAR_DISPLAY) ;
            LCD_displayString("NOT CONFIRMED") ;
            _delay_ms(2000) ;
        }
    }
}


void HMI_send_password(uint8 *a_ptrPass)
{
    uint8 i = 0 ;
    for(i = 0 ; i<PASSWORD_LENGTH ; i++)
    {
        UART_sendByte(a_ptrPass[i]) ;
    }
}

第二微码

代码语言:javascript
运行
复制
#define PASSWORD_LENGTH 5
#define PASSWORD_ADDRESS 0x0311
#define HMI_READY 0
#define CONTROL_READY 1
#define IS_PASSWORD_EXIST 6
#define PASSWORD_EXISTS 7
#define PASSWORD_NOT_EXISTS 8

#include "lcd.h"
#include "uart.h"
#include "eeprom.h"


void CONTROL_init(void) ;
uint8 CONTROL_password_exist(void) ;
void CONTROL_receive_password(uint8 *a_ptrPass) ;
void CONTROL_save_password(uint8 *a_ptrPass) ;


uint8 g_received_password[PASSWORD_LENGTH] = {0}  ;
int main(void)
{
    CONTROL_init() ;
    if(CONTROL_password_exist() == 0)
    {
        while(UART_receiveByte() != HMI_READY) ;
        UART_sendByte(PASSWORD_NOT_EXISTS) ;
    }
    else
    {
        while(UART_receiveByte() != HMI_READY) ;
        UART_sendByte(PASSWORD_EXISTS) ;
        while(1)
        {

        }
    }
}

void CONTROL_init(void)
{
    LCD_init() ;
    LCD_sendCommand(CLEAR_DISPLAY) ;
    LCD_sendCommand(CURSOR_OFF) ;
    EEPROM_init() ;
    UART_init() ;
    SREG |=(1<<7) ;
}

uint8 CONTROL_password_exist(void)
{
    uint8 i = 0 ;
    uint8 temp = 0 ;
    for(i=0 ; i<PASSWORD_LENGTH ; i++)
    {
        EEPROM_readByte((PASSWORD_ADDRESS+i) , &temp) ;
        _delay_ms(150) ;
        if(temp != 0xFF)
        {
            return 1 ;
        }
    }
    return 0 ;
}


void CONTROL_receive_password(uint8 *a_ptrPass)
{
    uint8 i = 0 ;
    for(i = 0 ; i<PASSWORD_LENGTH ; i++)
    {
        a_ptrPass[i] = UART_receiveByte() ;
    }
}

void CONTROL_save_password(uint8 *a_ptrPass)
{
    uint8 i = 0 ;
    for(i = 0 ; i<PASSWORD_LENGTH ; i++)
    {
        EEPROM_writeByte((PASSWORD_ADDRESS+i) , a_ptrPass[i]);
        _delay_ms(150) ;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-10-17 22:17:11

同步实现得很糟糕--您有一个启动竞赛条件。当人机界面发送其单个HMI_READY字节时,您无法保证控件已经就绪。

人机界面应该等待响应,如果在合理的时间内没有收到任何响应(毫秒),则重新发送HMI_READY

伪代码--这只是为了说明一种可能的结构和逻辑:

代码语言:javascript
运行
复制
 // Wait for response
 response = false ;
 while( !response )
 {
     // Send or resend synchronisation byte
     send( HMI_READY )

     // Wait for response or timeout
     start_time = now() ;
     while( !response && (now() - start_time) < TIMEOUT )
     {
         ch = receive() ;
         response = ( ch == PASSWORD_NOT_EXISTS || ch == PASSWORD_EXISTS )
     }
}

if( ch == PASSWORD_NOT_EXISTS)
{
    ...
}
else // password exists
{
    ...
}

您的控件可能很好,因为它确实无限期地等待HMI_READY,但最好是这样构造:

代码语言:javascript
运行
复制
// Wait for synch byte
while( receive() == HMI_READY )
{
    // wait
}

if( CONTROL_password_exist() )
{
    send( PASSWORD_EXISTS )
}
else 
{
    send( PASSWORD_NOT_EXISTS )
}

...

这样,无论何时或以何种顺序启动每个微设备,它们都将等待另一个同步。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58434833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档