Page 1 of 1

Reading state of DMA Timer 0 input pin

Posted: Tue Sep 06, 2011 7:42 am
by mmk-tsm
Hello all,
I am using DMA Timero 0 as an input capture pin, to time the signal on timer T0IN, J2 pin 31. I have configured Timer 0 input as a timer pin. I also use the reference compare feature of the timer. (J2[31].function( PINJ2_31_TIN0 ); //setup pin 31 for DTIN0 input)

Everything works, but I cant manage to read the state of the input signal. I need to read state (hi/lo) at the ref compare times. I imagine I should still be able to read the state of the input even though it is not configured as gpio???
I am using the following to try and read the pin state:

BYTE ReadDT0IN( void )
{
return (sim.gpio.ppdsdr_timer & 0x02); // return b1 of timer reg ( DT0IN).
};

Is this possible to do, and if so where am I going wrong please??

Re: Reading state of DMA Timer 0 input pin

Posted: Tue Sep 06, 2011 9:25 am
by mmk-tsm
An update,
I have tried using the pin class to read the pin state, T0IN. - bool bPinState2 = J2[31];

This works if the pin is initialised for GPIO, but not if pin cofigured as timer input.

And, I cant seem to change the function dynamically. If I initialise at reset the pin function to GPIO, I can read state. If however, it is initialised for T0IN, then if I set it to gpio later, I then cant read the pin state. :?

Re: Reading state of DMA Timer 0 input pin

Posted: Tue Sep 06, 2011 10:04 am
by mhoyt
I remember running into the same problem when using the DMA timer to read the pulse widths from a temperature sensor. The solution is simple. If you set up the DMA timer to interrupt on either rising or falling edge only, you will know the state of the line at the time of the capture interrupt. It is then a simple matter of toggling the edge capture edge bits in the isr prior to the next edge. Here is my working isr (t1 and t2 are external DWORDs):

Code: Select all

	DWORD t1, t2;

	INTERRUPT (TMR0_isr, 0x2700)
	{
		static DWORD last_capture;
		sim.timer[0].ter= 1;	//clear the event flag
		if ((sim.timer[0].tmr & 0xc0) == 0x40)	//captured rising edge?
		{
			// capture value is duration of low TMP03 output.
			t2= sim.timer[0].tcr - last_capture;
		}
		else	//captured falling edge... capture value is duration of high TMP03 output.
		{
			t1= sim.timer[0].tcr - last_capture;
		}
		sim.timer[0].tmr ^= 0xc0;	//toggle edge bits so we detect other edge next
		last_capture= sim.timer[0].tcr;
	}
Hope this helps!

--Mike

Re: Reading state of DMA Timer 0 input pin

Posted: Wed Sep 07, 2011 8:17 am
by mmk-tsm
Thanks Mike,
I have adapted your suggestion to our application, and it works fine.
Cheers,
Michael.