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

📄 example_gpio.c

📁 闻亭2812dsp例子
💻 C
字号:
//###########################################################################
//
// FILE:    Example_GPIO.c
//
// TITLE:   Example_GPIO TEST 
//
// ASSUMPTIONS:
//
//          This program requires the DSP281x V1.00 header files.  
//          As supplied, this project is configured for "boot to H0" operation. 
//
//          Other then boot mode pin configuration, no other hardware configuration
//          is required.
//
//          Three different examples are included. Select the example 
//          (data, set/clear or toggle) to execute before compiling using
//          the #define statements found at the top of the code.   
//
// DESCRIPTION:
//
//          Toggle all of the GPIO PORT pins 
//        
//          The pins can be observed using Oscilloscope.  
// 
//
//###########################################################################
//
// Original by S.S.
//
//  Ver | dd mmm yyyy | Who  | Description of changes
// =====|=============|======|===============================================
// 1.00 | 11 Sep 2003 | L.H. | First Release
//###########################################################################

#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File
#include "Example_Flash281x_API.h"
#include "Flash281x_API_Library.h"

// Select the example to compile in.  Only one example should be set as 1
// the rest should be set as 0.
#define  LED_TIME  10000

void     Delay_loop();
void     Gpio_select();
void     Gpio_example1();  
void     Gpio_example2();
void     Gpio_example3();
void     LedRun();//led测试
Uchar    EepromTest(Uchar data,Uint16 addr);
Uint16   Delay_count=100;

Uint16   GportVariable,Input[2],Output[2],HighSpeedIn,HighSpeedOut,DzValue[128];
Uchar    i2c_value;
void main(void)
{


/*------------------------------------------------------------------
 To use the F2812, F2811 or F2810 Flash API, the following steps
 must be followed:

      1. Modify Flash281x_API.config.h for your targets operating
         conditions.
      2. Include Flash281x_API_Library.h in the application.
      3. Add the approparite Flash API library to the project.

  The user's code is responsible for the following:

      4. Initalize the PLL to the proper CPU operating frequency.
      5. If required, copy the flash API functions into on-chip zero waitstate
         RAM.  
      6. Initalize the Flash_CPUScaleFactor variable to SCALE_FACTOR
      7. Optional: Run the Toggle test to confirm proper frequency configuration
         of the API. 
      8. Optional: Unlock the CSM.  
      9. Call the API functions: Flash_Erase(), Flash_Program(), Flash_Verify()
         
  The API functions will:
      
       Disable the watchdog
       Check the device revision (REVID).  This API is for Revision C silicon
       Perform the desired operation and return status
------------------------------------------------------------------*/

   volatile Uint16 iVol,i,Status;  
    
 // Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
   InitSysCtrl();

   
// Step 2. Initalize GPIO: 
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
   InitGpio();  // Skipped for this example
 

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts 
   DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.  
// This function is found in the DSP281x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt 
// Service Routines (ISR).  
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
   InitPieVectTable();

// Enable interrupts required for this example
   XIntruptRegs.XINT1CR.bit.ENABLE=1;   //enable xint1
   XIntruptRegs.XINT1CR.bit.POLARITY=0;// on a falling edge
   
   PieCtrlRegs.PIEIER1.bit.INTx4=1;     // PIE Group 1, INT4
   IER = 0x001;	// Enable CPU INT1
 	
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// Step 5. User specific code:
   EnableInterrupts();
   
   
   Gpio_select();//GPIO选择
   
   //Gpio_example1();//GPIO测试1   
   //Gpio_example2();//GPIO测试2
   //Gpio_example3();//GPIO测试3
   

    if(*PLLCR_ABS != PLLCR_VALUE) 
    {
       EALLOW;
       *PLLCR_ABS = PLLCR_VALUE;
       
       // Wait for PLL to lock
       // Each time through this loop takes ~14 cycles
       // PLL Lock time is 131072 Cycles
       for(i= 0; i< 131072/14; i++){
           *WDKEY_ABS = 0x0055;
           *WDKEY_ABS = 0x00AA;
       }
       EDIS;
    }

/*------------------------------------------------------------------
 Unlock the CSM.
    If the API functions are going to run in unsecured RAM
    then the CSM must be unlocked in order for the flash 
    API functions to access the flash.
   
    If the flash API functions are executed from secure memory 
    (L0/L1) then this step is not required.
------------------------------------------------------------------*/
    

/*------------------------------------------------------------------
    Copy API Functions into SARAM
    
    The flash API functions MUST be run out of internal 
    zero-waitstate SARAM memory.  This is required for 
    the algos to execute at the proper CPU frequency.
    If the algos are already in SARAM then this step
    can be skipped.  
    DO NOT run the algos from Flash
    run the algos from external memory
------------------------------------------------------------------*/

    // Copy the Flash API functions to SARAM
//屏蔽-debug,开放-release
    Example_MemCopy(&Flash28_API_LoadStart, &Flash28_API_LoadEnd, &Flash28_API_RunStart);
    ////屏蔽-debug,开放-release
    // We must also copy required user interface functions to RAM. 
    Example_MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
    ////屏蔽-debug,开放-release
//    
    
/*------------------------------------------------------------------
  Initalize Flash_CPUScaleFactor.

   Flash_CPUScaleFactor is a 32-bit global variable that the flash
   API functions use to scale software delays. This scale factor 
   must be initalized to SCALE_FACTOR by the user's code prior
   to calling any of the Flash API functions. This initalization
   is VITAL to the proper operation of the flash API functions.  
   
   SCALE_FACTOR is defined in Example_Flash281x_API.h as   
     #define SCALE_FACTOR  1048576.0L*( (200L/CPU_RATE) )
     
   This value is calculated during the compile based on the CPU 
   rate, in nanoseconds, at which the algorithums will be run.
------------------------------------------------------------------*/
   
   Flash_CPUScaleFactor = SCALE_FACTOR;
      
   // Jump to SARAM and call the Flash API functions

   Example_CallFlashAPI();// notice Example_CallFlashAPI has been changed by jun   
   
//-------------------------------------------------------------------
// Below for user application program
//
   
   LedRun();//led测试
}
#pragma CODE_SECTION(Example_CallFlashAPI,"ramfuncs");
void Example_CallFlashAPI(void)
{
   Uint16 i;
   Uint16 Status;
   Uint16 *Flash_ptr;     // Pointer to a location in flash
   Uint32 Length;         // Number of 16-bit values to be programmed



/*********************below added   by jun *************************/   
   InitFlash();      //just make the flash run fast, This function is found DSP281x_sysCtrl.c
   return;
/*********************above added   by jun *************************/   
}
void Example_MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr)
{
    while(SourceAddr < SourceEndAddr)
    { 
       *DestAddr++ = *SourceAddr++;
    }
    return;
}
 	
