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

📄 board_led.c

📁 基于Nios II的串口通信
💻 C
字号:

#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"

  /* 
   * This is a freestanding application, so we want to use alt_main
   * as the entry point.  However, if the debugger is run on this
   * application, it will try to set a breakpoint at main, which 
   * the application does not contain.  The below line creates an
   * alias so the debugger is able to set a breakpoint at main, 
   * yet the application retains alt_main as it's entry point.
   */

int main (void) __attribute__ ((weak, alias ("alt_main")));

  /* 
   * Use alt_main as entry point for this free-standing application
   */

// delay function
void delay(void)
{ 
  int k=0;
  while(k<100000)
  k++;
}

/*void delay1(void)
{ 
  int k=0;
  while(k<1000)
  k++;
}*/
//the function of writing data to the regesisters
void wdata (alt_u8 add, alt_u8 da)
{
    int i;
 
    IOWR_ALTERA_AVALON_PIO_DATA(NCS0_BASE, 1);  // nCS0=1,关片选
    IOWR_ALTERA_AVALON_PIO_DATA(NWE_BASE, 1);   // 关写
    IOWR_ALTERA_AVALON_PIO_DATA(NOE_BASE, 1);   //关读
    delay();
   
    
   
    IOWR_ALTERA_AVALON_PIO_DATA(NCS0_BASE, 0);  // nCS0=0,chipselect
    IOWR_ALTERA_AVALON_PIO_DATA(NWE_BASE, 0);   //写使能
    IOWR_ALTERA_AVALON_PIO_DATA(BOARD_ADD_BASE,add); //给地址
    IOWR_ALTERA_AVALON_PIO_DATA(BOARD_DATA_BASE, da); //给数据
    {
      i = 0;
      while(i<100)
        i++;
    }                                               //延迟
    IOWR_ALTERA_AVALON_PIO_DATA(NWE_BASE, 1);   // 关写
    //IOWR_ALTERA_AVALON_PIO_DATA(NCS0_PIO_BASE, 1);  // nCS0=1,关片选
     
}

//the function using the function above to draw a heart picture
//void draw_heart()
//{
 
//}

int alt_main (void)
{
  alt_u8 led = 0x2;
  alt_u8 dir1 = 0;

  volatile int i;
   
  /* 
   * Infinitely shift a variable with one bit set back and forth, and write
   * it to the LED PIO.  Software loop provides delay element.
   */
  
 //     draw_heart();
    //IOWR_ALTERA_AVALON_PIO_DATA(NCS0_PIO_BASE, 0);  // nCS0=0,chipselect
    
   /* wdata (0xa,0x42); 
    wdata (0xb,0xa5);
    wdata(0xc,0x00);
    wdata(0xd,0x00);
    wdata(0xe,0x24);
    wdata(0xf,0x18);
    wdata(0x10,0x00);
    wdata(0x11,0x00);
    delay(); */     
    while(1)
    {
      wdata (0x2,0x00);
    delay();
    wdata (0x3,0x00);
    delay();
    wdata(0x4,0x00);
    delay();
    wdata(0x5,0x00);
    delay();
    wdata(0x6,0x00);
    delay();
    wdata(0x7,0x00);
    delay();
    wdata(0x8,0x00);
    delay();
    wdata(0x9,0x00);
    delay();  
      //delay();

    wdata (0xa,0x42); 
    wdata (0xb,0xa5);
    wdata(0xc,0x00);
    wdata(0xd,0x00);
    wdata(0xe,0x24);
    wdata(0xf,0x18);
    wdata(0x10,0x00);
    wdata(0x11,0x00);
    delay(); 
    
    IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 8);  
    delay();
    IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 4); 
    delay();
    IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 2); 
    delay();
    
    wdata (0x2,0x24);
    delay();
    wdata (0x3,0x5a);
    delay();
    wdata(0x4,0x81);
    delay();
    wdata(0x5,0x81);
    delay();
    wdata(0x6,0x42);
    delay();
    wdata(0x7,0x24);
    delay();
    wdata(0x8,0x18);
    delay();
    wdata(0x9,0x00);
    delay();  
      delay();
      //delay();
    
   }  
    
  while (1) 
  {
    if (led & 0x81) 
    {
      dir1 = (dir1 ^ 0x1);
    }

    if (dir1) 
    {
      led = led >> 1;
    } 
    else 
    {
      led = led << 1;
    }

 IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
// the code below is written for the base board's leds    
  
 /*   if (shuju & 0x81) 
    {
      dir2 = (dir2 ^ 0x1);
    }

    if (dir2) 
    {
      shuju = shuju >> 1;
    } 
    else 
    {
      shuju = shuju << 1;
    }
   
    /* 
    * The delay element in this design has been written as a while loop
    * to avoid confusing the software debugger.  A tight, one line software
    * delay loop such as:
    *   for(i=0; i<200000; i++);
    * can cause problems when it is stepped through using a software debugger.
    * The while loop below produces the same behavior as the for loop shown 
    * above, but without causing potential debugger problems.  
    */
 
    /*i = 0;
    while (i<100000)
      i++;*/
  }

  return 0;
}

/******************************************************************************
*                                                                             *
* License Agreement                                                           *
*                                                                             *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
* All rights reserved.                                                        *
*                                                                             *
* Permission is hereby granted, free of charge, to any person obtaining a     *
* copy of this software and associated documentation files (the "Software"),  *
* to deal in the Software without restriction, including without limitation   *
* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
* and/or sell copies of the Software, and to permit persons to whom the       *
* Software is furnished to do so, subject to the following conditions:        *
*                                                                             *
* The above copyright notice and this permission notice shall be included in  *
* all copies or substantial portions of the Software.                         *
*                                                                             *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
* DEALINGS IN THE SOFTWARE.                                                   *
*                                                                             *
* This agreement shall be governed in all respects by the laws of the State   *
* of California and by the laws of the United States of America.              *
* Altera does not recommend, suggest or require that this reference design    *
* file be used in conjunction or combination with any other product.          *
******************************************************************************/

⌨️ 快捷键说明

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