东方城 发表于 2020-6-23 09:57

单片机LCD1602显示程控测量放大器

<div class='showpostmsg'><p>&nbsp; 因为这学期的课程设计我做了这个测量放大器,并且给他加上了单片机的功能以及串行通信的功能,可以说是非常全面的。</p>

<p>刚开始的时候是放大1000倍,然后放大1000倍的数值可以在单片机中显示。然后我们通过第一模块对他进行筛检,并且通过五个按键实现零到1000倍的衰减衰减后再通过D&nbsp;A&nbsp;C输出。整个过程,我们在测量放大器的基础上做了很多的改进单片机程序相对复杂,然后再试相比来说我们用了七天的时间。还是挺成功的<br />
总体电路图</p>

<p>对应的单片机部分程序:</p>

<p>#include&lt;reg51.h&gt;<br />
#include &quot;lcd.h&quot;<br />
#include&lt;absacc.h&gt;<br />
#define DAC1208 XBYTE<br />
#define uint unsigned int<br />
#define uchar unsigned char<br />
#define ulong unsigned long</p>

<p>sbit k2 = P3^2;<br />
sbit k3 = P3^3;<br />
sbit k4 = P3^4;<br />
sbit k5 = P3^5;<br />
sbit relay=P2^4;<br />
sbit relayAD=P3^7;</p>

<p>char temp=-1;<br />
int cent;<br />
ulong volt;//测量的电压值<br />
ulong sum;<br />
uchar mode,a,b,y=0,t=0,y1=0,t1=0,y2=0,t2=0,abs=5;<br />
uchar KEY_Scan(uchar mode);<br />
uchar timebit,multiplebit;<br />
uchar vol_buf,multiple_buf,out_buf;<br />
uint multiple=500;<br />
char time=-1;<br />
void delay1us(uint i);<br />
void communication();<br />
void kai_display();<br />
void kai_display1();<br />
void count();<br />
void display1();<br />
void display();</p>

<p>uint vtime;&nbsp;&nbsp; &nbsp;// 用来控制测量地址位的改变<br />
uchar addr;//测量地址位,指示测量的是哪一个模拟值 (其实就是TLC2543的控制字)</p>

<p><br />
sbit CLK=P2^3;//定义时钟信号口<br />
sbit DIN=P2^1;//定义2543数据写入口<br />
sbit DOUT=P2^0;//定义2543数据读取口<br />
sbit CS=P2^2;//定义2543片选信号口</p>

<p>void delay1us(uint i)<br />
{<br />
&nbsp;&nbsp; &nbsp;while(i--);<br />
}<br />
/**********************************************************/<br />
//函数名:read2543(uchar addr)<br />
//功能:2543驱动程序<br />
//调用函数:<br />
//输入参数:addr<br />
//输出参数:<br />
//说明:进行ad转换将结果存于volt变量中 addr为测量位地址<br />
/**********************************************************/<br />
void read2543(uchar addr) //读取数值volt是被缩小两倍的电压<br />
{<br />
&nbsp;&nbsp; &nbsp;uint ad=0;<br />
&nbsp;&nbsp; &nbsp;uchar i;<br />
&nbsp;&nbsp; &nbsp;CLK=0;<br />
&nbsp;&nbsp; &nbsp;CS=0;//片选段,启动2543<br />
&nbsp;&nbsp; &nbsp;addr&lt;&lt;=4;//对地址位预处理<br />
&nbsp;&nbsp; &nbsp;for(i=0;i&lt;12;i++) //12个时钟走完,完成一次读取测量<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(DOUT==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ad=ad|0x01;//单片机读取ad数据<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DIN=addr&amp;0x80;//2543读取测量地址位<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CLK=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;;//很短的延时<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CLK=0;//产生下降沿,产生时钟信号<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;addr&lt;&lt;=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ad&lt;&lt;=1;//将数据移位准备下一位的读写<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;CS=1;//关2543<br />
&nbsp;&nbsp; &nbsp;ad&gt;&gt;=1;<br />
&nbsp;&nbsp; &nbsp;volt=ad;//取走转换结果<br />
&nbsp;&nbsp; &nbsp;volt=volt*1221*2;//例子的满量程为5V,转换分辩率为12位(2的12次方=4096) 。即最大值是255,5/4096=1221mV,即例子中的1V代表实际1221mV &nbsp; &nbsp; &nbsp; &nbsp;<br />
}</p>

<p>void communication()<br />
{<br />
&nbsp;&nbsp; &nbsp;if(RI != 0)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RI = 0; &nbsp; &nbsp;//接收收据后进行清0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = SBUF; &nbsp; &nbsp; &nbsp;//读取数据存入temp中<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;if(temp==0)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;time=0;//选择相应的指令<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;abs=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=-1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;else if(temp==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;time=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;abs=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=-1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; else if(temp==2)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;time=2;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;abs=2;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=-1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;else if(temp==3)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;time=3;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=-1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;else if(temp==4)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;time=4;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=-1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}<br />
}<br />
#define &nbsp;K0 &nbsp;0<br />
#define &nbsp;K1_MODE &nbsp;1<br />
#define &nbsp;K2_ADD &nbsp;2<br />
#define &nbsp;K3_DEC &nbsp;3<br />
#define &nbsp;K4 4<br />
uchar KEY_Scan(uchar mode)<br />
{<br />
&nbsp;&nbsp; &nbsp;uchar key=1;<br />
&nbsp;&nbsp; &nbsp;if(key&amp;&amp;(time&gt;=0&amp;&amp;time&lt;=4))<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(time==0)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return K0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if(time==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return K1_MODE;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if(time==2)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return K2_ADD;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if(time==3)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return K3_DEC;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if(time==4)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return K4;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;else if(time&gt;=5&amp;&amp;time&lt;=-1)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key=1;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;if(a)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key=1;<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;return 5;&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>void kai_display()<br />
{<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(0,0,&quot;WangZheng And&quot;);<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(0,1,&quot;GuoXuKang&#39;s Work&quot;);<br />
}</p>

<p>void kai_display1()<br />
{<br />
&nbsp;&nbsp; &nbsp;uchar key;<br />
&nbsp;&nbsp; &nbsp;key=KEY_Scan(0);<br />
&nbsp;&nbsp; &nbsp;if(key==K4||b==K4) &nbsp; //模式选择&nbsp;&nbsp; &nbsp;直流<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;relay=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;relayAD=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;display();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;b=K4;&nbsp;&nbsp; &nbsp;//便于按一次直流,之后一直是直流<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LCD_Dispstring(2,0,&quot;DIR: &nbsp; &nbsp; &nbsp; V&quot;);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LCD_Dispstring(0,1,&quot;M: &nbsp; &nbsp;O: &nbsp; &nbsp; &nbsp; V&quot;);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;if(key==K3_DEC||b==K3_DEC) //交流档位<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;relay=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;relayAD=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;display1();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;b=K3_DEC;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LCD_Dispstring(2,0,&quot;ALT: &nbsp; &nbsp; &nbsp; V&quot;);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LCD_Dispstring(0,1,&quot;M: &nbsp; &nbsp;O: &nbsp; &nbsp; &nbsp; V&quot;);&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;}<br />
}<br />
void display()//直流显示<br />
{<br />
&nbsp;&nbsp; &nbsp;ulong invol;<br />
&nbsp;&nbsp; &nbsp;float ivol;<br />
&nbsp;&nbsp; &nbsp;count();<br />
&nbsp;&nbsp; &nbsp;ivol=(volt*1.000*multiple)/1000;<br />
&nbsp;&nbsp; &nbsp;invol=(long)ivol;<br />
&nbsp;&nbsp; &nbsp;vol_buf=volt/10000000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;vol_buf=volt/1000000+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;vol_buf=&#39;.&#39;;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(volt/100000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(volt/10000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(volt/1000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;multiple_buf=multiple/1000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple/100%10+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple/10%10+0x30;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple%10+0x30;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;out_buf=invol/10000000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;out_buf=invol/1000000+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;out_buf=&#39;.&#39;;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/100000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/10000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/1000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;LCD_Dispstring(6,0,vol_buf);<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(2,1,multiple_buf);<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(8,1,out_buf);<br />
}<br />
void display1()//交流显示<br />
{<br />
&nbsp;&nbsp; &nbsp;ulong invol,trans;<br />
&nbsp;&nbsp; &nbsp;float ivol;<br />
&nbsp;&nbsp; &nbsp;count();<br />
&nbsp;&nbsp; &nbsp;trans=volt*0.825;<br />
&nbsp;&nbsp; &nbsp;ivol=(volt*0.825*multiple)/1000;<br />
&nbsp;&nbsp; &nbsp;invol=(long)ivol;<br />
&nbsp;&nbsp; &nbsp;vol_buf=trans/10000000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;vol_buf=trans/1000000+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;vol_buf=&#39;.&#39;;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(trans/100000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(trans/10000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=(trans/1000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;vol_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;multiple_buf=multiple/1000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple/100%10+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple/10%10+0x30;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=multiple%10+0x30;<br />
&nbsp;&nbsp; &nbsp;multiple_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;out_buf=invol/10000000+0x30;&nbsp;<br />
&nbsp;&nbsp; &nbsp;out_buf=invol/1000000+0x30; &nbsp;<br />
&nbsp;&nbsp; &nbsp;out_buf=&#39;.&#39;;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/100000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/10000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=(invol/1000)%10+0x30;<br />
&nbsp;&nbsp; &nbsp;out_buf=&#39;\0&#39;;</p>

<p>&nbsp;&nbsp; &nbsp;LCD_Dispstring(6,0,vol_buf);<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(2,1,multiple_buf);<br />
&nbsp;&nbsp; &nbsp;LCD_Dispstring(8,1,out_buf);</p>

<p>}</p>

<p>void count()<br />
{<br />
&nbsp;&nbsp; &nbsp;if(multiple&lt;=999&amp;&amp;multiple&gt;=0)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp; &nbsp; if(abs==0)<br />
&nbsp;&nbsp; &nbsp; &nbsp; {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; multiple++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(multiple%10==9||t==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;y++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t=1; &nbsp; //到9后下一次变为0<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(y==2)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;multiple=multiple-10;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;y=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;abs=5;<br />
&nbsp;&nbsp; &nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp; &nbsp; if(abs==1)<br />
&nbsp;&nbsp; &nbsp; &nbsp; {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; multiple=multiple+10;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;if((multiple/10%10==9)||t1==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;y1++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t1=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(y1==2)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t1=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;multiple=multiple-100;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;y1=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;abs=5;<br />
&nbsp;&nbsp; &nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp; &nbsp; if(abs==2)<br />
&nbsp;&nbsp; &nbsp; &nbsp; { &nbsp;<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;multiple=multiple+100;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if((multiple/100==9)||t2==1)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;y2++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t2=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(y2==2)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t2=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;multiple=multiple-1000;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;y2=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; abs=5;<br />
&nbsp;&nbsp; &nbsp; &nbsp; }<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;if(multiple&lt;0)//y用来监视从1减小,然后维持,最小为0<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp; &nbsp; multiple=0;<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;if(multiple&gt;999)//y用来监视从1减小,然后维持,最小为0<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp; &nbsp; multiple=999;<br />
&nbsp;&nbsp; &nbsp;}<br />
}</p>

<p>void main(void)<br />
{<br />
&nbsp;&nbsp; &nbsp; &nbsp; LCD_Init(); //LCD初始化<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;kai_display();//最初显示<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay1us(50);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LCD_Clear();//清除</p>

<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TMOD = 0x22; &nbsp; &nbsp; //设置定时器T1为方式2<br />
&nbsp; &nbsp; &nbsp; &nbsp;TH1 = 0xfd; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp; &nbsp; TL1 = 0xfd;<br />
&nbsp;&nbsp; &nbsp; &nbsp; TH0=0x06;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TL0=0x06;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;EA=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ET0=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TR0=1;<br />
&nbsp; &nbsp; &nbsp; &nbsp;SCON = 0x50; &nbsp; &nbsp; &nbsp;//设置串口为方式1接收,REN = 1<br />
&nbsp; &nbsp; &nbsp; &nbsp;PCON = 0x00; &nbsp; &nbsp; &nbsp;//SMOD = 0;,波特率不加倍<br />
&nbsp; &nbsp; &nbsp; &nbsp;TR1 = 1; //开启定时器T1<br />
&nbsp; &nbsp; &nbsp; &nbsp;TI = 1;<br />
&nbsp;&nbsp; &nbsp; &nbsp; relay=0;<br />
&nbsp;&nbsp; &nbsp; &nbsp; relayAD=0;<br />
&nbsp; &nbsp; &nbsp; &nbsp;while(1)<br />
&nbsp; &nbsp; &nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;read2543(addr);//调用2543驱动程序测量地址为0<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;communication();&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; kai_display1();<br />
&nbsp; &nbsp; &nbsp; &nbsp;}<br />
}<br />
void DA_OUT(int i)<br />
{<br />
&nbsp;&nbsp; &nbsp;int num,num1,num2;<br />
&nbsp;&nbsp; &nbsp;num=i*4.099*8;<br />
&nbsp;&nbsp; &nbsp;num=num&gt;&gt;3;<br />
&nbsp;&nbsp; &nbsp;num1=num&gt;&gt;8;<br />
&nbsp;&nbsp; &nbsp;num2=num&amp;0xff;<br />
&nbsp;&nbsp; &nbsp;DAC1208=num2;<br />
&nbsp;&nbsp; &nbsp;k2=num1&amp;0x01;<br />
&nbsp;&nbsp; &nbsp;k3=num1&amp;0x02;<br />
&nbsp;&nbsp; &nbsp;k4=num1&amp;0x04;<br />
&nbsp;&nbsp; &nbsp;k5=num1&amp;0x08;<br />
}<br />
void f1 () interrupt 1<br />
{<br />
&nbsp;&nbsp;&nbsp; &nbsp;cent++;<br />
&nbsp;&nbsp; &nbsp;if(cent==5000)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;cent=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DA_OUT(multiple);<br />
&nbsp;&nbsp; &nbsp;}<br />
}</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 单片机LCD1602显示程控测量放大器