void Delay_loop()
{
       Uint16 j,i,sum;
       j=0;
       sum=0;
       for (i = 0; i < Delay_count; i++) {
           for(j=0;j<4;j++)
              sum=sum+1;        
           }

}

void Gpio_example1(void)
{ 
   // Example 1:
   // Toggle I/Os using DATA registers
   // Note: When using the DATA reigsters, input values
   // may be lost.  If there are inputs on the port then
   // use the CLEAR/SET/TOGGLE registers instead. 
   Uint16 i;
   i=0;
   while(1)
   {    
       Input[0]=GpioDataRegs.GPADAT.all;                 // input status
       Input[1]=GpioDataRegs.GPBDAT.all;                 // input status
       if(Output[0])                                    //test output0
          GpioDataRegs.GPFDAT.bit.GPIOF0=1;
       else
          GpioDataRegs.GPFDAT.bit.GPIOF0=0;
       if(Output[1])                                   //test output1
          GpioDataRegs.GPFDAT.bit.GPIOF1=1;
       else   
          GpioDataRegs.GPFDAT.bit.GPIOF1=0;
       HighSpeedIn=GpioDataRegs.GPFDAT.bit.GPIOF4;     //test HighSpeedIn
       if(HighSpeedOut)                                //test HighSpeedOut
          GpioDataRegs.GPFDAT.bit.GPIOF5=1;
       else   
          GpioDataRegs.GPFDAT.bit.GPIOF5=0;
       GpioDataRegs.GPGDAT.all    =GportVariable;   
       Delay_loop(); 
       GpioDataRegs.GPGDAT.all    =GportVariable & 0xFFDF;  //  GPGDAT.bit5
       Delay_loop(); 
       if (Delay_count==10000){
          i++;
          if (i==10){
             i=0;
             Delay_count=1000;
             }
          }
    }
}

void Gpio_example2(void)
{ 
   // Example 2:
   // Toggle I/Os using SET/CLEAR registers
   while(1)
   {    
       GpioDataRegs.GPASET.all    =0xAAAA;
       GpioDataRegs.GPACLEAR.all  =0x5555;     			   
       
       GpioDataRegs.GPBSET.all    =0xAAAA;
       GpioDataRegs.GPBCLEAR.all  =0x5555;     
                   
       GpioDataRegs.GPDSET.all    =0x0022;
       GpioDataRegs.GPDCLEAR.all  =0x0041;    // Four I/Os only

       GpioDataRegs.GPESET.all    =0x0002;
       GpioDataRegs.GPECLEAR.all  =0x0005;    // ThreeI/Os only
               
       GpioDataRegs.GPFSET.all    =0xAAAA;
       GpioDataRegs.GPFCLEAR.all  =0x5555;    
                  
       GpioDataRegs.GPGSET.all    =0x0020;
       GpioDataRegs.GPGCLEAR.all  =0x0010;    // Two  I/Os only
               	   			  
       Delay_loop();
       
       GpioDataRegs.GPACLEAR.all  =0xAAAA;
       GpioDataRegs.GPASET.all    =0x5555;     			   
       
       GpioDataRegs.GPBCLEAR.all  =0xAAAA;
       GpioDataRegs.GPBSET.all    =0x5555;     
                   
       GpioDataRegs.GPDCLEAR.all  =0x0022;
       GpioDataRegs.GPDSET.all    =0x0041;    // Four I/Os only

       GpioDataRegs.GPECLEAR.all  =0x0002;
       GpioDataRegs.GPESET.all    =0x0005;    // ThreeI/Os only
               
       GpioDataRegs.GPFCLEAR.all  =0xAAAA;
       GpioDataRegs.GPFSET.all    =0x5555;    
                  
       GpioDataRegs.GPGCLEAR.all  =0x0020;
       GpioDataRegs.GPGSET.all    =0x0010;    // Two  I/Os only        	   			      

       Delay_loop();	
    }
}

