
前天刚做完的sopc课程设计 做的万年历 c编程
  有些还是能用的 - - 
头文件 LCD.h
#ifndef _LCD_H_
#define _LCD_H_
//LCD Module 16*2
#define lcd_write_cmd(base,data) IOWR(base,0,data)
#define lcd_read_cmd(base) IORD(base,1)
#define lcd_write_data(base,data) IOWR(base,2,data)
#define lcd_read_data(base) IORD(base,3)
//----------------------
void LCD_Init();
void LCD_Show_Text(char *Text);
void LCD_Line2();
void LCD_Test();
//---------------------------
#endif //_LCD_H_
程序 wnl.c
#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_regs.h"
//#include"count_binary.h"
#include "LCD.h"
#include "io.h"
volatile int edge_capture;
void LCD_Init()
{ 
  //LCD初始化
    lcd_write_cmd(LCD_BASE,0x38);
    usleep(2000);
    lcd_write_cmd(LCD_BASE,0x0C);
    usleep(2000);
    lcd_write_cmd(LCD_BASE,0x01);
    usleep(2000);
    lcd_write_cmd(LCD_BASE,0x06);
    usleep(2000);
    lcd_write_cmd(LCD_BASE,0x80);
    usleep(2000);
}
void LCD_Show_Text(char*Test)
{
  //LCD输出格式
  int i;
  for(i=0;i<strlen(Test);i++)
  {
    lcd_write_data(LCD_BASE,Test[i]);
    usleep(2000);
  }
}
void LCD_Line1()
{ 
  //向LCD写命令
  lcd_write_cmd(LCD_BASE,0x80);
  usleep(2000);
}
void LCD_Line2()
{
  lcd_write_cmd(LCD_BASE,0xC0);
  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_BASE);
    
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_BASE,0);
}
  //按键初始化
static void init_button_pio()
{
  void *edge_capture_ptr=(void*)&edge_capture;
  IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_BASE,0xf);
  IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_BASE,0x0);
  alt_irq_register
(BUTTON_IRQ,edge_capture_ptr,handle_button_interrupts);
}
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;
  else if((month==4)||(month==6)||(month==9)||(month==11)) return 0;
  else //if(month==2) 
    return 2;
}
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) __attribute__ ((weak, alias ("alt_main")));
int alt_main (void)
//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=1;
    int year2=1;
    int year3=0;
    int year4=2;
    int month1=6;
    int month2=0;
    int day1=8;
    int day2=1;
    int hour4,hour3,hour2,hour1,minute2,minute1,second2,second1;
    unsigned int screenflag;
    hour=0;minute=0;second=0;year=2011;month=6;day=18;
    //年月日时分秒初始化
    alt_irq_init(ALT_IRQ_BASE);
    init_button_pio();
    IOWR_ALTERA_AVALON_PIO_DATA(LCD_ON_BASE,1);
    LCD_Init();
    while(1)
    {
        if(pos>=3) pos=0;//共有三个位置0,1,2
        if(screen>=2) screen=0; 
        //na_LED8->np_piodata=1<<pos;
        if(screen==0) screenflag=8;//用一个LED指示当前调整的位置
        else screenflag=0;
        IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,(1<<screen)|screenflag);
        usleep(1000000);//等待1秒的定时时间
        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;
                       }
                 }
                else 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;}
                        else if (check_month(month)==1)
                          { if (day<3) day++;else day=1;}
                        else if(check_month(month)==2)
                          {
                            if (check_year(year)) 
                              { if(day<29) day++ ; else day=1;}
                            else { if (day<28) day++ ; else day=1;}
                          }
                    }
                    else if(screen==1)
                      {
                        if(second<59) second++; else second=0;
                      }
                 }
              else if (pos==1)
                {
                    if(screen==0)
                      {
                       if (month<12) month++; else month=1;
                      }
                   
                    else if(screen==1)
                      {
                       if (minute<59) minute++; else minute=0;
                      }
               }
             
              if(pos==2)
                {
                  if (screen==0)
                    {
                     if (year<9999) year++; else year=2005;
                    }
                  else 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);
}
//将数据转换为显示器件可以接收的格式
  sum=year4*0x1000000+year3*0x1000000+year2*0x100000+year1*0x10000;
  sum=sum+month2*0x1000+month1*0x100+day2*0x10+day1;
//数据送显示器件(8个7段数码管)进行显 示
    //IOWR_ALTERA_AVALON_PIO_DATA(LCD_BASE,sum);
}
    
}