EDA365电子工程师网

标题: [经验] DSP28335定时器和外部输入中断 [打印本页]

作者: zhoumi    时间: 2016-6-28 14:50
标题: [经验] DSP28335定时器和外部输入中断
外部中断初始化
& w. f, [: |5 t; T#include "extint.h"
5 K6 X' _0 F& {+ Z/ P- R/ y//按键和外部中断都用了GPIO13,查询和中断不能同时使用
2 L$ n9 F$ J# T  \void InitExtInt(void)1 r% j+ S. J, w- U2 K; F" k
{
& S: t1 \) A: O5 S     EALLOW;# g5 E, w& L/ S2 B, W6 F8 Y1 D4 J
     GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;5 K9 P5 s4 j  P5 n7 @2 D7 j
     GpioCtrlRegs.GPADIR.bit.GPIO13 = 0;           //作为输入IO口
# I& h+ R, H5 Y* j: Y+ W- @+ XGpioCtrlRegs.GPAQSEL1.bit.GPIO13= 0;          //和时钟同步1 D; o0 {) v! S2 H3 y, e# Z  K
     GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 13;    //选择GPIO13为外部输入XINT1输入引脚5 j& r  k0 h5 J- D# `
XIntruptRegs.XINT1CR.bit.POLARITY= 0;         //下降沿触发中断
* J( ?  \8 K$ s( WXIntruptRegs.XINT1CR.bit.ENABLE = 1;          //使能XINT1中断; b/ p  R9 N- i* h4 @! p- V
EDIS;
6 f; X$ c' K: L  N}  Z8 H# U+ `! N/ n0 I5 T

; h( ?0 T, H1 Z  Z& ^8 q- H//外部中断1服务函数  ,按下按键,进中断,亮灯响鸣9 E: H* H# W: H9 ~. j8 e
interrupt void ISRExint1(void)5 i7 v! C- A4 ?
{
' z* X3 A8 c" t( w9 ^        PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
" S. s# e) W: M2 J5 ]; @DELAY_US(1000);
$ l+ F# Z4 I7 @% }1 C( tif(GpioDataRegs.GPADAT.bit.GPIO13 == 0)
7 X1 @3 x7 S. u, k! O4 }{' G( C2 j5 O$ O9 @. R( r
    LED4=~LED4;7 h; K' l9 ]% [; s7 v
    BUZZ_ON* O+ L' c2 O, }
    DELAY_US(100000);
8 {) w* q' e. t$ q6 z- P" V9 q0 D2 B    BUZZ_OFF/ W- O- ~# k7 _
      DELAY_US(100000);
( {+ I# Q4 p8 V' _    BUZZ_ON; I" _2 S. ?- _  _2 @! p" T
    DELAY_US(100000);
0 K. S0 y4 X9 h7 v; [0 B6 v    BUZZ_OFF( D  V& c! K/ n4 Y
    }
4 S( W: E# m6 \0 g3 N}
; f. l6 F* s+ d/ @* A$ k5 F
% c3 K1 [8 b+ u) B: b* |# K! T9 ?, [# b
定时器0,1,2中断初始化
4 g/ h; K3 X9 R- W; z  E    InitCpuTimers();   // For this example, only initialize the Cpu Timers. \) i/ w) ^. \7 e, h
    ConfigCpuTimer(&CpuTimer0, 150, 500000);  // 500us 150MHz CPU Freq, 1 second Period (in uSeconds)' L5 W, X, L( c
    ConfigCpuTimer(&CpuTimer1, 150, 1000000);5 A" M8 A* L) j) h
    ConfigCpuTimer(&CpuTimer2, 150, 3000000);2 M1 j8 S$ ?: y1 I7 q/ s& ~  `
    StartCpuTimer0();
4 Z* W: V7 ?. ^! I9 x+ l* ?! O    StartCpuTimer1();
2 ~/ U0 y* p* W0 M  _( w6 N    StartCpuTimer2();
/ F  b* u8 C8 p+ w" U) n$ f" _$ K+ _! ^  D. y* [/ I
$ C# R9 G5 N6 \3 H6 D
#include "timer.h"# Q% O0 _/ I% U/ g: i7 c- \5 Q
interrupt void ISRTimer0(void)
; J$ s- ?* N) B* ]{
; `- v- b9 D* E, A9 U! H. k    static u8 timer0_count=0;
) V) z$ x! f  m// Acknowledge this interrupt to receive more interrupts from group 1
: ?5 W9 a: o- M, f: b' s    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断
4 J; w) y2 w  z1 c( V+ y    CpuTimer0Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志/ e4 Z5 i0 T1 {  j% h
    CpuTimer0Regs.TCR.bit.TRB=1;  // 重载Timer0的定时数据* I7 F/ K3 j; G7 n2 G
& _8 j0 w4 |% l0 X+ b
    timer0_count++;5 |: g( ?. ?5 f1 ?2 {4 T
    if(timer0_count==4)  //2000ms =2s
; h( U" y+ r) c/ v$ `$ X! V    {
3 b% j; `9 k8 `. |7 X2 B: @    timer0_count =0;
5 O8 D+ I) ]& w! N    LED1=~LED1;
# H+ u4 |5 w! I. [- `    }
! [+ l3 E3 v4 ?5 M}
! o2 i$ b. C9 m
( u  e3 J, c7 `3 J& n- P4 z( m' U) }! H1 R( d6 d+ E" X( Q
//1s
2 o( g' A4 J0 x+ Ninterrupt void ISRTimer1(void)
( G  o% e5 N7 L8 F) [{. }4 x- J) B& Y$ q" v2 @# g# k3 d& ?
    static u8 timer1_count=0;4 m! v& L8 ]3 |+ @5 w: a8 G/ {! n
// Acknowledge this interrupt to receive more interrupts from group 1
( q3 e% G0 K: Y    //PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断; [8 s- R( @6 Z
    CpuTimer1Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志
# m- G( K& i$ b    CpuTimer1Regs.TCR.bit.TRB=1;  // 重载Timer0的定时数据
6 L. H; ~( a8 U! c
' A8 Z7 r3 U, Y2 ]5 M    timer1_count++;7 u6 S3 A# y8 c  u, j, R  E
    if(timer1_count==1)0 _* _3 O) ?# M3 d" S& r) h7 I
    {$ ~. t. d! c- B$ N# P
    timer1_count =0;
/ z( W3 ~! o% ?0 P- M    LED2=~LED2;
( F6 v7 X  u8 F! p! i    }
+ B, W* \4 z) b% ]& i' H/ j}
. E! g% ^+ N& M1 E+ k( U/ {
/ W5 G4 B; R% O: |1 i//3s
5 B. @& k/ P+ Y6 M/ _; o: Uinterrupt void ISRTimer2(void)
1 U2 ]8 Z3 \# v{
5 I( [& b" G/ W7 ^. `9 A! m. o    static u8 timer2_count=0;
0 G1 Q2 t5 d; u, J// Acknowledge this interrupt to receive more interrupts from group 1
7 I: v! l9 J6 ^' P" d    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器,对其全部清除,不接受其他中断! `# _1 c1 R- t3 X/ n
    CpuTimer2Regs.TCR.bit.TIF=1; // 定时到了指定时间,标志位置位,清除标志
/ n% ^% s! V: R" E/ T    CpuTimer2Regs.TCR.bit.TRB=1;  // 重载Timer0的定时数据
2 X1 R& d$ p* U- ^% x* e& G6 \5 q5 }7 F! x  z5 N( S$ b0 c, P
    timer2_count++;) W& I- B8 Y: `3 \
    if(timer2_count==1)* s0 ?/ v  z, `9 [' @- U; Y
    {
, d3 A& s, Z, Z0 j    timer2_count =0;  U+ }8 `4 K( K7 H' o& l
    //LED3=~LED3;
' j5 {& r# W. j6 n3 q* @    }
3 @! c+ ]/ u, w7 D}
  h& v, G/ q& X8 V) G( t
) [- t4 K0 C/ r0 h. y指定中断服务函数地址4 k. V, z$ d' b' _" ]
     EALLOW;  //  protected registers
6 x* s( B  I0 i( D    PieVectTable.XINT1 = &ISRExint1;  //外部中断1服务程序1 t- n: P8 H  Q" t5 k* x8 e" L
    PieVectTable.TINT0 = &ISRTimer0;   //定时器0中断服务; {' \! U" e( T7 ^. T
    PieVectTable.XINT13 = &ISRTimer1;   //定时器1中断服务8 S% `2 H, ]  w  ~. t
    PieVectTable.TINT2  =  &ISRTimer2;    //定时器2中断服务: o3 u; e, [5 b; t6 f
    EDIS;    // This is needed to disable write to EALLOW protected registers
2 C+ h( y6 y/ E8 m! M. a. t4 P& O4 {. X- U9 p' O
开CPU级中断* h- m" A' L, N+ X! R/ L
    IER |= M_INT1;    //开启CPU中断 组1# G7 z3 Q+ g* t( t
    IER |= M_INT13;   //开启CPU中断 13   XINT13 / CPU-Timer1
" u7 Z/ f: f6 w! R* C9 r, }. |    IER |= M_INT14;   //开启CPU中断 组14 TINT27 ^  {. j& K6 g- s0 ?
   
6 S: i3 T- B  [% h0 @    PieCtrlRegs.PIEIER1.bit.INTx7= 1;  //CPU定时器 TIMER0中断   组1的第4个中断使能
# P" ]; O) u" J" G: A* x    CpuTimer1Regs.TCR.all = 0x4001; // 使能TIMER1 Use write-only instruction to set TSS bit = 0
* V- ~" g( u; a2 x3 \2 y. M; F. z+ n    CpuTimer2Regs.TCR.all = 0x4001; // 使能TIMER2 Use write-only instruction to set TSS bit = 0  N' u5 A4 d; `

" m2 a; P  N: |' F' y  X* {- q    EINT;   //开启全局中断
7 c% v; Y. h. }) S# C. K: R6 g! S    ERTM;   //开启全局实时中断,调试用DBGM
作者: helendcany    时间: 2016-6-29 09:53
资源多,学习不止步' V* o6 {. j9 F: V

作者: yhg-lee    时间: 2016-6-29 10:08
必须赞一个~
) e7 C4 {# V+ v4 G




欢迎光临 EDA365电子工程师网 (https://bbs.elecnest.cn/) Powered by Discuz! X3.2