Stopwatch using AT89S52 URGENT HELP REQUIRED

Discussion in 'Homework Help' started by sgtom, Mar 13, 2011.

  1. sgtom

    Thread Starter New Member

    Mar 13, 2011
    4
    0
    Dear guys,

    I have built this circuit (with AT89S52) but have the following problems:


    (C code below)


    Problem 1. (very important)

    when I press the start and stop button.... the time jumps an extra second on the LED display. Is that coding problem? Please check the code.



    Problem 2. (very important)

    when i compare the time measurement to an actual stopwatch it seems to lag a few seconds behind (as in just press start on both at same time and compare time without stopping) Again coding problem?



    Problem 3.



    When powering up the circuit I have to switch the power supply on and off numerous times before I sucessfully get "0000" diplayed on led. Other wise i get random displays, no display, etc. any ideas why?




    Question 1. (very important)

    I would also like to display max 9 seconds and rest miliseconds accurately instead of current setup. any help on coding will be great!



    Question 2.

    How would I go about interfacing this cuircuit with my computer ( i need the recorded times displayed on comp too)



    Question 4.

    Can i connect 2 microcontrollers recording two sperate times to one port? If sO how do I do that?



    Question 5.



    Possible to have 1. start button, two sperate stops (with 2 separate led displays) on ONE mircocontroller?





    THANK YOU SOO MUCH....



    time and help greatly appreciated to any of help/ questions....



    Look forward to hearing from you.



    Thank you!


    Code ( (Unknown Language)):
    1.  
    2. #include<reg51.h>
    3. #define msec 1
    4. unsigned int sec1,sec2;
    5. int sec1_1,sec1_2,sec2_1,sec2_2;
    6.  
    7. unsigned int digi_val[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x1 0};
    8. sbit dig_ctrl_1=P1^0; // Declare the control pins of seven segments
    9. sbit dig_ctrl_2=P1^1;
    10. sbit dig_ctrl_3=P1^2;
    11. sbit dig_ctrl_4=P1^3;
    12. sbit start_pin = P1^4; // Start pin to start the watch.
    13. sbit stop_pin = P1^5; // Stop pin to stop the watch.
    14. sbit reset_pin = P1^6; // Reset pin to reset the watch.
    15. int s,t;
    16.  
    17. void mplex_delay(unsigned int time) // Function to provide a time delay of approximatelty one second using Timer 1
    18. {
    19. int i,j;
    20. for (i=0;i<=time;i++)
    21. for(j=0;j<=50;j++);
    22. }
    23.  
    24. void digi_out(unsigned int current_num)
    25. {
    26. P2=digi_val[current_num];
    27. mplex_delay(msec);
    28. }
    29.  
    30. void display(unsigned int dig1,unsigned int dig2) // Function to display  the digits on seven segmnet. For more details refer seven segment  multiplexing.
    31. {
    32. sec1_2=dig1%10;
    33. sec1_1=dig1/10;
    34. sec2_2=dig2%10;
    35. sec2_1=dig2/10;
    36. TMOD=0x01; //Enable Timer 0
    37. TL0=0xFF;
    38. TH0=0xDB;
    39. TR0=1; // Triger Timer 0
    40. while(TF0==0)
    41. {
    42. dig_ctrl_1 = 1;
    43. dig_ctrl_2 = dig_ctrl_3 = dig_ctrl_4 = 0;
    44. digi_out(sec1_1);
    45. dig_ctrl_2 = 1;
    46. dig_ctrl_1 = dig_ctrl_3 = dig_ctrl_4 = 0;
    47. digi_out(sec1_2);
    48. dig_ctrl_3 = 1;
    49. dig_ctrl_2 = dig_ctrl_1 = dig_ctrl_4 = 0;
    50. digi_out(sec2_1);
    51. dig_ctrl_4 = 1;
    52. dig_ctrl_2 = dig_ctrl_3 = dig_ctrl_1 = 0;
    53. digi_out(sec2_2);
    54. }
    55.  
    56. TR0=0;
    57. TF0=0;
    58. }
    59.  
    60. void main()
    61. {
    62. while(1)
    63. {
    64. start: // Segment to start the stop watch
    65. start_pin = 1;
    66. stop_pin = 1;
    67. reset_pin = 1;
    68. dig_ctrl_1 = 0;
    69. dig_ctrl_2 = 0;
    70. dig_ctrl_3 = 0;
    71. dig_ctrl_4 = 0;
    72. P2 = 0xFF;
    73. s = t = 0;
    74. while(start_pin == 1)// Check if start pin is pressed
    75. {
    76. display(0,0);
    77. }
    78.  
    79. stopwatch: // Segment to stop the watch
    80. for (sec1=s;sec1<=99;sec1++)
    81. {
    82. if (stop_pin == 0 ) //Check if stop pin is pressed
    83. break;
    84. for (sec2=t;sec2<=99; sec2++)
    85. {
    86. if (stop_pin == 0 ) //Check if stop pin is pressed
    87. break;
    88. t=0;
    89. display(sec1,sec2);
    90. }
    91. }
    92. stop_pin = 1;
    93. s = sec1;
    94. t = sec2;
    95.  
    96. while ( start_pin != 0 && reset_pin != 0 ) //Check if start pin or reset pins are not pressed
    97. {
    98. display(sec1,sec2);
    99. }
    100.  
    101. if (start_pin == 0) //Check if start pin is pressed
    102. {
    103. goto stopwatch;
    104. }
    105. else
    106. {
    107. if (reset_pin == 0 ) //Check if reset pin is pressed
    108. {
    109. s = t = 0;
    110. goto start;
    111. }
    112. }
    113. }
    114. }
    115.  
    116.  
     
    Last edited by a moderator: Mar 13, 2011
  2. n1ist

    Active Member

    Mar 8, 2009
    171
    16
    - You need to debounce your pushbuttons - google "Ganssle debounce" for more info
    - At the top of main(), start by displaying 0. That will get past your init issue.
    - To communicate with a PC, use the UART. You will need to add a level shifter (MAX232 or similar) between the AT89S52 and the PC.
    - You can add multiple displays if you have enough I/O pins and processor time to handle the multiplexing, or you could use a display driver chip.
    - Please post code within CODE tags. Otherwise the formatting and indenting gets lost, and the code becomes very hard to read.
    - You may want to use timer interrupts for the counting; that will simplify things and make the counting more accurate
    - In general, using GOTO is frowned upon
    - Depending on your compiler, your mplex_delay() routine might be totally optimized away.
    - Right now, when your timer fires off, you still can have up to four digits of mplex_delay() plus a bunch of other code before you start the timer again.
    /mike
     
  3. sgtom

    Thread Starter New Member

    Mar 13, 2011
    4
    0
    Thank you for your reply.

    Your time and help is greatly appreciated.

    What is code tags?


    programming is weak so any help would be great!

    "I would also like to display max 9 seconds and rest miliseconds, etc (9.999 seconds) accurately instead of current setup. any help on coding will be great!"


    - In general, using GOTO is frowned upon

    ANY ALTERNATIVES?

    - Depending on your compiler, your mplex_delay() routine might be totally optimized away.

    USING KEIL COMPLIER
     
Loading...