当前位置:文档之家› 万年历电子钟设计报告

万年历电子钟设计报告

课程设计报告课程设计名称 SOPC原理及应用专业电子科学与技术班级电子13-1班学号姓名郑航指导教师冯丽成绩2016年1月13日目录一、设计目的 (1)二、设计内容要求 (1)三、系统软、硬件需求分析 (1)1. 硬件系统组成规划 (1)2. 软件系统规划 (2)四、设计步骤 (3)3. 新建工程“count_binary” (3)4. 添加ip核 (4)5. 添加SDRAM Controller (5)6. 添加flash (6)7. 添加外部RAM总线(Avalon三态桥) (7)8. 添加pio核 (7)9. 添加cpu核 (8)10. 添加LCD核 (9)11. 自动分配基地址并生成系统 (9)12. 设置顶层模块图 (10)13. 管脚分配并编译 (11)14. 启动Nios II IDE,新建工程 (12)15. 导入设计程序 (12)16. 编译工程并烧录 (13)五、设计结果 (14)六、源程序 (16)1. 程序......................................... 错误!未定义书签。

2. 程序......................................... 错误!未定义书签。

3. 程序......................................... 错误!未定义书签。

4. 程序 (16)七、实验心得 (28)项目基于NiosII系统的电子钟设计一、设计目的1.掌握基本的开发流程。

2.熟悉QUARTUS II软件的使用。

3.熟悉NIOS II软件的使用。

4.掌握SOPC硬件系统的搭建和NIOSII软件编程方法。

5.掌握SOPC系统设计方法。

6.进一步了解简单的设置及其编程。

二、设计内容要求NiosII系统的硬件设计,软件设计,该系统能实现一个电子钟功能。

三、系统软、硬件需求分析1.硬件系统组成规划根据系统要实现的功能和开发板配置,本项目中需要用到的Cyclone II开发板上的外围器件有:LCD:电子钟显示屏幕按钮:电子钟设置功能键Flash存储器:存储软、硬件程序SRAM存储器:程序运行时将其导入SRAM根据所用到的外设和器件特性,在SOPC Builder中建立系统要添加的模块包括:NiosII CPU定时器,按键PIO,LCD,外部RAM总线(Avalon三态桥),外部RAM接口,外部Flash接口。

2.软件系统规划要实现系统所需的功能,大量的工作应该集中在软件设计和优化上。

电子钟的软件功能主要分为显示、设置和时间算法三大部分。

①显示部分显示部分的功能包括:显示日期(年-月-日)显示时间(小时:分钟:秒)②设置部分设置部分的功能包括:设置小时,设置分钟,设置年份,设置月份,设置日期。

设置部分的程序主要用在对按键的响应。

在编写程序前要对开发板上的四个按键功能进行如下分配:SW0:换行键,可选择设置第一行或者第二行SW1:选项数字减少SW2:选项数字增加SW3:选择键,可依次选择设置小时,分钟或者年份,月份和日期③时间算法部分时间算法部分的功能包括:(a). 时间累加(b). 确定每个月的最大天数,使年、月、日能正确累加通过对软件要完成的功能进行分析可以看出,这里只有按键响应和显示部分需要涉及硬件,其它子程序可在进行硬件设计的同时开始编写。

③软件流程图四、设计步骤3.新建工程“count_binary”4.添加ip核点击 Tools->SOPC Builder,进入SOPC Builder,运行SOPC Builder,添加ip核。

5.添加SDRAM Controller6.添加flashAddress Wicth设置为22,Data Wicth设置为16。

Setup、Wait、Hold都设置为40。

7.添加外部RAM总线(Avalon三态桥)8.添加pio核添加一组PIO(Parallel I/O)宽度设置为4位,方向设置为输入,并命名为button_pio。

