#include "SKEAZ1284.h"

#define FTM2_MOD_VALUE 0xFFFF

volatile uint16_t rising_edge_time; volatile uint16_t falling_edge_time; volatile uint16_t pulse_width; volatile uint32_t frequency;

void FTM2_IRQHandler(void) { // Check if rising edge interrupt flag is set if (FTM2_C3SC & FTM_CnSC_CHF_MASK) { rising_edge_time = FTM2_C3V; // Clear the interrupt flag FTM2_C3SC &= ~FTM_CnSC_CHF_MASK; } // Check if falling edge interrupt flag is set else if (FTM2_C3SC & FTM_CnSC_CHF_MASK) { falling_edge_time = FTM2_C3V; // Calculate pulse width pulse_width = falling_edge_time - rising_edge_time; // Calculate frequency frequency = FTM2_MOD_VALUE / pulse_width; // Clear the interrupt flag FTM2_C3SC &= ~FTM_CnSC_CHF_MASK; } }

int main(void) { // Enable clock for Port D SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Enable clock for FTM2 SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK;

// Configure PTD3 as FTM2 channel 3
PORTD_PCR3 = PORT_PCR_MUX(4);

// Configure FTM2 for input capture rising edge and falling edge interrupt
FTM2_MODE |= FTM_MODE_WPDIS_MASK; // Disable write protection
FTM2_MODE |= FTM_MODE_FTMEN_MASK; // Enable FTM
FTM2_SC = FTM_SC_CLKS(0x01);      // Select system clock as FTM clock source
FTM2_C3SC = FTM_CnSC_ELSB_MASK;   // Capture on falling edge
FTM2_C3SC |= FTM_CnSC_CHIE_MASK;  // Enable channel interrupt

// Enable FTM2 interrupt
NVIC_EnableIRQ(FTM2_IRQn);

while (1)
{
    // Do nothing
}

return 0;

}

使用 SKEAZ1284.h 头文件编写 PTD 端口 FTM2 通道 3 中断测量方波频率和占空比程序

原文地址: https://www.cveoy.top/t/topic/pFQ3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录