#include "stdio.h" #include "sys/unistd.h" #include "io.h" #include "string.h" #include "system.h" #include "altera_avalon_pio_regs.h" #include "altera_avalon_timer_regs.h" #include "alt_types.h" #include "sys/alt_irq.h" //0-9 unsigned char segtab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char led_buffer[8]={0}; //unsigned char bittab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; static unsigned char cnt1=0; static unsigned char cnt2=0; static unsigned char cnt3=0; static unsigned char cnt4=0; static unsigned char cnt5=0; static unsigned char cnt6=0;
static void timer_init(void);
int main(void) { unsigned char i=0,j=0; unsigned char buf[20];
timer_init();
while(1){ sprintf(buf,"%08u",j++); for(i=0;i<8;i++){ led_buffer=buf[7-i]-'0'; } //usleep(500000); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xfe); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt1]]); if(cnt1==8) { cnt1=0; cnt2++;} usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xfd); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt2]]); //cnt2++; if(cnt2==4&&cnt1==8) { cnt2=0; cnt3++; } usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xfb); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,0xbf); usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xf7); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt3]]); //cnt3++; if(cnt3==8) { cnt3=0; cnt4++; } usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xef); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt4]]); //cnt4++; if(cnt4==4&&cnt3==8) { cnt4=0; cnt5++; } usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xdf); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,0xbf); usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xbf); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt5]]); //cnt5++; if(cnt5==3) { cnt5=0; cnt6++; } usleep(500); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE,0x7f); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,segtab[led_buffer[cnt6]]); //cnt6++; if(cnt6==1&&cnt5==3) { cnt6=0; cnt1=0; } usleep(500); } return 0; }
static void timer_irq(void *context,alt_u32 id) { cnt1++; IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE,0); }
static void timer_init(void) { IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE,0); IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE,200000); IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE,200000>>16); IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE,0x07); alt_irq_register(TIMER_0_IRQ,NULL,timer_irq); } 以上是我的代码:在实验板上结果是88-88-XX,之所以写XX是因为后两位一直在跳变,可惜的是不是按照我希望的从00—59计时,我觉得应该是定时器我掌握的不太好,希望路过的大侠发表下意见···
|