📄 adc.c
字号:
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "f2812a.h"
#include "LCD.h"
#include "math.h"
#include "filter.h"
#define ADCNUMBER 256
#define FIRNUMBER 25
#define LMSNUMBER 32
#define PI 3.1415926
float FIR();
float LMS();
//*********************//
float fWn1[LMSNUMBER];
float fWn2[LMSNUMBER];
float uN=0.0005;
float eN;
float dn;
float flmsout;
float en[256];
/*********************/
float fXn[LMSNUMBER]={ 0.0 };
int i;
int n;
// 定义指示灯寄存器地址和寄存器类型
#define LBDS (*((unsigned int *)0xc0000))
// Prototype statements for functions found within this file.
interrupt void adc_isr(void);
void Delay(unsigned int nDelay);
struct struLCDGraph struGraph,struGraph1;
unsigned int nScreenBuffer[30*128];
// Global variables used in this example:
Uint16 LoopCount;
Uint16 ConversionCount;
//Uint16 Voltage1[1024];
//Uint16 Voltage2[1024];
Uint16 Voltage_1,Voltage_2,flage=0;
Uint16 nGraphBuf1[ADCNUMBER],nGraphBuf2[ADCNUMBER],nGraphBuf3[ADCNUMBER];
int ci=0,keyflage,nAD;
Uint16 nMixing[1024];
// 液晶 ----------------------------------------------------------
#define CTRLED (*(unsigned int *)0x108004) // port8004
#define MCTRKEY (*(unsigned int *)0x108005) // port8005
#define CTRCLKEY (*(unsigned int *)0x108006) // port8006
#define CTRSTATUS (*(unsigned int *)0x108000) //port8000
#define pi 3.1415926
int nModeAD;
Uint16 ad1,ad2;
/*fir参数*/
int j=0,a=0;
main()
{int j,uWork,uWork1;
unsigned int * pWork;
InitSysCtrl();//初始化cpu
//InitPll(0x5);
DINT;//关中断
for(i=0;i<LMSNUMBER;i++)
{
fWn1[i]=fWn2[i]=0.0;
fXn[i]=0;
}
LCDTurnOff();
LCDSetScreenBuffer(nScreenBuffer);
for ( uWork=0,pWork=nScreenBuffer;uWork<30*128;uWork++,pWork++ ) (*pWork)=0;
LCDSetDelay(0);
LCDTurnOn(); // 打开显示
LCDCLS(); // 清除显示内存
InitXintf();
InitPieCtrl();//初始化pie寄存器
IER = 0x0000;//禁止所有的中断
IFR = 0x0000;
InitPieVectTable();//初始化pie中断向量表
// 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.ADCINT = &adc_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
AdcRegs.ADCTRL1.bit.RESET = 1; // Reset the ADC module
asm(" RPT #10 || NOP"); // Must wait 12-cycles (worst-case) for ADC reset to take effect
AdcRegs.ADCTRL3.all = 0x00C8; // first power-up ref and bandgap circuits
AdcRegs.ADCTRL3.bit.ADCBGRFDN = 0x3; // Power up bandgap/reference circuitry
AdcRegs.ADCTRL3.bit.ADCPWDN = 1; // Power up rest of ADC
// Enable ADCINT in PIE
PieCtrlRegs.PIEIER1.bit.INTx6 = 1;
IER |= M_INT1; // Enable CPU Interrupt 1
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
LoopCount = 0;
ConversionCount = 0;
// Configure ADC
AdcRegs.ADCMAXCONV.all = 0x0001; // Setup 2 conv's on SEQ1
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA3 as 1st SEQ1 conv.
AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1; // Setup ADCINA2 as 2nd 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.T1CMPR = 0x0080; // Setup T1 compare value
EvaRegs.T1PR = 0x5000; // Setup period register
EvaRegs.GPTCONA.bit.T1TOADC = 1; // Enable EVASOC in EVA
EvaRegs.T1CON.all = 0x1042; // Enable timer 1 compare (upcount mode)
//struGraph.uLineMode=LINEMODE;
//LCDDrawGraph(struGraph);
keyflage=0;
// Wait for ADC interrupt
for(;;)
{
if(flage==1)
{ flage=0;
LCDSetScreenBuffer(nScreenBuffer);
LCDCLS(); // 清除显示内存*/
if(keyflage==0)//k8
{
struGraph.pData=nGraphBuf2;
struGraph.uDataMode=DATAUINTMODE;
struGraph.uDataLength=256;
struGraph.uMaxValue=2048;
struGraph.uWindowX0=0;
struGraph.uWindowY0=0;
struGraph.uWindowX1=240;
struGraph.uWindowY1=128;
struGraph.nOriginX=0;
struGraph.nOriginY=0;
struGraph.uLineMode=LINEMODE;
LCDGraph(&struGraph);
// struGraph.uLineMode=LINEMODE;
struGraph.pData=nGraphBuf1;
struGraph.uDataMode=DATAUINTMODE;
struGraph.uDataLength=256;
struGraph.uMaxValue=1024;
struGraph.uWindowX0=0;
struGraph.uWindowY0=0;
struGraph.uWindowX1=240;
struGraph.uWindowY1=128;
struGraph.nOriginX=0;
struGraph.nOriginY=64;
struGraph.uLineMode=LINEMODE;
LCDGraph(&struGraph);
for ( j=0;j<20;j++ ) _Delay(50);
LCDSetScreenBuffer(nScreenBuffer);
LCDCLS(); // 清除显示内存
}
if(keyflage==1)//k7
{
struGraph.pData=nMixing;
struGraph.uDataMode=DATAUINTMODE;
struGraph.uDataLength=256;
struGraph.uMaxValue=2500;
struGraph.uWindowX0=0;
struGraph.uWindowY0=0;
struGraph.uWindowX1=240;
struGraph.uWindowY1=128;
struGraph.nOriginX=0;
struGraph.nOriginY=0;
struGraph.uLineMode=LINEMODE;
LCDGraph(&struGraph);
for ( j=0;j<20;j++ ) _Delay(50);
LCDSetScreenBuffer(nScreenBuffer);
LCDCLS();
}
if(keyflage==2)/*第6键*/
{
/***********************************/
/***********************************/
struGraph.pData=nGraphBuf3;
struGraph.uDataMode=DATAUINTMODE;
struGraph.uDataLength=256;
struGraph.uMaxValue=2500;
struGraph.uWindowX0=0;
struGraph.uWindowY0=0;
struGraph.uWindowX1=240;
struGraph.uWindowY1=128;
struGraph.nOriginX=0;
struGraph.nOriginY=0;
struGraph.uLineMode=LINEMODE;
LCDGraph(&struGraph);
for ( j=0;j<20;j++ ) _Delay(50);
LCDSetScreenBuffer(nScreenBuffer);
LCDCLS();
}
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
if(uWork1==128)
{
keyflage=0;
}
if(uWork1==64)
{
keyflage=1;
}
if(uWork1==32)
{
keyflage=2;
}
}
}
}
interrupt void adc_isr(void)
{ if(j==0) {LBDS=0x1;j=1;}
else {LBDS=0x0;j=0;}
Voltage_1 = AdcRegs.ADCRESULT0 >>4;
Voltage_2 = AdcRegs.ADCRESULT1 >>4;
nGraphBuf1[ConversionCount]=(Voltage_1)/4 ;
nGraphBuf2[ConversionCount]=(Voltage_2)/4;
nMixing[ConversionCount]=nGraphBuf1[ConversionCount]+nGraphBuf2[ConversionCount];
dn=nGraphBuf1[ConversionCount]/128;
for ( i=LMSNUMBER-1;i>0;i-- )
{
fXn[i]=fXn[i-1];
}
fXn[0]=nMixing[ConversionCount]/128;
flmsout=LMS();
nGraphBuf3[ConversionCount]=(Uint16)(flmsout*128);
//***********************************************//
eN=dn-flmsout;
en[ConversionCount]=eN;
for(i=0;i<LMSNUMBER;i++)
{
fWn2[i]=fWn1[i]+uN*fXn[i]*eN;
}
for(i=0;i<LMSNUMBER;i++)
{
fWn1[i]=fWn2[i];
}
//***********************************************/
ConversionCount++;
if(ConversionCount==256)
{ConversionCount=0;flage=1;}
/******************************/
// 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;
}
float LMS()
{
float sum=0;
for(i=0;i<LMSNUMBER;i++)
{
sum+=(fXn[i]*fWn1[i]);
}
return(sum);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -