# Push button in AVR(Atmega16 , codevision)

#### eric_s88

Joined Apr 20, 2011
157
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,029
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

#### eric_s88

Joined Apr 20, 2011
157
Can you tell me your idea more clearly?? Is this related to C coding? or reading key in avr??

#### MrChips

Joined Oct 2, 2009
19,768
It has nothing to do with C, coding or AVR.
It has everything to do with problem solving.

#### eric_s88

Joined Apr 20, 2011
157
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
}

#### eric_s88

Joined Apr 20, 2011
157
can anybody help???

#### andre@electrocomp.co.za

Joined Jun 22, 2011
2
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/embedded-design/state-machines-in-embedded-systems/state-table-implementation/

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

#### eric_s88

Joined Apr 20, 2011
157
thank you, may you write an example of code for timer interrupts?? I dont know this yet

#### eric_s88

Joined Apr 20, 2011
157

#### u-will-neva-no

Joined Mar 22, 2011
230
I would if I knew! I need to check it out because I need a timer interrupt for my project also!