⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 count.c

📁 FPGA设计初级班和提高班培训课堂PPT;实验的源代码;实验指导书!
💻 C
字号:
#include "alt_types.h"
#include <stdio.h>
#include <unistd.h>
#include "system.h"
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h"

#define ESC 27
#define ESC_TOP_LEFT "[1;0H"
#define ESC_COL2_LEFT "[2;0H"
#define ESC_COL1_INDENT5 "[1;5H"
#define ESC_CLEAR "K"
//-------------------declare---------------------
static alt_u8 count;
static alt_u16 seg[16]={0xfcf,0x60f,0xdaf,0xf2f,0x66f,0xb6f,0xbef,0xe0f,0xfef,
0xf6f,0xeef,0x3ef,0x9cf,0x7af,0x9ef,0x8ef};
volatile int edge_capture;
//----------button_capture-------------------------------
#ifdef BUTTON_PIO_BASE
static void handle_button_interrupts(void* context, alt_u32 id)
{
  volatile int* edge_capture_ptr = (volatile int*) context;
  /* Store the value in the Button's edge capture register in *context. */
  *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
  /* Reset the Button's edge capture register. */
  IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
}
//---------------Initialize the button-----------------
static void init_button_pio()
{
  void* edge_capture_ptr = (void*) &edge_capture;
  /* Enable all 4 button interrupts. */
  IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
  /* Reset the edge capture register. */
  IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
  /* Register the interrupt handler. */
  alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr, handle_button_interrupts ); 
}
#endif
//--------------------clear_lcd-----------------
void clear(FILE *fp)
{
  fprintf(fp,"%c%s\n",ESC,ESC_CLEAR);
  fprintf(fp,"%c%s\n",ESC,ESC_CLEAR);
}

//------------------main---------------------------
int main(void)
{ 
/* Declare variables used only in main() */
FILE * fp;
init_button_pio();
  fp=fopen("/dev/lcd", "w");
  printf("\n***************************\n");
  printf("*Hello to red logic world!*\n");
  printf("*sw1 Counting from 0 to f *\n");
  printf("*sw2 Counting from f to 0 *\n");
  printf("***************************\n");
while(1)
{
  if(edge_capture==0x1)
  {
    if(count<15)
    {
      usleep(500000);
      IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,count);
      //IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_LED_BASE,seg[count]);
      clear(fp);
      fprintf(fp,"%c%ssw1 count 0-f:%x",ESC,ESC_TOP_LEFT,count);
      count++;
    }
    else
    {
      usleep(500000);
      IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,count);
      //IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_LED_BASE,seg[count]);
      clear(fp);
      fprintf(fp,"%c%ssw1 count 0-f:%x",ESC,ESC_TOP_LEFT,count); 
      count=0;
    }
  }
  else if(edge_capture==0x2)
  {
    if(count==0)
    {
      usleep(500000);
      IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,count);
      //IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_LED_BASE,seg[count]);
      clear(fp);
      fprintf(fp,"%c%ssw2 count f-0:%x",ESC,ESC_TOP_LEFT,count);
      count=15;
    }
    else
    {
      usleep(500000);
      IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,count);
      //IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_LED_BASE,seg[count]);
      clear(fp);
      fprintf(fp,"%c%ssw2 count f-0:%x",ESC,ESC_TOP_LEFT,count); 
      count--;
    }
  }
  else
  {
    IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x0);
    //IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_LED_BASE,0x000);
    clear(fp);
    fprintf(fp,"%c%swait swtich mode",ESC,ESC_TOP_LEFT); 
  }
}
fclose(fp); 
return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -