📄 example_280xadc.c
字号:
//Example_280xAdc.c
//###########################################################################
#include "DSP280x_Device.h" // DSP280x Headerfile Include File
#include "DSP280x_Examples.h" // DSP280x Examples Include File
// Prototype statements for functions found within this file.
interrupt void adc_isr(void);
// Global variables used in this example:
float Voltage[16],adclo=0;
main()
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP280x_SysCtrl.c file.
InitSysCtrl();
// For this example, set HSPCLK to SYSCLKOUT / 8 (12.5Mhz assuming 100Mhz SYSCLKOUT)
EALLOW;
SysCtrlRegs.HISPCP.all = 0x4; // HSPCLK = SYSCLKOUT/8
EDIS;
// Step 2. Initialize GPIO:
// This example function is found in the DSP280x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// 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 DSP280x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// 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 DSP280x_DefaultIsr.c.
// This function is found in DSP280x_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.ADCINT = &adc_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
InitAdc(); // For this example, init the ADC
// Step 5. User specific code, enable interrupts:
// 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
// Assumes ePWM1 clock is already enabled in InitSysCtrl();
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0x1; // TBCLK=SYSCLKOUT/(HSPCLKDIV*CLKDIV)=60M/(2*2)=15M
EPwm1Regs.TBCTL.bit.CLKDIV = 0x1;
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.CMP_A=0x0080; // Set compare A value
EPwm1Regs.TBPRD = 750; // Set period for ePWM1, frequency 20KHz(adc sample frequency 20KHz )
EPwm1Regs.TBCTL.bit.CTRMODE = 0; // count up and start
// Wait for ADC interrupt
for(;;)
{
}
}
interrupt void adc_isr(void)
{
Voltage[0] = ((float)AdcRegs.ADCRESULT0)*3.0/65520.0+adclo;
Voltage[1] = ((float)AdcRegs.ADCRESULT1)*3.0/65520.0+adclo;
Voltage[2] = ((float)AdcRegs.ADCRESULT2)*3.0/65520.0+adclo;
Voltage[3] = ((float)AdcRegs.ADCRESULT3)*3.0/65520.0+adclo;
Voltage[4] = ((float)AdcRegs.ADCRESULT4)*3.0/65520.0+adclo;
Voltage[5] = ((float)AdcRegs.ADCRESULT5)*3.0/65520.0+adclo;
Voltage[6] = ((float)AdcRegs.ADCRESULT6)*3.0/65520.0+adclo;
Voltage[7] = ((float)AdcRegs.ADCRESULT7)*3.0/65520.0+adclo;
Voltage[8] = ((float)AdcRegs.ADCRESULT8)*3.0/65520.0+adclo;
Voltage[9] = ((float)AdcRegs.ADCRESULT9)*3.0/65520.0+adclo;
Voltage[10] = ((float)AdcRegs.ADCRESULT10)*3.0/65520.0+adclo;
Voltage[11] = ((float)AdcRegs.ADCRESULT11)*3.0/65520.0+adclo;
Voltage[12] = ((float)AdcRegs.ADCRESULT12)*3.0/65520.0+adclo;
Voltage[13] = ((float)AdcRegs.ADCRESULT13)*3.0/65520.0+adclo;
Voltage[14] = ((float)AdcRegs.ADCRESULT4)*3.0/65520.0+adclo;
Voltage[15] = ((float)AdcRegs.ADCRESULT5)*3.0/65520.0+adclo;
// 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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -