Need help writting program for my project.

Discussion in 'Programmer's Corner' started by sgtom, Mar 21, 2011.

  1. sgtom

    Thread Starter New Member

    Mar 13, 2011
    4
    0
    Dear All,

    I would like the times to be sent to accumulator register when i press stop button.

    how do i go about writting a code in C for that?

    I just need the times to be stored in some register and move it to the SBUF register so we can send the times serially.

    Please help.

    Kindly find my original code below:

    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 21, 2011
  2. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
  3. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    Also, you probably want to debounce your switch. And use interrupts.
     
    Last edited: Mar 23, 2011
Loading...