0

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

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

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

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

第一个微码

#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]) ;
    }
}

第二个微码

#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) ;
    }
}
4

1 回答 1

0

同步实施得很差 - 你有一个启动竞争条件。HMI_READY当 HMI 发送其单个字节时,您无法保证 CONTROL 已准备好。

HMI 应该等待响应,如果在合理的时间内(几毫秒)没有收到响应,则重新发送HMI_READY

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

 // 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
{
    ...
}

您的 CONTROL 可能没问题,因为它确实无限期地等待 HMI_READY,但可能更好地构造如下:

// Wait for synch byte
while( receive() == HMI_READY )
{
    // wait
}

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

...

这样,每个微启动的时间或顺序都无关紧要,每个都将等待另一个 - 同步。

于 2019-10-17T22:17:11.140 回答