dsp280x_adc.c
来自「2808单相全桥spwm逆变工程.rar」· C语言 代码 · 共 223 行
C
223 行
// TI File $Revision: /main/1 $
// Checkin $Date: December 1, 2004 11:11:30 $
//###########################################################################
//
// FILE: DSP280x_Adc.c
//
// TITLE: DSP280x ADC Initialization & Support Functions.
//
//###########################################################################
// $TI Release: DSP280x V1.10 $
// $Release Date: April 18, 2005 $
//###########################################################################
#include "DSP280x_Device.h" // DSP280x Headerfile Include File
#include "DSP280x_Examples.h" // DSP280x Examples Include File
#include "CommonDefine.h"
#define ADC_usDELAY 5000L
//---------------------------------------------------------------------------
// InitAdc:
//---------------------------------------------------------------------------
// This function initializes ADC to a known state.
//
// PLEASE NOTE, ADC INIT IS DIFFERENT ON 281x vs 280x DEVICES!!!
//
void InitAdc(void)
{
extern void DSP28x_usDelay(Uint32 Count);
// To powerup the ADC the ADCENCLK bit should be set first to enable
// clocks, followed by powering up the bandgap, reference circuitry, and ADC core.
// Before the first conversion is performed a 5ms delay must be observed
// after power up to give all analog circuits time to power up and settle
// Please note that for the delay function below to operate correctly the
// CPU_CLOCK_SPEED define statement in the DSP280x_Examples.h file must
// contain the correct CPU clock period in nanoseconds.
AdcRegs.ADCTRL3.all = 0x00E0; // Power up bandgap/reference/ADC circuits
// Copy time critical code and Flash setup code to RAM
// The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the F2808.cmd file.
MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
DELAY_US(ADC_usDELAY); // Delay before converting ADC channels
}
// Specific ADC setup for this example:
void SetupAdc(void) //by yb
{
//adc clock 最高为12.5M
AdcRegs.ADCTRL3.bit.ADCCLKPS=ADC_CKPS; //adc内部分频4位 现在4分频 12.5M
AdcRegs.ADCTRL1.bit.CPS=ADC_CPS; //可以再进行一次额外的二分频,为0则不分
//一次ADC转换的周期
AdcRegs.ADCTRL1.bit.ACQ_PS=ADC_SHCLK; //采样保持窗口时间,16×A Cclock
//后面采样采用同步模式,所以一次一个ADC周期采样转换两个ADC通道,一个A一个B,需要(16+2)个ADC时钟
AdcRegs.ADCTRL3.bit.SMODE_SEL=1; //同步采样
//级联模式
AdcRegs.ADCTRL1.bit.SEQ_CASC=1;
//允许epwm事件触发adc
AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1=0; //禁止seq1中断, disable SEQ1 interrupt (every EOS)
AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1=1; // Enable EPwmSOC to start SEQ1
//最大转换数8(同步即8×2)
AdcRegs.ADCMAXCONV.all=0x0007;
//不采用自动连续转换方式(采样到最大转换数后,自动跳转到开始状态)
//采用启动-停止方式,因为只是每个载波频率采样而已
AdcRegs.ADCTRL1.bit.CONT_RUN=0;
//A板DCDC输入电压和输入电流:adcA0和adcB0
//B板DCDC输入电压和输入电流:adcA2和adcB2
// 逆变器锁相电压和输出电流: adcA4和adcB4
//直流母线电压:adcA6 温度传感器:adcB6
AdcRegs.ADCCHSELSEQ1.bit.CONV00=0x0;
AdcRegs.ADCCHSELSEQ1.bit.CONV01=0x0;
AdcRegs.ADCCHSELSEQ1.bit.CONV02=0x2;
AdcRegs.ADCCHSELSEQ1.bit.CONV03=0x2;
AdcRegs.ADCCHSELSEQ2.bit.CONV04=0x4;
AdcRegs.ADCCHSELSEQ2.bit.CONV05=0x4;
AdcRegs.ADCCHSELSEQ2.bit.CONV06=0x6;
AdcRegs.ADCCHSELSEQ2.bit.CONV07=0x6;
//adc中断模式选择
AdcRegs.ADCTRL2.bit.INT_MOD_SEQ1=0; //中断模式0,即每次SEQ1结束则置中断标志位int_seq1
}
void Adc_Inquire(void) //查询方式的adc
{
int16 VolBatA; //DCDC A板输入电压电流
int16 CurBatA;
int16 VolBatB; //DCDC B板输入电压电流
int16 CurBatB;
int16 VoltageAIn,CurrentAOut; //逆变器输入相电压,输出电流
int16 VoltageLine; //直流母线电压
int16 TemperVol; //温度
//查询ad采样,直到完成
while (AdcRegs.ADCST.bit.INT_SEQ1== 0){}
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; //adc的seq1序列转换完成后,软件清零中断标志int_seq1
//采样adc转换的值
//DCDC A板输入电压电流
VolBatA=((int16)(AdcRegs.ADCRESULT0>>4)+(AdcRegs.ADCRESULT2>>4))>>1;
CurBatA=((int16)(AdcRegs.ADCRESULT1>>4)+(AdcRegs.ADCRESULT3>>4))>>1;
//DCDC B板输入电压电流
VolBatB=((int16)(AdcRegs.ADCRESULT4>>4)+(AdcRegs.ADCRESULT6>>4))>>1;
CurBatB=((int16)(AdcRegs.ADCRESULT5>>4)+(AdcRegs.ADCRESULT7>>4))>>1;
//逆变器锁相电压和输出电流
VoltageAIn=((int16)(AdcRegs.ADCRESULT8>>4)+(AdcRegs.ADCRESULT10>>4))>>1;
CurrentAOut=((int16)(AdcRegs.ADCRESULT9>>4)+(AdcRegs.ADCRESULT11>>4))>>1;
//直流母线电压和温度电压
VoltageLine= ((int16)(AdcRegs.ADCRESULT12>>4)+(AdcRegs.ADCRESULT14>>4))>>1;
TemperVol=((int16)(AdcRegs.ADCRESULT13>>4) +(AdcRegs.ADCRESULT15>>4))>>1;
if(ADSampIniFlag==1) //the first power up, sample offset initialize
{
//voltage is not done
CurrAInitSum+=CurrentAOut;
CurrInitCount++;
}
else //offset is already
{
//逆变器实际计算
VolLockA=VoltageAIn-AdcVOffset; //锁相电压
CurrAArray=CurrentAOut-AdcCOffset; //输出电流,以逆变电流为正向
VolAArray=VoltageLine-AdcBusOffset; //母线直流电压
//DCDC用
DCAVoltage=VolBatA-DCAVOffset;
DCACurrent=CurBatA-DCACOffset;
DCBVoltage=VolBatB-DCBVOffset;
DCBCurrent=CurBatB-DCBCOffset;
//温度
Temperature=TemperVol;
//power
PowerAIns=(int32)VolLockA*CurrAArray;//输出瞬时功率
//关于mmpt的功率计算可以在这里
//////////////////////////////////////////////////////////////////
//a voltage squaresum
SquareStep=(Uint32)((int32)VolLockA*VolLockA);
SquareStep=SquareStep>>4; //right shift 4 bit, not overflow
VolASquSum+=SquareStep;
//a current squaresum
SquareStep=(Uint32)((int32)CurrAArray*CurrAArray);
SquareStep=SquareStep>>4; //right shift 4 bit, not overflow
CurrASquSum+=SquareStep;
//dc A voltage and current sum
DCAVolSum+=(Uint32)DCAVoltage;
DCACurrSum+=(Uint32)DCACurrent;
//dc B voltage and current sum
DCBVolSum+=(Uint32)DCBVoltage;
DCBCurrSum+=(Uint32)DCBCurrent;
///////////////////////////////////////////////////////////////////////
//调试用
if(temptemp == 1200)
{
temptemp=0;
// ybyb2=CurrAArray;
VolAArray1[temptemp]=ybyb1;
RefAAAA[temptemp]=VolLockA;
}
else
{
temptemp++;
// ybyb2=CurrAArray;
VolAArray1[temptemp]=ybyb1;
RefAAAA[temptemp]=VolLockA;
}
}
// Reinitialize for next ADC sequence
AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1
return;
}
//===========================================================================
// End of file.
//===========================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?