Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Stopwatch C code - Urgent help required plz!

Status
Not open for further replies.

sgtom

Junior Member level 1
Joined
Mar 14, 2011
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,492
Dear guys,

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


(C code below) was advised to use new code using timers (help!)


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!

#include<reg51.h>
#define msec 1
unsigned int sec1,sec2;
int sec1_1,sec1_2,sec2_1,sec2_2;

unsigned int digi_val[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x1 0};
sbit dig_ctrl_1=P1^0; // Declare the control pins of seven segments
sbit dig_ctrl_2=P1^1;
sbit dig_ctrl_3=P1^2;
sbit dig_ctrl_4=P1^3;
sbit start_pin = P1^4; // Start pin to start the watch.
sbit stop_pin = P1^5; // Stop pin to stop the watch.
sbit reset_pin = P1^6; // Reset pin to reset the watch.
int s,t;

void mplex_delay(unsigned int time) // Function to provide a time delay of approximatelty one second using Timer 1
{
int i,j;
for (i=0;i<=time;i++)
for(j=0;j<=50;j++);
}

void digi_out(unsigned int current_num)
{
P2=digi_val[current_num];
mplex_delay(msec);
}

void display(unsigned int dig1,unsigned int dig2) // Function to display the digits on seven segmnet. For more details refer seven segment multiplexing.
{
sec1_2=dig1%10;
sec1_1=dig1/10;
sec2_2=dig2%10;
sec2_1=dig2/10;
TMOD=0x01; //Enable Timer 0
TL0=0xFF;
TH0=0xDB;
TR0=1; // Triger Timer 0
while(TF0==0)
{
dig_ctrl_1 = 1;
dig_ctrl_2 = dig_ctrl_3 = dig_ctrl_4 = 0;
digi_out(sec1_1);
dig_ctrl_2 = 1;
dig_ctrl_1 = dig_ctrl_3 = dig_ctrl_4 = 0;
digi_out(sec1_2);
dig_ctrl_3 = 1;
dig_ctrl_2 = dig_ctrl_1 = dig_ctrl_4 = 0;
digi_out(sec2_1);
dig_ctrl_4 = 1;
dig_ctrl_2 = dig_ctrl_3 = dig_ctrl_1 = 0;
digi_out(sec2_2);
}

TR0=0;
TF0=0;
}

void main()
{
while(1)
{
start: // Segment to start the stop watch
start_pin = 1;
stop_pin = 1;
reset_pin = 1;
dig_ctrl_1 = 0;
dig_ctrl_2 = 0;
dig_ctrl_3 = 0;
dig_ctrl_4 = 0;
P2 = 0xFF;
s = t = 0;
while(start_pin == 1)// Check if start pin is pressed
{
display(0,0);
}

stopwatch: // Segment to stop the watch
for (sec1=s;sec1<=99;sec1++)
{
if (stop_pin == 0 ) //Check if stop pin is pressed
break;
for (sec2=t;sec2<=99; sec2++)
{
if (stop_pin == 0 ) //Check if stop pin is pressed
break;
t=0;
display(sec1,sec2);
}
}
stop_pin = 1;
s = sec1;
t = sec2;

while ( start_pin != 0 && reset_pin != 0 ) //Check if start pin or reset pins are not pressed
{
display(sec1,sec2);
}

if (start_pin == 0) //Check if start pin is pressed
{
goto stopwatch;
}
else
{
if (reset_pin == 0 ) //Check if reset pin is pressed
{
s = t = 0;
goto start;
}
}
}
}
 

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.

Ans: Its programming mistake

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?

Ans: there will be some time delay when you press the switch.. you need to make the switch debounce =0.
Key press should be handled as interrupt for accuracy.

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?

Ans: Its reset triggering problem . check your crystal and capacitors or give the 1st command 2 or 3 times ( only the 1st LCD init command) or give some delay in the starting of main().

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!

Ans: will take some time not for urgent.

Question 2.

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

Ans: not a good idea as serial communication may be slow in capturing the event and you need to send data as interrupt.


Question 4.

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

Ans: question not clear... one port of what???? what will 2 controllers measure?? where will you take the output??


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

Ans: at the cost of loosing the accuracy and the purpose of this project.. Not at all a idea to be implemented...
 
Dear Sir,

Thank you for your time and detailed advice.

I am a novice at programming and would appreciate the help in altering my code.

Also i was told to debounce my pushbuttons... any point?

Thank you!!
 

Done for 89s51.. there is no much difference in S52 and 551 controller series...



#include <REG51.H>
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
unsigned char second;
unsigned char keycnt;
unsigned int tcnt;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
ET0=1;
EA=1;
second=0;
P0=dispcode[second/10];
P2=dispcode[second ];
while(1)
{
if(P3^5==0)
{
for(i=20;i>0;i--)
for(j=248;j>0;j--);
if(P3^5==0)
{
keycnt++;
switch(keycnt)
{
case 1:
TH0=0x06;
TL0=0x06;
TR0=1;
break;
case 2:
TR0=0;
break;
case 3:
keycnt=0;
second=0;
P0=dispcode[second/10];
P2=dispcode[second];
break;
}
while(P3^5==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==400)
{
tcnt=0;
second++;
if(second==100)
{
second=0;
}
P0=dispcode[second/10];
P2=dispcode[second];
}
}
 
Dear sir,

Thank you soo much for the circuit and code. I hope it'll solve my problems. However do you have any idea on how I can send my recorded times to my Pc? I have a circuit for serial interface but not sure how to program the microcontroller.

Thanks again
 

Re: Stopwatch C code - Urgent help required plz!

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.
Ans: Its programming mistake

IF YOU CAN PLEASE POINT OUT WHERE THAT ERROR IS TO CORRECT 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?
Ans: there will be some time delay when you press the switch.. you need to make the switch debounce =0.
Key press should be handled as interrupt for accuracy.

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?
Ans: Its reset triggering problem . check your crystal and capacitors or give the 1st command 2 or 3 times ( only the 1st LCD init command) or give some delay in the starting of main().

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!
Ans: will take some time not for urgent.

ANY HELP AGAIN IN ALTERING MY PRESENT CODE 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)
Ans: not a good idea as serial communication may be slow in capturing the event and you need to send data as interrupt.
 

URGENT

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

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.

URGENT
 

you can do serial communication for this purpose. In C you can store the value in SBUF and transmit the same using UART to PC... you can store it in array and then put each element of array into sbuf and transfer the data........
 

hey thanks for the reply, so how exactly do we do it?, if u can please send soem code or modify the code that we have put up!!!!
 

Assumed you have done serial comm initialization.....

unsigned char time[10] = {0x10,0x20........}

for (i=0;i<10;i++)
{
SBUF = time
while (TI == 0);
TI=0
}
 

    V

    Points: 2
    Helpful Answer Positive Rating
according to our code would u be able to tell us where the time value is stores as in which register?, like u know what addresses is it stored in?

Thanks a ton n ways for so much help mate, really appreciate it.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top