logo资料库

sopc程序(c语言).doc

第1页 / 共15页
第2页 / 共15页
第3页 / 共15页
第4页 / 共15页
第5页 / 共15页
第6页 / 共15页
第7页 / 共15页
第8页 / 共15页
资料共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) { led = led << 1; dir = (dir ^ 0x1); if (led & 0x81) { } if (dir) { } else { } IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led); led = led >> 1; //若200000改为1000000则延迟时间为1s。 i = 0; while (i<200000) 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; IORD(base, 0) IOWR(base, 0, data) 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) #define IOWR_ALTERA_AVALON_PIO_DATA(base, data) #define IOADDR_ALTERA_AVALON_PIO_DIRECTION(base) __IO_CALC_ADDRESS_NATIVE(base, 1) #define IORD_ALTERA_AVALON_PIO_DIRECTION(base) #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) #define IOWR_ALTERA_AVALON_PIO_IRQ_MASK(base, data) #define IOADDR_ALTERA_AVALON_PIO_EDGE_CAP(base) __IO_CALC_ADDRESS_NATIVE(base, 3) #define IORD_ALTERA_AVALON_PIO_EDGE_CAP(base) #define IOWR_ALTERA_AVALON_PIO_EDGE_CAP(base, data) IORD(base, 1) IORD(base, 2) IOWR(base, 2, data) IORD(base, 3) 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) {
else dir=1; } 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)
switch(edge_capture) { case 0x00:break; case 0x01: case 0x02: case 0x04: case 0x08: count++; edge_capture=0;break; count--; edge_capture=0;break; count+=2; edge_capture=0;break; count-=2; edge_capture=0;break; } IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,count); } return 0; } 按键中断2: #include #include "system.h" #include "altera_avalon_pio_regs.h" #include "alt_types.h" void initpio(void) { { alt_u8 count=0; initpio(); while(1) { 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,0x00); IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0x00); } int main (void) { alt_u8 key,led; initpio(); while(1) { } return 0; } 代码(二): key=IORD_ALTERA_AVALON_PIO_DATA(KEY_PIO_BASE); led=key; IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
#include #include "system.h" #include "altera_avalon_pio_regs.h" #include "alt_types.h" #include "sys/alt_irq.h" volatile int edge_capture=0; 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); alt_u8 data1,data2,data3,data4; } int main (void) { data1=0x0f; data2=0x3c; data3=0xf0; data4=0xff; initpio(); while(1) { switch(edge_capture) { case 0x00:break; case 0x01: edge_capture=0; break; case 0x02: edge_capture=0; break; case 0x04: edge_capture=0; break; IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,data1); IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,data2); IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,data3);
case 0x08: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,data4); edge_capture=0; break; } } return 0; } 流水灯带按键控制 #include #include "system.h" #include "altera_avalon_pio_regs.h" #include "alt_types.h" #include "sys/alt_irq.h" volatile int count=0; const alt_u8 led_datac[4][8]={{0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF}, {0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01}, {0x01,0x07,0x03,0x0F,0x13,0x32,0x7F,0xFF}, {0x00,0x03,0x00,0x0F,0x10,0x30,0x70,0xF0}}; void key_interrupts(void* context,alt_u32 id) { IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PAUSE_BASE,0); if (count==3) else count=0; count++; } void initpio(void) { IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PAUSE_BASE,0x0f); IOWR(PAUSE_BASE,3,0x00); alt_irq_register(PAUSE_IRQ,NULL,key_interrupts); } int main (void) { int i,j; initpio(); while(1) { i--; } for(j=0;j<8;j++) {IOWR(LED_BASE,0,led_datac[count][j]); i=500000; while(i>0)
} return 0; } 实验三:定时器 usleep(1000000) 使用sleep函数的定时程序: #include "alt_types.h" #include #include #include "system.h" #include "altera_avalon_pio_regs.h" int main(void) { alt_u8 second=0x0; while(1) { second=0x0; second++; if (second==0x7) else IOWR(LED_PIO_BASE,0,second); usleep(1000000); } return 0; } 定时器 1
分享到:
收藏