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

📄 convert_assembak_isa20060613.c

📁 DSP2812编写的模拟退火算法改进程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//###########################################################################
//
// FILE:   Example_281xEvPwm.c
//
// TITLE:  DSP281x Event Manager PWM Generation.
//
// 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.    
//
// DESCRIPTION:
//
//          This program sets up the EV timers (TIMER1, TIMER2, TIMER3 and TIMER4) 
//          to generate T1PWM, T2PWM, T3PWM, T4PWM and PWM1-12 waveforms.  
//          The user can then observe the waveforms using an scope.  
//          
//
//###########################################################################
//
//  Ver | dd mmm yyyy | Who  | Description of changes
// =====|=============|======|===============================================
//  1.00| 11 Sep 2003 | L.H. | No change since previous version (v.58 Alpha)
//###########################################################################



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

#define ADC_usDELAY  8000L
#define ADC_usDELAY2 20L
#define ADC_CKPS   0x3   // ADC module clock = HSPCLK/2*ADC_CKPS   = 25MHz/(1*2) = 12.5MHz
#define ADC_SHCLK  0x0   // S/H width in ADC module periods                      = 16 ADC clocks

//#define WaveCon(void) DSP28x_WaveCon(void)
// Prototype statements for functions found within this file.
void init_eva(void);
void init_evb(void);
void InitAdc(void);

interrupt void T1PINTIsr(void);
interrupt void T1UFINTIsr(void);
interrupt void T2UFINTIsr(void);
interrupt void ADCINTIsr(void);
// Global counts used in this example
Uint16 sinv[100]={0,58,117,176,234,293,351,409,466,523,579,635,690,744,798,851,903,
                  954,1004,1053,1102,1149,1195,1239,1283,1325,1366,1406,1444,1481,
                  1516,1550,1583,1613,1643,1670,1696,1720,1743,1764,1783,1800,
                  1816,1829,1841,1851,1860,1866,1871,1874,1875,1874,1871,1866,1860,
                  1851,1841,1829,1816,1800,1783,1764,1743,1720,1696,1670,1643,1613,
                  1583,1550,1516,1481,1444,1406,1366,1325,1283,1239,1195,1149,1102,
                  1053,1004,954,903,851,798,744,690,635,579,523,466,409,351,293,234,
                  176,117,58};


Uint16 step,CMPR_LAST,CMPR_LAST1; 
Uint16 UD,UDK;
Uint16 IL,ILK_H,ILK_L;
Uint16 Io,IoK_L,IoK_H;
Uint16 Uo,UoK_L,UoK_H;
Uint16 AX,BX,CX;
Uint16 CM[16],energy_step,CM_largest,CM_larger;
int16 DIF;
int16 DIF_op;
int16 DIF_last;
int32  CM_energy,CM_energy_last;
int32 Vares;
int32 Varea;

float M;
int16 flag;
float T;
float a;
float StartT;

int16 p;
int16 q;
int16 Step1;
int16 Step2;
int16 N;

// produce the random number

int16 i;
float rand_x0=0.2157;
float rand_a=10.3256;
float rand_c=0.3427;
float rand_m=1;
float rand_x[];


//union FLAG_REG      FLAG1;

