
155
CHAPTER 6
16-BIT TIMER/EVENT COUNTER 0
User’s Manual U12790EJ2V0UD
6.5.5 PPG output
/******************************************************************************/
/*
*/
/*
Timer 0 PPG mode setting example
*/
/*
Cycle set to 98 as intervalTM0
*/
/*
Active period set to 49 as active_time
*/
/*
Array ppgdata prepared as data area for rewriting
*/
/*
[0]: Active period (0000: no change, 0xffff: stop)
*/
/*
[1]: Cycle (0000: no change)
*/
/*
ppgdata to be checked at every INTTM00, and changed if required.
*/
/*
Therefore, if change is required, set the change data in ppgdata.
*/
/*
When changed, ppgdata cleared to 0000.
*/
/*
*/
/******************************************************************************/
#pragma sfr
#pragma EI
#pragma DI
#define intervalTM0 98
/* Cycle data to be set to CR00 */
#define active_time 49
/* Initial value data of CR01 */
#pragma interrupt INTTM00 ppgint rb2
unsigned int ppgdata[2];
/* Data area to be set to timer 0 */
void main(void)
{
PCC = 0x0;
/* Set high-speed operation mode */
ppgdata[0] = 0;
ppgdata[1] = 0;
/* Set port */
P3 = 0b11111101;
/* Clear P31 */
PM3.1 = 0;
/* Set P31 to output */
/* Set interrupt */
TMMK00 = 0;
/* Cancel INTTM00 interrupt mask */
/* Set timer 0 */
PRM0 = 0b00000010;
/* Count clock is fx/2^6 */
CRC0 = 0b00000000;
/* Set CR00 and CR01 to compare register */
CR00 = intervalTM0;
/* Set initial value of cycle */
CR01 = active_time;
/* Set initial value of active period */
TOC0 = 0b00010111;
/* Inverted on match between CR00 and CR01,
initial value L */
TMC0 = 0b00001100;
/* Clear & start on match between TM0 and CR00 */
EI();
while(1);
}
/* Timer 0 interrupt function */
void ppgint()
{
unsigned int work;
work = ppgdata[0];
if (work != 0)
{
CR01 = work;
ppgdata[0] = 0;
if (work == 0xffff)
{
TMC0 = 0b00000000;
/* Stop timer */
}
work = ppgdata[1];
if (work != 0)
{
CR00 = work;
ppgdata[1]=0;
}