📄 adccalibrationmain.c
字号:
//###########################################################################
//
// FILE: ADCcalibrationMain.c
//
// TITLE: ADC Calibration - Example Program
//
//###########################################################################
//
// Ver | dd-mmm-yyyy | Who | Description of changes
// =====|=============|=======|==============================================
// 1.0 | 20 Jun 2003 | AT/SD | Original Release.
// -----|-------------|-------|----------------------------------------------
// 1.1 | 26 Nov 2004 | AT | Fixed Bug In ADCcalibrationInitADC function.
// | | | Was incorrectly disabling ADC clock.
//
//###########################################################################
//
// DESCRIPTION
//
// This example program runs from RAM on the EzDSP. It initializes the
// event manager to generate a periodic start of conversion (SOC) pulse
// to the ADC. This will trigger a conversion of the ADC and when completed
// the ADC will generate an interrupt. The interrupt is serviced and
// the ADC calibration function is called. This function will read two
// user selected reference channels and calculate the appropriate
// calibration gain and offset and then calibrate all other user channels.
//
// The program supports configuring the ADC for simultaneous or sequential
// conversion modes:
//
// SEQUENTIAL: ADC channels are converted one at a time:
// A0->A1->A2->...B0->B1->B2->....
//
// SIMULTANEOUS: ADC channels are converted in pairs:
// A0->A1->A2->....
// B0 B1 B2
//
// The calibrated and converted channels are stored in a RAM structure
// which contains the following information:
//
// typedef struct {
// Uint16 *RefHighChAddr; // Channel Address of RefHigh
// Uint16 *RefLowChAddr; // Channel Address of RefLow
// Uint16 *Ch0Addr; // Channel 0 Address
// Uint16 Avg_RefHighActualCount; // Ideal RefHigh Count (Q4)
// Uint16 Avg_RefLowActualCount; // Ideal RefLow Count (Q4)
// Uint16 RefHighIdealCount; // Ideal RefHigh Count (Q0)
// Uint16 RefLowIdealCount; // Ideal RefLow Count (Q0)
// Uint16 CalGain; // Calibration Gain (Q12)
// Uint16 CalOffset; // Calibration Offset (Q0)
// // Store Calibrated ADC Data (Q0):
// // Simultaneous Sequential
// // ============ ============
// Uint16 ch0; // A0 A0
// Uint16 ch1; // B0 A1
// Uint16 ch2; // A1 A2
// Uint16 ch3; // B1 A3
// Uint16 ch4; // A2 A4
// Uint16 ch5; // B2 A5
// Uint16 ch6; // A3 A6
// Uint16 ch7; // B3 A7
// Uint16 ch8; // A4 B0
// Uint16 ch9; // B4 B1
// Uint16 ch10; // A5 B2
// Uint16 ch11; // B5 B3
// Uint16 ch12; // A6 B4
// Uint16 ch13; // B6 B5
// Uint16 ch14; // A7 B6
// Uint16 ch15; // B7 B7
// Uint16 StatusExtMux; // Status Of External Mux
// }ADC_CALIBRATION_DRIVER_VARS;
//
// This program also supports the user toggling a GPIO pin for toggling
// an external analog mux to expand the usable channels.
//
// To adapt to the user system needs, the user needs to configure
// assembly time switches and settings contained in the header file:
//
// ADCcalibrationDriver.h
//
// The user needs to select simultaneous or sequential sampling mode
// of operation. For example:
//
// #define SEQUENTIAL 1
// #define SIMULTANEOUS 0
// #define ADC_SAMPLING_MODE SIMULTANEOUS
//
// And needs to select which ADC channels are connected to reference high
// and reference low and the ideal count value. For example:
//
// A6 = RefHigh = 2.5V ( 2.5*4095/3.0 = 3413 ideal count)
// A7 = RefLow = 1.25V (1.25*4095/3.0 = 1707 ideal count)
//
// #define REF_HIGH_CH A6
// #define REF_LOW_CH A7
// #define REF_HIGH_IDEAL_COUNT 3413
// #define REF_LOW_IDEAL_COUNT 1707
//
//###########################################################################
#include <DSP28_Device.h>
#include <ADCcalibrationDriver.h>
// Initialize frequency parameters for this program:
//
#define CPU_FREQ 150E6
#define PWM_FREQ 250E3
#define PWM_PRD CPU_FREQ/PWM_FREQ
float a1[16]; //by cuidezhi
float adclo = 0.0;
// Prototype statements for functions found within this file:
//
interrupt void ADC_ISR(void);
// Allocate calibration driver variables:
//
ADC_CALIBRATION_DRIVER_VARS adc;
//==========================================================================
// MAIN:
//==========================================================================
//
void main(void)
{
// Step 1. Initialize System Control registers, PLL, WatchDog:
// SYSCLK=(10*CLK_IN)/2
// HSPCLK=SYSCLK: Note that EV & ADC runs on High Speed clock
// LSPCLK=SYSCLK/2: Note that all serial port are operation from Low Speed Clock
// Input Clock: 30M, SYSCLK=150Mhz, HSPCLK=150Mhz, LSPCLK=75Mhz
//
InitSysCtrl();
// Step2. Initialize PIE module:
//
InitPieCtrl();
EALLOW; // Enable access to protected registers
PieVectTable.ADCINT = &ADC_ISR;
EDIS; // Disable access to protected registers
PieCtrlRegs.PIEIER1.bit.INTx6 = 1; // Enable PIE INT1.6 = ADCINT
// Step 3. Initialize ADC:
//
ADCcalibrationInitAdc();
// Step 4. Initialize Calibration Driver Variables:
//
ADCcalibrationDriverInit(&adc);
// Step 5. Initialize Event Manager:
//
InitEv();
EvaRegs.T1PR = PWM_PRD; // Setup period register
EvaRegs.T1CMPR = PWM_PRD; // Setup T1 compare value for 0% duty cycle
// Step 6. Initialize GPIO for toggling external Mux (optional):
// For this example we have used GPIOG4 (also used as SCI-B TX pin)
//
GpioMuxRegs.GPGMUX.bit.SCITXDB_GPIOG4 = 0; // Set Mux G for GPIO mode
GpioMuxRegs.GPGDIR.bit.GPIOG4 = 1; // Set pin as output
GpioDataRegs.GPGSET.bit.GPIOG4 = 1; // Force pin high as default
// Step 7: Enable core interrupts:
//
IER |= M_INT1; // ADC Interrupt
// Step 8: Enable global interrupts and real-time mode:
//
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// Loop for ever...
//
while (1) {};
}
//==========================================================================
// ADC Interrupt Service Routine:
//==========================================================================
// This ISR will be called for all ADC conversions:
//
interrupt void ADC_ISR(void)
{
//---------------------------------------------------------------------
// Read ADC results, calibrate & store in "adc" memory structure:
//
ADCcalibrationDriverUpdate(&adc); // Benchmark = 137 cycles
//---------------------------------------------------------------------
// Switch External Channel Mux (optional):
// Note: Uses GPIOG4 for this example.
adc.StatusExtMux = GpioDataRegs.GPGDAT.all; // Save current state of pin
GpioDataRegs.GPGTOGGLE.bit.GPIOG4 = 1; // Toggle pin and mux will switch
// Benchmark = 9 cycles
//---------------------------------------------------------------------
// 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
//---------------------------------------------------------------------
// User code....
//By cuidezhi
a1[0]=((float)AdcRegs.ADCRESULT0)*3.0/4095.0+adclo;
// a1[0]=((float)AdcRegs.ADCRESULT0)*3.0/65520.0+adclo;
a1[1]=((float)AdcRegs.ADCRESULT1)*3.0/65520.0+adclo;
a1[2]=((float)AdcRegs.ADCRESULT2)*3.0/65520.0+adclo;
a1[3]=((float)AdcRegs.ADCRESULT3)*3.0/65520.0+adclo;
a1[4]=((float)AdcRegs.ADCRESULT4)*3.0/65520.0+adclo;
a1[5]=((float)AdcRegs.ADCRESULT5)*3.0/65520.0+adclo;
a1[6]=((float)AdcRegs.ADCRESULT6)*3.0/65520.0+adclo;
a1[7]=((float)AdcRegs.ADCRESULT7)*3.0/65520.0+adclo;
a1[8]=((float)AdcRegs.ADCRESULT8)*3.0/65520.0+adclo;
a1[9]=((float)AdcRegs.ADCRESULT9)*3.0/65520.0+adclo;
a1[10]=((float)AdcRegs.ADCRESULT10)*3.0/65520.0+adclo;
a1[11]=((float)AdcRegs.ADCRESULT11)*3.0/65520.0+adclo;
a1[12]=((float)AdcRegs.ADCRESULT12)*3.0/65520.0+adclo;
a1[13]=((float)AdcRegs.ADCRESULT13)*3.0/65520.0+adclo;
a1[14]=((float)AdcRegs.ADCRESULT14)*3.0/65520.0+adclo;
a1[15]=((float)AdcRegs.ADCRESULT15)*3.0/65520.0+adclo;
//
return;
}
//==========================================================================
// No more.
//==========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -