C语言基于NIOSII的软件开发及流水灯设计
一、Quartus II 12.1 (32-Bit)进行硬件设计
1.所需要的系统元器件组成
2.系统电路图
二、Nios II 12.1 Software Build Tools for Eclipse进行软件学号及流水灯设计
1.程序代码
/* * "Hello World" example. * * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT * device in your system's hardware. * The memory footprint of this hosted application is ~69 kbytes by default * using the standard reference design. * * For a reduced footprint version of this template, and an explanation of how * to reduce the memory footprint for a given application, see the * "small_hello_world" template. * */ #include <stdio.h> #include "system.h" #include "altera_avalon_pio_regs.h" #include "io.h" #include "unistd.h" #include "alt_types.h" typedef unsigned int u16; typedef unsigned char u8; #define led_on IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00) #define led_off IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff) #define cs_off IOWR_ALTERA_AVALON_PIO_DATA(DTUBE_CS_RL_BASE,0xff) u8 csdm[4]={0xf7,0xfb,0xfd,0xfe}; u8 xhdm[4]={0x1e,0x1e,0x14,0x14}; u8 i,j,k,v,h; void delay(u16 i) { while(i--); } void led1() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { k=0; for(i=0;i<9;i++) { j=0x01<<i; k=k|j;//保留上一状态 IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); delay(1000000); //大约延时1s } } } void led2() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { for(i=0;i<9;i++) { j=(0x80>>i); IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,j); delay(1000000); } } } void led3() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); delay(50000); for(v=0;v<2;v++) { for(i=0;i<9;i++) { j=0xff<<i; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,j); delay(1000000); } IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); } } void led4() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); delay(50000); for(v=0;v<2;v++) { for(i=0;i<9;i++) { j=0xff>>i; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,j); delay(1000000); } IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); } } void led50() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); delay(50000); for(v=0;v<2;v++) { k=0x01; j=0xff; for(i=0;i<8;i++) { if(i%2==0) { h=k^j; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,h); k=0x01<<i+2; k=k|h; delay(1000000); } } j=0x20; k=0xff; for(i=0;i<8;i++) { if(i%2!=0) { h=j^k; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,h); k=0x20<<i+2; k=h|k; delay(1000000); } } IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0xff); } } void led5() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { h=0x01; for(i=0;i<9;i++) { if(i%2==0) { k=~h;//保留上一状态 IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); j=0x01<<i+2; h=h|j; delay(1000000); //大约延时1s } } h=0x02; for(i=0;i<9;i++) { if(i%2==0) { k=~h;//保留上一状态 IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); j=0x02<<i+2; h=h|j; delay(1000000); //大约延时1s } } } } void led6() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { j=0x40; for(i=0;i<9;i++) { if(i%2==0) { k=~j; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); j=0x40>>i+2; delay(1000000); //大约延时1s } } j=0x80; for(i=0;i<9;i++) { if(i%2==0) { k=~j; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); j=0x80>>i+2; delay(1000000); //大约延时1s } } } } void led7() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { for(i=0;i<8;i++) { if(i%2!=0) { j=0x80>>i; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,j); delay(1000000); k=j<<1; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); delay(1000000); } } } } void led8() { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0x00); delay(50000); for(v=0;v<2;v++) { for(i=0;i<8;i++) { if(i%2!=0) { j=0x01<<i; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,j); delay(1000000); k=j>>1; IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,k); delay(1000000); } } } } void xhxs() { for(i=0;i<4;i++) { IOWR_ALTERA_AVALON_PIO_DATA(DTUBE_CS_RL_BASE,csdm[i]); IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,xhdm[i]); delay(100); } return 0; } int main() { led_off; while(1) { if(IORD_ALTERA_AVALON_PIO_DATA(SWITCH0_BASE)==0) { xhxs(); } if(IORD_ALTERA_AVALON_PIO_DATA(SWITCH1_BASE)==0) { cs_off; led1(); led2(); led3(); led4(); led5(); led6(); led7(); led8(); } } return 0; }
2.实验现象