|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
外部中断初始化
) r* t7 m6 H+ Z- C: ? F7 b#include "extint.h"" m" b. h4 S+ P. x
//按键和外部中断都用了GPIO13,查询和中断不能同时使用5 D7 u$ G M# Q! A. w% N
void InitExtInt(void): S9 _: |" m5 y! g2 r- w1 f
{4 |" ]8 Z. R, u2 S
EALLOW;
( k" I* S* x9 @& S8 A8 ` GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;9 T& ^ r: F+ w3 ^4 l* q/ l. g
GpioCtrlRegs.GPADIR.bit.GPIO13 = 0; //作为输入IO口# ]( S* O+ {3 `# o4 x
GpioCtrlRegs.GPAQSEL1.bit.GPIO13= 0; //和时钟同步4 M4 ~4 [4 t ~" s' ?+ e
GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 13; //选择GPIO13为外部输入XINT1输入引脚
0 a t: z* E" o4 f' xXIntruptRegs.XINT1CR.bit.POLARITY= 0; //下降沿触发中断
, s7 a0 X( Z7 M1 y; cXIntruptRegs.XINT1CR.bit.ENABLE = 1; //使能XINT1中断
n$ ]3 O1 @8 S, [2 N7 ^EDIS;2 B7 L1 a8 @; X& R
}3 z5 ]0 ^( K" P0 T) S
% o5 R8 H# {) H1 T1 d$ O//外部中断1服务函数 ,按下按键,进中断,亮灯响鸣! C* q6 ]' K2 J0 m
interrupt void ISRExint1(void)+ ?$ v9 x! ~. a
{
' A& n& x' j' ?9 x1 m PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;) C4 F& I, Z% Q% z+ l
DELAY_US(1000);) |# n" \$ e: B" H% _
if(GpioDataRegs.GPADAT.bit.GPIO13 == 0)
+ U/ k# G* L/ T{
+ Y5 Z7 N) W; H5 U4 W" o0 M6 ?1 ~ LED4=~LED4;
5 [0 u R5 ^1 C$ b3 l& @5 _$ l BUZZ_ON" z2 U- u) {4 k/ @' i
DELAY_US(100000);
1 x8 J% s0 F( }3 M BUZZ_OFF
3 F9 P8 }1 \3 P4 I' y8 h+ c. v4 g DELAY_US(100000);! X% G5 ` q6 o( F
BUZZ_ON1 J+ j- l0 [. t. M, J
DELAY_US(100000);
4 c$ }6 ^' t$ u/ P: b6 o0 C& ? V+ R BUZZ_OFF4 y% m- v r' {6 K- j! f" a6 K
}0 |) Y( F# ]* a# ]6 S# D
}. B$ ^4 x( W X) `3 z3 @
! C/ L/ e) h4 X$ \$ h
: W0 \# R8 `" [, ?5 p定时器0,1,2中断初始化
: Y4 B" l5 T; }" d InitCpuTimers(); // For this example, only initialize the Cpu Timers% M% k- x7 {8 n
ConfigCpuTimer(&CpuTimer0, 150, 500000); // 500us 150MHz CPU Freq, 1 second Period (in uSeconds)2 F$ }8 H0 z( {: f! [) r3 Q
ConfigCpuTimer(&CpuTimer1, 150, 1000000);- X* k6 ?& K( u
ConfigCpuTimer(&CpuTimer2, 150, 3000000);; R; g+ V2 u5 ~$ x9 u
StartCpuTimer0();$ Q) [) ~4 b/ i0 c7 g) [
StartCpuTimer1();$ m+ A. }. G% D. W: Y- a
StartCpuTimer2();4 y8 |' E z; T8 A& U* F& M/ `
& D+ ?: J' I, X0 ?7 R+ P
$ ^' l- U3 u( G+ a# C#include "timer.h"3 ^' C9 L/ u* y. \
interrupt void ISRTimer0(void)
1 x5 r9 D8 d' t{: F6 g+ _" J0 n- X3 Y2 O
static u8 timer0_count=0;' h9 H4 a4 s% C( c: i) P, p
// Acknowledge this interrupt to receive more interrupts from group 11 T4 d5 w+ P) P$ i& i& T6 l- m. @
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断
4 ^) d. B! C2 K1 y. |! }9 Y CpuTimer0Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志5 l4 l% W( J6 B2 U
CpuTimer0Regs.TCR.bit.TRB=1; // 重载Timer0的定时数据. p( D0 c' P/ L+ K" p2 E
, v; w6 w- N1 M+ {4 l
timer0_count++;
) D+ m- R& f( T8 g& f+ l8 [ if(timer0_count==4) //2000ms =2s
2 ~3 L: J& J$ T' W9 f/ d {
! n3 [/ ~' p2 j$ y3 Q/ P timer0_count =0;
* e3 b7 X% r, U3 } LED1=~LED1;) f: r% {0 R: y
}3 x; {1 l6 D, U+ t9 ]
}. H# {3 b V0 |1 L$ P
! z2 l! _4 c0 c- I! E
9 z1 T2 K% N5 i/ H7 S//1s
; e+ Q& C; A# _" i; Finterrupt void ISRTimer1(void)6 H Z0 b8 |, [5 U
{
; v8 K2 u' D$ P, g1 T# @ static u8 timer1_count=0;
: g2 a/ b9 I" _" H! o5 r: q B3 W// Acknowledge this interrupt to receive more interrupts from group 1
5 n3 U' n6 J. r! F! H" O //PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断
# a' ?3 P) i9 { CpuTimer1Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志
! h2 h L' D4 u" v6 x# Q- W CpuTimer1Regs.TCR.bit.TRB=1; // 重载Timer0的定时数据, D3 w" m) h* k' ^/ a0 q- g: j
1 Q5 C8 T. T( g. J timer1_count++;
4 p1 v7 Z8 U7 H1 X. t) a |8 H if(timer1_count==1) [9 \0 a" m* U) u( h# L* |% X
{
' G7 r8 K& h$ }1 O3 _ timer1_count =0;
# h+ D: O0 J" L. E& z& d LED2=~LED2;& ]& t/ ^$ j. h% V& B$ A; D
}: b |( ~6 l; g
}# H: k" u3 E( V8 j& b
r3 a% X0 ?" y0 c ~8 n//3s
1 p3 ^8 R% m3 y0 Y" ]interrupt void ISRTimer2(void)
0 }* O" | v+ g; L8 I{
7 E& @3 s$ m! f0 T- v static u8 timer2_count=0;1 w0 t5 U, T- U- O5 H
// Acknowledge this interrupt to receive more interrupts from group 1
4 ?7 p, t, H5 W PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断
, Z& Y* e m, ]% n1 T4 Y CpuTimer2Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志
9 [# S. R+ X- l* \- b" z# c CpuTimer2Regs.TCR.bit.TRB=1; // 重载Timer0的定时数据# m; {0 A1 I# k
2 m- e" r. b& M" P) x8 |( k0 | timer2_count++;9 X. n1 L# e, o7 |' I, W: w; k
if(timer2_count==1)
4 q" \2 v) p" |9 Z1 V {1 w- M' [* c" q+ C
timer2_count =0;( |! V( {. @7 p4 _5 f% p
//LED3=~LED3;5 v `+ B2 K0 G5 Y8 S6 g
}+ T) o; o9 g1 @, H! y- U3 @" X
}
' ~# g" H$ ~: l& E9 G: ~' v; P- ?) t2 X6 ] k
指定中断服务函数地址; p. y* S/ e6 j( |
EALLOW; // protected registers8 W, N$ \, p! `
PieVectTable.XINT1 = &ISRExint1; //外部中断1服务程序% ^; `: |4 {& r
PieVectTable.TINT0 = &ISRTimer0; //定时器0中断服务& \0 a: O: m: Z- v* f
PieVectTable.XINT13 = &ISRTimer1; //定时器1中断服务6 c' ]9 ?! T! b% O
PieVectTable.TINT2 = &ISRTimer2; //定时器2中断服务
$ r; E' T. Z+ ~* k" H( I EDIS; // This is needed to disable write to EALLOW protected registers
0 _% g" V F& I8 }4 t( n
& i3 Q9 G3 @% a. |开CPU级中断
& b+ v( w$ ]) n: e4 H IER |= M_INT1; //开启CPU中断 组17 _7 T, l. m3 h- A- [1 N
IER |= M_INT13; //开启CPU中断 13 XINT13 / CPU-Timer1. J# ^+ F& `! Y& f
IER |= M_INT14; //开启CPU中断 组14 TINT2
, ^( `6 ^8 m( Y0 U; e- ]* | ' K( [ L6 G7 E I7 O6 p
PieCtrlRegs.PIEIER1.bit.INTx7= 1; //CPU定时器 TIMER0中断 组1的第4个中断使能
6 D5 K5 u. L6 p CpuTimer1Regs.TCR.all = 0x4001; // 使能TIMER1 Use write-only instruction to set TSS bit = 0
$ e* m: g) A) J4 `6 d1 Z' \' I) I CpuTimer2Regs.TCR.all = 0x4001; // 使能TIMER2 Use write-only instruction to set TSS bit = 0
7 R0 Z; B8 Z) U3 P: o# h4 k4 Q& @' _4 M4 n
EINT; //开启全局中断
' I2 f* V6 V( k: K! g ERTM; //开启全局实时中断,调试用DBGM |
|