Sorry I am not good in ASM code but I have a C code that maybe can help you
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define OVF_COUNTS 50
unsigned char counts = OVF_COUNTS; //number of Timer Overflows before increasing the pulse-width
unsigned char up_down = 0; //0 for up, 1 for down
unsigned int capture;
void main(void) {
EnableInterrupts;
/* include your code here */
/* WDOG_CNT: CNT=0xC520 */
WDOG_CNT = 0xC520; /* First part of the WDG unlock sequence */
/* WDOG_CNT: CNT=0xD928 */
WDOG_CNT = 0xD928; /* Second part of the WDG unlock sequence */
/* WDOG_TOVAL: TOVAL=4 */
WDOG_TOVAL = 0x04;
/* WDOG_CS2: WIN=0,FLG=0,??=0,PRES=0,??=0,??=0,CLK=1 */
WDOG_CS2 = 0x01;
/* WDOG_CS1: EN=0,INT=0,UPDATE=0,TST=0,DBG=0,WAIT=0,STOP=0 */
WDOG_CS1 = 0x00; /* Disable watchdog */
//PTG0's Output Enable Register is set to enable output
PORT_PTGOE_PTGOE0 = 1 ;
//PTG0 pin is driven low (LED ON)
PORT_PTGD_PTGD0 = 0;
// FTM clock = BUSCLK = 8MHz, Overflow Interrupt Enabled
FTM2_SC = FTM2_SC_CLKS0_MASK | FTM2_SC_TOIE_MASK;
FTM2_MOD = 8000; // PWM frequency is about 1 KHz
// Configure channel 0 to PWM mode (high-true pulses)
FTM2_C0SC = FTM2_C0SC_ELSB_MASK | FTM2_C0SC_MSB_MASK;
FTM2_C0V = 80; // channel 0 set to 1%
// FTM clock = BUSCLK = 8MHz
FTM1_SC = FTM1_SC_CLKS0_MASK;
// Configure channel 0 to input capture mode, falling edge, channnel interrupt enable.
FTM1_C0SC = FTM1_C0SC_ELSB_MASK|FTM1_C0SC_CHIE_MASK;
for(;;) {
// __RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
/**************************************************************************************************/
interrupt VectorNumber_Vftm2ovf void FTM2ovfISR (void)
{
//Clear interrupt flag
(void)FTM2_SC;
FTM2_SC_TOF = 0;
counts--;
if (!counts)
{
if (!up_down) //up counting
{
FTM2_C0V += 80;
if (FTM2_C0V == 7920) up_down = 1;
//when reaching 99%, change to down counting
}
else
{
FTM2_C0V -= 80;
if (FTM2_C0V == 80) up_down = 0;
//when reaching 1%, change to up counting
}
counts = OVF_COUNTS;
}
}
/**************************************************************************************************/
interrupt VectorNumber_Vftm1ch0 void FTM1ch0ISR (void)
{
//Clear interrupt flag
(void)FTM1_C0SC;
FTM1_C0SC_CHF = 0;
PORT_PTGD_PTGD0 = ~PORT_PTGD_PTGD0;
capture = FTM1_C0V;
}
/**************************************************************************************************/