当前位置:首页 > 单片机 > 单片机
[导读]  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

  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

#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.


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

Microchip公司的PIC32MZ EF系列是高达250MHz的集成浮点单元(FPU),具有广泛的外设和包括局域网(CAN)的极好的连接选择,工作电压2.1V到 3.6V,DSP增强核具有四

关键字: Microchip pic32mz 处理器

  An interrupt is an internal or external event that requires quick attention from the controller. The PIC32MZ...

关键字: interrupt pic32mz timer tutorial

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

关键字: pic32mz tutorial change notification

/*Capture mode时,外部CCP1事件触发后,CCPR1H和CCPR1L将获取Timer1的TMR1H和TMR1L中的数值对于CCP1的触发事件,设置键CCP1Con中的相应位CCP1M3--CCP1M0CCP...

关键字: capture mode pic16f877a

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

关键字: interrupt pic32mz tutorial external

Windows CE Features > InternationalMicrosoft? Windows? CE includes the Input Method Manager (IMM)

关键字: input manager

经过千辛万苦,今天终于完工PIC32MZ EC Starter Kit的ethernet bootloader项目。我将整个项目, 命名为PhnBootloader。它分为两个部分。第一个部分是PC 端的host程序Ph...

关键字: bootloader ethernet pic32mz udp协议

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

关键字: communication pic32mz tutorial uart

  Core Timer is a very popular feature of PIC32 since it isa piece of the MIPS M4K core itself and is common t...

关键字: core pic32mz timer tutorial

  The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has four 32-bit...

关键字: pic32mz timer tutorial 32-bit
关闭