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.

时间: 2024-08-26 03:08:25

PIC32MZ tutorial -- Input Capture的相关文章

PIC32MZ tutorial -- OC Interrupt

In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Compare without interrupt to generate PWM signal. I also tried the Output Compare interrupt. I selected OC to be PWM mode without fault pin (OCM = "110&qu

PIC32MZ tutorial -- External Interrupt

In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce with port polling. At this moment, I write an application which uses External Interrupt.  Therefore, only generates interrupt and starts debounce when the f

PIC32MZ tutorial -- 32-bit Timer

The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has four 32-bit synchronous timers are available by combining Timer2 with Timer3, Timer4 with Timer5, Timer6 with Timer7, and Timer8 with Timer9. The 32-bit

PIC32MZ tutorial -- Output Compare

Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the values stored in the OCxR and/or the OCxRS registers to the value in the selected timer. When a match occurs, the Output Compare module generates an

PIC32MZ tutorial -- Change Notification

In my last post I implement "Key Debounce" with port polling, port polling is not very efficient. And this time, I will use change notification instead of port polling. It generates interrupt and starts debounce when the level of digital port ch

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. This timer has four components – a controller, a prescaler (PSC), an “auto-reload” register (ARR) and a counter (CNT). The function of the prescal

PIC32MZ tutorial -- Blinky LED

Today I finish the "Blinky LED" application on PIC32MZ starter kit. This application let LED1 blink with 0.5HZ frequence. The pseudo code is like LOOP: LED ON Delay 1 second LED OFF Delay 1 second It uses Timer1 to control the delay time. So fir

STM32之输入捕获以及小小应用(库)

五一之际,先祝大家五一快乐.其实快乐很简单,工作的人有假放,学习的人也有假放,像我,有假放才有更多的时间学自己想学的东西.51假期学51,可惜没有32假期呀.好了..言归正传,大家听过吸星大法吧..在这里.智商和情商比我高的人估计又知道我要说什么了..没错了..今天我们来了解"葵花宝典"第STM32篇之输入捕获,也就是上文所讲的"吸星大法", 那输入捕获可以用来干嘛呢??这个问题问的好,输入捕获可以用来测量脉冲宽度或者测量频率,假如要捕获一个脉冲的高电平脉宽,我们要

定时器配置 中断配置 GPIO

GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO