niosII button 中断

上一篇 / 下一篇  2006-08-16 10:38:51 / 天气: 晴朗 / 心情: 高兴 / 个人分类:Backup

volatile int edge_capture;
}XS+I3|;Cjy lDvLBQ0
~-zB&F6Y}0static void handle_button_interrupts(void* context, alt_u32 id)EDA中国门户网站:A!D5tzPN N!X%Fk r
{
&TC)QSi|#?#] B\{0/* Cast context to edge_capture's type.EDA中国门户网站g_*]*A&l\
  * It is important to keep this volatile,
5l6uwr uUn/k/F0  * to avoid compiler optimization issues.EDA中国门户网站C`5_;S9I?r
  */
Hhk T0o nU0volatile int* edge_capture_ptr = (volatile int*) context;
qo'FB+bX2O;@1G0/* Store the value in the Button's edge capture register in *context. */
7c dU'H$bn;[pQ0*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
)K t*y,f,KWlsr0/* Reset the Button's edge capture register. */EDA中国门户网站!GJ-?5R\p6VL9|/_
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);EDA中国门户网站)Xz Tx3C'E%r
}
h#r*gHsaJ0
BCq DY/?*]~M4P0/* Initialize the button_pio. */EDA中国门户网站M [$`/}jL V x/`

y6z@ c y'So0static void init_button_pio()
;l-Oh8OR0{EDA中国门户网站hzH#GQ K { D"D\
/* Recast the edge_capture pointer to match the alt_irq_register() functionEDA中国门户网站,qqixfyZ
* prototype. */EDA中国门户网站)a _I/pb
void* edge_capture_ptr = (void*) &edge_capture;EDA中国门户网站Oxtz0F T[ j
/* Enable all 4 button interrupts. */
(^{i9Y4LgN0IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
4a'Q9L\7` y0/* Reset the edge capture register. */EDA中国门户网站?,et;m*H
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
0v9q r&|8S0/* Register the interrupt handler. */EDA中国门户网站,zp%c7r|in
alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr, handle_button_interrupts );
C3Vg4?y%ob#l Z$U9f0}
-T_![o1H9r+Je_A3JX0
4L8V {L~s){yb1F0static void TestButtons( void )
3_f:P+Z8x$S5VW9u-N0{EDA中国门户网站&Q.U"iD%g0LA
alt_u8 buttons_tested;EDA中国门户网站CckS:} A"rAo
alt_u8 all_tested;EDA中国门户网站+N:e G'q5z'M]
/* Variable which holds the last value of edge_capture to avoid EDA中国门户网站D3n lHs4L-b
  * "double counting" button/switch pressesEDA中国门户网站II#d U(V{ F
  */
;||]&Z"Y0int last_tested;EDA中国门户网站mt&]N(Gd~#W
/* Initialize the Buttons/Switches (SW0-SW3) */EDA中国门户网站 {tzv9nB{GpUz
init_button_pio();EDA中国门户网站 y2FZ]eoT+D,XY
/* Initialize the variables which keep track of which buttons have been tested. */EDA中国门户网站!G;uz#_*o!c3_M
buttons_tested = 0x0;
4qE a5NV'cxv/a/Bo0all_tested = 0xf;
T;f2W] P0EDA中国门户网站T K"_F9@,M*v
/* Initialize edge_capture to avoid any "false" triggers fromEDA中国门户网站H5W(H-^Y%G
  * a previous run.EDA中国门户网站!T w)z C"N3C
  */EDA中国门户网站1j uU c~%CO!E.q
  EDA中国门户网站Dd T/}uK3Zz`
edge_capture = 0;EDA中国门户网站#K1Aiqo ^A$[
EDA中国门户网站]%q;LtIAM
/* Set last_tested to a value that edge_capture can never equal
U2sY b'z+QR,L0  * to avoid accidental equalities in the while() loop below.EDA中国门户网站pU[:V6T,_e
  */EDA中国门户网站Ms3ff$w,se
 
W&shaF0last_tested = 0xffff;
H|0G;J8X N0EDA中国门户网站eDFHW"Hd
/* Print a quick message stating what is happening */
H}c(ZG0z z0
pac7Tz y/Z0printf("\nA loop will be run until all buttons/switches have been pressed.\n\n");
Q:lfb,sw1c,_$_'DB3y2w0printf("\n\tNOTE: Once a button press has been detected, for a particular button,\n\tany further presses will be ignored!\n\n");EDA中国门户网站a Z4t0K#rH5H
EDA中国门户网站pr\x6t
/* Loop until all buttons have been pressed.EDA中国门户网站xp9v'J:sz2p,`
  * This happens when buttons_tested == all_tested.EDA中国门户网站1S/P5IL9t1{3jg_4WJdj
  */
%M#k9c]6M`0EDA中国门户网站"dG C;`E)r
while ( buttons_tested != all_tested )EDA中国门户网站3x,j^Zi,K?D8uM)V_2X
{ EDA中国门户网站L:L)s-B,nd:`;s
  if (last_tested == edge_capture)EDA中国门户网站_XGz]5c"q\D
  {EDA中国门户网站kB$Z-]7z7]
    continue;EDA中国门户网站T)~{ `5O\]r Y
  }EDA中国门户网站$KS1K-WB;y)o!g7e
  elseEDA中国门户网站3a"B3X'S_3|
  {EDA中国门户网站 Ee'LS2h/U"pui
    last_tested = edge_capture;EDA中国门户网站#RF$j&wVy9~5B
    switch (edge_capture)
HM0q g;} i&v0    {
7Km,v EK W0    case 0x1:EDA中国门户网站UA@M@g6?Y
      printf("\nButton 1 (SW0) Pressed.\n");EDA中国门户网站8Y9p*q/LJ3}(efLs(Z ~
      buttons_tested = buttons_tested | 0x1;
'SGA-b{ [0      break;EDA中国门户网站!O7kC,wgv+^ q
    case 0x2:
b4i8Vt Ta0      printf("\nButton 2 (SW1) Pressed.\n");EDA中国门户网站*z'Dn:BM
      buttons_tested = buttons_tested | 0x2;
H,m:t2S ^.B:B e.U0Q)A+O0      break;
-U ? |1o1yf4R i0    case 0x4:EDA中国门户网站#Yl-C%}t"Zj
      printf("\nButton 3 (SW2) Pressed.\n");
W7u\ ]4oBk0      buttons_tested = buttons_tested | 0x4;
]am(B!mz ~:?+v Q`0      break;EDA中国门户网站6g1BY9VDkJ'RQ"L
    case 0x8:
%i3eIa)k+C^0      printf("\nButton 4 (SW3) Pressed.\n");
f3eB({M5@\5V s0      buttons_tested = buttons_tested | 0x8;
x;e,nFCM0      break;
)n0Fd i2q&Ji0    }
U/i"~gC fFD0  }
yK+v*Rl-y7g`;_!C0}
)wU%a9W)y3v}(o0/* Disable button interrupts for anything outside this loop. */EDA中国门户网站Tq4A'Ze ?
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0x0);
"\khaT0printf ("\nAll Buttons (SW0-SW3) were pressed, at least, once.\n");
?9j9v,ri]:M&jN0usleep(2000000);EDA中国门户网站4U$Ro v F;a V&a[
return;EDA中国门户网站8k'H*S9q?C
}


TAG: EDA

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 8557
  • 日志数: 96
  • 建立时间: 2006-08-07
  • 更新时间: 2007-06-30

RSS订阅

Open Toolbar