void main(void)
{

// 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  

// Initialize only GPAMUX and GPBMUX for this test
   EALLOW;
   // Enable PWM pins
   GpioMuxRegs.GPAMUX.all = 0x00FF; // set 1,EVA PWM 1-6  pins
   GpioMuxRegs.GPBMUX.all = 0x00FF; // set 1,EVB PWM 7-12 pins,
   EDIS;
    
// 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();
   
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.  
   EALLOW;	// This is needed to write to EALLOW protected registers
   PieVectTable.T1PINT = &T1PINTIsr;
   PieVectTable.T1UFINT = &T1UFINTIsr;
   PieVectTable.T2UFINT = &T2UFINTIsr;
   PieVectTable.ADCINT=&ADCINTIsr;
   EDIS;   // This is needed to disable write to EALLOW protected registers 
 

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
   init_eva();
   init_evb();

//---------------------------------------------------------------------------
// InitAdc: 
//---------------------------------------------------------------------------
// This function initializes ADC to a known state.
//
   InitAdc();
// Specific ADC setup for this example:
   AdcRegs.ADCTRL1.bit.ACQ_PS = 0;
   AdcRegs.ADCTRL3.bit.ADCCLKPS = 3;		//ADC_CKPS;    
   AdcRegs.ADCTRL1.bit.CPS =0;				//1; 
   AdcRegs.ADCTRL1.bit.SEQ_CASC = 1;        // 1  Cascaded mode
   AdcRegs.ADCTRL1.bit.CONT_RUN = 0;       // Setup continuous run



// Step 5. User specific code, enable interrupts:
   EALLOW;
   PieCtrlRegs.PIECRTL.bit.ENPIE = 1;   // Enable the PIE block
   PieCtrlRegs.PIEIER1.bit.INTx6=1;     // PIE Group 1, INT6,ADCINT
   PieCtrlRegs.PIEIER2.bit.INTx4=1;     // PIE Group 2, INT4,T1PINT
   PieCtrlRegs.PIEIER2.bit.INTx6=1;     // PIE Group 2, INT6,T1UFINT
   PieCtrlRegs.PIEIER3.bit.INTx3=1;     // PIE Group 3, INT3,T2UFINT
   IER = 0x0007;						// Enable CPU INT1,INT2,INT3
   
   
// Set I/O
   GpioMuxRegs.GPFMUX.bit.MFSXA_GPIOF10 =0;
   GpioMuxRegs.GPFDIR.bit.GPIOF10 =1;	//SET FPIOF10 AS OUTPUT PIN
   GpioDataRegs.GPFDAT.all=0x0000;
   //GpioMuxRegs.GPFDIR.bit.GPIOF11 =1;	//SET FPIOF10 AS OUTPUT PIN
   	EINT;   
   EDIS;
   
// initialize the variable
   flag_regs.flag1.bit.CM_start=0;
   CM_energy_last=0;
   CM_energy=0;
   energy_step=512;
   step=0;
   M=0.8;
   UoK_L=0;
   UoK_H=0;
   DIF=0;
   DIF_op=0;
   DIF_last=0;
   CM_largest=CM_larger=0;
  // Just sit and loop forever:
  // initialize the variable of annealing process
  Step1=5;
  p=0;
  N=0;
  // PWM pins can be observed with a scope.	
  
  rand_x[1]=rand_x0;
  i=0;
  
  while(1)
  {
  	while (flag<0xFFFF & p>=Step2 & N>=50)   //searching is ended and waitiing for next searching
  		{
  			DIF=DIF_op;
  			if (energy_step==0)
  				{
  					flag=flag+1;
  				}
  		}
   			if (flag_regs.flag1.bit.CM_start==1)//Improved Simualted Annealing Algorithm
  				{
  					while(q<Step1)
  					{
  						i=i+1;
  						DIF_last=DIF;
  						DIF=DIF+(int)((rand_x[i]-0.5)*4);
  						rand_x[i+1]=mod((rand_x[i]*rand_a+rand_c),rand_m);
  						
  						if (DIF>375)
  						  	{
            					DIF=375;	  	
  						  	}
  						 else if (DIF<-375)
  						   {
  						     DIF=-375;
  						   } 	
  						if (energy_step==0)
  							{
  								if(CM_energy-CM_energy_last<0)
  									{
  										CM_energy_last=CM_energy;
  										DIF=DIF;
  										if (Vares>CM_energy_last)
  											{
  												Vares=CM_energy_last;
  												q=0;
  											}
  											else
  												{
  													q=q+1;
  												}
  									}
  								else if (exp((CM_energy-CM_energy_last)/T)>0.5)
  									{
  										CM_energy_last=CM_energy;
  										DIF=DIF;
  										q=q+1;
  									}	
  								else
  									{
  										DIF=DIF_last;
  										q=q+1;
  									}	
  							}
  					}
  				}
  			else
  				{
  					if (energy_step==0)
  						{
  							flag_regs.flag1.bit.CM_start=1;//clear flag
  							CM_energy_last=CM_energy;
  							energy_step=512;
  							CM_energy=0;
  							StartT=100;     //initialize the parameters of ISA
  							a=0.9;
  							Step2=5;   //initialize the parameters of annealing process
  							p=0;
  							Varea=CM_energy_last;
  							N=0;		
  							Step1=25;  //initialize the parameters of sampling process
  							//k=0;
  							q=0;
  							Vares=CM_energy_last;
  							DIF=DIF_op=DIF_last=0;
  						}
  				}	
  			N=N+1;
  			T=T*a;
  			flag=0;
  			if (Varea-Vares>0)
  				{
  					Varea=Vares;
  					DIF_op=DIF;
  					p=0;  					
  				}	
  			else
  				{
  					p=p+1;
  				}	
  		
  }

}

void init_eva()
{	
	// EVA Configure T1PWM, T2PWM, PWM1-PWM6
	// Initalize the timers	
	// Initalize EVA Timer1
	EvaRegs.T1PR = 0x1D4C;       // Timer1 period
	EvaRegs.T1CMPR = 0x1C00;     // Timer1 compare

⌨️ 快捷键说明

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