火辣西米秀 发表于 2021-6-28 07:36

一则CH548/CH549例程ADC程序的bug解决

<p>用官方例程测量外部电压,使用的AIN1通道,也就是P1.1引脚。外部使用2个电阻分压输入到P1.1引脚。实测发现电压大于实际电压,4V电压分压后应该是2V左右,用万用表量确有2.6V。</p>

<p>以为电阻错了,检查后没有问题。</p>

<p>怀疑单片机引脚使能了上拉。查看了WCH例程发现了问题。</p>

<p><br />
#define &nbsp;CH0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br />
#define &nbsp;CH1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br />
#define &nbsp;CH2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br />
#define &nbsp;CH3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3<br />
&hellip;&hellip;</p>

<p>ADC_ChSelect(CH1);</p>

<p><br />
/*******************************************************************************<br />
* Function Name &nbsp;: ADC_ChSelect<br />
* Description &nbsp; &nbsp;: ADC 外部通道选择<br />
* Input &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: UINT8 ch<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ch, 位域 0~15bit分别对应外部16通道<br />
* Output &nbsp; &nbsp; &nbsp; &nbsp; : None<br />
* Return &nbsp; &nbsp; &nbsp; &nbsp; : None<br />
*******************************************************************************/<br />
void ADC_ChSelect( UINT8 ch )<br />
{<br />
&nbsp; &nbsp; ADC_CHAN = (ADC_CHAN &amp; ~MASK_ADC_CHAN) | ch;<br />
&nbsp; &nbsp; if(ch&lt;=7) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//P10~P17引脚配置,不用每次都设置<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; P1_MOD_OC &amp;= ~(ch&amp;0xFF); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //高阻输入<br />
&nbsp; &nbsp; &nbsp; &nbsp; P1_DIR_PU &amp;= ~(ch&amp;0xFF);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if(ch&gt;7 &amp;&amp; ch&lt;=0x0f) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//P00~P07引脚配置,不用每次都设置<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; P0_MOD_OC &amp;= ~((ch-7)&amp;0xFF); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//高阻输入<br />
&nbsp; &nbsp; &nbsp; &nbsp; P0_DIR_PU &amp;= ~((ch-7)&amp;0xFF);<br />
&nbsp; &nbsp; }<br />
}</p>

<p>官方库的选择通道配置的是高阻输入,但当通道1参数进来时实际上是将P1.0配置成了高阻输入,而产非P1.1。<br />
P1_MOD_OC = 0xfe; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
P1_DIR_PU= 0xfe;</p>

<p>看了手册引脚上电默认是准双向带上拉模式。</p>

<p>因此这个应该是1&lt;&lt;ch后再操作吧???</p>

火辣西米秀 发表于 2021-6-28 07:37

<p>解决办法也很简单,这样定义下就好了</p>

<p>如下:</p>

<p>//通道号选择,分别对应P1.0~P1.7, P0.0~P0.7<br />
#define &nbsp;CH0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0001<br />
#define &nbsp;CH1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0002<br />
#define &nbsp;CH2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0004<br />
#define &nbsp;CH3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0008<br />
#define &nbsp;CH4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0010<br />
#define &nbsp;CH5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0020<br />
#define &nbsp;CH6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0040<br />
#define &nbsp;CH7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0080<br />
#define &nbsp;CH8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0100<br />
#define &nbsp;CH9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x0200<br />
#define &nbsp;CH10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x0400<br />
#define &nbsp;CH11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x0800<br />
#define &nbsp;CH12 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x1000<br />
#define &nbsp;CH13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x2000<br />
#define &nbsp;CH14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x4000<br />
#define &nbsp;CH15 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x8000<br />
#define &nbsp;CHALL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0xFFFF<br />
&nbsp;</p>

littleshrimp 发表于 2021-6-28 11:26

<p>刚刚看了官方的代码,好像确实像你说的这样,应该设置对应的位来选择对应的通道。</p>

<p>官方的代码使用的是0~16,如果是这样,如果输出参数是3,这时实际测量的数据应该是通道1和2的轮询值吗?</p>

<p></p>

高级灰0090 发表于 2024-10-7 12:26

页: [1]
查看完整版本: 一则CH548/CH549例程ADC程序的bug解决