<font face="微软雅黑"><font style="font-size:18px">如图里面所示,无论我是在ID端口输入恒定电压,还是直接在A0端口输入,结果寄存器ADCRESULT0的数一直都在波动,波动值大概有100左右,偶尔还会发生突变,变成接近0或者4095,我用的是12位AD模式,用16位的话波动值更大,用示波器检测电源信号,看到电源只有几毫伏的波动,所以一直不知道是哪出问题了,下面是AD的配置程序,还望大神们答疑解惑</font></font><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">ConfigureADC</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
EALLOW;
<span class="hljs-comment">//write configurations</span>
AdcaRegs.ADCCTL2.bit.PRESCALE = <span class="hljs-number">6</span>; <span class="hljs-comment">//set ADCCLK divider to /4</span>
AdcbRegs.ADCCTL2.bit.PRESCALE = <span class="hljs-number">6</span>; <span class="hljs-comment">//set ADCCLK divider to /4</span>
AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
AdcSetMode(ADC_ADCB, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
<span class="hljs-comment">//Set pulse positions to late</span>
AdcaRegs.ADCCTL1.bit.INTPULSEPOS = <span class="hljs-number">1</span>;
AdcbRegs.ADCCTL1.bit.INTPULSEPOS = <span class="hljs-number">1</span>;
<span class="hljs-comment">//power up the ADCs</span>
AdcaRegs.ADCCTL1.bit.ADCPWDNZ = <span class="hljs-number">1</span>;
AdcbRegs.ADCCTL1.bit.ADCPWDNZ = <span class="hljs-number">1</span>;
<span class="hljs-comment">//delay for 1ms to allow ADC time to power up</span>
DELAY_US(<span class="hljs-number">1000</span>);
EDIS;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">SetupADC</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
Uint16 acqps;
<span class="hljs-comment">//determine minimum acquisition window (in SYSCLKS) based on resolution</span>
IF(ADC_RESOLUTION_12BIT == AdcaRegs.ADCCTL2.bit.RESOLUTION){
acqps = <span class="hljs-number">14</span>; <span class="hljs-comment">//75ns</span>
}
<span class="hljs-keyword">else</span> { <span class="hljs-comment">//resolution is 16-bit</span>
acqps = <span class="hljs-number">63</span>; <span class="hljs-comment">//320ns</span>
}
<span class="hljs-comment">//Select the channels to convert and end of conversion flag</span>
<span class="hljs-comment">//ADCA</span>
EALLOW;
AdcaRegs.ADCSOC0CTL.bit.CHSEL = <span class="hljs-number">0</span>; <span class="hljs-comment">//SOC0 will convert pin A0</span>
AdcaRegs.ADCSOC0CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcaRegs.ADCSOC1CTL.bit.CHSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//SOC1 will convert pin A1</span>
AdcaRegs.ADCSOC1CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC1CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcaRegs.ADCSOC2CTL.bit.CHSEL = <span class="hljs-number">2</span>; <span class="hljs-comment">//SOC1 will convert pin A1</span>
AdcaRegs.ADCSOC2CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC2CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcaRegs.ADCSOC3CTL.bit.CHSEL = <span class="hljs-number">3</span>; <span class="hljs-comment">//SOC1 will convert pin A1</span>
AdcaRegs.ADCSOC3CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC3CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcaRegs.ADCSOC4CTL.bit.CHSEL = <span class="hljs-number">4</span>; <span class="hljs-comment">//SOC1 will convert pin A1</span>
AdcaRegs.ADCSOC4CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC4CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcaRegs.ADCSOC5CTL.bit.CHSEL = <span class="hljs-number">5</span>; <span class="hljs-comment">//SOC1 will convert pin A1</span>
AdcaRegs.ADCSOC5CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC5CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
<span class="hljs-comment">//ADCB</span>
AdcbRegs.ADCSOC0CTL.bit.CHSEL = <span class="hljs-number">0</span>; <span class="hljs-comment">//SOC0 will convert pin B0</span>
AdcbRegs.ADCSOC0CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcbRegs.ADCSOC0CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcbRegs.ADCSOC1CTL.bit.CHSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//SOC1 will convert pin B1</span>
AdcbRegs.ADCSOC1CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcbRegs.ADCSOC1CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcbRegs.ADCSOC2CTL.bit.CHSEL = <span class="hljs-number">2</span>; <span class="hljs-comment">//SOC1 will convert pin B1</span>
AdcbRegs.ADCSOC2CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcbRegs.ADCSOC2CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
AdcbRegs.ADCSOC3CTL.bit.CHSEL = <span class="hljs-number">3</span>; <span class="hljs-comment">//SOC1 will convert pin B1</span>
AdcbRegs.ADCSOC3CTL.bit.ACQPS = acqps; <span class="hljs-comment">//sample window is acqps + 1 SYSCLK cycles</span>
AdcaRegs.ADCSOC3CTL.bit.TRIGSEL = <span class="hljs-number">1</span>; <span class="hljs-comment">//这里设置1为timer0中断,设置0为软件中断</span>
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">AdcSetMode</span><span class="hljs-params">(Uint16 adc, Uint16 resolution, Uint16 signalmode)</span>
</span>{
Uint16 adcOffsetTrimOTPIndex; <span class="hljs-comment">//index into OTP table of ADC offset trims</span>
Uint16 adcOffsetTrim; <span class="hljs-comment">//temporary ADC offset trim</span>
<span class="hljs-comment">//re-populate INL trim</span>
CalAdcINL(adc);
<span class="hljs-keyword">if</span>(<span class="hljs-number">0xFFFF</span> != *((Uint16*)GetAdcOffsetTrimOTP)){
<span class="hljs-comment">//offset trim function is programmed into OTP, so call it</span>
<span class="hljs-comment">//calculate the index into OTP table of offset trims and call</span>
<span class="hljs-comment">//function to return the correct offset trim</span>
adcOffsetTrimOTPIndex = <span class="hljs-number">4</span>*adc + <span class="hljs-number">2</span>*resolution + <span class="hljs-number">1</span>*signalmode;
adcOffsetTrim = (*GetAdcOffsetTrimOTP)(adcOffsetTrimOTPIndex);
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">//offset trim function is not populated, so set offset trim to 0</span>
adcOffsetTrim = <span class="hljs-number">0</span>;
}
<span class="hljs-comment">//Apply the resolution and signalmode to the specified ADC.</span>
<span class="hljs-comment">//Also apply the offset trim and, if needed, linearity trim correction.</span>
<span class="hljs-keyword">switch</span>(adc){
<span class="hljs-keyword">case</span> ADC_ADCA:
AdcaRegs.ADCCTL2.bit.RESOLUTION = resolution;
AdcaRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
AdcaRegs.ADCOFFTRIM.all = adcOffsetTrim;
<span class="hljs-keyword">if</span>(ADC_RESOLUTION_12BIT == resolution){
<span class="hljs-comment">//12-bit linearity trim workaround</span>
AdcaRegs.ADCINLTRIM1 &= <span class="hljs-number">0xFFFF0000</span>;
AdcaRegs.ADCINLTRIM2 &= <span class="hljs-number">0xFFFF0000</span>;
AdcaRegs.ADCINLTRIM4 &= <span class="hljs-number">0xFFFF0000</span>;
AdcaRegs.ADCINLTRIM5 &= <span class="hljs-number">0xFFFF0000</span>;
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCB:
AdcbRegs.ADCCTL2.bit.RESOLUTION = resolution;
AdcbRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
AdcbRegs.ADCOFFTRIM.all = adcOffsetTrim;
<span class="hljs-keyword">if</span>(ADC_RESOLUTION_12BIT == resolution){
<span class="hljs-comment">//12-bit linearity trim workaround</span>
AdcbRegs.ADCINLTRIM1 &= <span class="hljs-number">0xFFFF0000</span>;
AdcbRegs.ADCINLTRIM2 &= <span class="hljs-number">0xFFFF0000</span>;
AdcbRegs.ADCINLTRIM4 &= <span class="hljs-number">0xFFFF0000</span>;
AdcbRegs.ADCINLTRIM5 &= <span class="hljs-number">0xFFFF0000</span>;
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCC:
AdccRegs.ADCCTL2.bit.RESOLUTION = resolution;
AdccRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
AdccRegs.ADCOFFTRIM.all = adcOffsetTrim;
<span class="hljs-keyword">if</span>(ADC_RESOLUTION_12BIT == resolution){
<span class="hljs-comment">//12-bit linearity trim workaround</span>
AdccRegs.ADCINLTRIM1 &= <span class="hljs-number">0xFFFF0000</span>;
AdccRegs.ADCINLTRIM2 &= <span class="hljs-number">0xFFFF0000</span>;
AdccRegs.ADCINLTRIM4 &= <span class="hljs-number">0xFFFF0000</span>;
AdccRegs.ADCINLTRIM5 &= <span class="hljs-number">0xFFFF0000</span>;
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCD:
AdcdRegs.ADCCTL2.bit.RESOLUTION = resolution;
AdcdRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
AdcdRegs.ADCOFFTRIM.all = adcOffsetTrim;
<span class="hljs-keyword">if</span>(ADC_RESOLUTION_12BIT == resolution){
<span class="hljs-comment">//12-bit linearity trim workaround</span>
AdcdRegs.ADCINLTRIM1 &= <span class="hljs-number">0xFFFF0000</span>;
AdcdRegs.ADCINLTRIM2 &= <span class="hljs-number">0xFFFF0000</span>;
AdcdRegs.ADCINLTRIM4 &= <span class="hljs-number">0xFFFF0000</span>;
AdcdRegs.ADCINLTRIM5 &= <span class="hljs-number">0xFFFF0000</span>;
}
<span class="hljs-keyword">break</span>;
}
}
<span class="hljs-comment">/*
* Loads INL trim values from OTP into the trim registers of the specified ADC.
* Use only as part of AdcSetMode function, since linearity trim correction
* is needed for some modes.
*/</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CalAdcINL</span><span class="hljs-params">(Uint16 adc)</span>
</span>{
<span class="hljs-keyword">switch</span>(adc){
<span class="hljs-keyword">case</span> ADC_ADCA:
<span class="hljs-keyword">if</span>(<span class="hljs-number">0xFFFF</span> != *((Uint16*)CalAdcaINL)){
<span class="hljs-comment">//trim function is programmed into OTP, so call it</span>
(*CalAdcaINL)();
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">//do nothing, no INL trim function populated</span>
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCB:
<span class="hljs-keyword">if</span>(<span class="hljs-number">0xFFFF</span> != *((Uint16*)CalAdcbINL)){
<span class="hljs-comment">//trim function is programmed into OTP, so call it</span>
(*CalAdcbINL)();
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">//do nothing, no INL trim function populated</span>
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCC:
<span class="hljs-keyword">if</span>(<span class="hljs-number">0xFFFF</span> != *((Uint16*)CalAdccINL)){
<span class="hljs-comment">//trim function is programmed into OTP, so call it</span>
(*CalAdccINL)();
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">//do nothing, no INL trim function populated</span>
}
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> ADC_ADCD:
<span class="hljs-keyword">if</span>(<span class="hljs-number">0xFFFF</span> != *((Uint16*)CalAdcdINL)){
<span class="hljs-comment">//trim function is programmed into OTP, so call it</span>
(*CalAdcdINL)();
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">//do nothing, no INL trim function populated</span>
}
<span class="hljs-keyword">break</span>;
}
}
</code></pre><p>
<br>
<br>
<ignore_js_op>
</ignore_js_op></p><dl class="tattl attm">
<dd>
<img src="data/attach/1911/yep3g54z1e5xnzzti4gcte6rrx34dmyq.png" alt="微信截图_20190723100314.png" title="微信截图_20190723100314.png">
</dd>
</dl>
<p><br></p><p></p><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>