void Gpio_example3(void)
{ 
   // Example 2:
   // Toggle I/Os using TOGGLE registers

   // Set pins to a known state
   GpioDataRegs.GPASET.all    =0xAAAA;
   GpioDataRegs.GPACLEAR.all  =0x5555;     			   
       
   GpioDataRegs.GPBSET.all    =0xAAAA;
   GpioDataRegs.GPBCLEAR.all  =0x5555;     
   
   GpioDataRegs.GPDSET.all    =0x0022;
   GpioDataRegs.GPDCLEAR.all  =0x0041;    // Four I/Os only

   GpioDataRegs.GPESET.all    =0x0002;
   GpioDataRegs.GPECLEAR.all  =0x0005;    // ThreeI/Os only
               
   GpioDataRegs.GPFSET.all    =0xAAAA;
   GpioDataRegs.GPFCLEAR.all  =0x5555;    
                  
   GpioDataRegs.GPGSET.all    =0x0020;
   GpioDataRegs.GPGCLEAR.all  =0x0010;    // Two  I/Os only
   
   // Use TOGGLE registers to flip the state of
   // the pins. 
   // Any bit set to a 1 will flip state (toggle)
   // Any bit set to a 0 will not toggle.   
   while(1)
   {    
       GpioDataRegs.GPATOGGLE.all = 0xFFFF;
       GpioDataRegs.GPBTOGGLE.all = 0xFFFF;
       GpioDataRegs.GPDTOGGLE.all = 0xFFFF;
       GpioDataRegs.GPETOGGLE.all = 0xFFFF;
       GpioDataRegs.GPFTOGGLE.all = 0xFFFF;
       GpioDataRegs.GPGTOGGLE.all = 0xFFFF;
               	   			  
       Delay_loop();
    }
}



void Gpio_select(void)
{

    Uint16 var1;
    Uint16 var2;
    Uint16 var3;

    var1= 0x0000;		// sets GPIO Muxs as I/Os
    var2= 0xFFFF;		// sets GPIO DIR as outputs
    var3= 0x0000;		// sets the Input qualifier values
   
    EALLOW;
	 
	GpioMuxRegs.GPAMUX.all=var1;
    GpioMuxRegs.GPBMUX.all=var1;   
    GpioMuxRegs.GPDMUX.all=var1;
    GpioMuxRegs.GPFMUX.all=var1;		 
    GpioMuxRegs.GPEMUX.all=var1; 
    GpioMuxRegs.GPGMUX.all=var1;
										
    GpioMuxRegs.GPADIR.all=var2;		// GPIO PORTs  as output
    GpioMuxRegs.GPBDIR.all=var2;   		// GPIO DIR select GPIOs as output 
    GpioMuxRegs.GPDDIR.all=var2;
    GpioMuxRegs.GPEDIR.all=var2;		
    GpioMuxRegs.GPFDIR.all=var2; 
    GpioMuxRegs.GPGDIR.all=var2;

    GpioMuxRegs.GPAQUAL.all=var3;  		// Set GPIO input qualifier values
    GpioMuxRegs.GPBQUAL.all=var3;   
    GpioMuxRegs.GPDQUAL.all=var3;
    GpioMuxRegs.GPEQUAL.all=var3;
 
    EDIS;
     
}     
Uint32 LedOnTime,LedOffTime;
Uint16 LedFlag=0;
void LedRun(void)
{
    for(;;)     
    {
		 if((LedOnTime>LED_TIME)){
		   LedOnTime=0;
		   LedFlag=0xff;
		   }
		 else if(LedFlag==0){
		   LedOnTime++;
		   LedOffTime=0;
		   }     
		 if(LedOffTime>LED_TIME){
		   LedOffTime=0;
		   LedFlag=0;
		   }       
		 else if(LedFlag==0xff){
		   LedOffTime++;
		   LedOnTime=0;
		   }
		 Delay_loop();                             
		 if(LedFlag==0)
		    GpioDataRegs.GPFDAT.bit.GPIOF3=0; // Select GPIOF12 Output for test
		 else if(LedFlag==0xff)
		    GpioDataRegs.GPFDAT.bit.GPIOF3=1; // Select GPIOF12 Output for test
    }     
}      
//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

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