⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5020b0496a3d001d1edb944bae15cd54

📁 利用Nios Ⅱ软核处理器
💻
字号:
#include"alt_types.h"
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include "system.h"
#include"sys/alt_irq.h"
#include"altera_avalon_pio_reg.h"
//#include"count_binary.h"
#include"lcd.h"
volatile int edge_capture;

void LCD_Init()
{
	//LCD初始化
	lcd_write_cmd(LCD_16207_BASE,0x38);
	usleep(2000);
	lcd_write_cmd(LCD_16207_BASE,0x0C);
	usleep(2000);
	lcd_write_cmd(LCD_16207_BASE,0x01);
	usleep(2000);
	lcd_write_cmd(LCD_16207_BASE,0x06);
	usleep(2000);
	lcd_write_cmd(LCD_16207_BASE,0x80);
	usleep(2000);
}
void LCD_Show_Text(char* Text)
{
	//LCD输出格式
	int i;
	for(i=0;i<strlen(Text);i++)
	{
		lcd_write_data(LCD_16207_BASE,Text[i]);
		usleep(2000);
	}
}
void LCD_Line1()
{
	//向LCD写命令,第一行
	lcd_write_cmd(LCD_16207_BASE,0x80);
	usleep(2000);
}
void LCD_Line2()
{
	//向LCD写命令,第二行
	lcd_write_cmd(LCD_16207_BASE,0xC0);
	usleep(2000);
}

static void handle_button_interrupts(void* context,alt_u32 id)
{
	volatile int* edge_capture_ptr=(volatile int*)context;
	/*存储按钮的值到边沿捕获寄存器*/
	IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
	/*复位边沿捕获寄存器*/
	IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE,0);
}

/*初始化button_pio*/
static void init_button_pio()
{
	void* edge_capture_ptr=(void*)&edge_capture;
	/*开放全部四个按钮的中断*/
	IOWR_ALTERA_AVALON_PIO_EDGE_CAP(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_interrupts);
}


void delay(unsigned int x)
{
	while(x--);
}
int check_month(int month)
{
	//若是1,3,5,7,8,10,12月,则每月31天,程序返回1   
	if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
		return 1;
	//若是4,6,9,11月,则每月30天,程序返回0
	if((month==4)||(month==6)||(month==9)||(month==11))
		return 0;
	//若是2月,程序返回2,具体的天数还要根据年份的判断来确定
	if(month==2)
		return 2;
}

/*计算闰年,方法是:公元纪年可以被4整除,即为闰年可以被100整除但不能被400整除的为平年;既可被100整除又可被400整除的是闰年*/
int check_year(int year)
{
	if(((year%400)==0)||(((year%4)==0)&&((year%100)!=0)))
		return 1;
	//是闰年返回1
	else 
		return 0;
	//是平年返回0
}
int main(void)
{
	int screen=0;//共两行,一行显示年月日,一行显示时间
	int pos=0;//每行都有三个位置,第一行显示年月日,第二行显示时间
    int year,month,day,hour,min,sec;
	//unsigned long sum;//sum要设置为长整型变量,否则会溢出
	char date[16];
	char time[16];
	int year1=8;
	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,min2,min1,sec2,sec1;
	unsigned int screenflag;
	hour=0;min=0;sec=0;year=2008;month=1;day=1;
	//年月日时分秒初始化
  alt_irq_init(ALT_IRQ_BASE);
	init_button_pio();
	LCD_Init();
	while(1)
	{
		if(pos>=3)
			pos=0;//每行共三个位置0,1,2,超过了2就马上清零
		if(screen>=2)
			screen=0;//LCD显示共两行0,1,超过了1就马上清零
		//na_LED8->np_piodata=1<<pos;//用一个LED指示当前调整的位置
		if(screen==0) 
			screenflag=8;
		else screenflag=0;
		IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,(1<<pos)|screenflag);
		usleep(1000000);//等待1秒的定时时间
		if(sec<59) sec++;
		else 
		{
			sec=0;
			if(min<59) min++;
			else
			{
				min=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=2000;
						}
					}
				}
			}
		}
		
    
    switch(edge_capture)//检测按钮
    {
    case 0x08:pos=pos+1; break;  //改变调整位置
      
      /* 对数据进行加减操作的case: case 0x02和case 0x04
         根据当前调整位置,判断当前屏幕显示的是年、月、日还是时、分、秒,
         然后决定是对年月日进行加减还是对时分秒进行加减*/
    
    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)==1) day=29;
              else day=28;
            }
          }
        }
          if(screen==1)
          {
            if(sec>0) sec--;
            else sec=59;
          }
      }
      if(pos==1)
      {
        if(screen=0)
        {
          if(month>1) month--;
          else month=12;
        }
        if(screen==1)
        {
          if(min>0) min--;
          else min=59;
        }
      }
      if(pos==2)
      {
        if(screen==0)
        {
          if(year>0) year--;
          else year=2000;
        }
        if(screen==1)
        {
          if(hour>0) hour--;
          else hour=23;
        }
      }
      break;
    case 0x04://对当前位置上的数据执行加操作
      if(pos==0)
      {
        if(screen==0)
        {
          if(check_month(month)==0) day=30;
          if(check_month(month)==1) day=31;
          if(check_month(month)==2) 
          {
            if(check_year(year)==1) 
            {
              if(day<29) day++;
              else day=1;
            }
            else
            {
              if(day<28) day++;
              else day=1;
            }
          }
        }
        if(screen==1)
          {
            if(sec<59) sec++;
            else sec=0;
          }
      }
      if(pos==1)
      {
        if(screen=0)
        {
          if(month<12) month++;
          else month=1;
        }
        if(screen==1)
        {
          if(min<59) min++;
          else min=0;
        }
      }
      if(pos==2)
      {
        if(screen==0)
        {
          if(year<9999) year++;
          else year=2000;
        }
        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;
          min2=min/10;min1=min%10;
          sec2=sec/10;sec1=sec%10;
          time[0]=' ';time[1]=' ';
          time[2]=hour2+0x30;time[3]=hour1+0x30;
          time[4]=' ';time[5]=' ';
          time[6]=min2+0x30;time[7]=min1+0x30;
          time[8]=' ';time[9]=' ';
          time[10]=sec2+0x30;time[11]=sec1+0x30;
          time[12]=' ';time[13]=' ';
          time[14]=' ';time[15]=' ';
          LCD_Line2();
          LCD_Show_Text(time);
        }
        //将数据转换为显示屏可以接受的格式
        //sum=year4*0x10000000+year3*0x1000000+year2*0x100000+year1*0x10000;
        //sum=sum+month2*0x1000+month1*0x100+day2*0x10+day1;
        //数据送显示屏(8个7段数码管)进行显示
        //IOWR_ALTERA_AVALON_PIO_DATA(seg7_DISPLAY_BASE,sum);
      }
  }
}

				
				

					

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -