Hi,
or another approach, just do not start timer before you add a first value into the OC register.
For example, see bellow. However, as Edward says and I can confirm it is good approach if you clear interrupt flags before interrupt is enabled to avoid servicing accidental flag.
******************************************************************************
Description: SW-XET256-ECT-OC-CW47
Documentation: MC9S12XEP100RMV1.pdf
This software is classified as Engineering Sample Software.
******************************************************************************
- the board includes 16MHz crystal / the MCU's bus frequency 8MHz is used
- the sample software does:
. TC0 interrupt is set to be generated with constant period.
==============================================================================*/
//******************************************************************************
#include <hidef.h>
#include <MC9S12XET256.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xet256"
//******************************************************************************
//==============================================================================
// SETUP DEFINITIONS
//==============================================================================
#define INTERVAL 50000
//==============================================================================
// COMMON DEFINITIONS
//==============================================================================
#define UBYTE unsigned char
#define UWORD unsigned int
//==============================================================================
// FUNCTION PROTOTYPES
//==============================================================================
void ECT_Init(void);
//==============================================================================
// Variables section
//==============================================================================
// NA
//==============================================================================
// CODE section
//==============================================================================
void ECT_Init(void)
{
ECT_TSCR2 = 0x07; // TOV disable, no reset timer from T7, prescaller=128
//--- channel 0 setup ---------------
ECT_TCTL1 = 0x00; // timer 7~4 no output compare action
ECT_TCTL2 = 0x01; // timer 3~1 no output compare action, T0 toggle on compare
ECT_TIOS = 0x01; // channel 0 is output compare
ECT_OCPD = 0xFE; // enable OC at timer 0 port
//-----------------------------------
ECT_TC0 = (UWORD) (INTERVAL + ECT_TCNT); // base setup of the first OC at T0
//-----------------------------------
ECT_TIE = 0x01; // enable interrupt from channel 0
//---------------------------------
ECT_TSCR1 = 0xE0; // enable timer,stop in wait,stop in freeze, no fast flag clear, legacy timer
}
//******************************************************************************
#pragma CODE_SEG NON_BANKED
//-------------------------------------
interrupt 8 void TC0_Isr(void)
{
ECT_TC0 = INTERVAL + ECT_TCNT; // set new period end
ECT_TFLG1 = 0x01; // clear interrupt flag
}
//-------------------------------------
#pragma CODE_SEG DEFAULT
//******************************************************************************
// void main(void)
//******************************************************************************
void main(void)
{
//----------------------------------------
ECT_Init();
//----------------------------------------
EnableInterrupts;
//----------------------------------------
for(;;)
{
}
}
//******************************************************************************
Best regards,
Ladislav