Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Two parallel time measurements.

Status
Not open for further replies.

reachrishikh

New Member
Hi,

We need to create a timer system. We already have the pseudo-code, logic, requirements and everything sorted out. But our electronics engineer cannot figure out what microcontroller to use for this, and what language to code the appropriate microcontroller selected in. We're thinking of using an 8051 for this, and would like to know if it would do the job.

You can imagine we're measuring the time taken to cross between two given points. It's a race where athletes have to cross this race-distance (the distance between the two given points) and have their time recorded. Ideally, there should be one athlete crossing that distance at a time to get his time recorded, but since the course is large enough to accommodate them such that they don't get in the way of each other, and since we have to record the time of a large number of people, so to save overall time, we can have two people on the race-distance at any given point of time.

Also, if an athlete covers the race-distance within a certain pre-set amount of time, a beep sounds.


I'm listing out the description and pseudo-code below.

Would someone please be kind enough to tell us what microcontroller to use, and what language would be required for it. And yeah, some code in the target language would also be appreciated, since I don't speak low-level.



We have IR pair triggers at two points, both triggering a Start and a Stop each. When the Start is triggered, it needs to start counting time, and displaying it on our LED display in Hours, Minutes, Seconds, Milliseconds format. When the stop is triggered, the time counter stops, and the display freezes on the last incremented time, till it is reset by another Start trigger.
There are two such displays, meaning we can have two timers counting simultaneously, triggered by the same physical IR pair Start and Stop triggers. Timer 1 is connected to Display 1, and Timer 2 is connected to Display 2.


pseudo-code:

variable latch //this variable stores which timer was triggered last. this can even be a boolean value with 0 assigned to timer_1 and 1 assigned to timer_2

constant preset_time = //assign pre-set time value here in hours:minutes:seconds:milliseconds format

Start trigger event handler

if (both timers running)
{
do nothing
}
else
{
if (both timers stopped)
{
trigger timer_1 //the first display device starts showing time being counted upwards
add timer_1 to latch
}
elseif (timer_1 is running)
{
trigger timer_2 //the second display device starts showing the time being counted upwards
add timer_2 to latch
}
elseif (timer_2 is running)
{
trigger timer_1
add timer_1 to latch
}
}

Stop trigger event handler
if (both timers running) //if both timers are running at the same time, stop the one which was triggered first, which is established by seeing which was the last trigger added to the latch, then stop the other one which is not in the latch
{
if (timer_1 in latch)
{
stop timer_2
if (timer_2.time <= preset_time)
{
sound beep
}
}
elseif (timer_2 in latch)
{
stop timer_1
if (timer_1.time <= preset_time)
{
sound beep
}
}
}
else //only one timer is running, then stop whichever timer is running
{
if (timer_1 is running)
{
stop timer_1
if (timer_1.time <= preset_time)
{
sound beep
}
}
elseif (timer_2 is running)
{
stop timer_2
if (timer_2.time <= preset_time)
{
sound beep
}
}
}


As an example to put things into perspective, if I were to do this in Visual Basic Dot Net (a language I am familiar with), I would code it like this:

Dim latch as Boolean = Nothing 'this variable stores which timer was triggered last. is a boolean value with 0 assigned to timer_1 and 1 assigned to timer_2

Const presettime as integer = 60000 'store time in milliseconds.

Dim WithEvents Timer_1 As New Windows.Forms.Timer()
Dim WithEvents Timer_2 As New Windows.Forms.Timer()

Sub StartTrigger()
If Timer_1.Enabled = True AND Timer_2.Enabled = True Then
'do nothing
Else
If Timer_1.Enabled = False AND Timer_2.Enabled = False Then
Timer_1.Start()
latch = 0
ElseIf Timer_1.Enabled = True Then
Timer_2.Start()
latch = 1
ElseIf Timer_2.Enabled = True Then
Timer_1.Start()
latch = 0
End If
End If
End Sub

Sub StopTrigger()
If Timer_1.Enabled = True AND Timer_2.Enabled = True Then
If latch = 0 Then
Timer_2.Stop()
ComparePresetTime(Timer_2.Interval)
ElseIf latch = 1 Then
Timer_1.Stop()
ComparePresetTime(Timer_1.Interval)
Else
If Timer_1.Enabled = True Then
Timer_1.Stop()
ComparePresetTime(Timer_1.Interval)
ElseIf Timer_2.Enabled = True Then
Timer_2.Stop()
ComparePresetTime(Timer_2.Interval)
End If
End Sub

Sub ComparePresetTime(ByVal TimeToCompareInMilliseconds As Integer)
If TimeToCompareInMilliseconds <= presettime Then
'sound beep
End If
End Sub


However, like I said above, I am not familiar with low level languages, and we need help with machine-level code for the microcontroller we select that is appropriate for this job.

Once we know the microcontroller model to use and have the code ready and programmed into it, we will try setting the hardware of the timer system up, and also need some interfacing help to connect the microcontroller to show the countdown on some large LED 7 segment displays. These LED displays need to be large enough to be seen from a distance of 50 feet in broad daylight.

