此程序是ADC.h函数中
在interrupt void adc_isr(void) 函数中做数据处理:
/*
* ADC.c
*
* Created on: 2017年8月5日
* Author: chris
*/
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#include "ADC.h"
#define ADC_CKPS 0x0
#define BUF_SIZE 10 // Sample buffer size
Uint16 LoopCount=0;
Uint16 ADC_ConvertedValue[BUF_SIZE][2];
//float FBPS_Vget=0; //fbps电压获取
//float FBPS_Iget=0; //fbps电流获取
Uint16 ConversionCount=0;
Uint16 Voltage1[10];
void ADC_init(void)
{
EALLOW;
#if (CPU_FRQ_150MHZ) // Default - 150 MHz SYSCLKOUT
#define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3) = 25.0 MHz
#endif
EDIS;
EALLOW;
SysCtrlRegs.HISPCP.all = ADC_MODCLK;
EDIS;
EALLOW; // This is needed to write to EALLOW protected register
PieVectTable.ADCINT = &adc_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER1.bit.INTx6 = 1;
// IER |= M_INT1; // Enable CPU Interrupt 1
InitAdc(); // For this example, init the ADC
// Configure ADC
AdcRegs.ADCMAXCONV.all = 0x0003; // Setup 2 conv's on SEQ1
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA3 as 1st SEQ1 conv.
AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x2; // Setup ADCINA2 as 2nd SEQ1 conv.
AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0x4; // Setup ADCINA3 as 1st SEQ1 conv.
AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0x6; // Setup ADCINA2 as 2nd SEQ1 conv.
AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1 = 1;// Enable SOCA from ePWM to start SEQ1
AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; // Enable SEQ1 interrupt (every EOS)
// Assumes ePWM1 clock is already enabled in InitSysCtrl();
EPwm1Regs.ETSEL.bit.SOCAEN = 1; // Enable SOC on A group
EPwm1Regs.ETSEL.bit.SOCASEL = 4; // Select SOC from from CPMA on upcount
EPwm1Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EPwm1Regs.CMPA.half.CMPA = 0x0080; // Set compare A value
EPwm1Regs.TBPRD = 0xFFFF; // Set period for ePWM1
EPwm1Regs.TBCTL.bit.CTRMODE = 0; // count up and start
}
interrupt void adc_isr(void)
{
static Uint16 array_ele1=0,array_ele2=0;
//Voltage1[ConversionCount] = AdcRegs.ADCRESULT0;
//Voltage2[ConversionCount] = AdcRegs.ADCRESULT1;
ADC_ConvertedValue[array_ele1++][0]=( (AdcRegs.ADCRESULT0)>>4); //电压采集
ADC_ConvertedValue[array_ele2++][1]=( (AdcRegs.ADCRESULT1)>>4); //电流采集
//
if(array_ele1>(BUF_SIZE-1))
array_ele1=0;
if(array_ele2>(BUF_SIZE-1))
array_ele2=0;
//FBPS_Vget = Voltage1[ConversionCount]*3.3/65536; //电压
// FBPS_Iget = Voltage2[ConversionCount]*3.3/65536; //电流
// Reinitialize for next ADC sequence
AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; // Clear INT SEQ1 bit
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge interrupt to PIE
return;
}
/**************************中值滤波*****************************/
#define N_MID 10
float filter_mid(int k)
{
int i,j;
float temp,adc=0.0;
for (j=0;jADC_ConvertedValue[i][k] )
{
temp = ADC_ConvertedValue[i][k];
ADC_ConvertedValue[i][k]= ADC_ConvertedValue[i][k];
ADC_ConvertedValue[i][k] = temp;
}
}
}
adc=ADC_ConvertedValue[(N_MID-1)/2][k]/4095.0*3.3;
return adc;
}
在定时器函数中:
在__interrupt void cpu_timer0_isr(void)定时器中断函数中实现双闭环
/*
* Time.c
*
* Created on: 2017年10月19日
* Author: Sun zhaohu
*/
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
//#include "Time.h"
#include "include.h"
/**************************************************/
fbps FBPS = {0,0,0,0,0,0,1.826,0,0,0,0};
float filter_mid(int k);
/************************************************/
void Time_init(void)
{
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr;
// PieVectTable.XINT13 = &cpu_timer1_isr;
// PieVectTable.TINT2 = &cpu_timer2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
InitCpuTimers(); // For this example, only initialize the Cpu Timers
ConfigCpuTimer(&CpuTimer0, 150, 100);
// ConfigCpuTimer(&CpuTimer1, 150, 1000000);
// ConfigCpuTimer(&CpuTimer2, 150, 1000000);
CpuTimer0Regs.TCR.all = 0x4000; // Use write-only instruction to set TSS bit = 0
// CpuTimer1Regs.TCR.all = 0x4000; // Use write-only instruction to set TSS bit = 0
// CpuTimer2Regs.TCR.all = 0x4000; // Use write-only instruction to set TSS bit = 0
// IER |= M_INT1;
// IER |= M_INT13;
// IER |= M_INT14;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
}
__interrupt void cpu_timer0_isr(void)
{
//static float FBPS_pid_V=0,FBPS_pid_I=0;
// Acknowledge this interrupt to receive more interrupts from group 1
// PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
FBPS.Vcount++;
if( FBPS.Vcount>= 3000) //软起动开始结电容充电adc采集存值
FBPS.Flag = 1;
if(FBPS.Flag == 1)
{
FBPS.Vget = filter_mid(0); //中值滤波得电压
FBPS.Iget = filter_mid(1); //中值滤波得电流
FBPS.ivlue = FBPS.Iget*0.4816 - 0.0373; //电流线性关系
if( FBPS.ivlue>=0.94)
FBPS.Vset = 1.83;
else if( FBPS.ivlue<0.94&&FBPS.ivlue>0.65)
FBPS.Vset = 1.832; //采集输出电压 电压1.83/36.2
else
FBPS.Vset = FBPS.changeset;
FBPS.PIDvout = PID_V(FBPS.Vset,FBPS.Vget); //输出电压设定为36v 1.83对应36v 电压环
//FBPS.PIDvout = fuzzy_pid( FBPS.Vset,FBPS.Vget); //输出电压设定为36v 1.83对应36v 电压环
FBPS.PIDiout = PID_I(FBPS.PIDvout,FBPS.ivlue); //电流环 可以不加没用
/****************************显示使用************************************/
FBPS.radio = FBPS.ivlue*FBPS.Vget*19.72/24/1.47*100;
FBPS.angle = FBPS.PIDiout/3000*360;
/*****************************************************************/
EPwm1Regs.TBPHS.half.TBPHS = FBPS.PIDiout; //改变移相角大小
EPwm1Regs.TBCTL.bit.SWFSYNC = 1;
}
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}