📄 10473f393b2e001e1d49a4c70fa2aa82
字号:
/* * "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 "alt_types.h"#include "sys/alt_irq.h"#include "altera_avalon_timer_regs.h"#define alt_cpu_freq 50000000 /* * Use alt_main as entry point for this free-standing application */ int count = 0; int led = 0; //volatile alt_u8 count;static void handle_Timer0_interrupts(void* context, alt_u32 id){ alt_u8 a; volatile alt_u8 *countptr = (volatile alt_u8 *)context; IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);//清TO标志 a = *countptr; //取出count中的值 a=a<<1; //作移一位 if (a == 0x10) a=1; //我只有4个led,所以要让led循环闪烁 *countptr=a; //重新赋值给count //IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, a); //写到LED输出口 printf("Value shift! (%d)\n", a);} static void USER_IRQ(void* context,alt_u32 id){ IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE,0);//清除定时器状态寄存器。 if(led == 0xff) { led = 0x00; } else { led = 0xff; } IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, led); if (count == 0xff) { count = 0; } else { count++; } printf("One second passed! (%d)\n", count);}int main (void){ //******************************************************** /*count=1; alt_irq_register( TIMER_0_IRQ, (void *)&count, handle_Timer0_interrupts); //注册中断函数 //因为我在添加定时器timer_0的时候,设置了初始值为 定时周期500ms的值,我的晶振是50Mhz, //因为只是简单测试一下定时中断代码的书写方法,所以这里没有重新设置预制值,直接采用初始化时自动 //设置的初始值,当然,也是我懒得去计算初始值,如果要重新设置为不同的定时周期,用下面的2个函数 // IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE, TimerValueLow); // IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE, TimerValueHigh); // 其中TimerValueLow和TimerValueHigh是你要设置的低16位和高16位的定时器初值。 //定时器工作时是将这2个寄存器的值调入32位计数器,然后根据CPU的时钟,逐步递减计数器 //的值,直到减到0为止,然后触发中断,并且再次从预制寄存器中将预制值调入32位计数器中, //再次重复【递减->到0->中断&重新装载初值 】的这个过程 IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE,0xF080);//定义定时器的计数周期; IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE,0x02FA); IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, 7); //启动timer允许中断,连续计数 while (1) {;} */ //******************************************************** //******************************************************** /*volatile unsigned long timer_capture; IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE,0xF080);//定义定时器的计数周期; IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE,0x02FA); IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE,0x0007);//写定时器的控制寄存器; //while(1) { alt_irq_register( TIMER_0_IRQ, (void*)timer_capture, USER_IRQ );//中断注册函数 } while(1){}*/ //******************************************************** /* alt_u8 led = 0x0; int t = 0; IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE, (short)(alt_cpu_freq&0x0000ffff)); IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE, (short)((alt_cpu_freq>>16)&0x0000ffff)); IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, ALTERA_AVALON_TIMER_CONTROL_START_MSK + ALTERA_AVALON_TIMER_CONTROL_CONT_MSK); while(1) { if(IORD_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE) & ALTERA_AVALON_TIMER_STATUS_TO_MSK) { printf("One second passed! (%d)\n", t++); IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0x0); if(led == 0xff) { led = 0x00; } else { led = 0xff; } IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, led); } } */ while(1) { led = 0x00; count = 0; while(count < 0x50000) { count++; } IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, led); led = 0xff; count = 0; while(count < 0x50000) { count++; } IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, led); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -