Priority-less Quiz buzzer code

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

  1. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    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?

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

    Well-Known Member

    Jun 19, 2012
    1,133
    267
    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.
     
  3. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    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.
     
  4. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,133
    267
    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!
     
  5. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    @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?
     
Loading...