📄 miaobiaochengxu.txt
字号:
#include <stdlib.h>
#include <stdio.h>
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include <unistd.h>
#include "system.h"
#include "altera_avalon_timer_regs.h"
#include "priv/alt_busy_sleep.h"
#define ESC 27
#define ESC_TOP_LEFT "[1;0H"
#define ESC_CLEAR "[2J"
#define ESC_COL2_INDENT2 "[2;2H"
#define ESC_COL2_INDENT4 "[2;4H"
#define ESC_COL2_INDENT "[2;13H"
#define ESC_COL2 "[2B"
volatile long TimerCount=0;
volatile long MAX_COUNT=3600*24*100;
volatile int Buttons=0x000f;
volatile alt_u8 count;
volatile alt_u8 i=0;
volatile alt_u8 j=0;
volatile alt_u8 k=0;
volatile long counter[50];
volatile char s[]=" stop ";
volatile char z[]="2-sta;8-stop ";
volatile char ji[]="2-memorize ";
volatile char x[]="2+4-";
static FILE *lcd;
static void start_timer(void)
{
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_TIMER_BASE, 0x07);
return;
}
static void pause_timer(void)
{
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_TIMER_BASE, 0x8);
return;
}
static int show_timer(void)
{
fprintf(lcd, "%c%s%02d:%02d:%02d:%02d\n",ESC,ESC_TOP_LEFT,
(TimerCount/360000)%24,
(TimerCount/6000)%60,
(TimerCount/100)%60,
(TimerCount%100));
fprintf(lcd," \n");
}
static void show(m)
{
fprintf(lcd, "%c%s%02d:%02d:%02d:%02d\n",ESC,ESC_TOP_LEFT,
(m/360000)%24,
(m/6000)%60,
(m/100)%60,
(m%100));
return 1;
}
static void showr(x)
{
fprintf(lcd,"%c%s%02d-record\n",ESC,ESC_COL2_INDENT4,x);
return 1;
}
static void showinf(t)
{
fprintf(lcd,"%c%s%s\n",ESC,ESC_COL2_INDENT,t);
return 1;
}
static void showa(s)
{
fprintf(lcd,"%c%s%d\n",ESC,ESC_COL2_INDENT2,s);
return 1;
}
static void showinfor(i)
{
fprintf(lcd,"%c%s%s\n",ESC,ESC_COL2_INDENT4,i);
return 1;
}
////////////////////////////////////////////////////////////
static void button_interrupts(void* context, alt_u32 id)
{
alt_u8 a;
alt_u8 n=0;
//volatile alt_u8 k=0;
volatile alt_u8 *countptr = (volatile alt_u8 *)context;
a=*countptr;
usleep(50000);
if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x01)
{
a=a+1;
printf("%d\n",a);
showa(a);
switch(a)
{
case 1:showinfor(z);
break;
case 2:showinfor(ji);
break;
case 3:showinfor(x);
break;
//case 4:fprintf(lcd," \n");
// break;
}
}
if(a>3) a=0;
*countptr=a;
switch(a)
{case 1:
{
showinfor(z);
usleep(50000);
if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x02) //开始计时
{
i=0;
j=0;
k=0;
start_timer();
TimerCount=0;
show_timer();
}
else if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x08) //停止计时
{
pause_timer();
show_timer();
showinf(s);
showa(a);
}
}
break;
case 2: //记录时间
{
showa(a);
showinfor(ji);
usleep(50000);
if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x02)
{
i++;
counter[i]=TimerCount;
printf(" i=%d ---j=%d ",i,j);
printf("%02d:%02d:%02d:%02d\n",
(counter[i]/360000)%24,
(counter[i]/6000)%60,
(counter[i]/100)%60,
(counter[i]%100));
j=i;
}
}
break;
case 3: //显示记录的时间2--递增显示;4--递减显示
{
showinf(x);
showa(a);
usleep(50000);
if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x02) //正向显示
{
n++;
k++;
show(counter[k]);
while(n!=0)
{
showr(k);
printf(" %d ---inc~~record ",k); //递增显示
printf("%02d:%02d:%02d:%02d\n",
(counter[k]/360000)%24,
(counter[k]/6000)%60,
(counter[k]/100)%60,
(counter[k]%100));
n--;
}
}
else if(~(IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE))&0x04) //逆向显示
{
n++;
//j--;
show(counter[j]);
printf(" %d ---des~~record ",j); //递减显示
//j--;
showa(a);
while(n!=0)
{
showr(j);
// printf(" %d ---des~~record ",j); //递减显示
printf("%02d:%02d:%02d:%02d\n",
(counter[j]/360000)%24,
(counter[j]/6000)%60,
(counter[j]/100)%60,
(counter[j]%100));
n--;
}
j--;
}
}
break;
}
}
//////////////////////////////////////////////////////////////////
static void handle_Timer0_interrupts(void* context, alt_u32 id)
{
TimerCount++;
if(TimerCount>=MAX_COUNT)
TimerCount=0;
show_timer();
IOWR_ALTERA_AVALON_TIMER_STATUS(SYS_TIMER_BASE, 0);
}
////////////////////////////////////////////////////
int main(void)
{
count=0;
lcd=fopen("/dev/lcd","w");
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, (void *)&count, button_interrupts);
alt_irq_register(SYS_TIMER_IRQ, (void *)&count, handle_Timer0_interrupts);
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_TIMER_BASE, 0x08);
show_timer();
while(1)
{;}
fclose(lcd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -