//该程序用于信号ADC,其中,采用定时器0中断读取采样结果 //
//该程序ADC触发源为软件触发(S/w),然后,通过AdcRegs.ADCST.bit.INT_SEQ1标志位为1判断转换结果
//采用连续采样方式,连续采集A0和A1口数据,A0口数据放在Result0,2-15中,A1口数据放到Result1中
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
interrupt void cpu_timer0_isr(void);
#define startCpuTimer0() CpuTimer0Regs.TCR.bit.TSS=0
//int ncount;
// ADC start parameters
#if (CPU_FRQ_150MHZ) // Default - 150 MHz SYSCLKOUT
#define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3) = 25.0 MHz
#endif
#if (CPU_FRQ_100MHZ)
#define ADC_MODCLK 0x2 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 100/(2*2) = 25.0 MHz
#endif
#define ADC_CKPS 0x0 // ADC module clock = HSPCLK/1 = 25.5MHz/(1) = 25.0 MHz
#define ADC_SHCLK 0x1 // S/H width in ADC module periods = 2 ADC cycle
#define BUF_SIZE 1024 // Sample buffer size
// Global variable for this example
Uint16 SampleTable[BUF_SIZE];
Uint16 SampleTable1[BUF_SIZE];
Uint16 Data=0;
void main(void)
{
Uint16 i;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
// Specific clock setting for this example:
EALLOW;
SysCtrlRegs.HISPCP.all = ADC_MODCLK; // HSPCLK = SYSCLKOUT/ADC_MODCLK
EDIS;
EALLOW;
// Step 2. Initalize GPIO:
GpioCtrlRegs.GPBMUX2.all=0x00000000; //功能选择寄存器 普通IO使用,GPIO48属于GPBMUX2
GpioCtrlRegs.GPBDIR.all=0xFFFFFFFF; //设置为输出模式
EDIS;
// 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 DSP2833x_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 DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2833x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
InitAdc(); // For this example, init the ADC
// Specific ADC setup for this example:
AdcRegs.ADCTRL1.bit.ACQ_PS = ADC_SHCLK; //01 采样时间窗大小,Sequential mode: Sample rate = 1/[(2+ACQ_PS)*ADC clock in ns]
// = 1/(3*40ns) =8.3MHz (for 150 MHz SYSCLKOUT)
// = 1/(3*80ns) =4.17MHz (for 100 MHz SYSCLKOUT)
// If Simultaneous mode enabled: Sample rate = 1/[(3+ACQ_PS)*ADC clock in ns]
AdcRegs.ADCTRL1.bit.CPS = 0;
AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS; //0
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1; // 1 Cascaded mode
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0;
AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1;
AdcRegs.ADCTRL1.bit.CONT_RUN = 1; // Setup continuous run
AdcRegs.ADCTRL1.bit.SEQ_OVRD = 1; // Enable Sequencer override feature
AdcRegs.ADCCHSELSEQ1.all = 0x10; // 将Results0、Results2—15全部指向A0,Results1存储A1
AdcRegs.ADCCHSELSEQ2.all = 0x0;
AdcRegs.ADCCHSELSEQ3.all = 0x0;
AdcRegs.ADCCHSELSEQ4.all = 0x0;
AdcRegs.ADCMAXCONV.bit.MAX_CONV1 = 0x1; // convert and store in 8 results registers
AdcRegs.ADCTRL2.all = 0x2000; // s/w启动方式,启动AD转换
// 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 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
// Step 4. Initialize the Device Peripheral. This function can be
// found in DSP2833x_CpuTimers.c
InitCpuTimers(); // For this example, only initialize the Cpu Timers
#if (CPU_FRQ_150MHZ)
// Configure CPU-Timer 0, 1, and 2 to interrupt every 100 usecond:
// 150MHz CPU Freq, 1 second Period (in uSeconds)
ConfigCpuTimer(&CpuTimer0, 150, 500);
//ConfigCpuTimer(&CpuTimer1, 150, 1000000);
//ConfigCpuTimer(&CpuTimer2, 150, 1000000);
#endif
#if (CPU_FRQ_100MHZ)
// Configure CPU-Timer 0, 1, and 2 to interrupt every 100 usecond:
// 100MHz CPU Freq, 1 second Period (in uSeconds)
ConfigCpuTimer(&CpuTimer0, 100, 500)
//ConfigCpuTimer(&CpuTimer1, 100, 1000000);
//ConfigCpuTimer(&CpuTimer2, 100, 1000000);
#endif
// To ensure precise timing, use write-only instructions to write to the entire register. Therefore, if any
// of the configuration bits are changed in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
// below settings must also be updated.
// Step 5. User specific code, enable interrupts: 下面代码已经在ConfigCpuTimer中定义过了
// 启动定时器
startCpuTimer0();
// Enable CPU int1 which is connected to CPU-Timer 0, CPU int13
// which is connected to CPU-Timer 1, and CPU int 14, which is connected
// to CPU-Timer 2:
IER |= M_INT1;
//IER |= M_INT13;
//IER |= M_INT14;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
for (i=0; i<BUF_SIZE; i++)
{
SampleTable[i] = 0;
SampleTable1[i] = 0;
}
while(1)
{
}
}
interrupt void cpu_timer0_isr(void)
{ Uint16 i;
CpuTimer0.InterruptCount++; //中断计数器,用于计算中断的响应次数 if(CpuTimer0.InterruptCount==2000) //每个1sLED闪烁一次,用于程序调试,可删除
{
GpioDataRegs.GPBTOGGLE.bit.GPIO48 = 1;
CpuTimer0.InterruptCount=0;
}
for (i=0; i<BUF_SIZE-1; i++) //更新数据缓存区
{
SampleTable[i] = SampleTable[i+1];
SampleTable1[i] = SampleTable1[i+1];
}
while (AdcRegs.ADCST.bit.INT_SEQ1== 0){} //等待直到转换结束
SampleTable[BUF_SIZE-1]= AdcRegs.ADCRESULT0>>4;
SampleTable1[BUF_SIZE-1]= AdcRegs.ADCRESULT1>>4;
AdcRegs.ADCST.bit.INT_SEQ1_CLR=1; //清除转换结束标志位
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
CpuTimer0Regs.TCR.bit.TIF=1;//清除中断标志位
CpuTimer0Regs.TCR.bit.TRB=1;//重新装载初值
}