مع تحياتي وشكري وتقديري لكاتب الموضوع
لكن بالنسبه للكود المختصر من قبل الاخ الكريم اشرف الخطيب كان اسهل وفهمته والحمد لله مع اني لازلت مبتدي لكن هناك مشكلة واجهتني وهو انه عند عمل جرس الانذار بعد ثلاثة محاولات فاشله لاحظت ان جرس الانذار يستمر بالعمل ولا يسمح لك بالمحاولة مجددا الا بعد قطع الدائرة وتشغيلها من جديد حاولت التعديل في الكود ولم استطع
وهذا هو الكود
كود:
void tone1()
{
sound_play ( 659, 250 ) ; // Frequency = 659Hz, duration = 250ms
}
void tone2() {
sound_play ( 698, 250 ) ; // Frequency = 698Hz, duration = 250ms
}
void Tone3() {
sound_play ( 784, 250 ) ; // Frequency = 784Hz, duration = 250ms
}
void melody()
{
tone1(); tone2(); tone3(); tone3();
tone1(); tone2(); tone3(); tone3();
tone1(); tone2(); tone3();
tone1(); tone2(); tone3(); tone3();
tone1(); tone2(); tone3();
tone3(); tone3(); tone2(); tone2(); tone1();
}
// main program
void main()
{
// variables declaration
char m1 = 0 , m2 = 0 , m3 = 0 ; // keypad_read variables
char attempts = 0 ; // attempts variables
// i/p & o/p ports configuration
trisc = 0 ; portc = 0 ;
trisb = 0 ; portb = 0 ;
trisd = 0xff ;
// lcd initialization
lcd_init(&portb) ;
lcd_cmd(lcd_cursor_off) ;
// keypad initialization
keypad_init(&portd) ;
// sound play initialization
sound_init(&portc,1) ;
main :
while(1) // endless loop
{
lcd_cmd(lcd_clear) ;
lcd_out(1,1,"ENTER PASS: ") ;
get_first_no :
m1 = 0 ;
m1 = keypad_released() ;
if( m1 == 0 ) goto get_first_no ;
if(attempts == 3)
{
lcd_out( 2,5,"ALARM ON" ) ;
melody() ;
goto main ;
}
if( m1 == 16 || m1 == 14 ) goto get_first_no ;
delay_ms(10) ; // debounce time
lcd_chr_cp('*') ; // lcd_print
get_second_no :
m2 = 0 ;
m2 = keypad_released() ;
if( m2 == 0 || m2 == 14 ) goto get_second_no ;
if( m2 == 16 )
{
lcd_cmd(lcd_clear) ;
goto main ;
}
delay_ms(10) ; // debounce time
lcd_chr_cp('*') ; // lcd_print
confirm :
m3 = 0 ;
m3 = keypad_released() ;
if( m3 == 0 || m3 != 16 ) goto confirm ;
delay_ms(10) ; // debounce time
check :
if( m1 == 15 && m2 == 7 )
{
lcd_out(2,5," DOOR1 ON ") ;
portc.f5 = 1 ; delay_ms(5000) ; portc.f5 = 0 ;
attempts = 0 ;
}
else if( m1 == 3 && m2 == 11 )
{
lcd_out(2,5," DOOR2 ON ") ;
portc.f6 = 1 ; delay_ms(5000) ; portc.f6 = 0 ;
attempts = 0 ;
}
else if( m1 == 11 && m2 == 3 )
{
lcd_out(2,5," DOOR3 ON ") ;
portc.f7 = 1 ; delay_ms(5000) ; portc.f7 = 0 ;
attempts = 0 ;
}
else if ( m1 == 2 && m2 == 2 )
{
lcd_out(2,5,"DOORS1&2 ON") ;
portc.f5 = portc.f6 =1 ; delay_ms(5000) ;
portc.f5 = portc.f6 = 0 ;
attempts = 0 ;
}
else // if invalid pass is entered
{
attempts++ ;
lcd_out(2,5,"INVALID") ;
while( keypad_released() == 0 ) {}
}
} // end loop
} // end program