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 = ;
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的更多相关文章
- PIC32MZ tutorial -- OC Interrupt
In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Comp ...
- PIC32MZ tutorial -- External Interrupt
In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce w ...
- PIC32MZ tutorial -- UART Communication
At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...
- PIC32MZ tutorial -- Output Compare
Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...
- PIC32MZ tutorial -- 32-bit Timer
The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...
- PIC32MZ tutorial -- Change Notification
In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...
- PIC32MZ tutorial -- Key Debounce
Today I accomplish a application on PIC32MZ EC Starter Kit. The feature of application is to light u ...
- PIC32MZ tutorial -- Hello World
Today I implement "Hello World" on PIC32MZ EC starter kit. The application of "Hello ...
- 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 ...
随机推荐
- iOS学习之NSPredictae及搜索框的实现
NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...
- BlackHat会议上将公布一款免费的汽车黑客工具
汽车,无可厚非是现代社会很重要的交通工具,但与此同时却也带来了诸多安全隐患,不管怎样,汽车安全都是我们不可忽视的一个重大问题. 即将免费分享该工具 近日一名法国研究者将发布一款检测汽车安全漏洞的工具, ...
- web.xml在Servlet3.0中的新增元素
metadata-complete: 当属性为true时,该Web应用将不会加载注解配置的Web组件(如Servlet.Filter.Listener) 当属性为false时,将加载注解配置的Web组 ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
- JavaScript闭包学习笔记
此文都是大牛们关于闭包的观点,在此只是总结. 闭包应用的两种情况即可——函数作为返回值,函数作为参数传递. 1 深入理解javascript原型和闭包 判断一个变量是不是对象非常简单.值类型的类型判断 ...
- Objective C运行时(runtime)技术的几个要点总结
前言: Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...
- [转]http://makefiletutorial.com/
Intro This makefile will always run. The default target is some_binary, because it is first. some_bi ...
- 移动互联网实战--Web Restful API设计和基础架构
前言: 在移动互联网的大潮中, Web Restful API逐渐成为Web Server重要的一个分支. 移动端和服务端的交互, 主流的方式还是通过Http协议的形式来进行. 请求以Get/Post ...
- 2015GitWebRTC编译实录13
2015.07.21 libboringssl.a 编译通过主要是生成路径,去除test文件比较啰嗦,后继测试需要重点跟进下 CC obj/third_party/boringssl/boringss ...
- android首次点击没反应,第二次才反应
比如我写了个重置密码确认按钮的animation动画,动画代码都没问题,但等我输入密码,再点击这个确认按钮,动画没反应,第二次才反应 解决办法已经写到我的公众号,二维码在下面,欢迎关注,谢谢. 本人联 ...