使用相同的代码我可以从ATMEGA16A的两个端口获得两个不同的答案

时间:2016-01-20 15:37:52

标签: c keyboard calculator avr lcd

我试图制作一个需要两个带ATMEGA16A键盘的计算器 我已经编写了一个从键盘获取输入的代码,并且它在PORTB上工作,但是相同的代码(只是将B更改为C)在PORTC上不起作用!!

请帮助我,我变得疯狂:(

 while(y[1]== 0 || y[0] ==24) 
        {
            getB(y);
            if(y[1]== 0 || y[0] ==24)
            {
                getC(y);
            }
        }

    int* getB(int *y)
{
    int key;
    DDRB = 0x0F;
    PORTB = 0xF0;
    delay_ms(5);
    key = PINB;
    DDRB = 0xF0;
    PORTB = 0x0F;
    delay_ms(5);       
    key = key | PINB;
    delay_ms(10); 
    if(key != 0xFF) {keyboardB(y);}
    return y;
}

int* keyboardB(int* y)
{
    DDRB = 0x0F;

    PORTB.0 = 1;
    PORTB.1 = 0;
    PORTB.2 = 0;
    PORTB.3 = 0;
    if(PINB.5 == 1){y[0] = 14; y[1] = 1; lcd_puts("*");while(PINB.5 == 1){PORTD.0 = 1;}PORTD.0 = 0;return y;}
    if(PINB.4 == 1){y[0] = 15; y[1] = 1; lcd_puts("/");while(PINB.4 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.6 == 1){y[0] = 13; y[1] = 1; lcd_puts("-");while(PINB.6 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.7 == 1){y[0] = 12; y[1] = 1; lcd_puts("+");while(PINB.7 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    //=================================================
    PORTB.0 = 0;
    PORTB.1 = 1;
    PORTB.2 = 0;
    PORTB.3 = 0;
    if(PINB.5 == 1){y[0] = 9; y[1] = 1; lcd_puts("9");while(PINB.5 == 1){PORTD.0 = 1;}PORTD.0 = 0;return y;}
    if(PINB.4 == 1){y[0] = 11; y[1] = 1; lcd_puts("=");while(PINB.4 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.6 == 1){y[0] = 6; y[1] = 1; lcd_puts("6");while(PINB.6 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.7 == 1){y[0] = 3; y[1] = 1; lcd_puts("3");while(PINB.7 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    //=================================================
    PORTB.0 = 0;
    PORTB.1 = 0;
    PORTB.2 = 1;
    PORTB.3 = 0;
    if(PINB.5 == 1){y[0] = 8; y[1] = 1; lcd_puts("8");while(PINB.5 == 1){PORTD.0 = 1;}PORTD.0 = 0;return y;}
    if(PINB.4 == 1){y[0] = 0; y[1] = 1; lcd_puts("0");while(PINB.4 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.6 == 1){y[0] = 5; y[1] = 1; lcd_puts("5");while(PINB.6 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.7 == 1){y[0] = 2; y[1] = 1; lcd_puts("2");while(PINB.7 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    //=================================================
    PORTB.0 = 0;
    PORTB.1 = 0;
    PORTB.2 = 0;
    PORTB.3 = 1;
    if(PINB.4 == 1)     //Mode
    {
        //delay_ms(1000);
        //if(PINB.4 == 1) {while(PINB.4 == 1){}y[0] = 10; y[1] = 1; return y;}
        //else {y[0] = 10; y[1] = 0; return y;}  //value of y[0] is not important
        y[0] = 24; y[1] = 1;while(PINB.4 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;
    }
    if(PINB.5 == 1){y[0] = 24; y[1] = 1;while(PINB.5 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.6 == 1){y[0] = 24; y[1] = 1;while(PINB.6 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
    if(PINB.7 == 1){y[0] = 24; y[1] = 1;;while(PINB.7 == 1){PORTD.0 = 1;}PORTD.0 = 0; return y;}
}

1 个答案:

答案 0 :(得分:1)

很可能你没有停用使用端口C上的一些引脚的JTAG端口。查看保险丝并将它们设置为停用JTAG以便能够在应用程序中使用该端口。