📄 eva_interrupt.c
字号:
//###########################################################################
//
// FILE: Example_281x eva_interrupt.c
//
// TITLE: DSP281x EVA Example Program.
//
// ASSUMPTIONS:
//
// This program requires the DSP281x V1.00 header files.
// As supplied, this project is configured for "boot to H0" operation.
//
// Make sure the CPU clock speed is properly defined in
// DSP281x_Examples.h before compiling this example.
//
// Connect signals to be converted to A2 and A3.
//
//
// DESCRIPTION:
//
// This example sets up the PLL in x10/2 mode, divides SYSCLKOUT
// by six to reach a 25Mhz HSPCLK (assuming a 30Mhz XCLKIN). The
// clock divider in the ADC is not used so that the ADC will see
// the 25Mhz on the HSPCLK. Interrupts are enabled and the EVA
// is setup to generate a periodic ADC SOC on SEQ1. Two channels
// are converted, ADCINA3 and ADCINA2.
//
// Watch Variables:
//
// Voltage1[10] Last 10 ADCRESULT0 values
// Voltage2[10] Last 10 ADCRESULT1 values
// ConversionCount Current result number 0-9
// LoopCount Idle loop counter
//
//
//###########################################################################
//
// Original Author: D.F.
//
// Ver | dd mmm yyyy | Who | Description of changes
// =====|=============|======|===============================================
// 1.00| 11 Sep 2003 | L.H. | Changes since previous version (v.58 Alpha)
// | | | Cleanup only. Results are shifted >> 4
//###########################################################################
#include "DSP28_Device.h" // DSP281x Headerfile Include File
#include "DSP28_Adc.h" // DSP281x Examples Include File
//#include "DSP28_Globalprototypes.h"
//#pragma CODE_SECTION(T1PINT_isr,"RAMH0");
//#pragma CODE_SECTION(InitFlash,"ramfuncs");
//#pragma CODE_SECTION(T1PINT_isr,"ramfuncs");
//extern unsigned int Ramfuncs_LoadStart;
//extern unsigned int Ramfuncs_LoadEnd;
//extern unsigned int Ramfuncs_RunStart;
// Prototype statements for functions found within this file.
interrupt void T1PINT_isr(void);
//void SCIfunction(void);
//interrupt void T1UFINT_isr(void);
// Global variables used in this example:
Uint16 LoopCount;
Uint16 ConversionCount;
Uint16 Current1[10];
Uint16 Current2[10];
Uint16 Voltage1[10];
Uint16 Voltage2[10];
Uint16 switch_flag;
Uint16 Vref=0;
Uint16 sum1=0,Sum1=0;
Uint16 sum2=0,Sum2=0;
Uint16 sum3=0,Sum3=0;
int speed1=0,speed2=0;
void KEYControl(void);
int Speedcontrol(int speed1, int speed2);
/*
void InitFlash(void)
{
EALLOW; // Enable EALLOW protected register access
FlashRegs.FPWR.bit.PWR = 3; // Flash set to active mode
FlashRegs.FSTATUS.bit.V3STAT = 1; // Clear the 3VSTAT bit
FlashRegs.FSTDBYWAIT.bit.STDBYWAIT = 0x01FF; // Sleep to standby cycles
FlashRegs.FACTIVEWAIT.bit.ACTIVEWAIT = 0x01FF; // Standby to active cycles
FlashRegs.FBANKWAIT.bit.RANDWAIT = 3; // F280x Random access wait states
FlashRegs.FBANKWAIT.bit.PAGEWAIT = 3; // F280x Paged access wait states
FlashRegs.FOTPWAIT.all = 5; // F280x OTP wait states
FlashRegs.FOPT.bit.ENPIPE = 1; // Enable the flash pipeline
EDIS; // Disable EALLOW protected register access
asm(" RPT #6 || NOP");
}
*/
//float M=0.8;
main()
{
InitSysCtrl();//系统初始化
switch_flag=1;//置关机标志
// while(GpioDataRegs.GPFDAT.bit.GPIOF13)//开关机拦截
//{ }
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
// For this example, set HSPCLK to SYSCLKOUT / 6 (25Mhz assuming 150Mhz SYSCLKOUT)
// EALLOW;
// SysCtrlRegs.HISPCP.all = 0x3; // HSPCLK = SYSCLKOUT/6 频率保护设定
// EDIS;
// Step 2. Initialize 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(); // 输入输出端口初始化
EALLOW;
GpioMuxRegs.GPAMUX.all=0x00ff;//保护设定
EDIS;
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;//关中断
IER = 0x0000;//后加
IFR = 0x0000;//后加
// Initialize the 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;//关闭CPU的中断使能和标志
// 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 register
//PieVectTable.T1PINT = &T1PINT_isr;//向中断向量表写入T1周期中断地址
//PieVectTable.T1UFINT = &T1UFINT_isr;
//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
InitAdc(); //初始化ADC
// Step 5. User specific code, enable interrupts:
// memcpy(&Ramfuncs_RunStart,&Ramfuncs_LoadStart,&Ramfuncs_LoadEnd-&Ramfuncs_LoadStart);
// InitFlash();
// Enable T1PINT T1UFINT in PIE
PieCtrl.PIEIER2.bit.INTx4 = 1;
PieCtrl.PIEIER2.bit.INTx6 = 1;
//IER |= M_INT2; // Enable CPU Interrupt 2
//EINT; // Enable Global interrupt INTM
//ERTM; // Enable Global realtime interrupt DBGM
//LoopCount = 0;
ConversionCount = 0;
// Configure ADC
AdcRegs.MAX_CONV.all = 0x0003; // Setup 2 conv's on SEQ1
AdcRegs.CHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv.
AdcRegs.CHSELSEQ1.bit.CONV01 = 0x1; // Setup ADCINA1 as 2nd SEQ1 conv.
AdcRegs.CHSELSEQ1.bit.CONV02 = 0x2; // Setup ADCINA1 as 3ird SEQ1 conv.
AdcRegs.CHSELSEQ1.bit.CONV03 = 0x3; // Setup ADCINA1 as 3ird SEQ1 conv.
AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1; // Enable EVASOC to start SEQ1
AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; // Enable SEQ1 interrupt (every EOS)
// Configure EVA
// Assumes EVA Clock is already enabled in InitSysCtrl();
EvaRegs.EVAIMRA.all=0x0080;//中断屏蔽寄存器,T1周期中断使能,下溢中断使能,电机驱动保护。
EvaRegs.EVAIFRA.all=0xFFFF;//EVA中断标志寄存器清零
EvaRegs.T1CNT=0x00;//计数寄存器附初值
EvaRegs.ACTRA.all=0x0666;//比较方式控制寄存器PWM6\4\2低有效,5\3\1高有效
EvaRegs.COMCONA.all=0xA600;//比较控制寄存器T1CNT=0或T1CNT=T1PR时(周期或下溢)装载
EvaRegs.DBTCONA.all=0x0A10;//死区时间设为A*16*1/30000000=5.33us,使能死区定时器1\2\3(pwm123456)
EvaRegs.GPTCONA.all=0x441;//全局通用定时器控制寄存器,定时器1比较输出低有效,周期中断启动模数转换
EvaRegs.T1CMPR = 0x0A80; // Setup T1 compare value
EvaRegs.CMPR1 = 625;
EvaRegs.T1PR = 1250; // Setup period register
EvaRegs.GPTCONA.bit.T1TOADC = 1; // Enable EVASOC in EVA
//EvaRegs.T1CON.all = 0x1042; // Enable timer 1 compare (upcount mode)
EvaRegs.T1CON.all=0x0842;//定时器1控制寄存器,连续增减,内部时钟/128,定时器比较使?
while(1)
{
if(GpioDataRegs.GPFDAT.bit.GPIOF13==0&&switch_flag==1)
{
GpioDataRegs.GPEDAT.bit.GPIOE2=1;//
switch_flag=0;
for(Vref=0;Vref<=10;Vref++)
{
EvaRegs.CMPR1=Vref*4096/8;
EvaRegs.CMPR2=Vref*4096/8;
}
EvaRegs.CMPR1 = 625;
}
else if(GpioDataRegs.GPFDAT.bit.GPIOF13==1&&switch_flag==0)
{
GpioDataRegs.GPEDAT.bit.GPIOE2=0;//
switch_flag=1;
EvaRegs.CMPR1 = 0x0;
EvaRegs.CMPR2 = 0x0;
}
// LoopCount++;//
//SCIfunction();
//KEYControl();
}//while层
}//main层
interrupt void T1PINT_isr(void)//
{
Current1[ConversionCount] = AdcRegs.RESULT0 >>4;//
Current2[ConversionCount] = AdcRegs.RESULT1 >>4;//
Voltage1[ConversionCount] = AdcRegs.RESULT2 >>4;//
Voltage2[ConversionCount] = AdcRegs.RESULT3 >>4;//
// If 40 conversions have been logged, start over
if(ConversionCount == 9) //
{
ConversionCount = 0;//
for(ConversionCount=0;ConversionCount<=9;ConversionCount++)
{
sum1=sum1+Current1[ConversionCount];
if(ConversionCount==9)
{Sum1=sum1/10;}
}
for(ConversionCount=0;ConversionCount<=9;ConversionCount++)
{
sum2=sum2+Current2[ConversionCount];
if(ConversionCount==9)
{Sum2=sum2/10;}
}
for(ConversionCount=0;ConversionCount<=9;ConversionCount++)
{
sum3=sum3+Voltage1[ConversionCount];
if(ConversionCount==9)
{
Sum3= sum3/10;
}
}
//Speedcontrol(speed1,speed2);
}//
else //
ConversionCount++;//
// Reinitialize for next ADC sequence
AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1
AdcRegs.ADC_ST_FLAG.bit.INT_SEQ1_CLR = 1; // Clear INT SEQ1 bit
EvaRegs.EVAIFRA.bit.T1PINT=1;//Clear interrupt flag bit 7
PieCtrl.PIEACK.all = PIEACK_GROUP2; // Acknowledge interrupt to PIE
EINT;//
return;//
}//
void KEYControl(void)
{
switch(GpioDataRegs.GPBDAT.all)
{
case 0xfeff: Speedcontrol(500,600);
break;
case 0xfdff: Speedcontrol(1000,1500);
break;
case 0xfcff: Speedcontrol(2000,2500);
break;
case 0xfbff: Speedcontrol(3000,3500);
break;
case 0xfaff: Speedcontrol(4000,4500);
break;
case 0xf9ff: Speedcontrol(speed1,speed2);
break;
case 0xf8ff: Speedcontrol(speed1,speed2);
break;
case 0xf7ff: Speedcontrol(speed1,speed2);
break;
default: break;
}//switch层
}//KEY层
Speedcontrol(speed1,speed2)
{
for(Vref=0;Vref<=4094;Vref++)
{
EvaRegs.CMPR1=Vref;
EvaRegs.CMPR2=Vref;
}
EvaRegs.CMPR1 = speed1;
EvaRegs.CMPR2 = speed2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -