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 = ;
TMR3 = ; 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 = ;
unsigned int i;
for ( i = ; i < SIZE_MAX; i++)
{
IC1_ST.buf[i] = ;
} IC1CON |= 0x8000;
} unsigned long IC1_ReadCapture(void)
{
while ((IC1CON & 0x8) == 0x8)
{
if (IC1_ST.count == SIZE_MAX)
{
IC1_ST.count = ;
}
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()
{
; // 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.

PIC32MZ tutorial -- Input Capture的更多相关文章

  1. PIC32MZ tutorial -- OC Interrupt

    In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Comp ...

  2. PIC32MZ tutorial -- External Interrupt

    In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce w ...

  3. PIC32MZ tutorial -- UART Communication

    At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...

  4. PIC32MZ tutorial -- Output Compare

    Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...

  5. PIC32MZ tutorial -- 32-bit Timer

    The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...

  6. PIC32MZ tutorial -- Change Notification

    In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...

  7. PIC32MZ tutorial -- Key Debounce

    Today I accomplish a application on PIC32MZ EC Starter Kit. The feature of application is to light u ...

  8. PIC32MZ tutorial -- Hello World

    Today I implement "Hello World" on PIC32MZ EC starter kit. The application of "Hello ...

  9. STM32 Timer : Base Timer, Input Capture, PWM, Output Compare

    http://www.cs.indiana.edu/~geobrown/book.pdf An example of a basic timer is illustrated in Figure 10 ...

随机推荐

  1. 关于jsonp跨域过程中 cookie中的值一直为null的原因

    今天技术交流群里的小伙伴一直被一个问题而困扰,就是写入cookie里面的值 再次进行请求时 cookie 就为空了 他被问题纠结了一天  我也好奇了一天 终于在快下班的时候 他解决掉了 下面我来收一个 ...

  2. 北大poj-1062

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43523   Accepted: 12760 Descripti ...

  3. DIY FSK RFID Reader

    This page describes the construction of an RFID reader using only an Arduino (Nano 3.0 was tested, b ...

  4. html table动态合并单元格 js方法

    <script> $(document).ready(function(){ function mc(tableId, startRow, endRow, col) { var tb = ...

  5. 传智博客.NET培训第13季 Ajax教程(共十三季) 学习资源

    http://wangpengnimei.ctfile.com/u/1235801/47146 传智博客.NET培训第13季 Ajax教程(十三季).rar 19

  6. java.lang.ClassNotFoundException: Didn't find class "*****(转载)

    很多人出现了java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*****Activity}: java. ...

  7. Android 开发之拦截EditText的输入内容,定制输入内容

    1.EditText作为一个比较成熟的View,在Android的应用开发中得到极为广泛的使用.在某些特殊情况下,我们可能需要定制EditText的输入内容, 只允许指定功能的输入,例如输入一个”dd ...

  8. python数据结构与算法——完全树 与 最小/大堆

    # 完全树 最小堆 class CompleteTree(list): def siftdown(self,i): """ 对一颗完全树进行向下调整,传入需要向下调整的节 ...

  9. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  10. 【linux】三十分钟学会AWK

    本文大部分内容翻译自我开始学习AWK时看到的一篇英文文章 AWK Tutorial ,觉得对AWK入门非常有帮助,所以对其进行了粗略的翻译,并对其中部分内容进行了删减或者补充,希望能为对AWK感兴趣的 ...