An interrupt is an internal or external event that requires quick attention from the controller. The PIC32MZ architecture provides a rich interrupt system that can manage up to 190 sources of interrupts. Each interrupt source can have a unique piece of code, called the Interrupt Service Routine (ISR) directly associated via a pointer, also called a "vector", to provide the required response action.

  At the moment, I use Timer1 interrupt as a example to show how to enable interrupt and how to write interrupt service routine for PIC32MZ. The implementation has three parts. The first is the Timer1 interfaces. The second is interrupts interfaces. And the third is the Timer1 interrupt service routine.

  The Timer1 interfaces include TMR1_Open() and TMR1_Write(). In the TMR1_Open(), will enable Timer1 and Timer1 interrupt. configure Timer1 to overflow and interrupt per millisecond. set Timer1 interrupt priority level and subpriority level. Below code show me how to do that.

/**
<p><b>Function: TMR1_Open</b></p> <p><b>Summary: Initialization of Timer </b></p> <p><b>Description: TMR1 on; 0.08 microsecond every tick, overflow and interrupt per ms </b></p> <p><b>Remarks: Pre-scale 1:8; PB 100MHz; PR1 0x30D3</b></p>
*/
// TODO Insert function definitions (right here) to leverage live documentation
void TMR1_Open(void)
{
T1CON = 0x8010;
PR1 = 0x30D3;
IPC1SET = 0x5;
IEC0SET = 0x10;
IFS0CLR = 0x10;
}
// Comment a function definition and leverage automatic documentation
/**
<p><b>Function: TMR1_Write</b></p> <p><b>Summary: Write TMR1</b></p> <p><b>Description: Write a value to TMR1</b></p> <p><b>Remarks: the value is range of 0~65535</b></p>
*/
// TODO Insert function definitions (right here) to leverage live documentation
void TMR1_Write(unsigned int value)
{
TMR1 = value & 0xFFFF;
}

  The interrupts interfaces include EnableINT(), DisableINT() and SelectMultiVector(). When we want to use any interrupt source, call EnableINT() to enable interrupt module first. PIC32 have two interrupt vector modes, the singlevector mode and multivector mode. SelectMultiVector() will help to set interrputs for multivector mode. I do that like below.

/**
@Function
EnableINT @Summary
Enable interrupts @Remarks
This function need to be called first before using any interrupt source
*/
void EnableINT(void)
{
asm volatile("ei");
} /**
@Function
DisableINT @Summary
Disable interrupts @Remarks */
void DisableINT(void)
{
asm volatile("di");
} /**
@Function
SelectMultiVector @Summary
Set system to use multivector mode for interrupts @Remarks */
void SelectMultiVector(void)
{
unsigned long MVEC_MASK = 0x1000;
INTCONSET = MVEC_MASK;
}

  At the end, I show the main function and the Timer1 interrupt service routine. There are styles of interrupt service routine, the interrupt attribute style, like

__attribute__((interrupt([IPLn[SRS|SOFT|AUTO]]))),

and the interrupt pragma style, like

# pragma interrupt function-name IPLn[AUTO|SOFT|SRS] [vector
       [@]vector-number [, vector-number-list]]
       # pragma interrupt function-name single [vector [@] 0

  It strongly recommend the first style. So there they are.

#include <xc.h>
#include "TMR.h"
#include "Interrupt.h"
#include "ConfigurationBits.h" //#define LED_IOCTL() TRISHbits.TRISH0 = 0
//#define LED_SETON() LATHbits.LATH0 = 1
//#define LED_SETOFF() LATHbits.LATH0 = 0
//#define LED_OPEN() ANSELHbits.ANSH0 = 0 //#define LED_IOCTL() TRISH &= 0xFFFFFFFE
//#define LED_SETON() LATH |= 0x00000001
//#define LED_SETOFF() LATH &= 0xFFFFFFFE
//#define LED_OPEN() ANSELH &= 0xFFFFFFFE #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 volatile unsigned int COUNTER; void __attribute__((vector(_TIMER_1_VECTOR), interrupt(ipl1AUTO), nomips16)) TMR1_ISR(void)
{
if (COUNTER++ >= )
{
COUNTER = ;
LED_ONOFF();
}
TMR1_Write();
IFS0CLR = 0x10; // Clear flag
} void main(void)
{
LED_OPEN();
LED_IOCTL();
TMR1_Open();
TMR1_Write();
SelectMultiVector();
EnableINT();
COUNTER = ;
while ()
{
; // do nothing
}
}

  This application run well on PIC32MZ EC starter kit. I see the LED blink perfectly as expectation.

