zoukankan      html  css  js  c++  java
  • PIC32MZ tutorial -- Input Capture

      Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capture feature of PIC32MZ. The Input Capture module captures the 32-bit value of the selected Time Base registers when an event occurs at the ICx pin.  The timer source for each Input Capture module depends on the setting of the ICACLK bit in the CFGCON register. To change this bit, the unlock sequence must be performed. In the implementation I just use IC1 to capture the 32-bit timer with combining Timer2 and Timer3.

      First, see the 32-bit timer initialization.

    void T32_Init(void)
    {
        T2CON = 0x0;
        T3CON = 0x0;
        TMR2 = 0;
        TMR3 = 0;
        
        PR3 = 0xFFFF;
        PR2 = 0xFFFF;
    
        T2CON = 0x8008;
    }

      I use PPS to set RB14 as IC1. And I define a array IC1_ST.buf[] to store capture valure. there is a point to clarify. To set IC1CON, if using one sentence like "IC1CON = 0x8012;"  It will cause a input capture interrupt. It is not expectation. Instead of setting IC1CON with one sentence, I use two sentences like below.

      IC1CON = 0012;

      IC1CON |= 0x8000;

    Enable IC1CON last then input capture works as expectation. For the detail, please see the interface of IC1.

    // In IC.h
    #define SIZE_MAX 20
        typedef struct _IC_ST_t{
            unsigned int count;
            unsigned long buf[SIZE_MAX];
        } IC_STR_t;
        
        extern IC_ST_t IC1_ST;
     
    void IC1_Init(void);
    unsigned long IC1_ReadCapture(void);
    
    // In IC.c
    IC_ST_t IC1_ST;
    
    void IC1_Init(void)
    {
        //AN9|RPB14|RB14 with digital IO, disable AN first
        ANSELB &= 0xFFFFBFFF;
        TRISBSET = 0x4000;
        //Enable internal pull-up
        CNPUBSET = 0x4000;
        
        // Interrupt with priority 7 and sub-priority 0
        IPC1SET = 0x1C0000; 
        IFS0CLR = 0x40;
        IEC0SET = 0x40;
        // RPB14 set as IC1 with PPS
        IC1R = 0x2;
        IC1CON = 0x0102;
    
        IC1_ST.count = 0;
        unsigned int i;
        for ( i = 0; i < SIZE_MAX; i++)
        {
            IC1_ST.buf[i] = 0;
        }
        
        IC1CON |= 0x8000;
    }
    
    unsigned long IC1_ReadCapture(void)
    {
        while ((IC1CON & 0x8) == 0x8)
        {
            if (IC1_ST.count == SIZE_MAX)
            {
                IC1_ST.count = 0;
            }
            IC1_ST.buf[IC1_ST.count++] = IC1BUF;
        }
    }

      The final, see the main function and interrupt service routine.

    #include <sys/attribs.h>
    #include "T32.h"
    #include "IC.h"
    #include "CFB.h"
    
    #define LED_IOCTL()       TRISHCLR = (1<<0)
    #define LED_SETON()       LATHSET = (1<<0)
    #define LED_SETOFF()      LATHCLR = (1<<0)
    #define LED_ONOFF()       LATHINV = (1<<0)
    #define LED_OPEN()        ANSELH &= 0xFFFFFFFE
    
    #define Mvec_Interrupt() INTCONSET = 0x1000; asm volatile("ei")
    
    void __ISR(_INPUT_CAPTURE_1_VECTOR,ipl7AUTO) IC1_Handler(void)
    {
        LED_ONOFF();
        IC1_ReadCapture();
        IFS0CLR = 0x40;
    }
    void main(void)
    {
        LED_OPEN();
        LED_IOCTL();
        T32_Init();
        IC1_Init();
        Mvec_Interrupt();
        while(1)
        {
            ; // do nothing
        }
    }

      On the PIC32MZ EC Starter Kit, RB14 connects to a push button. I push down this button with 1 Hz frequency. I can see my array IC1_ST.buf[] filled with values indicating a frequency of 1 Hz frequency in debug mode.

  • 相关阅读:
    服务器&域名那些事儿
    原生js实现简单打字机效果
    Mac OS X 11以上系统的Rootless机制问题
    用hexo书写github.io博客 学习心得 教程
    冒泡排序
    选择排序
    TCP三次握手和四次挥手
    常见HTTP状态码
    JSONP
    前端缓存(cookies、localStorage、sessionStorage)
  • 原文地址:https://www.cnblogs.com/geekygeek/p/pic32mz_inputcapture.html
Copyright © 2011-2022 走看看