F2812内部集成了ADC转换模块。该模块是一个12位、具有流水线结构的模数转换器,内置双采样保持器(S/H),可多路选择16通道输入,快速转换时间运行在25 MHz、ADC时钟或12.5 Msps,16个转换结果寄存器可工作于连续自动排序模式或启动/停止模式。在实际使用中,ADC的转换结果误差较大,如果直接将此转换结果用于控制回路,必然会降低控制精度。(最大转换误差可以达到9%左右)
F2812的ADC转换精度较差的主要原因是存在增益误差和失调误差,要提高转换精度就必须对两种误差进行补偿。
对于ADC模块采取了如下方法对其进行校正:
选用ADC的任意两个通道(如A3,A4)作为参考输入通道,并分别提供给它们已知的直流参考电压作为输入(RefHigh和RefLow),通过读取相应的结果寄存器获取转换值,利用两组输入输出值求得ADC模块的校正增益和校正失调,然后利用这两个值对其他通道的转换数据进行补偿,从而提高了ADC模块转换的准确度。
实现校准的硬件电路在本文中不作描述,在有关资料中可以查到。下面是该算法的C语言实现:
- //首先计算两个通道的参考电压转换后的理想结果
- // A4 = RefHigh = 2.5V ( 2.5*4095/3.0 = 3413 ideal count)
- // A3 = RefLow = 0.5V ( 0.5*4095/3.0 = 683 ideal count)
- #define REF_HIGH_IDEAL_COUNT 3413
- #define REF_LOW_IDEAL_COUNT 683
- #define SAMPLES 63
- //定义所需的各个变量
- Uint16 Avg_RefHighActualCount;
- Uint16 Avg_RefLowActualCount; /
- Uint16 CalGain; // Calibration Gain
- Uint16 CalOffset; // Calibration Offset
- Uint16 SampleCount;
- Uint16 RefHighActualCount;
- Uint16 RefLowActualCount;
- //对各个变量进行初始化
- void InitCalib()
- {
- Avg_RefLowActualCount = 0;
- Avg_RefLowActualCount = 0;
- Avg_RefHighActualCount = 0;
- RefHighActualCount = 0;
- RefLowActualCount = 0;
- CalGain = 0;
- CalOffset = 0;
- SampleCount = 0;
- }
- //获得校准增益和校准失调
- // Algorithm: Calibration formula used is:
- //
- // ch(n) = ADCRESULTn*CalGain - CalOffset
- // n = 0 to 15 channels
- // CalGain = (RefHighIdealCount - RefLowIdealCount)
- // -----------------------------------------
- // (Avg_RefHighActualCount - Avg_RefLowActualCount)
- //
- // CalOffset = Avg_RefLowActualCount*CalGain - RefLowIdealCount
- //
- // A running weighted average is calculated for the reference inputs:
- //
- // Avg_RefHighActualCount = (Avg_RefHighActualCount*SAMPLES
- // + RefHighActualCount) / (SAMPLES+1)
- //
- // Avg_RefLowActualCount = (Avg_RefLowActualCount*SAMPLES
- // + RefLowActualCount) / (SAMPLES+1)
- //
- void GetCalibParam()
- {
- RefHighActualCount = AdcRegs.ADCRESULT4 >>4;
- RefLowActualCount = AdcRegs.ADCRESULT3 >>4;
- if(SampleCount > SAMPLES)
- SampleCount = SAMPLES;
- Avg_RefHighActualCount = (Avg_RefHighActualCount * SampleCount
- + RefHighActualCount) / (SampleCount+1);
- Avg_RefLowActualCount = (Avg_RefLowActualCount * SampleCount
- + RefLowActualCount) / (SampleCount+1);
- CalGain = (REF_HIGH_IDEAL_COUNT - REF_LOW_IDEAL_COUNT)
- / (Avg_RefHighActualCount - Avg_RefLowActualCount);
- CalOffset = Avg_RefLowActualCount*CalGain - RefLowIdealCount;
- SampleCount++;
- }
- //在ADC_ISR中,对其他各个通道的结果进行修正:
- interrupt void adc_isr(void)
- {
- GetCalibParam();
- ......
- newResult n= AdcRegs.ADCRESULTn*CalGain - CalOffset;
- ......
- }
[color=rgb(51, 102, 153) !important]复制代码
通过上面的代码,配合硬件电路改动,可以大幅实现提高ADC采样的精度,实现更灵敏、更精确的控制。