PIC32MZ tutorial -- Timer Interrupt的更多相关文章

  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 -- 32-bit Timer

    The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...

  4. PIC32MZ tutorial -- Watchdog Timer

    Watchdog is a very necessary module for embedded system. Someone said that embedded system operates ...

  5. (LPC1769) Timer Interrupt Application

    void TIMER0_IRQHandler (void) { if(LPC_TIM0->IR & 0x01) { LPC_GPIO1->FIOPIN ^= ( << ...

  6. PIC32MZ tutorial -- Core Timer

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

  7. PIC32MZ tutorial -- Output Compare

    Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...

  8. PIC32MZ tutorial -- Input Capture

    Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capt ...

  9. PIC32MZ tutorial -- Change Notification

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

随机推荐

  1. 无线网络的切换bat

    内网外网都是无线,切换起来麻烦,做了两个个bat(注意,切换网络的时候有延时,就点两次). -------------------下面的是切换到内网-------------------------- ...

  2. 带优先级的队列 - PHP实现

    很久以前写的一个功能,当时需要一个优先级的队列,特用新学的swoole写了一个简单的demo,仅满足当时的需求. 功能说明: 完全参考httpsqs增加优先级参数level 例:           ...

  3. windows调试器尝鲜

    曾几何时,我也下载过看雪论坛精华看的津津有味.可惜一直没有动手去调试,学到的x86汇编指令也忘得差不多了.最近将老机器的T4200 CPU换成了更省电,温度更低的P8800,为了支援新的VT虚拟化,特 ...

  4. 移动互联网实战--Web Restful API设计和基础架构

    前言: 在移动互联网的大潮中, Web Restful API逐渐成为Web Server重要的一个分支. 移动端和服务端的交互, 主流的方式还是通过Http协议的形式来进行. 请求以Get/Post ...

  5. Opencv创建有滚动条的视频

    #include "stdafx.h"#include "cv.h"#include "cxcore.h"#include "hi ...

  6. JAVA生成TXT日志文件

    /** * 生成日志文件(文件的位置在Tomcat的安装路径下) * @param str */ public static void LogForTXT(String str) { try { St ...

  7. js封装的方法

    1.JS封装就是尽量把使用的方式简单化,内部逻辑和使用解耦.通俗的说就是使用的时候只需要知道参数和返回值,其他条件尽量不要使用人员进行设置. 2.JS封装的方法有函数方式.对象的方式.闭包的方式. 举 ...

  8. Spring源码学习之:@async 方法上添加该注解实现异步调用的原理

    在我们使用spring框架的过程中,在很多时候我们会使用@async注解来异步执行某一些方法,提高系统的执行效率.今天我们来探讨下 spring 是如何完成这个功能的.    spring 在扫描be ...

  9. JS(去掉前后空格或去掉所有空格)的用法 推荐使用jquery 方法

        说明:     如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前后所有空格的字符串. 推荐 1.  去掉字符串前后所有空格: 代码如下: function Tri ...

  10. 【转】关于.net framework4.0以及4.5安装失败,“安装时发生严重错误”……

    也不知道管不管用,先记着 今天忽然想装一个vs2010,然后装了好几遍,每次都在安装.net4.0的时候失败.好吧,我自己手动装行么.于是手动去装.net 4.0. 结果在还是返回"安装时发 ...