|
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#include "derivative.h" /* include peripheral declarations */
#include "common.h"
int main(void)
{
SysInit(); // initialize system clock and SysTick timer
// FTM2_CH0 Pin Select
SIM_PINSEL1 |= SIM_PINSEL1_FTM2PS0(2); // select FTM2_CH0 on PTF0
SIM_SCGC |= SIM_SCGC_FTM2_MASK; // enable FTM clock
FTM2_C0SC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK; // edge-aligned PWM, high-true pulse
FTM2_C0V = 2000; // set channel value, PWM duty = 20%
FTM2_MOD = 9999; // set modulo value, PWM frequency = 2KHz
FTM2_SC |= FTM_SC_CLKS(1) | FTM_SC_PS(0); // FTM clock = system clock/OUTDIV3/PS = 40/2/1 = 20MHz
NVIC_ISER |= 1 << (INT_FTM2 - 16); // enable FTM interrupt in NVIC register
FTM2_SC |= FTM_SC_TOIE_MASK; // enables FTM overflow interrupt
for(;;)
{
}
return 0;
}
void FTM2_IRQHandler(void)
{
static int cnv = 0;
FTM2_SC &= ~FTM_SC_TOF_MASK; // clear FTM interrupt flag
FTM2_C0V = cnv; // change duty
cnv += 5;
if (cnv > 10000)
{
cnv = 0;
}
}
|
|