why I can not stop the alarm?????

Discussion in 'Embedded Systems and Microcontrollers' started by eric_s88, Jan 26, 2012.

  1. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    Hi everyone
    I have written a program for an alarm clock, everything is Ok,and the alarm stop after ringing fot 1min but I decided to put a push button for operator in order to stop alarm manually..
    but while the alarm is ringing and I push the button, the alarm does not stop.. whats the problem?? I put here the schematic and the code. whats the problem??
    I use the code like this >> PORTC.0=1; //the alarm starts

    if(PIND.0==0) PORTC.0=0; // the alarm must stop!

    Code ( (Unknown Language)):
    1. /*****************************************************
    2. This program was produced by the
    3. CodeWizardAVR V2.05.0 Professional
    4. Automatic Program Generator
    5. © Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
    6. http://www.hpinfotech.com
    7.  
    8. Project :
    9. Version :
    10. Date    : 13-Jan-2012
    11. Author  : NeVaDa
    12. Company :
    13. Comments:
    14.  
    15.  
    16. Chip type               : ATmega16
    17. Program type            : Application
    18. AVR Core Clock frequency: 1.000000 MHz
    19. Memory model            : Small
    20. External RAM size       : 0
    21. Data Stack size         : 256
    22. *****************************************************/
    23. #include <stdlib.h>
    24. #include <mega16.h>
    25. #include <delay.h>
    26. // Alphanumeric LCD Module functions
    27. #include <alcd.h>
    28. int s=0,m=0,h=0,mo=0,ho=12,x=0,i=0;
    29. unsigned char xd[4],sd[4],md[4],hd[4],mod[4],hod[4];
    30. void key_clock()
    31. {
    32.     if(PINB.0==0)
    33.     {m++;
    34.     delay_ms(1);
    35.         if(m==60) m=0;
    36.     }
    37.     while(PINB.0==0);
    38.     if(PINB.1==0)
    39.     {h++;
    40.     delay_ms(1);
    41.         if(h==24) h=0;
    42.     }
    43.     while(PINB.1==0);
    44. }
    45. void key_alarm()
    46. {
    47.     if(PINB.5==0)
    48.     {mo++;
    49.     delay_ms(1);
    50.         if(mo==60) mo=0;
    51.     }
    52.     while(PINB.5==0);
    53.     if(PINB.6==0)
    54.     {ho++;
    55.     delay_ms(1);
    56.         if(ho==24) ho=0;
    57.     }
    58.     while(PINB.6==0);
    59. }
    60.  
    61. void main(void)
    62. {
    63. // Declare your local variables here
    64.  
    65. // Input/Output Ports initialization
    66. // Port A initialization
    67. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    68. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    69. PORTA=0x00;
    70. DDRA=0x00;
    71.  
    72. // Port B initialization
    73. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    74. // State7=T State6=P State5=P State4=T State3=T State2=T State1=P State0=P
    75. PORTB=0x63;
    76. DDRB=0x00;
    77.  
    78. // Port C initialization
    79. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    80. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    81. PORTC=0x00;
    82. DDRC=0xFF;
    83.  
    84. // Port D initialization
    85. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    86. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    87. PORTD=0x01;
    88. DDRD=0x00;
    89.  
    90. // Timer/Counter 0 initialization
    91. // Clock source: System Clock
    92. // Clock value: Timer 0 Stopped
    93. // Mode: Normal top=0xFF
    94. // OC0 output: Disconnected
    95. TCCR0=0x00;
    96. TCNT0=0x00;
    97. OCR0=0x00;
    98.  
    99. // Timer/Counter 1 initialization
    100. // Clock source: System Clock
    101. // Clock value: Timer1 Stopped
    102. // Mode: Normal top=0xFFFF
    103. // OC1A output: Discon.
    104. // OC1B output: Discon.
    105. // Noise Canceler: Off
    106. // Input Capture on Falling Edge
    107. // Timer1 Overflow Interrupt: Off
    108. // Input Capture Interrupt: Off
    109. // Compare A Match Interrupt: Off
    110. // Compare B Match Interrupt: Off
    111. TCCR1A=0x00;
    112. TCCR1B=0x00;
    113. TCNT1H=0x00;
    114. TCNT1L=0x00;
    115. ICR1H=0x00;
    116. ICR1L=0x00;
    117. OCR1AH=0x00;
    118. OCR1AL=0x00;
    119. OCR1BH=0x00;
    120. OCR1BL=0x00;
    121.  
    122. // Timer/Counter 2 initialization
    123. // Clock source: System Clock
    124. // Clock value: Timer2 Stopped
    125. // Mode: Normal top=0xFF
    126. // OC2 output: Disconnected
    127. ASSR=0x00;
    128. TCCR2=0x00;
    129. TCNT2=0x00;
    130. OCR2=0x00;
    131.  
    132. // External Interrupt(s) initialization
    133. // INT0: Off
    134. // INT1: Off
    135. // INT2: Off
    136. MCUCR=0x00;
    137. MCUCSR=0x00;
    138.  
    139. // Timer(s)/Counter(s) Interrupt(s) initialization
    140. TIMSK=0x00;
    141.  
    142. // USART initialization
    143. // USART disabled
    144. UCSRB=0x00;
    145.  
    146. // Analog Comparator initialization
    147. // Analog Comparator: Off
    148. // Analog Comparator Input Capture by Timer/Counter 1: Off
    149. ACSR=0x80;
    150. SFIOR=0x00;
    151.  
    152. // ADC initialization
    153. // ADC disabled
    154. ADCSRA=0x00;
    155.  
    156. // SPI initialization
    157. // SPI disabled
    158. SPCR=0x00;
    159.  
    160. // TWI initialization
    161. // TWI disabled
    162. TWCR=0x00;
    163.  
    164. // Alphanumeric LCD initialization
    165. // Connections specified in the
    166. // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    167. // RS - PORTA Bit 0
    168. // RD - PORTA Bit 1
    169. // EN - PORTA Bit 2
    170. // D4 - PORTA Bit 4
    171. // D5 - PORTA Bit 5
    172. // D6 - PORTA Bit 6
    173. // D7 - PORTA Bit 7
    174. // Characters/line: 16
    175. lcd_init(16);
    176. lcd_gotoxy(0,1);
    177. lcd_putsf("Alarm T ");
    178. h=11;
    179. m=59;
    180. s=58;
    181. while (1)
    182.       {
    183.       x++;
    184.       if(x==99)
    185.       {x=0;
    186.       s++;
    187.         if(s==60)
    188.             {s=0;
    189.             m++;
    190.             PORTC.0=0;
    191.                 if(m==60)
    192.                 {m=0;
    193.                 h++;
    194.                     if(h==24) h=0;
    195.                 }
    196.             }
    197.       }
    198.       if(h==ho&&m==mo) PORTC.0=1;
    199.       if(PIND.0==0) PORTC.0=0;
    200.       key_clock();
    201.       key_alarm();
    202.       itoa(x,xd);      
    203.       itoa(s,sd);
    204.       itoa(m,md);
    205.       itoa(h,hd);
    206.       itoa(mo,mod);
    207.       itoa(ho,hod);
    208.       lcd_gotoxy(8,1);
    209.       if(ho<10) lcd_putchar('0');
    210.       lcd_puts(hod);
    211.       lcd_puts(":");
    212.       if(mo<10) lcd_putchar('0');
    213.       lcd_puts(mod);
    214.       lcd_gotoxy(0,0);
    215.       if(h<10) lcd_putchar('0');
    216.       lcd_puts(hd);
    217.       lcd_putsf(":");
    218.       if(m<10) lcd_putchar('0');
    219.       lcd_puts(md);
    220.       lcd_putsf(":");
    221.       if(s<10) lcd_putchar('0');
    222.       lcd_puts(sd);
    223.       lcd_putsf("   ");
    224.       //if(x<10) lcd_putchar('0');
    225.       //lcd_puts(xd);
    226.       delay_us(4480);      
    227.  
    228.       }
    229. }  
    [​IMG]
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Wouldn´t that work only as long as you hold that button?
    Otherwise the code is terribly spaghetti like, placing a comment here and there and dividing it into more functions with names that actually say what the function does wouldn´t hurt.
     
  3. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    no it has no action but add a sound (like noise) to the alarm sound.. the alarm stops when 1minute in passed, and I cant stop it manually... I'm really confused.. :(
     
  4. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Ok, what do key_clock(); and key_alarm() do?
    Do you realize at all what how your code works? It goes in a loop, and when alarm is on and reset button is on it sets the portc.0 to 1 and immediately after that to 0.

    You want it to remember that the button was once set and stop activating the portc.0 after that.
     
    eric_s88 likes this.
  5. ifixit

    Distinguished Member

    Nov 20, 2008
    638
    108
    • You need to declare an alarm_cancel variable. Set this flag when the alarm cancel PB is pressed. This way the program remembers that state.
    • Now use that flag as an extra condition in your IF statement that puts the alarm on.
    • e.g. if(h==ho && m==mo && alarm_cancel==0) PORTC.0=1;
    • The alarm is only on if the alarm_cancel flag is false.
    • Also; where you set PORTC.0=0 in the IF statement, set the alarm_cancel flag to 0 as well.
    Regards,
    Ifixit
     
    eric_s88 likes this.
  6. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1

    key_clock(); << this is the function that controls the keys to adjust the time.
    key_alarm(); << this is the function that controls the keys to adjust the alarm time.

    yes I written it myself and I know how it works, yes I put the deactivation code right after the activation, but look there.. it is in a condition .. if(PIND.0==0)...
     
  7. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1

    thank you, I will try it and tell the result
     
  8. eric_s88

    Thread Starter Member

    Apr 20, 2011
    157
    1
    yes!!!!!! that was the problem, when I pressed the key the alarm went off but after key is opened the time of clock and alarm time was still the same so the ring continue to ring!!
    the idea of ifixit worked, and now I have the problem, solved..

    thank you
     
Loading...