Did you know that you can navigate the posts by swiping left and right?

비밀번호 입력수정

28 Jan 2018 . category: . Comments
#atmega 128 #mechatronics #science knowledge

전에 lcd와 스위치를 이용하여 비밀번호가 스위치 입력에 따라 입력번호가 나타내고 사전에 설정한 비밀번호와 일치하면 correct가 나타내도록 하는 코드를 작성하였는데 이게 record라는 임의의 변수값을 두어서 총 8가지 비트로 각 비트와 숫자를 비교대응시켜 토글되도록 하여 비밀번호를 찾아가도록 하였는데 이게 4번째 숫자가 토글이 되면 바로 correct가 나와버려서 이 오류를 수정하기 위해서 토글이 아닌 처음에 집합의 개념으로 4가지 숫자를 집합으로 저장해서 각 집합에 대응시켜 첫번째부터 4번째까지 순차적으로 비교하도록 설정한 후에 4번째가 맞았을때 motor 함수로 넘어가도록 새롭게 코드를 짜보았다. 이렇게 하니 오류가 없어지고 정확한 비밀번호 입력을 할 수 있었다. 추가 및 수정한 코드는 대략

#include <mega128.h>
#include <delay.h>
#include <alcd.h>

#define Switch_1 (!PINE.4)
#define Switch_2 (!PINE.5)
#define Switch_3 (!PINE.6)
#define Switch_4 (!PINE.7)
#define Motor_off TCCR0 = 0x00;

unsigned char number = 1;
unsigned char x = 0;
unsigned char y = 1;
unsigned char z = 0;
unsigned char record = 0b00000000;
unsigned char record2 = 0b00001111;
unsigned char str1[] = {1,2,3,4};
unsigned char str2[4] = {0};
unsigned int tim0_cnt, mot_pos, change;
unsigned int rc_time[2] = {54, 155};

void reset(void)
{
    tim0_cnt = 0;
    mot_pos = 0;
    change = 0;
    TIMSK = 0x01;
    TCCR0 = 0x02;
    TCNT0 = 238;
    SREG = 0x80;
    
    tim0_cnt = 0;
    PORTC = 0xFF;
    
    while(1)
    {
        if(tim0_cnt >= 2000)
        {
            tim0_cnt = 0;
            PORTC = 0xFF;
            change++;
            
            if(change == 100)
            {
                change = 0;
                Motor_off;  
            }    
        }
        if(tim0_cnt >= rc_time[1]) PORTC = 0x0;
    }
    
}
void motor_off(void)
{
    Motor_off;
    TIMSK = 0x00;
    PORTC = 0x00;
    mot_pos++;
    while(1)
    {
        if(Switch_4)
        {
          reset();
        }
    }     
}

void motor_on(void)
{
    tim0_cnt = 0;
    change = 0;
    TIMSK = 0x01;
    TCCR0 = 0x02;
    TCNT0 = 238;
    SREG = 0x80;
    
    tim0_cnt = 0;
    PORTC = 0xFF;
    
    while(1)
    {   
        if(tim0_cnt >= 2000)
        {
            tim0_cnt = 0;
            PORTC = 0xFF; // 모터 HIGH 출력 
            change++;
            if(change == 100)
            {   
                mot_pos = (mot_pos + 1) % 2;
                change = 0;
                motor_off();
            }
        }
        if(tim0_cnt >= rc_time[0])
        {
            PORTC = 0x0;  // 모터 LOW 출력
        }
    } 
}

interrupt [TIM0_OVF] void tim_int0(void)
{
    tim0_cnt++;
    TCNT0 = 238;
}

void check4(void)
{
    if(str1[3] == str2[3])
    {
        lcd_clear();
        lcd_gotoxy(0,1);
        lcd_putsf("correct");
        motor_on();
        delay_ms(100);        
    }
}
void check3(void)
{
    if(str1[2] == str2[2])
    {
        check4();
    }
}
void check2(void)
{
    if(str1[1] == str2[1])
    {
        check3();
    }
}
void check(void)
{
    if(str1[0] == str2[0])
    {
        check2();
    }
}

void main(void)
{
    DDRC = 0xFF;
    DDRD = 0xFF;
    DDRE = 0x00;
    PORTE = 0x00;
    PORTD = 0x00;
           
    lcd_init(8);
    
    while(1)
    {      
        if(Switch_1)
        {
            number++;
            if(number >= 9)
            {
                number = 1;
            }
            delay_ms(100);
        }
        else if(Switch_2)
        {
            number--;
            if(number <= 0)
            {
                number = 8;
            }
            delay_ms(100);
        }
        switch(number)
        {
            case 1:
                lcd_gotoxy(0,0);
                lcd_putsf("1");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("1");
                    x++;
                    str2[z] = 1;
                    z++;
                    check(); 
                    delay_ms(100);
                }
                break;
            case 2:
                lcd_gotoxy(0,0);
                lcd_putsf("2");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("2");
                    x++;
                    str2[z] = 2;
                    z++;
                    check();
                    delay_ms(100);
                }
                break;
            case 3:
                lcd_gotoxy(0,0);
                lcd_putsf("3");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("3");
                    x++;
                    str2[z] = 3;
                    z++;
                    check();
                    delay_ms(100);
                }
                break;
            case 4:
                lcd_gotoxy(0,0);
                lcd_putsf("4");
                delay_ms(100);
                if(Switch_3)
                {        
                    lcd_gotoxy(x,y);
                    lcd_putsf("4");
                    x++;
                    str2[z] = 4;
                    z++;
                    check();
                    delay_ms(100);
                }
                break;
            case 5:
                lcd_gotoxy(0,0);
                lcd_putsf("5");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("5");
                    x++;
                    record ^= 0x10;
                    check(); 
                    delay_ms(100);
                }
                break;
            case 6:
                lcd_gotoxy(0,0);
                lcd_putsf("6");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("6");
                    x++;
                    record ^= 0x20;
                    check();
                    delay_ms(100);
                }
                break;
            case 7:
                lcd_gotoxy(0,0);
                lcd_putsf("7");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("7");
                    x++;
                    record ^= 0x40;
                    check();
                    delay_ms(100);
                }
                break;
            case 8:
                lcd_gotoxy(0,0);
                lcd_putsf("8");
                delay_ms(100);
                if(Switch_3)
                {   
                    lcd_gotoxy(x,y);
                    lcd_putsf("8");
                    x++;
                    record ^= 0x80;
                    check();
                    delay_ms(100);
                }
                break;          
        }
    }
}

check문을 늘려 1:1 대응 식으로 만들었고 이렇게 오류없이 첫번째부터 네번째까지 순차적으로 확인가능하게 하였다.


Me

github를 이용해 만들어본 블로그입니다.