
151
CHAPTER 6
16-BIT TIMER/EVENT COUNTER 0
User’s Manual U12790EJ2V0UD
6.5.2 Pulse width measurement by free-running counter and one capture register
/******************************************************************************/
/*
*/
/*
Timer 0 operation sample
*/
/*
Pulse width measurement example by free-running and CR01
*/
/*
Measurement results up to 16 bits and not 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 INTTM01 intervalint rb2
unsigned int data[3];
/* Data area */
void main(void)
{
unsigned int length;
PCC = 0x0;
/* Set high-speed operation mode */
data[0] = 0;
data[1] = 0;
data[2] = 0;
/* Set port */
PM3.2 = 1;
/* Set P32 as input */
/* Set interrupt */
TMMK01 = 0;
/* Cancel INTTM01 interrupt mask */
/* Set timer 0 */
PRM0 = 0b00110010;
/* Both rising and falling edges for TI00 */
/* Count clock is fx/2^6
*/
CRC0 = 0b00000100;
/* Set CR01 to capture register */
TMC0 = 0b00000100;
/* Start in free-run mode */
EI();
while(1){
/* Dummy loop */
while(data[0] == 0);
/* Wait for measurement completion */
DI();
/* Prohibit interrupt for exclusive operation */
length = data[1];
/* Read measurement results */
data[0] = 0;
/* Clear end flag */
EI();
/* Exclusive operation completed */
}
/* Timer 0 interrupt function */
void intervalint()
{
unsigned int work;
/*****************************************************/
/*
*/
/* Define variables required for interrupt here
*/
/*
*/
/*****************************************************/
work = CR01;
/* Read capture value */
data[1] = work - data[2];
/* Calculate and update interval */
data[2] = work;
/* Update read value */
data[0] = 0xffff;
/* Set measurement completion flag*/
/***********************************************************/
/*
*/
/* Describe processing required for interrupt below
*/
/*
*/
/***********************************************************/
}