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. ios学习之UISwipeGestureRecognizer手势识别

    ios学习之UISwipeGestureRecognizer手势识别   本文部分转自俺是一个瓜娃!!!的博客UISwipeGestureRecognizer ---手指动作,转载过来仅是为了自己查询 ...

  2. PHP中常用的函数

    1.php 字符串截取函数 2.php取得当前时间函数 3.php 字符串长度函数 4.几种php 删除数组元素方法 5.php中var_dump()函数的详解说明 6.PHP preg_match正 ...

  3. sublime text3 快捷键设置

    //插入到key binding user 里面,浏览器安装路径修改成自己的路径 1[ //firefox测试快捷键 { "keys":["f3"], &quo ...

  4. php用压栈的方式,循环遍历无限级别的数组(非递归方法)

    php用压栈的方式,循环遍历无限级别的数组(非递归方法) 好久不写非递归遍历无限级分类...瞎猫碰到死老鼠,发刚才写的1段代码,压栈的方式遍历php无限分类的数组... php压栈的方式遍历无限级别数 ...

  5. Java开发环境的配置

    为了能够在计算机上开发Java程序和运行Java程序,就需要在Windows操作系统上配置Java开发环境. 首先,安装JDK: 1.在Oracle官网上下载JavaSE: 2.在Download下载 ...

  6. maven 基本常识以及命令

    Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1. 创建Maven的普通 ...

  7. asp.net 服务器Button控件使用(onclick和onclientclick使用)

    <asp:Button ID="btn_Save" class="button green" Style="width: 100px; heig ...

  8. c数据结构栈的基本操作(字符逆序输出)

    线性栈 输入字符,再输出 #include "stdafx.h" #include<stdlib.h> #include<malloc.h> #define ...

  9. passing argument 3 of ‘wtk_hlv_rec_init’ discards ‘const’ qualifier from pointer target type

    -Werror,编译出现如下错误: src/wtk/exam/wtk_ndx.c:154:6: error: passing argument 3 of ‘wtk_hlv_rec_init’ disc ...

  10. UML常用图的几种关系的总结

    在UML的 类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization), 关联(Association), 聚合(Aggregation), 组合(Com ...