Page 1 of 1

Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 5:00 pm
by MasterFrmMO88
95% of this post is explanation that is not necessary to answer the question but it gives background as to why I have the question. If you want to skip it all, the question is in the last paragraph.

I have a 5270 that is connected to a rather simple circuit. The circuit takes a digital input to switch a low current circuit which, in turn, switches a high current circuit. The response time on each of these switches is in the range of nano-seconds but there is still a delay. The way this works is the 5270 switches this circuit on and, if the circuit is complete, a current detection pin on the 5270 detects the complete circuit. As a precautionary measure, I have the 5270 measure current without switching the circuit on to make sure it comes up low, signifying that there are no short circuits. Then it switches the circuit on and measures current to see that it is high, so there is a complete circuit. Then it switches the circuit back off, checks that the current detection pin is low to insure that the circuit is still in operating condition. If all three of these conditions are met, it returns a positive test result. To help explain this a little better, here's the code for a test function:

Code: Select all

int Test_01(int a){

	a = 0;
	J2[15].clr();
	OSTimeDly(1);
	if(!J2[13]){
		J2[15].set();
		OSTimeDly(1);
		if(J2[13]){
			J2[15].clr();
			OSTimeDly(1);
			if(!J2[13]){
				a++;
			}
		}
	}
	J2[15].clr();
return a;
}
J2[13] is the current detections pin, J2[15] is the pin being switched on and off. a is the test result: 0 = fail, 1 = pass.
Each time the circuit is switched, on or off, I have to include the OSTimeDly(1) so the circuit has time to respond before Pin 13 takes a reading.

My current TICKS_PER_SECOND is set to 20 so each test takes about 3/20 of a second or 0.15 seconds. The circuit takes about 600 nanoseconds to respond or 0.0000006 Seconds. So this entire test process could be completed in 1800 nanoseconds as opposed to 0.15 seconds.
Normally this would be negligible but one single test procedure could test upwards of 4000 of these circuits. With the current test code I have, that's 10 minutes or more. When it could be as short as 0.008 seconds to test all 4000 of them, neglecting any external calls that need to be made to perform the test.

I've done the math and, if it's correct, a 147MHz processor (the one on the 5270) can wait about 80 cycles for this circuit to react before testing for current. So there should be a way to program a wait code in that is shorter than 1/20th of a second.

So this may be a pretty simple question, but is there a way to make a program on my 5270 wait for a shorter time period other than using OSTimeDly? Preferably code that would run off of the processor clock as opposed to an OS timer. I mean, I know I can change my TICKS_PER_SECOND, though I'd rather not because it is handy in other parts of my code as 20 per second. So short of doing that or creating a function that has the processor add 1+1 100 times, is there an easier way to create a shorter delay?

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 5:02 pm
by jdal
Some time ago i wrote this for the MOD5270.

Code: Select all

//*********************************************************************
//************************  Delay MS Routine  *************************
//*********************************************************************
void delay_ms(WORD time)
{
    sim.pit[1].pcsr = 0x217;                    // CSR = 0000 0001 0001 0111
    sim.pit[1].pmr = 0x4800;                    // Reload 0x4800 for 1ms counter

	do
	{
        while(!(sim.pit[1].pcsr & 0x0004));     // stay here while bit is clear
        sim.pit[1].pcsr |= 0x0004;              // clear the Bit by setting the bit ;) 
	
	}while(--time >0);		    
}

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 5:17 pm
by MasterFrmMO88
Thanks a lot! I'll give it a go.

EDIT: Works great! Thanks again! :D

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 5:36 pm
by jdal
Welcome, but know that it is a blocking call, no other tasks will run until its done. There is probably a more elegant way to do it.

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 5:47 pm
by Ridgeglider
For an interrupt based (non-blocking) solution see Larry's well-documented and tested stopwatch code for a variety of short delay routine types:
http://forum.embeddedethernet.com/viewt ... ?f=7&t=397

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 6:00 pm
by MasterFrmMO88
Thanks for the link. As of this moment, I only have the one thread running on my app so I can use a blocking delay. But I like the idea of the stopwatch functionality.

Re: Shorter wait time than OSTimeDly?

Posted: Tue Jan 10, 2012 6:59 pm
by pbreed
Take a look here for a way to do it that blocks.

http://www.netburner.com/downloads/mod5 ... 70-PIT.pdf

Use the pit timer and setup whatever interval you want.....

Re: Shorter wait time than OSTimeDly?

Posted: Wed Jan 11, 2012 11:43 am
by tod
Since this seems to come up regularly I enhanced the FAQ entry How Do I Generate Ticks Faster Than 50 MS with the links posted in this thread.