Search code examples
ctimeratmelatmelstudio

Can't find error from atmega128 C code


This is 3 digit timer's code in ATMEGA 128 microcontroller. If you push button which connected to port D, timer will start. However If you press it for while, timer will reset. In proteus simulation microcontroller counts to only 1 after i press button. From that on nothing happens (Display is still "001")

#define F_CPU   4000000UL
#include<avr/io.h>
#include<avr/delay.h>
int x;

int button_check(int x)
{   DDRD = 0x00;
    if(PIND==0xFF)
    {
        _delay_ms(200);
        if(PIND==0xFF)
        {   x=1;}
        else
        {   x=0;}
    }
    else
    { x=x;}
    return x;
}

int main(void)
{
    int a,b,c;
    DDRA = 0xFF;
    DDRB = 0xFF;
    DDRC = 0xFF;
    x=1;
    while(1)
    {   
        a=0;
        b=0;
        c=0;

        while(button_check(x)==0) //starting condition
        {  _delay_ms(200);
            if(a==9)
            { a=0;
                if(b==9)
                {  b=0;
                    if(c==9)
                    {  c=0;}
                    else
                    {  c=c+1;}
                }
                else
                {   b=b+1;}
            }
            else
            {   a=a+1;}
            PORTA = a;
            PORTB = b;
            PORTC = c;
        }
    }
    return 0;
}

Solution

  • The value from x never changes in main().

    You want

    while((x=button_check(x))==0)