Push button in AVR(Atmega16 , codevision)

Thread Starter

eric_s88

Joined Apr 20, 2011
158
Hi everyone
according to my homework, I have designed a digital clock via 6, 7segments using 3ports of ATMEGA16. now I want to add push buttons to adjust min and hour.. so I need 4push button to decrease or increase them. but I dont know what is the algorithm for keys. I put the schematic and C codes of my program. any body knows how to do this?



and this is my code (without codes for keys):

Rich (BB code):
#include <mega16.h>
#include <delay.h>
unsigned int h,h1,m,m1,s,s1;
void main()
{
DDRA=0xff;
DDRC=0xff;
DDRD=0xff;
while(1)
  {
    for(h=0;h<=5;h++)
    {PORTA=h<<4;
        for(h1=0;h1<=9;h1++)
        {
        if(h==2 && h1==4) goto S1;
        PORTA=(h<<4)|h1;
            for(m=0;m<=5;m++)
            {PORTD=m<<4;
                for(m1=0;m1<=9;m1++)
                {PORTD=(m<<4)|m1;
                    for(s=0;s<=5;s++)
                    {PORTC=s<<4;
                        for(s1=0;s1<=9;s1++)
                        {PORTC=(s<<4)|s1;
                        delay_us(100);
                        }
                    }
                }
            }
            
        }
     }   S1:
  }
}
 

hgmjr

Joined Jan 28, 2005
9,027
I recommend you familiarize yourself with the Finite State Machine coding technique. It makes it much easier to initially code and then to debug your code when it fails to operate as you intended.

Here is just one explanatory link to the technique. There are hundreds of other links that can be found on the Web.

hgmjr
 

Thread Starter

eric_s88

Joined Apr 20, 2011
158
I just want to add some code to my previous code .. sth like this:
if(PINB.0=1) //when button is pressed once
{
m1=m1++; //increases minute 1unit
delay_ms(20) //delay as button debounce
}
 
It would be easier to do the updating of the time using one of the timer interrupts(100uS interrupt if you use deicmal seconds). Then in your main loop just check for pushbutton presses.
It is not good coding practice to use GOTO. Try to move away from that.
 

u-will-neva-no

Joined Mar 22, 2011
230
Listen to what hgmjr said as it is alot easier to use a state transition table approach. I can see your confusion so have a look at this post as it is much closely related to avr. You will have to think about all the states that you want in your system, what action you want to be executed for the given input(such as a button press as you require). Here is the link to get you started:http://bennthomsen.wordpress.com/em...-embedded-systems/state-table-implementation/

Also check out the 'stop watch example' as I understood it better from that one.
 
Top