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.

 

posted on 2015-12-26 18:03 GeekyGeek 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/geekygeek/p/pic32mz_inputcapture.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值