Push button in AVR(Atmega16 , codevision)

Discussion in 'Homework Help' started by eric_s88, Jan 3, 2012.

  1. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    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?
    [​IMG]


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

    Code ( (Unknown Language)):
    1.  
    2. #include <mega16.h>
    3. #include <delay.h>
    4. unsigned int h,h1,m,m1,s,s1;
    5. void main()
    6. {
    7. DDRA=0xff;
    8. DDRC=0xff;
    9. DDRD=0xff;
    10. while(1)
    11.   {
    12.     for(h=0;h<=5;h++)
    13.     {PORTA=h<<4;
    14.         for(h1=0;h1<=9;h1++)
    15.         {
    16.         if(h==2 && h1==4) goto S1;
    17.         PORTA=(h<<4)|h1;
    18.             for(m=0;m<=5;m++)
    19.             {PORTD=m<<4;
    20.                 for(m1=0;m1<=9;m1++)
    21.                 {PORTD=(m<<4)|m1;
    22.                     for(s=0;s<=5;s++)
    23.                     {PORTC=s<<4;
    24.                         for(s1=0;s1<=9;s1++)
    25.                         {PORTC=(s<<4)|s1;
    26.                         delay_us(100);
    27.                         }
    28.                     }
    29.                 }
    30.             }
    31.            
    32.         }
    33.      }   S1:
    34.   }
    35. }            
    36.            
    37.  
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    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
     
  3. eric_s88

    Thread Starter Member

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

    Moderator

    Oct 2, 2009
    12,420
    3,355
    It has nothing to do with C, coding or AVR.
    It has everything to do with problem solving.
     
  5. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    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
    }
     
  6. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    can anybody help???
     
  7. andre@electrocomp.co.za

    New Member

    Jun 22, 2011
    2
    0
    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.
     
  8. u-will-neva-no

    Member

    Mar 22, 2011
    230
    2
    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.
     
  9. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    thank you, may you write an example of code for timer interrupts?? I dont know this yet :)
     
  10. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
  11. u-will-neva-no

    Member

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

    Thread Starter Member

    Apr 20, 2011
    157
    1
    oh good.. so if you find out how it works put that here, and If I do so I will put ;)
     
Loading...