Buzzer does not stop when its supposed to stop!

Thread Starter

astrotom

Joined Jul 14, 2012
16
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:

Rich (BB code):
#include <REGX52.H>
sbit rs = P3^5;
sbit rw = P3^6;
sbit en = P3^7;
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;

void buzzer(void);

void ISR_timer0(void) interrupt 1
{
    cnt++;
    TH0 = 0x6F;
    TL0 = 0xFF;
    if(cnt==1000)
    {
        P0_7 = 1;
        TR0 = 0;
        cnt = 0;
    }
}

void delay()
{
    TR0 = 0;
    P0_7 = 0;
    TMOD = 0x01;
    TH0 = 0x6F;
    TL0 = 0xFF;
    IE = 0x82;
    TR0 = 1;
}

void delay2(unsigned int m)
{
    unsigned int i;
    for(i=0;i<m;i++);
}

void lcmd(unsigned char cmd)
{
    P2 = cmd;
    rs = 0;
    rw = 0;
    en = 1;
    delay2(1);
    en = 0;
}

void ldata(unsigned char dat)
{
    P2 = dat;
    rs = 1;
    rw = 0;
    en = 1;
    delay2(1);
    en = 0;
}

void call()
{
    P1 = 0xFF;
    P3_1 = 1;
    flag1=flag2=flag3=flag4=flag5=flag6=flag7=flag8=team1=team2=team3=team4=team5=team6=team7=team8=0;
    counter = 1;
    cnt = 0;
    lcmd(0x38);
    delay2(5);
    lcmd(0x0E);
    delay2(5);
    lcmd(0x01);
    delay2(5);
    lcmd(0x80);
    delay2(5);
    buzzer();
}

void buzzer()
{
    while(P3_1)
    {
        while(P1==0xFF);        
        
        if(P1_0 == 0)
        {
            P1_0 = 1;
            if(flag1==0)
            {flag1 = 1;
            delay();
            team1 = counter;
            counter++;}
        }

        if(P1_1 == 0)
        {
            P1_1 = 1;
            if(flag2==0){
            flag2=1;
            delay();
            team2 = counter;
            counter++;
        }}

        if(P1_2 == 0)
        {
            P1_2 = 1;
            if(flag3==0){
            flag3=1;
            delay();
            team3 = counter;
            counter++;
        }}

        if(P1_3 == 0)
        {
            P1_3 = 1;
            if(flag4==0){
            flag4=1;
            delay();
            team4 = counter;
            counter++;
        }}

        if(P1_4 == 0)
        {
            P1_4 = 1;
            if(flag5==0){
            flag5=1;
            delay();
            team5 = counter;
            counter++;
        }}

        if(P1_5 == 0)
        {
            P1_5 = 1;
            if(flag6==0){
            flag6=1;
            delay();
            team6 = counter;
            counter++;
        }}

        if(P1_6 == 0)
        {
            P1_6 = 1;
            if(flag7==0){
            flag7=1;
            delay();
            team7 = counter;
            counter++;
        }}

        if(P1_7 == 0)
        {
            P1_7 = 1;
            if(flag8==0){
            flag8=1;
            delay();
            team8 = counter;
            counter++;
        }}
        lcmd(0x01);
        lcmd(0x80);
        ldata('A');
        ldata(team1);
        ldata(' ');
        ldata('B');
        ldata(team2);
        ldata(' ');
        ldata('C');
        ldata(team3);
        ldata(' ');
        ldata('D');
        ldata(team4);
        ldata(' ');
        ldata('E');
        ldata(team5);
        ldata(' ');
        ldata('F');
        ldata(team6);
    }
    
    call();
}

void main()
{
    lcmd(0x38);
    delay2(5);
    lcmd(0x0E);
    delay2(5);
    lcmd(0x01);
    delay2(5);
    lcmd(0x80);
    delay2(5);
    
    P3_1 = 1;
    P1 = 0xFF;
    buzzer();
}
 
Last edited by a moderator:
Top