How to compare the inputs of atmega16 throught program ?

Discussion in 'Embedded Systems and Microcontrollers' started by hadeohsam, Dec 20, 2012.

  1. hadeohsam

    Thread Starter New Member

    Dec 19, 2012
    1
    0
    Dear Microcontrollers master,
    I have programmed the atmega16 to compare the Port B, port C and Port D, than out to port A as the result. the program is below :
    #include <mega16.h>
    // Declare your global variables here
    #define tombol1 PINC
    #define tombol2 PIND
    #define tombol3 PINB

    void main(void)
    {
    PORTA=0x00;
    DDRA=0xFF;
    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
    PORTB=0xFF;
    DDRB=0x00;

    // Port C initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
    PORTC=0xFF;
    DDRC=0x00;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
    PORTD=0xFF;
    DDRD=0x00;

    //
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    //
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    [​IMG]
    UCSRB=0x00;
    ACSR=0x80;
    SFIOR=0x00;
    SPCR=0x00;
    TWCR=0x00;

    while (1)
    {
    if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x00))PORTA=0x00;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x01))PORTA=0x01;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x02))PORTA=0x02;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x03))PORTA=0x03;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x04))PORTA=0x04;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x05))PORTA=0x05;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x06))PORTA=0x06;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x07))PORTA=0x07;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x08))PORTA=0x08;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x09))PORTA=0x09;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0A))PORTA=0x0A;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0B))PORTA=0x0B;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0C))PORTA=0x0C;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0D))PORTA=0x0D;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0E))PORTA=0x0E;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x0F))PORTA=0x0F;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x10))PORTA=0x10;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x11))PORTA=0x11;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x12))PORTA=0x12;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x13))PORTA=0x13;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x14))PORTA=0x14;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x15))PORTA=0x15;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x16))PORTA=0x16;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x17))PORTA=0x17;
    else if((tombol1==0x00)&&(tombol2==0x00)&&(tombol3==0x18))PORTA=0x18;
    …
    else if((tombol1==0x00)&&(tombol2==0x1A)&&(tombol3==0x01))PORTA=0x7F;
    else if((tombol1==0x00)&&(tombol2==0x1A)&&(tombol3==0x02))PORTA=0x80;
    else if((tombol1==0x00)&&(tombol2==0x1A)&&(tombol3==0x03))PORTA=0x81;
    else if((tombol1==0x00)&&(tombol2==0x1A)&&(tombol3==0x04))PORTA=0x82;
    else if((tombol1==0x00)&&(tombol2==0x1A)&&(tombol3==0x05))PORTA=0x83;

    else PORTA=0xFA;
    }
    }


    but it doesn't work. the program only respons to individual port, it doesn't work to compare two ports. please help me to solve the problem. thank you very much.
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I don´t see how is the program supposed to compare two ports, when you have three ports in each if. Also, how does "0x0 1" even compile? It should be 0x01.
    Also you can nest IFs, so you can have
    Code ( (Unknown Language)):
    1. if(tombol1==0)
    2. {
    3.   if(tombol2==0)do something;
    4.   if(tombol2==1)do something else;
    5.   ...
    6. }
    7. else if (tombol1==1)
    8. {
    9.  ...
    10. }
    11.  
    or in your case something like
    Code ( (Unknown Language)):
    1. if(tombol1==0&&tombol2==0)
    2. {
    3.   PORTA=tombol3;
    4. }
    5. else if (tombol1==1&&tombol1==0)
    6. {
    7.  ...
    8. }
    9.  
    It seems to me you should rethink your decision logic.
     
Loading...