PIC32MZ tutorial -- Timer Interrupt
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的更多相关文章
- 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 -- 32-bit Timer
The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...
- PIC32MZ tutorial -- Watchdog Timer
Watchdog is a very necessary module for embedded system. Someone said that embedded system operates ...
- (LPC1769) Timer Interrupt Application
void TIMER0_IRQHandler (void) { if(LPC_TIM0->IR & 0x01) { LPC_GPIO1->FIOPIN ^= ( << ...
- 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 ...
- PIC32MZ tutorial -- Output Compare
Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...
- PIC32MZ tutorial -- Input Capture
Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capt ...
- PIC32MZ tutorial -- Change Notification
In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...
随机推荐
- iOS学习之沙盒
1.iOS沙盒 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. 1 ...
- HTML编码规则、CSS属性声明顺序--简介
From AmazeUI:http://amazeui.org/getting-started/html-css-guide HTML 属性顺序 HTML 属性应当按照以下给出的顺序依次排列,确保代码 ...
- hdu 2084
ps:这道题...是DP题..所以我去看了百度一些东西,才知道了什么是状态方程,状态转移方程.. 做的第一个DP题,然后TLE一次.贴上TLE的代码: #include "stdio.h&q ...
- 三部曲一(数据结构)-1024-Eqs
解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少.取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解.具体做 ...
- 制作.frameWork的最全最真实的解决办法
这个制作流程 本博主 已经完全测试成功 我这边 制作的.frameWork 要接入游戏 我们游戏已经上架 所以这个东西完全可以用 http://www.cocoachina.com/bbs/read. ...
- [GodLove]Wine93 Tarining Round #2
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44704#overview 题目来源: ZOJ Monthly, June 2 ...
- C++中 destory() 和deallocate()以及delete函数的相关性和区别性
这里非常的绕口 需要仔细的来看看: destory(): 显示调用一个对象的析构函数 相当于释放一个对象需要释放的一些动态内存 为下次真正释放对象做准备 deallocate():真正的释放一个内存 ...
- Jquery 读取表单选中值
1.获取复选框的选中值 <title> JS 获取复选框选中的值</title> <script src="jquery-1.11.2.min.js" ...
- java 读写JSON(一)
算是第一次正式接触Json,没有深入研究,先贴上java的代码,日后才说! package priv.chenhy.datehandle; import java.io.BufferedReader; ...
- Svn服务器的安装和配置
1.安装svn服务器端软件 从镜像服务器或者YUM源下载安装SVN服务器软件:yum install subversion mkdir /usr/local/svn //创建SVN安装目录 c ...