Now our electronics engineer knows how to wire smaller LED displays, but we're stuck at the interfacing aspect when concerned with larger displays. If we get stuck while interfacing the larger LEDs, I guess I will have to be back for some more advice.

Would anyone please be kind enough to help us out? Thanks.
 
It shouldn't take more than a few minutes max to cover the distance, but the hours display is just there as a fail-safe. And if we ever need to adapt this device to another application, we won't have to go back to the drawing board.

And I'm guessing incorporating displaying the hours within this design isn't going to take much work in addition to the minutes, seconds and milliseconds (rounded to a tenth of a second).
 
Looking at your persudo code and your ability to write that, i surgest you look into the PICAXE chip range of micros, as your code is almost identical to the basic format used with the Picaxe.

They are not the fastest micro on the market but very easy to code with lots of built in features.

If you want good timing a DS-1307 real time clock chip would be a good combination with the picaxe.
A quick google search and you will find a whole forum dedicated to the picaxe and much good help if you choose to use this micro.

As for your display, some time back i think it was Sureelectronics on ebay use to do a jumbo led displays for score boards etc, and might be worth a look.

Pete.
 
Uh-oh, I just checked my first post again, and it seems vBulletin has messed up all the indentations in my code upon posting.

Pete, thanks for the reply. But I cannot really make do with a processor that's slow, we can't afford a time-delay in processing messing up the actual timing recorded of the athlete. And I think adding in an additional item in the form of that clock would get complicated.
(Plus, I'll admit, I'm not looking forward to posting this on yet another forum, I must have switched three electronics forums before I found my way here, and only posted because this board looked very active.)

BTW, I think my pseudo-code is similar to C variants/Java/PHP/etc and is written from an Object-Oriented point of view. I don't think that would work on these type of microcontrollers, do you? They're usually written in some machine-level language, aren't they? And I suppose for a small requirement such as a time-calculation, I really wouldn't want to spend more money on a more complicated chip.


Do you suppose an 8051 would work for our purpose? Because the guy assisting me in this (my electronics engineer I mentioned above) is a second year engineering student, and his knowledge in this is limited. And he expressed doubts over whether an 8051 would be capable of two simultaneous calculations.
 
Personally i would use the Picaxe, and if you call some where between 8mhz and 64mhz slow (depending on the chip chosen) than i think you have other problems to deal with.

To use a RTC is very simple and accurate, compared to just advancing a counter in a loop.

The only reason i said about the picaxe forum is the picaxe is not very well supported on this forum, but is the holly grail on the rev-ed forum.

It would have to be the simplest micro to interface/program on the market.

I dont care what you use, but due to the lack of help offered thus far thought it would be a easy way to acheive the results you require.

As for using the RTC you would simply do a read of time from the RTC when the start beam was broken and then another read of time when the finish beam was broken, then subtract one from the other.
That way you have a dedicated time keeping device and the micro just reads the time from it.

Pete.
 
As for using the RTC you would simply do a read of time from the RTC when the start beam was broken and then another read of time when the finish beam was broken, then subtract one from the other.
That way you have a dedicated time keeping device and the micro just reads the time from it.
Okay, but this just shows me the time difference and displays the result after the athlete has finished the course on the display. This approach won't show me an upward counting timer on the display as the athlete runs the course, will it?

And hey, I'm not calling the chip slow, I didn't even know what clock speed it ran at. You just said it was a slower chip, so I assumed it was slow. How slow, I did not know. And yeah, it should be able to run two simultaneous time calculations within a tenth of a second accuracy.
 
Alright, I just managed to write some of my own code for the above in C, but I still need someone's help in figuring out what to do next.
First, the code:
Code:
#include "mxapi.h"
void main(void)
{
   int t1milsec;
   int t1sec;
   int t1min;
   int t1started;
   int t1totalmilsec;

   int t2milsec;
   int t2sec;
   int t2min;
   int t2started;
   int t2totalmilsec( int timetocompare );

   int latch;
   int presettime = 60000;  /* preset course completion time in milliseconds */

   void timer1();
   void timer2();
   void comparepresettime();

   /*
   t1milsec=0;
   t1sec=0;
   t1min=0;

   t2milsec=0;
   t2sec=0;
   t2min=0;
   */

   /* assuming that the code in the main() function runs in an infinite loop as long as a current is supplied to the circuit, this means that the values of the variables above will keep getting reset infintely, even in the middle of a timer-run. therefore commenting them out at the moment based on my assumption, and letting the loop itself assign an initial value to the variable and increment the counter, keep the values when the loop is broken and keep displaying them on the LEDs, and reset them again when the loop/timer is started again. */

   TRISB4=1; /* assign pin B4 and B5 below as Input */
   TRISB5=1;

   lcd_init();

   while(RB5==0);   /* this is the start trigger */

   if ( t1started == 1 AND t2started == 1 )
   {
      /* do nothing */
   }
   else if ( t1started == 0 AND t2started == 0 )
   {
      latch = 0;
      timer1();
   }
   else if ( t1started == 1 )
   {
      latch = 1;
      timer2();
   }
   else if ( t2started == 1 )
   {
      latch = 0;
      timer1();
   }
   end();
}



