
181
CHAPTER 8 16-BIT TIMER/EVENT COUNTERS 00, 01
User
’
s Manual U14260EJ3V1UD
8.5.4 Pulse width measurement by restart
/**************************************************************************/
/* */
/* Timer 00 operation sample */
/* Pulse width measurement example by restart */
/* Measurement results up to 16 bits, not to be checked for errors */
/* data[0]: End flag */
/* data[1]: Measurement results (pulse width) */
/* data[2]: Previous read value */
/* */
/**************************************************************************/
#pragma sfr
#pragma EI
#pragma DI
#pragma interrupt INTTM010 intervalint rb2
unsigned int data[3];
/* Data area */
void main(void)
{
unsigned int length;
PCC = 0x0;
data[0] = 0;
data[1] = 0;
data[2] = 0;
/* Set high-speed operation mode */
/* Set port */
/* Set P70 as input */
/* Set interrupt */
/* Cancel INTTM010 interrupt mask */
/* Set timer 00 */
/* Both rising and falling edges */
/* Count clock is fx/2^6 */
/* Set CR010 to capture register */
/* Clear & start at TI000 valid edge */
PM7.0 = 1;
TMMK010 = 0;
PRM00 = 0b00110010;
CRC00 = 0b00000100;
TMC00 = 0b00001000;
EI();
while(1){
if(data[0] != 0)
{
/* Dummy loop */
/* Wait for TI000 measurement completion */
TMMK010 = 1;
/* Disable INTTM010 for exclusive
operation */
/* Cycle calculation based on
measurement results */
/* Clear end flag */
/* Exclusive operation completed */
length = data[1]+data[2];
data[0] = 0;
TMMK010 = 0;
}
}
}
/* Timer00 interrupt function */
void intervalint()
{
/******************************************************/
/* */
/* Define variables required for interrupt here */
/* */
/******************************************************/
data[2] = data[1];
data[1] = CR010;
data[0] = 0xffff;
/* Update old data */
/* Update read value */
/* Set measurement completion flag*/
/********************************************************/
/* */
/* Describe processing required for interrupt below */
/* */
/********************************************************/
}