1
#include <stdlib.h>
2
#include <io.h>
3
#include "sys/alt_irq.h"
4
#include "altera_avalon_pio_regs.h"
5
#include "system.h"
6
#ifdef PIO_BUTTON_BASE
7
/* 按键中断服务程序*/
8
static void handle_button_interrupts(void* context, alt_u32 id)
9
{
10
/* Cast context to edge_capture's type. It is important that this be
11
* declared volatile to avoid unwanted compiler optimization.
12
*/
13
volatile int* edge_capture_ptr = (volatile int*) context;
14
/* Store the value in the Button's edge capture register in *context. */
15
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE);
16
/* Reset the Button's edge capture register. */
17
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0x00);
18
}
19
/* 按键IO初始化 */
20
static void init_button_pio()
21
{
22
/* Recast the edge_capture pointer to match the alt_irq_register() function
23
* prototype. */
24
void* edge_capture_ptr = (void*) &edge_capture;
25
/* Enable all 4 button interrupts. */
26
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BUTTON_BASE, 0xff);
27
/* Reset the edge capture register. */
28
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE, 0x00);
29
/* Register the interrupt handler. */
30
alt_irq_register(PIO_BUTTON_IRQ, edge_capture_ptr,
31
handle_button_interrupts );
32
}
33
#endif
34
void delay(int i)
35
{
36
int j;
37
for(j=0;j<i;j++);
38
}
39
int main (void) __attribute__ ((weak, alias ("alt_main")));
40
int alt_main(void)
41
{
42
43
alt_irq_init(ALT_IRQ_BASE); //使能中断
44
45
init_button_pio();
46
while(1)
47
{
48
switch(edge_capture)
49
{
50
case 0x01:
51
{
52
led=led^0x01;
53
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
54
break;
55
}
56
case 0x02:
57
{
58
led=led~0x02;
59
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
60
break;
61
}
62
case 0x04:
63
{
64
led=led^0x04;
65
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
66
break;
67
}
68
case 0x08:
69
{
70
led=led^0x08;
71
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
72
break;
73
}
74
case 0x10:
75
{
76
led=led^0x10;
77
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
78
break;
79
}
80
case 0x20:
81
{
82
led=led^0x20;
83
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
84
break;
85
}
86
case 0x40:
87
{
88
led=led^0x40;
89
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
90
break;
91
}
92
case 0x80:
93
{
94
led=led^0x80;
95
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
96
break;
97
}
98
default:
99
{
100
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,0x00);
101
edge_capture=0;
102
break;
103
104
}
105
}
106
}
107
return 0;
108
}
#include <stdlib.h>2
#include <io.h>3
#include "sys/alt_irq.h"4
#include "altera_avalon_pio_regs.h"5
#include "system.h"6
#ifdef PIO_BUTTON_BASE7
/* 按键中断服务程序*/8
static void handle_button_interrupts(void* context, alt_u32 id)9
{10
/* Cast context to edge_capture's type. It is important that this be11
* declared volatile to avoid unwanted compiler optimization.12
*/13
volatile int* edge_capture_ptr = (volatile int*) context;14
/* Store the value in the Button's edge capture register in *context. */15
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE);16
/* Reset the Button's edge capture register. */17
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0x00);18
}19
/* 按键IO初始化 */20
static void init_button_pio()21
{22
/* Recast the edge_capture pointer to match the alt_irq_register() function23
* prototype. */24
void* edge_capture_ptr = (void*) &edge_capture;25
/* Enable all 4 button interrupts. */26
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BUTTON_BASE, 0xff);27
/* Reset the edge capture register. */28
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE, 0x00);29
/* Register the interrupt handler. */30
alt_irq_register(PIO_BUTTON_IRQ, edge_capture_ptr,31
handle_button_interrupts );32
}33
#endif34
void delay(int i)35
{36
int j;37
for(j=0;j<i;j++);38
} 39
int main (void) __attribute__ ((weak, alias ("alt_main")));40
int alt_main(void)41
{42
43
alt_irq_init(ALT_IRQ_BASE); //使能中断44

45
init_button_pio();46
while(1)47
{48
switch(edge_capture)49
{50
case 0x01:51
{52
led=led^0x01;53
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);54
break;55
}56
case 0x02:57
{58
led=led~0x02;59
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);60
break;61
}62
case 0x04:63
{64
led=led^0x04;65
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);66
break;67
}68
case 0x08:69
{70
led=led^0x08;71
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);72
break;73
}74
case 0x10:75
{76
led=led^0x10;77
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);78
break;79
}80
case 0x20:81
{82
led=led^0x20;83
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);84
break;85
}86
case 0x40:87
{88
led=led^0x40;89
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);90
break;91
}92
case 0x80:93
{94
led=led^0x80;95
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);96
break;97
}98
default:99
{100
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,0x00);101
edge_capture=0;102
break;103
104
}105
}106
}107
return 0;108
}
注意,在添加按键PIO时一定要使能中断!
研究过一段时间的Nios II,感觉还是很有收获,深刻体会到了SOPC技术的灵活性,但目前速度方面是其最大的缺点,相信Nios II会慢慢流行起来....


浙公网安备 33010602011771号