这是原程序代码,但是调试总出错! #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" void LCD_Init() { lcd_write_cmd(LCD_16207_0_BASE,0x38); usleep(2000); lcd_write_cmd(LCD_16207_0_BASE,0x0C); usleep(2000); lcd_write_cmd(LCD_16207_0_BASE,0x01); usleep(2000); lcd_write_cmd(LCD_16207_0_BASE,0x06); usleep(2000); lcd_write_cmd(LCD_16207_0_BASE,0x80); usleep(2000); } void LCD_Show_Text(char*Text) { int i; for(i=0;i<strlen(Text);i++) { lcd_write_data(LCD_16207_0_BASE,Text); usleep(2000); } } void LCD_Line1() { lcd_write_cmd(LCD_16207_0_BASE,0x80); usleep(2000); } void LCD_Line2() { lcd_write_cmd(LCD_16207_0_BASE,0xC0); usleep(2000); } volatile int edge_capture; 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_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; if((month==4)||(month==6)||(month==9)||(month==11)) return 0;
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) { int screen=0;
int pos=0;//每行都有三个位置,第一行是年月日,第二行是时分秒 int year,month,day,hour,minute,second; unsigned long sum; char date[16]; char time[16]; int year1=16; 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; na_LED8->np_piodata=1<<pos; if(screen==0) screenflag=8; else screenflag=0; IOWR_ALTERA_AVALON_PIO_DATA(LED_GREEN_BASE,(1<<pos)|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;
} } 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<3) 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 (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 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+0x03;date[1]=year3+0x30; date[2]=year2+0x30;date[3]=year1+0x30; date[4]=''; date[5]=''; date[6]=month2+0x30; date[7]=month1+-x30; 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; monute2=monute/10;minute1=minute%10; second2=socond/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]=''; tmie[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(SEG7_DISPLAY_BASE,sum); } }
#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_Line1(); void LCD_Test();
|