• (Kinetis K60)FTM输入捕捉中断


    128分频 FTM2_MOD=624 50000000次 PIT计数定时 算出采集的数为625,0x0271。

    main.c

    #include "project.h"
    
    
    void delay(U32);
    void FTM_PWM_Init(void);
    void FTM_INPUT_INIT(void);
    void PIT0_Init(void);
    
    volatile U16 count;
    
    void main(void)
    {
    
        DisableInterrupts;
    
        UART4_Init(115200);
        FTM_PWM_Init();
        FTM_INPUT_INIT();
        PIT0_Init();
       
        EnableInterrupts;
    
        while(1);
    }
    
    void delay(U32 count)
    {
        while(count--);
    }
    
    void FTM_PWM_Init(void)
    {
     
        PORTA_PCR10 = PORT_PCR_MUX(0x03) | PORT_PCR_DSE_MASK;
       
        SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK;
     
        FTM2_SC |= (FTM_SC_CLKS(1)           // 系统时钟
                   |FTM_SC_PS(7));           // 128 分频
       
        FTM2_C0SC |= (FTM_CnSC_MSB_MASK      // MSB 1
                     |FTM_CnSC_ELSA_MASK);   // ELSA 1
    
       
        FTM2_OUTMASK = 0XFE;                 // 屏蔽CH2~CH7通道 打开CH0 CH1
        FTM2_CNTIN = 0;                      //FTM 计数初始值 
        FTM2_MOD   = 624;                    // 计数的值 频率=分频/(MOD - CNTIN + 1)
           
        FTM2_C0V   = 300;                      // 占空比
    }
    
    void FTM_INPUT_INIT(void)
    {
    
        SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK;                         
        SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;
       
        PORTA_PCR7 = PORT_PCR_MUX(3) | PORT_PCR_DSE_MASK;
    
        FTM0_C4SC |=  ( FTM_CnSC_ELSA_MASK  | FTM_CnSC_CHIE_MASK );        //上升沿触发             
        FTM0_C4SC &= ~( FTM_CnSC_ELSB_MASK  | FTM_CnSC_MSB_MASK | FTM_CnSC_MSA_MASK); 
    
        FTM0_SC = FTM_SC_CLKS(1); 
    
        FTM0_MODE |= FTM_MODE_WPDIS_MASK;
        FTM0_COMBINE = 0;
        FTM0_MODE &= ~FTM_MODE_FTMEN_MASK;
        FTM0_CNTIN = 0;
    
        FTM0_STATUS = 0x00;               //清中断标志位
    
        enable_irq(62);
    }
    
    void PIT0_Init(void)
    {
     
        SIM_SCGC6 |= (1<<23);
       
        PIT_MCR = 0;
        
        PIT_LDVAL0 =  50000000;
       
        PIT_TCTRL0 |= (3); 
       
        enable_irq(68);
       
    }

    isr.h
    中断服务函数头文件

    #include "project.h"
    
    #undef  VECTOR_084
    #define VECTOR_084    PIT0_IRQHandler     //重新定义84号中断为PIT0_IRQHandler中断
    
    #undef  VECTOR_078
    #define VECTOR_078    FTM0_IRQHandler  
    
    
    
    extern void PIT0_IRQHandler(void);            //PIT0 定时中断服务函数
    extern void FTM0_IRQHandler(void);            //FTM0输入捕捉中断

    isr.c
    中断服务函数

    #include "isr.h"
    
    extern volatile U16 count;
    U8 count1,count2;
    
    void PIT0_IRQHandler(void)
    {
    
        count1=count;
        count2=count>>8;
        Uart4_SendByte(count2);
        Uart4_SendByte(count1);
        count=0; 
     
        PIT_TFLG0 |= PIT_TFLG_TIF_MASK;   //清中断标志
        PIT_MCR = 0;    
    }
    
    void FTM0_IRQHandler(void)
    {
        U8 Status = FTM0_STATUS;           
        FTM0_STATUS = 0x00;             //清中断标志位
    
        if(Status & (1<<4))
            count++;
    
    }

    -------------------------------------华丽的代码分割线---------------------------------
    短接PTA10(FTM2 CH0)和PTA7(FTM0 CH4) 采集到数据应为625(0x0271)

  • 相关阅读:
    使用视图显示Oracle ACFS信息
    ORA-00600: internal error code, arguments: [ktecgsc:kcbz_objdchk], [0], [0], [1], [], [], [], [], [], [], [], []
    ORA-00600 arguments: [4194]
    闪回查询
    改写exists
    淘宝mysql月报连接
    pdb表空间巡检脚本
    Oracle表空间名称不能超过30个字符
    PL/SQL package SYS.DBMS_BACKUP_RESTORE version 19.03.00.00 in TARGET database is not current
    Linux查看进程内存占用
  • 原文地址:https://www.cnblogs.com/hebaichuanyeah/p/3124132.html
Copyright © 2020-2023  润新知