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. 实际项目中积累的一些关于事件的简单应用JS代码段(能力有限,不喜轻喷,23333)

    1:鼠标移入移出显示另一张图片 var yuanquan_1 = document.getElementById("yuanquan_1" );  yuanquan_1. onmo ...

  2. 观看github前100开源项目的读后感

    文章来自:http://www.oschina.net/news/61416/github-top-100-objective-c-projects?from=20150412 ReactiveCoc ...

  3. Android 学习第5课,配置android

    昨天与今天都在配置安卓的开发环境, 搞的头都大了,步骤比较多 1. 先下载安装 java 的 jdk ,这个是最基础的组件 2. 再下载 android SDK,  http://developer. ...

  4. Directory的GetFiles方法

    想实现一个功能 :比如多个业务审批流程公用一个审批表的时候,有一个提示审批信息的页面 ,点击该页面不同的业务审批流程记录的时候,跳转到不同业务流程的详细显示界面 额 这样说 貌似以后我自己也看不明白 ...

  5. Windows Service 开发,安装与调试

    Visual Studio.net 2010 Windows Service 开发,安装与调试 本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Servic ...

  6. SQL级联删除——删除主表同时删除从表——同时删除具有主外键关系的表

    create table a(id  varchar(20) primary key,password varchar(20) not null) create table b(id int iden ...

  7. winform中dataGridView高度自适应填充完数据的高度

    // winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...

  8. SpringMVC学习系列(1) 之 初识SpringMVC

    1.前言: 与SpringMVC的结识源于个人之前想做一个微信公众账号的管理平台玩玩,既然要做就需要考虑平台的选择问题.由于我的朋友只有一台运行了Linux系统的虚拟主机,且上面还运行有他自己的一些论 ...

  9. [工具开发] keepalived使用nagios监控脚本

    最近在做开发和办公环境的高可用,采用的是keepalived:keepalived基于Linux内核支持的LVS,既能实现高可用,又能实现负载均衡,非常实用. keepalived监控服务状态时可以用 ...

  10. linux下的vim使用教程

    命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...