您的位置:首页 > 移动开发 > IOS开发

nios II--实验5——定时器软件部分

2016-02-29 20:59 337 查看

软件开发

首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分;打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进行Workspace Launcher(工作空间)路径的设置,需要注意的是路径中不要含有空格等,然后单击OK即可。





 

新建工程。单击File -> New -> Nios II Application and BSP from Template,弹出Nios II Application and BSP from Template对话框。先选择对应的SOPC系统,单击SOPC Information File name后面的浏览按钮,选择之前硬件部分做好的软核文件,后缀名为.sopcinfo,这里一定要注意,选择的文件一定要对应起来,否则会因为软硬不匹配导致系统失败。这里选择的lab4_timer.sopcinfo,然后系统会自动读取CPU name,不用再进行设置,下面填写Project name,这里填写为lab4_timer,工程模板(Project template)使用默认的即可。然后单击Finish完成即可。这时候会在左侧的Project Explorer中生成两个工程文件。



代码设计具体看源工程

右击工程,选择Nios II -> BSP Editor,进入Nios II BSP Editor配置界面。主要在main选项卡下hall中进行配置。然后单击Generate,生成BSP库。生成完成后,单击Exit退出即可。





编译工程。右击选择Build Project。第一次编译的话,时间也会比较常,耐心等待一下。

编译完成后,先将.sof文件下载到FPGA;



 

 

 

 

 
右击工程,选择Run As -> Nios II Hardware,弹出Run Configurations对话框,默认Project选项卡中Project name和Project ELF file name应该都是有内容的,没有的选一下。然后进入Target Connection选项卡,Connections中如果没有东西的话,单击右侧的Refresh Connection来查找下载器,查找后单击System ID Prroperties…,进行系统ID检测,检查是否是之前设置的ID号,无误后点击Apply,然后再点击Run,这是程序会被自动下载,最终在Nios II Console选项卡中会显示下载完成后程序运行的结果。





运行结果,板上的四个LED灯流动显示,表明测试成功!通过修改定时器的定时周期可以修改led流动显示的速度。



 

实验代码

/*

* "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 "system.h"

#include "altera_avalon_pio_regs.h"

#include "altera_avalon_timer_regs.h"

#include "alt_types.h"

#include "sys/alt_irq.h"

#include <stdio.h>

#include <unistd.h>

#include <io.h>

#include <string.h>

/********************************

variables

********************************/

void init_timer(void);

int i =
0, j =
0, flag;

alt_u32 timer_prd[4]
=
{5000000,
10000000,
50000000,
100000000};

// 这四个是定时器的时钟数,定时时间=定时器的时钟数/定时器的时钟周期

// 该实验中系统时钟为 50MHz,则上述定时时间为{0.1s,0.2s,1s,2s}

/*

*================================functions================================

Name: main

Description:

*=========================================================================

*/

int main(void)

{

    init_timer();
// 初始化定时器

    while(1);

    return
0;

}

/*

*================================functions================================

Name:         ISR_handle_time1

Description:

*=========================================================================

*/

void ISR_handle_timer1(void
*context)

{

    // 控制 4 个流水灯闪烁

    IOWR_ALTERA_AVALON_PIO_DATA( LED_PIO_BASE,
1<<i );

    i++;

    if(
4
== i )

    i =
0;

    // 清除中断标志寄存器

    IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_0_BASE,
0x0
);

}

/*

*================================functions================================

Name:             ISR_handle_timer2

Description:     通过定时器 2 来改变定时器 1 的周期,改变后需要重启定时器

*=========================================================================

*/

void ISR_handle_timer2(void
*context)

{

    // 改变定时器 1 的周期

    IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_0_BASE, timer_prd[j]
);

    IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_0_BASE, timer_prd[j]>>16
);

    // 重启定时器 1

    IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_0_BASE,
0x07
);

    // 闪烁频率先高后低然后又变高

    if(
0
== j )

    flag =
0;

    if(
3
== j )

    flag =
1;

    if(
0
== flag )

    j++;

    else

    j--;

    // 清除中断标志寄存器

    IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_1_BASE,
0x0
);

}

/*

*================================functions================================

Name:             init_timer

Description:     定时器初始化

*=========================================================================

*/

void init_timer(void)

{

    // timer1

    // 清除中断标志寄存器

    IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_0_BASE,
0x00
);

    // 设置定时周期 2s,输入的是时钟周期数

    IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_0_BASE,
10000000
);

    IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_0_BASE,
10000000>>16
);

    IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_0_BASE,
0x07
);
// 使能中断

    alt_ic_isr_register( TIMER_0_IRQ_INTERRUPT_CONTROLLER_ID,

    TIMER_0_IRQ, ISR_handle_timer1,
NULL,
0x0
);
// 注册中断

    // timer2

    // 清除中断标志寄存器

    IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_1_BASE,
0x00
);

    // 设置定时周期 10s,输入的是时钟周期数

    IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_1_BASE,
50000000
);

    IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_1_BASE,
50000000>>16
);

    IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_1_BASE,
0x07
);
// 使能中断

    alt_ic_isr_register( TIMER_1_IRQ_INTERRUPT_CONTROLLER_ID,

    TIMER_1_IRQ, ISR_handle_timer2,
NULL,
0x0
);
// 注册中断

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: