Buzzer does not stop when its supposed to stop!

Discussion in 'Embedded Systems and Microcontrollers' started by astrotom, Jul 30, 2012.

  1. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    I am making a quiz buzzer which displays the teams that pressed 1st, 2nd and so on. Everything's working fine, even the buzzer. But there's a little problem with how the buzzer works. I want the buzzer to stop sounding if another team presses a button and restart the buzzer for the specified delay period. I am using a timer delay instead of looping because I wish to scan for presses even when the buzzer is on. The presses seem to register, as they show up on the lcd. However, the buzzer from a previous press doesn't stop and restart when another team presses a switch even though their press is registered fine on the lcd. And if the team tries to press the buzzer after the earlier buzzer stops, it doesn't sound either as their press is already registered. Please help me guys. This is my code:

    Code ( (Unknown Language)):
    1. #include <REGX52.H>
    2. sbit rs = P3^5;
    3. sbit rw = P3^6;
    4. sbit en = P3^7;
    5. int cnt = 0, counter = 1, flag1=0, flag2=0, flag3=0, flag4=0, flag5=0, flag6=0, flag7=0, flag8=0, team1, team2, team3, team4, team5, team6, team7, team8;
    6.  
    7. void buzzer(void);
    8.  
    9. void ISR_timer0(void) interrupt 1
    10. {
    11.     cnt++;
    12.     TH0 = 0x6F;
    13.     TL0 = 0xFF;
    14.     if(cnt==1000)
    15.     {
    16.         P0_7 = 1;
    17.         TR0 = 0;
    18.         cnt = 0;
    19.     }
    20. }
    21.  
    22. void delay()
    23. {
    24.     TR0 = 0;
    25.     P0_7 = 0;
    26.     TMOD = 0x01;
    27.     TH0 = 0x6F;
    28.     TL0 = 0xFF;
    29.     IE = 0x82;
    30.     TR0 = 1;
    31. }
    32.  
    33. void delay2(unsigned int m)
    34. {
    35.     unsigned int i;
    36.     for(i=0;i<m;i++);
    37. }
    38.  
    39. void lcmd(unsigned char cmd)
    40. {
    41.     P2 = cmd;
    42.     rs = 0;
    43.     rw = 0;
    44.     en = 1;
    45.     delay2(1);
    46.     en = 0;
    47. }
    48.  
    49. void ldata(unsigned char dat)
    50. {
    51.     P2 = dat;
    52.     rs = 1;
    53.     rw = 0;
    54.     en = 1;
    55.     delay2(1);
    56.     en = 0;
    57. }
    58.  
    59. void call()
    60. {
    61.     P1 = 0xFF;
    62.     P3_1 = 1;
    63.     flag1=flag2=flag3=flag4=flag5=flag6=flag7=flag8=team1=team2=team3=team4=team5=team6=team7=team8=0;
    64.     counter = 1;
    65.     cnt = 0;
    66.     lcmd(0x38);
    67.     delay2(5);
    68.     lcmd(0x0E);
    69.     delay2(5);
    70.     lcmd(0x01);
    71.     delay2(5);
    72.     lcmd(0x80);
    73.     delay2(5);
    74.     buzzer();
    75. }
    76.  
    77. void buzzer()
    78. {
    79.     while(P3_1)
    80.     {
    81.         while(P1==0xFF);        
    82.        
    83.         if(P1_0 == 0)
    84.         {
    85.             P1_0 = 1;
    86.             if(flag1==0)
    87.             {flag1 = 1;
    88.             delay();
    89.             team1 = counter;
    90.             counter++;}
    91.         }
    92.  
    93.         if(P1_1 == 0)
    94.         {
    95.             P1_1 = 1;
    96.             if(flag2==0){
    97.             flag2=1;
    98.             delay();
    99.             team2 = counter;
    100.             counter++;
    101.         }}
    102.  
    103.         if(P1_2 == 0)
    104.         {
    105.             P1_2 = 1;
    106.             if(flag3==0){
    107.             flag3=1;
    108.             delay();
    109.             team3 = counter;
    110.             counter++;
    111.         }}
    112.  
    113.         if(P1_3 == 0)
    114.         {
    115.             P1_3 = 1;
    116.             if(flag4==0){
    117.             flag4=1;
    118.             delay();
    119.             team4 = counter;
    120.             counter++;
    121.         }}
    122.  
    123.         if(P1_4 == 0)
    124.         {
    125.             P1_4 = 1;
    126.             if(flag5==0){
    127.             flag5=1;
    128.             delay();
    129.             team5 = counter;
    130.             counter++;
    131.         }}
    132.  
    133.         if(P1_5 == 0)
    134.         {
    135.             P1_5 = 1;
    136.             if(flag6==0){
    137.             flag6=1;
    138.             delay();
    139.             team6 = counter;
    140.             counter++;
    141.         }}
    142.  
    143.         if(P1_6 == 0)
    144.         {
    145.             P1_6 = 1;
    146.             if(flag7==0){
    147.             flag7=1;
    148.             delay();
    149.             team7 = counter;
    150.             counter++;
    151.         }}
    152.  
    153.         if(P1_7 == 0)
    154.         {
    155.             P1_7 = 1;
    156.             if(flag8==0){
    157.             flag8=1;
    158.             delay();
    159.             team8 = counter;
    160.             counter++;
    161.         }}
    162.         lcmd(0x01);
    163.         lcmd(0x80);
    164.         ldata('A');
    165.         ldata(team1);
    166.         ldata(' ');
    167.         ldata('B');
    168.         ldata(team2);
    169.         ldata(' ');
    170.         ldata('C');
    171.         ldata(team3);
    172.         ldata(' ');
    173.         ldata('D');
    174.         ldata(team4);
    175.         ldata(' ');
    176.         ldata('E');
    177.         ldata(team5);
    178.         ldata(' ');
    179.         ldata('F');
    180.         ldata(team6);
    181.     }
    182.    
    183.     call();
    184. }
    185.  
    186. void main()
    187. {
    188.     lcmd(0x38);
    189.     delay2(5);
    190.     lcmd(0x0E);
    191.     delay2(5);
    192.     lcmd(0x01);
    193.     delay2(5);
    194.     lcmd(0x80);
    195.     delay2(5);
    196.    
    197.     P3_1 = 1;
    198.     P1 = 0xFF;
    199.     buzzer();
    200. }
     
    Last edited by a moderator: Jul 30, 2012
Loading...