SOPC程序代码
- 格式:doc
- 大小:142.50 KB
- 文档页数:15
实验一:
流水灯范例
#include "system.h"
#include "altera_avalon_pio_regs.h"
//该文件位于c:\altera\72\ip\sopc_builder_ip\altera_avalon_pio\inc
#include "alt_types.h"
int main (void) __attribute__ ((weak, alias ("alt_main")));
int alt_main (void)
{
alt_u8 led = 0x2;
alt_u8 dir = 0;
volatile int i;
while (1)
{
if (led & 0x81)
{
dir = (dir ^ 0x1);
}
if (dir)
{
led = led >> 1;
}
else
{
led = led << 1;
}
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
i = 0;
while (i<200000) //若200000改为1000000则延迟时间为1s。
i++;
}
return 0;
}
Alt_types.h文件代码:
typedef signed char alt_8;
typedef unsigned char alt_u8;
typedef signed short alt_16;
typedef unsigned short alt_u16;
typedef signed long alt_32;
typedef unsigned long alt_u32;
typedef long long alt_64;
typedef unsigned long long alt_u64;
altera_avalon_pio_regs.h文件代码
#define IOADDR_ALTERA_AVALON_PIO_DATA(base)
__IO_CALC_ADDRESS_NATIVE(base, 0)
#define IORD_ALTERA_AVALON_PIO_DATA(base) IORD(base, 0)
#define IOWR_ALTERA_AVALON_PIO_DATA(base, data) IOWR(base, 0, data)
#define IOADDR_ALTERA_AVALON_PIO_DIRECTION(base)
__IO_CALC_ADDRESS_NATIVE(base, 1)
#define IORD_ALTERA_AVALON_PIO_DIRECTION(base) IORD(base, 1)
#define IOWR_ALTERA_AVALON_PIO_DIRECTION(base, data) IOWR(base, 1, data)
#define IOADDR_ALTERA_AVALON_PIO_IRQ_MASK(base)
__IO_CALC_ADDRESS_NATIVE(base, 2)
#define IORD_ALTERA_AVALON_PIO_IRQ_MASK(base) IORD(base, 2)
#define IOWR_ALTERA_AVALON_PIO_IRQ_MASK(base, data) IOWR(base, 2, data)
#define IOADDR_ALTERA_AVALON_PIO_EDGE_CAP(base)
__IO_CALC_ADDRESS_NATIVE(base, 3)
#define IORD_ALTERA_AVALON_PIO_EDGE_CAP(base) IORD(base, 3)
#define IOWR_ALTERA_AVALON_PIO_EDGE_CAP(base, data) IOWR(base, 3, data)
流水灯程序1
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
int main (void)
{
alt_u8 led = 0x1;
alt_u8 dir = 0;
alt_u8 temp;
volatile int i;
while (1)
{
if (led & 0x80)
{ dir=1; }
else
if (led == 0x01)
{ dir=0;}
if (dir==0 )
{temp=led;
led = led << 1;
led=led|temp;}
else
{led=led>>1;}
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
i = 0;
while (i<500000)
i++;
}
return 0;
}
流水灯程序2
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
int main (void)
{alt_u8 led_data[8]={0x01,0xff,0xcd,0x04,0x40,0xdd,0xfe,0x02};
int count=0;
alt_u8 led = 0x1;
alt_u8 dir = 0;
alt_u8 temp;
volatile int i;
while (1)
{ if (count==7)
{count=0;}
else
{count++;}
led=led_data[count];
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
i = 0;
while (i<500000)
i++; }
return 0;
}
实验二:按键中断
使用7段LED模块:
#include "basic_io.h"
Alt_u32 data;
Data=0x01020304;
seg7_show(SEG7_LUT_8_0_BASE,data);
按键输入中断1:
#include
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
volatile int edge_capture=0; //volatile:易变的,反复无常,可使变量随时刷新,该变量不可优化处理。
void key_interrupts(void* context,alt_u32 id)
{
volatile int* edge_capture_ptr = (volatile int*)context;
*edge_capture_ptr= IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0);
}
void initpio(void)
{
void* edge_capture_ptr = (void*)&edge_capture;
IOWR_ALTERA_AVALON_PIO_DIRECTION(LED_PIO_BASE,0xff);
IOWR_ALTERA_AVALON_PIO_DIRECTION(KEY_PIO_BASE,0x00);
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_PIO_BASE,0xff);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0x00);
alt_irq_register(KEY_PIO_IRQ,edge_capture_ptr,key_interrupts);
}
int main (void)