9.添加cpu核双击Avalon Components ( NiosII Processor,添加一个 NiosII/s 处理器并将Reset Vector改为cfi _flash_0,Exception Vector改为sdram。

10.添加LCD核11.自动分配基地址并生成系统最小系统NIos_small所需组件添加完毕,自动分配基地址和中断,分别选择“System|Auto‐Assign Base Adresses”和“System| Auto‐Assign IRQs”。

并把ip核名字改为如下图。

点击“Generate”,SOPC Builder会提示生成系统的进程,系统生成完成的时会提示“SUCCESS:SYSTEM GENERATION COMPLETED”。

单击“EXIT”按钮退出SOPC Builder。

12.设置顶层模块图回到QuartusII里面,新建一个图形设计原理图,托出刚才SOPC Builder 生成的系统图形,再设计一个PLL锁相环,如图:13.管脚分配并编译打开,把需要用到的管脚复制到qsf文件中,并把引脚的名字改成与qsf 文件中的名字一致,未使用的管脚置成三态,设置完开始编译。

14.启动Nios II IDE,新建工程至此硬件设置已完成,动Nios II IDE,新建工程选择“File| New | Project”。

在New Project对话框中选择“NiosC/C++Application” 单击“Next”,左栏中选择“Blank Project”模板,工程名为“clock”,点击“Browse…”选SOPC Builder System选择刚生成的最小系统文件“”,15.导入设计程序把编写好的、、、文件复制路径count_binary\software\clock里,复制完成后,回到NiosII IDE下,右键点击clock,选择Refresh,刷新一下软件工程列表,打开加号就会看到添加的四个文件,双击可以看到C语言程序源码。

16.编译工程并烧录右击工程名选择“System Library Properties”,进入系统设置界面。

设置完成后选择“Project| Build all”进行编译。

编译完成后“Tools|Quartus II Programmer…”,将程序下载至开发板上的FPGA。

再选择“Run|Run As|Nios II Hardware”,在开发板的16x2 LCD上观看设计结果。

五、设计结果六、源程序程序#include ""#include <>#include <>#include <>#include ""#include "sys/"#include ""#include ""#include ""volatile int edge_capture;void LCD_Init(){lcd_write_cmd(LCD_BASE,0x38);usleep(2000);lcd_write_cmd(LCD_BASE,0x0c);usleep(2000);usleep(2000);lcd_write_cmd(LCD_BASE,0x06);usleep(2000);lcd_write_cmd(LCD_BASE,0x80);usleep(2000);}void LCD_Show_Text(char* Text){int i;for(i=0;i<strlen(Text);i++){lcd_write_data(LCD_BASE,Text[i]); usleep(2000); }}void LCD_Line1(){lcd_write_cmd(LCD_BASE,0x80);usleep(2000);}void LCD_Line2(){usleep(2000);}static void handle_button_interrupts(void* context,alt_u32 id){volatile int* edge_capture_ptr=(volatile int*)context;*edge_capture_ptr=IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE,0);}static void init_button_pio(){void* edge_capture_ptr=(void*)&edge_capture;IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE,0xf);IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE,0x0);alt_irq_register(BUTTON_PIO_IRQ,edge_capture_ptr,handle_button_interr upts);}void delay(unsigned int x){while(x--);}int check_month(int month){ if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))return 1;if((month==4)||(month==6)||(month==9)||(month==11))return 2;}int check_year(int year){if(((year%400)==0)||(((year%4)==0)&& ((year%100)!=0))) return 1;else return 0;}int main(void){int screen=0;int pos=0;int year,month,day,hour,minute,second;unsigned long sum;char date[16];char time[16];int year1=6;int year2=0;int year3=0;int year4=2;int month1=1;int month2=0;int day1=1;int day2=0;int hour4,hour3,hour2,hour1,minute2,minute1,second2,second1; unsigned int screenflag;hour=0;minute=0;second=0;year=2006;month=1;day=1;init_button_pio();LCD_Init();while(1){if(pos>=3)pos=0;if(screen>=2)screen=0;if(screen==0)screenflag=8;else screenflag=0;usleep(1000000);if(second<59)second++;else{second=0;if(minute<59)minute++; else{minute=0;if(hour<23)hour++;else{hour=0;if(day<30)day++;else{day=1;if(month<12)month++;else{month=1;if(year<9999)year++;else year=2005; }}}}}switch(edge_capture){case 0x08:pos=pos+1;break;case 0x02:if(pos==0){if(screen==0){if(day>1)day--;else{if(check_month(month)==0)day=30;if(check_month(month)==1)day=31;if(check_month(month)==2){if(check_year(year))day=29;else day=28; }}}if(screen==1){if(second>0)second--;else second=59;}} if(pos==1){if(screen==0){if(month>1)month--;else month=12;}if(screen==1){if(minute>0)minute--;else minute=59;}} if(pos==2){if(screen==0){if(year>0)year--;else year=2005;}if(screen==1){if(hour>0)hour--;else hour=23;}}break;case 0x04:if(pos==0){if(screen==0){if(check_month(month)==0){if(day<30)day++;else day=1;}if(check_month(month)==1){if(day<31)day++;else day=1;}if(check_month(month)==2){if(check_year(year)){if(day<29)day++;else day=1;} else{if(day<28)day++;else day=1;}}}if(screen==1){if(second<59)second++;else second=0;}}if(pos==1){if(screen==0){if(month<12)month++;else month=1;}if(screen==1){if(minute<59)minute++;else minute=0;}} if(pos==2){if(screen==0){if(year<9999)year++;else year=2005;}if(screen==1){if(hour<23)hour++;else hour=0; }} break;case 0x01:screen++;break; }edge_capture=0;{year4=year/1000;year3=(year-year4*1000)/100;year2=(year-year4*1000-year3*100)/10;year1=year%10;month2=month/10;month1=month%10;day2=day/10;day1=day%10;LCD_Line1();date[0]=year4+0x30;date[1]=year3+0x30;date[2]=year2+0x30;date[3]=year1+0x30;date[4]=' ';date[5]=' ';date[6]=month2+0x30;date[7]=month1+0x30;date[8]=' ';date[9]=' ';date[10]=day2+0x30;date[11]=day1+0x30;date[12]=' ';date[13]=' ';date[14]=' ';date[15]=' ';LCD_Show_Text(date);}{ hour4=0;hour3=0;hour2=hour/10;hour1=hour%10;minute2=minute/10;minute1=minute%10;second2=second/10;second1=second%10;time[0]=' ';time[1]=' ';time[2]=hour2+0x30;time[3]=hour1+0x30;time[4]=' ';time[5]=' ';time[6]=minute2+0x30;time[7]=minute1+0x30;time[8]=' ';time[9]=' ';time[10]=second2+0x30;time[11]=second1+0x30; time[12]=' ';time[13]=' ';time[14]=' ';time[15]=' ';LCD_Line2();LCD_Show_Text(time);}}}七、实验心得指导老师签字:年月日。

相关主题