日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nios中PIO的INT

發布時間:2024/1/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nios中PIO的INT 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

初涉nios中的INT,開始做的時候中斷雖然正常的產生了,但是debug發現初始化木有成功的返回值,main函數中代碼:

if(!init_KEY1()){printf("interupt register successfully!\n");}else{printf("Error: interupt register failed!\n");}while(1);

中斷能正常產生,這說明初始化一定好了。但是那兩個printf都木有打出來,還為這個苦惱了一會兒。咱不能光初始化了以后啥事兒都跑到ISR里做吧,嗯!問題一定得解決的。

后來看到了http://www.cnblogs.com/crazybingo/archive/2011/04/03/2004477.html#2356430這篇文章,真是一下點醒了我,以前做ARM時,記得在有INT初始化時,一定要clear pending bit的,否則直接掛掉了(ARM中進入ISR也要有這步的)。

最后問題解決了,寫點東西,做個備份吧,如果還能幫上別人,那更好了。如果有異議,希望大家指教。

?


?

做的是4bit PIO接4個按鍵,任何一個按下都會有獨立的LED響應(4個LED也用4bit的PIO接的)。4個鍵不能沖突,沒有中斷嵌套。貼上源碼:

SOPC.h

1 #ifndef SOPC_H_ 2 #define SOPC_H_ 3 4 #include "system.h" 5 6 #define _LED 7 #define _KEY 8 9 10 typedef struct 11 { 12 unsigned long int DATA; 13 unsigned long int DIRECTION; // 0-> input; 1->output 14 unsigned long int INTERRUPT_MASK; // 1-> enable the INT 15 unsigned long int EDGE_CAPTURE; 16 }PIO_STR; 17 18 #ifdef _LED 19 #define LED ((PIO_STR*)PIO_LED_BASE) 20 #endif 21 22 #ifdef _KEY 23 #define KEY ((PIO_STR*)PIO_KEY_BASE) 24 #endif 25 26 #endif /*SOPC_H_*/

main.c

1 /******************************************************************** 2 * enable bit cleaning for PIO-key-INT 3 * 4 * when any of the 4 key is down,interrupt will occured,and check the 5 * edge-capture-register to determine which key is down 6 * 7 * during INT register initial, bit clean should be done once. 8 * During ISR,bit clean should NOT be done(Line 48);INT disable/enable is useless(Line 41/72). 9 * 10 * 11 * 2012-4-16 Lefroy Guo 12 * 13 ********************************************************************/ 14 15 #include <stdio.h> 16 #include "../inc/sopc.h" 17 #include <system.h> 18 #include <unistd.h> 19 20 //******************************************************************* 21 22 23 // comment this line because there is no need to store key pressed imformation 24 //unsigned int key_flag = 0x0; // 4bit for 4 key. 0->UP, 1->DOWN 25 26 void blink(unsigned int times) 27 { 28 unsigned int i; 29 for(i=0;i<times;i++) 30 { 31 LED->DATA = 0xf; 32 usleep(50000); 33 LED->DATA = 0x0; 34 usleep(50000); 35 } 36 } 37 38 void ISR_KEY(void *context,unsigned long id) 39 { 40 unsigned int i; 41 KEY->INTERRUPT_MASK = 0x0; // disable INT 42 usleep(20000); // delay 20ms 43 for(i=0;i<4;i++) 44 { 45 if(((KEY->EDGE_CAPTURE>>(3-i))&1) == 1) 46 { 47 // (3-i)bit of key 's INT is occur,without knowing posedge or negedge! 48 // KEY->EDGE_CAPTURE |= 1<<(3-i); // Should NOT be done 49 if(((KEY->DATA>>(3-i))&1) == 0) 50 { 51 // (3-i)bit of key is down 52 // ((key_flag>>(3-i))&1) |= 1; 53 // mark key_flag (3-i) bit to 1, meaning this bit(3-i) of key is down 54 LED->DATA |= 1<<(3-i); 55 // set corresponding bit of led to high 56 } 57 else 58 { 59 // (3-i)bit of key is down 60 // ((key_flag>>(3-i))&1) &= 0; 61 // mark key_flag (3-i) bit to 0, meaning this bit(3-i) of key is up 62 LED->DATA &= ~(1<<(3-i)); 63 // set corresponding bit of led to low 64 } 65 } 66 // else 67 // { 68 // // (3-i)bit of key 's INT is NOT occur, nothing happend to this bit key 69 // } 70 71 } 72 KEY->INTERRUPT_MASK = 0xF; // enable INT 73 } 74 75 int init_KEY(void) // Declaration the KEY_INT's ISR 76 { 77 KEY->INTERRUPT_MASK = 0xF; // enable INT 78 KEY->EDGE_CAPTURE = 0xF; // clear all pending bit 79 return alt_irq_register(PIO_KEY_IRQ,NULL,ISR_KEY); 80 } 81 82 void main() 83 { 84 printf("*************************************************\n"); 85 blink(3); 86 init_KEY(); 87 usleep(1000000); 88 blink(3); 89 90 while(1); 91 reture(0); 92 }

結論: INT初始化中,必須要先enable了再clear一下,否則掛掉!個人感覺這點最關鍵了,以前做ARM也是這樣(都是RISC還挺像的);
   ?ISR里,不用clear,進去時先disable了,出來時再enable了(這點貌似沒區別,但養成這習慣了,呵呵)。如果clear了,中斷響應有問題!

轉載于:https://www.cnblogs.com/lefroy/archive/2012/04/16/2452880.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Nios中PIO的INT的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。