Priority-less Quiz buzzer code

Thread Starter

astrotom

Joined Jul 14, 2012
16
Hey guys!
I was making a buzzer for a school quiz contest using AT89C52 microcontroller. It works such that any team can press and the order of press is shown on an LCD module. My code is below. In the keil simulation window, everything works as it should, however, in my breadboard setup, the lcd module just lights up and nothing else happens. It does not respond to the button presses. Could you please check the 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:

Sensacell

Joined Jun 19, 2012
3,432
The expectation that you can just throw all this hardware and software together and expect it to work is wishful thinking.

Time to learn a bit of strategic troubleshooting!

Look at your project from a big-picture perspective, break it down into sub-systems and test them independently.

With any microprocessor project, the hardest part is just getting the CPU to load and run some code correctly the first time. Try writing a snippet of code that just toggles an I/O pin- nothing else, once that is working you can begin to add more of your code, observing what works and what doesn't.
 

Thread Starter

astrotom

Joined Jul 14, 2012
16
I have tried the basic led blinking test. And that doesn't work either. Its not due to a programming error for sure. It's either something to do with the wiring or the programmer board.
 

Sensacell

Joined Jun 19, 2012
3,432
I have tried the basic led blinking test. And that doesn't work either.
Things to check:

Oscillator circuit-
Make sure the oscillator runs.

Configuration bits-
This is the usual trouble spot, re-check all the configuration settings.

Programmer / debugger settings

Best of luck!
 

Thread Starter

astrotom

Joined Jul 14, 2012
16
@Sensacell Thanks for the reply.

At last I've got my buzzer working. It seems it was a problem with my wiring on the breadboard. When I used a development board, it works! However, the piece of code which starts the buzzer and timer 0 is not working as I wished. When a team presses a switch, if a buzzer is already on due to a previous press, I want the buzzer and timer to be stopped and then started again for the the timer duration. Can somebody help?
 
Top