void timer1()
{
   t1started = 1;
   t1totalmilsec = 0;
      for(t1min=0; t1min<=59; t1min++) 
      {
         for(t1sec=0; t1sec<=59; t1sec++)
         {
            for(t1milsec=0; t1milsec<=99; t1milsec++)
            {
               lcd_instruction(GOTO_LINE1+0);
               lcd_digits(t1min, BASE_10, LEADING_ZEROS, 2);
               lcd_text(":");
               lcd_digits(t1sec, BASE_10, LEADING_ZEROS, 2);
               lcd_text(":");
               lcd_digits(t1milsec, BASE_10, LEADING_ZEROS, 2);
               t1totalmilsec += 10;
               delay_ms(10);
               if( RB4 == 1 )
               {
                  if ( t1started == 1 AND t2started == 2 )
                  {
                     if ( latch == 1 )
                     {
                        break;
                        t1started = 0;
                        comparepresettime(t1totalmilsec);
                        /* keep displaying t1min, t1sec, t1milsec as-is, with the same values as when the loop was broken, till it is reset by the loop/timer being started again */
                        exit(); /* I sincerely hope this statement exits the entire function immediately, including all the nested for loops, and the nested if statements. */
                     }
                  }
                  else if ( t1started == 1 )
                  {
                     break;
                     t1started = 0;
                     comparepresettime(t1totalmilsec);
                     /* keep displaying t1min, t1sec, t1milsec as-is, with the same values as when the loop was broken, till it is reset by the loop/timer being started again */
                     exit(); /* I sincerely hope this statement exits the entire function immediately, including all the nested for loops, and the nested if statements. */
                  }
               }
            }
         }
      }
}


void timer2()
{
   t2started = 1;
   t2totalmilsec = 0;
      for(t2min=0; t2min<=59; t2min++) 
      {
         for(t2sec=0; t2sec<=59; t2sec++)
         {
            for(t2milsec=0; t2milsec<=99; t2milsec++)
            {
               lcd_instruction(GOTO_LINE1+0);
               lcd_digits(t2min, BASE_10, LEADING_ZEROS, 2);
               lcd_text(":");
               lcd_digits(t2sec, BASE_10, LEADING_ZEROS, 2);
               lcd_text(":");
               lcd_digits(t2milsec, BASE_10, LEADING_ZEROS, 2);
               t1totalmilsec += 10;
               delay_ms(10);
               if( RB4 == 1 )
               {
                  if ( t1started == 1 AND t2started == 2 )
                  {
                     if ( latch == 0 )
                     {
                        break;
                        t2started = 0;
                        comparepresettime(t2totalmilsec);
                        /* keep displaying t2min, t2sec, t2milsec as-is, with the same values as when the loop was broken, till it is reset by the loop/timer being started again */
                        exit(); /* I sincerely hope this statement exits the entire function immediately, including all the nested for loops, and the nested if statements. */
                     }
                  }
                  else if ( t2started == 1 )
                  {
                     break;
                     t2started = 0;
                     comparepresettime(t2totalmilsec);
                     /* keep displaying t2min, t2sec, t2milsec as-is, with the same values as when the loop was broken, till it is reset by the loop/timer being started again */
                     exit(); /* I sincerely hope this statement exits the entire function immediately, including all the nested for loops, and the nested if statements. */
                  }
               }
            }
         }
      }
}


void comparepresettime( int timetocompare )
{
   if ( timetocompare <= presettime )
   {
      /* sound beeep/buzzer */
   }
}

Assumptions -
The pins B4 and B5 on the microcontroller are connected to a stop and start button each.
The output has been written from an LCD point of view. (Later, we will need to change the output according to the 7-Segment LEDs we purchase).
When RB4 or RB5 have a value of 1 assigned to them, it means the button has been pressed.

My question once again - will this work on an 8051? Will it run the two simultaneous calculations?

Second, the first function, the main() block, how many times does this loop by itself when the circuit is running? Does it only run once, or does it run in continuous loop? Because if it only runs once or it needs an external trigger to make it run again after executing once, means after the first person triggers the timer to start, it won't be looking for the start timer to trigger again.

Third, what would be the best way for me to exit the innermost loop when we register that the stop button has been pressed?

Fourth, what would be the most efficient way to listen for the RB4 and RB5 button triggers?

Fifth, how do I work in the RTC into this code?

Sixth, is the call to mxapi.h for this example correct for the 8051 chip? Because I obtained part of the code from another stopwatch example I found on the net, and I'm not familiar with either C libraries, or individual microcontroller libraries.


Alright and Mods, if this thread has a better chance of receiving a reply in the Microcontroller or 8051 boards, could you please move this there?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top