主题:[讨论]LCD日历电子钟?帮忙增加闪烁
就是原程序设置年月日和时间时候选中的时候不会闪烁 所以请高手帮忙增加。课程是SOPC用的是DE2实验板
#include "alt_types.h"
#include <stdio.h>
#include <unistd.h>
#include <io.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "sys/alt_irq.h"
#include "LCD.h"
#include "sys/alt_alarm.h"
int year,month,day,hour,min,sec;
int year4,year3,year2,year1,month2,month1,day2,day1,hour2,hour1,min2,min1,sec2,sec1;
int row; //row计数key[1]按下的次数
char line1[16];
char line2[16];
int led,led7;
// char line1=(year<<24)|(" "<<20)|(month<<12)|(" "<<8)|day;
// char line2=(hour<<6)|(":"<<5)|(min<<3)|(":"<<3)|sec;
static alt_alarm alarm1;
volatile int edge_capture=0;
void int_time();
void lcd_show();
void int_key();
void data_init();
void led_data_init();
void initpio();
void int_key0();
void int_key1();
void int_key2();
void int_key3();
void int_day();
//主函数------------------------------------------------------------
int main()
{
data_init();
LCD_Init();
int_time();
initpio();
while(1)
{
int_key();
lcd_show();
}
}
//pio的初始化--------------------------------------------------------
void key_interrupts(void* context,alt_u32 id)
{
volatile int* edge_capture_ptr = (volatile int*)context;
*edge_capture_ptr= IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0);
}
void initpio()
{
void* edge_capture_ptr = (void*)&edge_capture;
IOWR_ALTERA_AVALON_PIO_DIRECTION(LED_PIO_BASE,0x00);
IOWR_ALTERA_AVALON_PIO_DIRECTION(KEY_PIO_BASE,0x00);
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_PIO_BASE,0xff);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0x00);
alt_irq_register(KEY_PIO_IRQ,edge_capture_ptr,key_interrupts);
}
//计数---------------------------------------------------------
alt_u32 my_alarm_callback(void *context)
{
if(sec<59)
sec++;
else
{
sec=0;
if(min<59)
min++;
else
{
min=0;
if(hour<23)
hour++;
else
{
hour=0;
int_day();
//if(day<30)
// day++;
// else
{
//day=1;
if(month<12)
month++;
else
{
month=1;
if(year<9999)
year++;
}
}
}
}
}
return alt_ticks_per_second();
}
void int_time() //定时1秒与时间的累加
{
if(alt_alarm_start(&alarm1,alt_ticks_per_second(),my_alarm_callback,NULL)<0)
{
printf("No system clock available\n");
}
}
//显示------------------------------------------------------------------
void lcd_show_line1() //显示第一行
{
LCD_Line1();
LCD_Show_Text(line1);
}
void lcd_show_line2() //显示第二行
{
LCD_Line2();
LCD_Show_Text(line2);
}
void lcd_show() //将数据转换为显示器件可以接收的格式
{
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;
hour2=hour/10;
hour1=hour%10;
min2=min/10;
min1=min%10;
sec2=sec/10;
sec1=sec%10;
line1[0]=year4+0x30;
line1[1]=year3+0x30;
line1[2]=year2+0x30;
line1[3]=year1+0x30;
line1[4]=' ';
line1[5]=' ';
line1[6]=month2+0x30;
line1[7]=month1+0x30;
line1[8]=' ';
line1[9]=' ';
line1[10]=day2+0x30;
line1[11]=day1+0x30;
line1[12]=' ';
line1[13]=' ';
line1[14]=' ';
line1[15]=' ';
line2[0]=' ';
line2[1]=' ';
line2[2]=hour2+0x30;
line2[3]=hour1+0x30;
line2[4]=' ';
line2[5]=' ';
line2[6]=min2+0x30;
line2[7]=min1+0x30;
line2[8]=' ';
line2[9]=' ';
line2[10]=sec2+0x30;
line2[11]=sec1+0x30;
line2[12]=' ';
line2[13]=' ';
line2[14]=' ';
line2[15]=' ';
year=year4*1000+year3*100+year2*10+year1;
month=month2*10+month1;
day=day2*10+day1;
hour=hour2*10+hour1;
min=min2*10+min1;
sec=sec2*10+sec1;
lcd_show_line1();
lcd_show_line2();
}
//按键判断----------------------------------------------------
void int_key()
{
switch(edge_capture)
{
case 0x00:break;
case 0x01:
int_key0();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x02:
int_key1();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x04:
int_key2();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x08:
int_key3();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
}
}
void int_key0() //处理按下key0 切换行 led[7]灭,表示当前为第一行,亮表示第二行
{
if(led7==0)
{
led7=1;
led=0x80;
LCD_Line2();
}
else
{
led7=0;
led=0x00;
LCD_Line1();
}
row=0;
}
void int_key1() //处理按下key1 切换列
{
if(row==3) //row==3表示已经按下三次,当前执行第四次按下
row=0;
if(led7==0)
{
switch(row)
{
case 0:
led=0x20;
break;
case 1:
led=0x10;
break;
case 2:
led=0x08;
break;
}
}
if(led7==1)
{
switch(row)
{
case 0:
led=0x84;
break;
case 1:
led=0x82;
break;
case 2:
led=0x81;
break;
}
}
row++;
}
void int_key2() //处理按下key2 数据加1
{
switch(led)
{
case 0x20:
if(year<9999)
year++;
else year=2010;
break;
case 0x10:
if(month<12)
month++;
else month=1;
break;
case 0x08:
int_day();
break;
case 0x84:
if(hour<23)
hour++;
else hour=0;
break;
case 0x82:
if(min<59)
min++;
else min=0;
break;
case 0x81:
break;
}
}
void int_key3() //处理按下key3 数据初始化
{
data_init();
}
void int_day()
{
if((month==1)|(month==3)|(month==5)|(month==7)|(month==8)|(month==10)|(month==12))
{ if(day<31)
day++;
else day=1;
}
else
{ if(month==2)
{
if(day<28)
day++;
else day=1;
}
else
{
if(day<30)
day++;
else day=1;
}
}
}
//数据的初始化-----------------------------------------------
void data_init()
{
led=0x00;
led7=0;
year4=0;
year3=0;
year2=0;
year1=0;
month2=0;
month1=0;
day2=0;
day1=0;
hour2=0;
hour1=0;
min2=0;
min1=0;
sec2=0;
sec1=0;
row=0;
// stu=0;
year=2010;
month=07;
day=07;
hour=12;
min=00;
sec=00;
}
#include "alt_types.h"
#include <stdio.h>
#include <unistd.h>
#include <io.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "sys/alt_irq.h"
#include "LCD.h"
#include "sys/alt_alarm.h"
int year,month,day,hour,min,sec;
int year4,year3,year2,year1,month2,month1,day2,day1,hour2,hour1,min2,min1,sec2,sec1;
int row; //row计数key[1]按下的次数
char line1[16];
char line2[16];
int led,led7;
// char line1=(year<<24)|(" "<<20)|(month<<12)|(" "<<8)|day;
// char line2=(hour<<6)|(":"<<5)|(min<<3)|(":"<<3)|sec;
static alt_alarm alarm1;
volatile int edge_capture=0;
void int_time();
void lcd_show();
void int_key();
void data_init();
void led_data_init();
void initpio();
void int_key0();
void int_key1();
void int_key2();
void int_key3();
void int_day();
//主函数------------------------------------------------------------
int main()
{
data_init();
LCD_Init();
int_time();
initpio();
while(1)
{
int_key();
lcd_show();
}
}
//pio的初始化--------------------------------------------------------
void key_interrupts(void* context,alt_u32 id)
{
volatile int* edge_capture_ptr = (volatile int*)context;
*edge_capture_ptr= IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0);
}
void initpio()
{
void* edge_capture_ptr = (void*)&edge_capture;
IOWR_ALTERA_AVALON_PIO_DIRECTION(LED_PIO_BASE,0x00);
IOWR_ALTERA_AVALON_PIO_DIRECTION(KEY_PIO_BASE,0x00);
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_PIO_BASE,0xff);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE,0x00);
alt_irq_register(KEY_PIO_IRQ,edge_capture_ptr,key_interrupts);
}
//计数---------------------------------------------------------
alt_u32 my_alarm_callback(void *context)
{
if(sec<59)
sec++;
else
{
sec=0;
if(min<59)
min++;
else
{
min=0;
if(hour<23)
hour++;
else
{
hour=0;
int_day();
//if(day<30)
// day++;
// else
{
//day=1;
if(month<12)
month++;
else
{
month=1;
if(year<9999)
year++;
}
}
}
}
}
return alt_ticks_per_second();
}
void int_time() //定时1秒与时间的累加
{
if(alt_alarm_start(&alarm1,alt_ticks_per_second(),my_alarm_callback,NULL)<0)
{
printf("No system clock available\n");
}
}
//显示------------------------------------------------------------------
void lcd_show_line1() //显示第一行
{
LCD_Line1();
LCD_Show_Text(line1);
}
void lcd_show_line2() //显示第二行
{
LCD_Line2();
LCD_Show_Text(line2);
}
void lcd_show() //将数据转换为显示器件可以接收的格式
{
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;
hour2=hour/10;
hour1=hour%10;
min2=min/10;
min1=min%10;
sec2=sec/10;
sec1=sec%10;
line1[0]=year4+0x30;
line1[1]=year3+0x30;
line1[2]=year2+0x30;
line1[3]=year1+0x30;
line1[4]=' ';
line1[5]=' ';
line1[6]=month2+0x30;
line1[7]=month1+0x30;
line1[8]=' ';
line1[9]=' ';
line1[10]=day2+0x30;
line1[11]=day1+0x30;
line1[12]=' ';
line1[13]=' ';
line1[14]=' ';
line1[15]=' ';
line2[0]=' ';
line2[1]=' ';
line2[2]=hour2+0x30;
line2[3]=hour1+0x30;
line2[4]=' ';
line2[5]=' ';
line2[6]=min2+0x30;
line2[7]=min1+0x30;
line2[8]=' ';
line2[9]=' ';
line2[10]=sec2+0x30;
line2[11]=sec1+0x30;
line2[12]=' ';
line2[13]=' ';
line2[14]=' ';
line2[15]=' ';
year=year4*1000+year3*100+year2*10+year1;
month=month2*10+month1;
day=day2*10+day1;
hour=hour2*10+hour1;
min=min2*10+min1;
sec=sec2*10+sec1;
lcd_show_line1();
lcd_show_line2();
}
//按键判断----------------------------------------------------
void int_key()
{
switch(edge_capture)
{
case 0x00:break;
case 0x01:
int_key0();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x02:
int_key1();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x04:
int_key2();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
case 0x08:
int_key3();
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led);
edge_capture=0;
break;
}
}
void int_key0() //处理按下key0 切换行 led[7]灭,表示当前为第一行,亮表示第二行
{
if(led7==0)
{
led7=1;
led=0x80;
LCD_Line2();
}
else
{
led7=0;
led=0x00;
LCD_Line1();
}
row=0;
}
void int_key1() //处理按下key1 切换列
{
if(row==3) //row==3表示已经按下三次,当前执行第四次按下
row=0;
if(led7==0)
{
switch(row)
{
case 0:
led=0x20;
break;
case 1:
led=0x10;
break;
case 2:
led=0x08;
break;
}
}
if(led7==1)
{
switch(row)
{
case 0:
led=0x84;
break;
case 1:
led=0x82;
break;
case 2:
led=0x81;
break;
}
}
row++;
}
void int_key2() //处理按下key2 数据加1
{
switch(led)
{
case 0x20:
if(year<9999)
year++;
else year=2010;
break;
case 0x10:
if(month<12)
month++;
else month=1;
break;
case 0x08:
int_day();
break;
case 0x84:
if(hour<23)
hour++;
else hour=0;
break;
case 0x82:
if(min<59)
min++;
else min=0;
break;
case 0x81:
break;
}
}
void int_key3() //处理按下key3 数据初始化
{
data_init();
}
void int_day()
{
if((month==1)|(month==3)|(month==5)|(month==7)|(month==8)|(month==10)|(month==12))
{ if(day<31)
day++;
else day=1;
}
else
{ if(month==2)
{
if(day<28)
day++;
else day=1;
}
else
{
if(day<30)
day++;
else day=1;
}
}
}
//数据的初始化-----------------------------------------------
void data_init()
{
led=0x00;
led7=0;
year4=0;
year3=0;
year2=0;
year1=0;
month2=0;
month1=0;
day2=0;
day1=0;
hour2=0;
hour1=0;
min2=0;
min1=0;
sec2=0;
sec1=0;
row=0;
// stu=0;
year=2010;
month=07;
day=07;
hour=12;
min=00;
sec=00;
}