提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
ADC 顺序采样模式测试教程
前言
ADC 顺序采样模式的代码记录:
一、ADC采样是什么
二、代码
//###########################################################################
//
// FILE: Example_2833xAdcSeqModeTest.c
//
// TITLE: ADC顺序采样模式测试
//
// ASSUMPTIONS:
//
// 此程序需要 DSP2833x头文件.
//
// 编译此例程前,在DSP2833x_Examples.h文件中确保CPU时钟速
// 度被合适地定义
//
// 连接信号到转换通道A0
// 功能描述:
//
// 通道A0一直被转换并被加载到缓存器中(SampleTable)
//
// 运行一会程序然后观察内容的变化
//
// 观察变量:
// SampleTable - 加载的转换值.
//
#include "DSP2833x_Device.h" // DSP2833x Header file Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
// ADC start parameters
// 设置CPU时钟频率
#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
// 手把手第169页
#define ADC_CKPS 0x1 // ADC module clock = HSPCLK/2*ADC_CKPS = 25.0MHz/(1*2) = 12.5MHz
#define ADC_SHCLK 0xf // S/H width in ADC module periods = 16 ADC clocks
#define AVG 1000 // Average sample limit
#define ZOFFSET 0x00 // Average Zero offset
#define BUF_SIZE 2048 // Sample buffer size
// Global variable for this example
Uint16 SampleTable[BUF_SIZE];
main()
{
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/2*ADC_MODCLK
EDIS;
// Step 2. Initialize GPIO:
// This example function is found in the DSP2833x_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 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; //采样窗口大小配置
AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS; // 对高速时钟进行分频
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1; // 1是选择级联模式
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // 采样通道1
AdcRegs.ADCTRL1.bit.CONT_RUN = 1; // 连续转换模式
// Step 5. User specific code, enable interrupts:
// Clear SampleTable
for (i=0; i<BUF_SIZE; i++)
{
SampleTable[i] = 0;
}
// Start SEQ
AdcRegs.ADCTRL2.all = 0x2000; // SOC_SEQ1 = 1,立即复位序列发生器状态为CONV00
// Take ADC data and log the in SampleTable array
for(;;)
{
for (i=0; i<AVG; i++)
{
while (AdcRegs.ADCST.bit.INT_SEQ1== 0) {} // Wait for interrupt,等待,直到转换完成,然后读取结果
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; //清除标志位
SampleTable[i] =((AdcRegs.ADCRESULT0>>4));
}
}
}
//===========================================================================
// No more.
//===========================================================================