找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1614|回复: 0
收起左侧

单片机之间的串口通信程序问题分析

[复制链接]
ID:606493 发表于 2020-3-25 11:47 | 显示全部楼层 |阅读模式
U1单片机负责接收串口信号,把接收到的信号在1602上面显示且1602同时显示DS18B20的温度,U2单片机负责发送信号,U2单片机连接的有一个ADC8080,通过ADC8080转换后的GP2D12的数据会显示在四位数码管的前三位,最后一位当DP2D12检测到的距离小于等于30时会为0,大于30时会为1;染然后把这个数据返回给U1,并在1602的第一行显示0或1 ,且该显示的数据会随着U2传送的数据而变化。
现在我遇到的问题是两个单片机之间有信号的传输,但是无论U2传输的信号为1还是为0,在U1上的1602都显示为0,不知道是传输的问题还是接受的问题,求大神解答一下!!!!非常感谢!!!!

一下是proteus仿真图
此时1602应显示1,但是显示的却是0 ,而且在仿真时U1的3.1口和U2的3.0口一直在高低电平之间切换,而U1的3.0和U2的3.1一直处于低电平
GYW]8153K@)X$K@}88QU8KD.png

单片机源程序如下:
  1. /********************U1的程序*******************************************/
  2. #include <reg52.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. #define LCD1602_DATAPINS P1
  6. uint temp=0,TJ=0,J=2,ADC;
  7. sbit DSPORT=P2^3;   //温度(DS18B20)
  8. sbit LCD1602_E=P2^2;
  9. sbit LCD1602_RW=P2^1;
  10. sbit LCD1602_RS=P2^0;
  11. sbit EN1=P2^4;
  12. uchar Display_1[]="beam            ";
  13. uchar Display_2[]="T's 11.11  W's 0";
  14. void Init()
  15. {
  16. TMOD=0x20;
  17.   TH1=0xfd;
  18.   TL1=0xfd;
  19.   SCON=0x50;
  20.   PCON=0x00;
  21.   TR1=1;
  22. EA=1;
  23. }
  24. void delay(uint time)
  25. {
  26. uint i;
  27. for(time;time>0;time--)
  28.   for(i=110;i>0;i--);
  29. }
  30. uchar Ds18b20_Init()   //Ds18b20初始化函数,返回值为1表示存在,为0表示不存在
  31. {
  32. uint i=70;   
  33. DSPORT=0;
  34. while(i--);    //642us
  35. DSPORT=1;
  36. i=0;
  37. while(DSPORT)
  38. {
  39.   delay(1);   
  40.   i++;
  41.   if(i>5)
  42.    return 0;
  43. }
  44. return 1;
  45. }  
  46. void Ds18b20_write(uchar dat)   //ds18b20写字节函数
  47. {
  48. uint i,j;
  49. for(j=0;j<8;j++)
  50. {
  51.   DSPORT=0;
  52.   i++;            //大于1us的延时
  53.   DSPORT=dat&0x01;
  54.   i=6;      
  55.   while(i--);          //68us
  56.   DSPORT=1;
  57.   dat>>=1;   
  58. }
  59. }
  60. uchar Ds18b20_read()      //ds18b20读字节函数
  61. {
  62. uint i,j;
  63. uchar preserve,byte;     //preserve用于保存每一位的数据,byte用于返回数据
  64. for(j=8;j>0;j--)
  65. {
  66.   DSPORT=0;
  67.   i++;             //大于1us的延时
  68.   DSPORT=1;
  69.   i++;      
  70.   i++;
  71.   preserve=DSPORT;
  72.   byte=(byte>>1)|(preserve<<7);
  73.   i=4;     
  74.   while(i--);
  75. }
  76. return byte;
  77. }
  78. void Ds18b20_chanre()       //ds18b20温度转换函数
  79. {
  80. Ds18b20_Init();
  81. delay(1);      
  82. Ds18b20_write(0xcc);
  83. Ds18b20_write(0x44);
  84. }
  85. void Ds18b20_temp()        //使温度传感器开始读取数据
  86. {
  87. Ds18b20_Init();
  88. delay(1);     
  89. Ds18b20_write(0xcc);
  90. Ds18b20_write(0xbe);
  91. }
  92. int Ds18b20_readtemp()       //温度读取函数
  93. {
  94. // int temp=0;
  95. uchar temph,templ;
  96. Ds18b20_chanre();
  97. Ds18b20_temp();
  98. templ=Ds18b20_read();
  99. temph=Ds18b20_read();
  100. temp=temph;
  101. temp<<=8;
  102. temp|=templ;
  103. return temp;
  104. }
  105. void datapros(int temp)      //数据处理函数
  106. {
  107. float tp;
  108. if(temp<0)
  109. {
  110.   temp=temp-1;
  111.   temp=~temp;
  112.   tp=temp;
  113.   temp=tp*0.0625*100+0.5;
  114. }
  115. else
  116. {
  117.   tp=temp;
  118.   temp=tp*0.0625*100+0.5;
  119. }
  120. Display_2[4]=temp % 10000 / 1000+'0';
  121. Display_2[5]=temp % 1000 / 100+'0';
  122. Display_2[7]=temp % 100 / 10+'0';
  123. Display_2[8]=temp % 10+'0';
  124. TJ = temp%10000/10;
  125. }
  126. void Lcd1602_Delay1ms(uint c)   //误差 0us
  127. {
  128.     uchar b;
  129. for(c; c>0; c--)
  130.   for(b=113;b>0;b--);   
  131. }
  132. void LcdWriteCom(uchar com)   //写入命令
  133. {
  134. LCD1602_E = 0;     //使能
  135. LCD1602_RS = 0;    //选择发送命令
  136. LCD1602_RW = 0;    //选择写入
  137. LCD1602_DATAPINS = com;     //放入命令
  138. Lcd1602_Delay1ms(1);  //等待数据稳定
  139. LCD1602_E = 1;           //写入时序
  140. Lcd1602_Delay1ms(5);   //保持时间
  141. LCD1602_E = 0;
  142. }
  143. void LcdWriteData(uchar dat)   //写入数据
  144. {
  145. LCD1602_E = 0; //使能清零
  146. LCD1602_RS = 1; //选择输入数据
  147. LCD1602_RW = 0; //选择写入
  148. LCD1602_DATAPINS = dat; //写入数据
  149. Lcd1602_Delay1ms(1);
  150. LCD1602_E = 1;   //写入时序
  151. Lcd1602_Delay1ms(5);   //保持时间
  152. LCD1602_E = 0;
  153. }
  154. void LcdInit()        //LCD初始化子程序
  155. {
  156.   LcdWriteCom(0x38);  //开显示
  157. LcdWriteCom(0x0c);  //开显示不显示光标
  158. LcdWriteCom(0x06);  //写一个指针加1
  159. LcdWriteCom(0x01);  //清屏
  160. LcdWriteCom(0x80);  //设置数据指针起点
  161. }
  162. void show()
  163. {
  164. uchar i;
  165. LcdInit();
  166. for(i=0;i<16;i++)
  167. {
  168.   LcdWriteData(Display_1[i]);
  169. }
  170. LcdWriteCom(0x40+0x80);
  171. for(i=0;i<16;i++)
  172. {
  173.   LcdWriteData(Display_2[i]);
  174. }
  175. // while(1);
  176. delay(5000);
  177. }
  178. void Fan()
  179. {
  180. if(TJ<250)
  181. {
  182.    EN1=0;
  183.    Display_2[15]='0';
  184. }
  185. else
  186. {
  187.   EN1=1;
  188.    Display_2[15]='1';
  189. }
  190. }
  191. void main()
  192. {
  193. Init();
  194. EN1=0;
  195. while(1)
  196. {
  197.   temp=Ds18b20_readtemp();
  198.   datapros(temp);
  199.   Fan();
  200.    while(RI==0);
  201.    RI=0;
  202.    ADC=SBUF;
  203.    if(ADC==0)
  204.     J=0;
  205.    else if(ADC==1)
  206.     J=1;
  207.   Display_1[8]=J+'0';
  208.   show();
  209. }
  210. }
  211. //void Init()interrupt 4
  212. //{
  213. // while(RI==0);
  214. //   RI=0;
  215. //   ADC=SBUF;
  216. //   if(ADC==0)
  217. //    J=0;
  218. //   else if(ADC==1)
  219. //    J=1;
  220. //}

  221. /**********************************以下是U2的程序**************************************/
  222. #include <reg52.h>
  223. #define uint unsigned int
  224. #define uchar unsigned char

  225. uint number[]={0,1,2,3,4,5,6,7,8,9,10};
  226. uint num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0X00};
  227. sbit wei_1=P2^0;
  228. sbit wei_2=P2^1;
  229. sbit wei_3=P2^2;
  230. sbit wei_4=P2^3;
  231. sbit EOC=P3^0;
  232. sbit OE=P3^1;
  233. sbit ST=P3^2;
  234. sbit CLK=P3^6;
  235. uint adc_out,a1,a2,a3,J,temp=2;
  236. void Init()
  237. {
  238. // TMOD=0x20;
  239. SCON=0X50;
  240. TH1=0xfd;
  241. TL1=0xfd;
  242. PCON=0x00;
  243. TR1=1;
  244. }
  245. void ADC_Init()
  246. {
  247. TMOD=0X20;
  248. TH0=(65536-2)/256;
  249. TL0=(65536-2)%256;
  250. EA=1;
  251. ET0=1;
  252. TR0=1;
  253. }
  254. void delay(uint time)
  255. {
  256. uint i;
  257. for(time;time>0;time--)
  258.   for(i=110;i>0;i--);
  259. }
  260. void judgement()
  261. {
  262. if((adc_out*49/25)>=94)
  263. {
  264.   J=0;
  265.   temp=0;
  266. }
  267. else
  268. {
  269.   J=1;
  270.   temp=1;
  271. }
  272. }
  273. void adc_display()
  274. {
  275. P2=0xFF;
  276. judgement();
  277. a1=(adc_out*49/25)/100;
  278.   a2=(adc_out*49/25)/10%10;
  279.   a3=(adc_out*49/25)%100%10;

  280. wei_1=0;
  281. P0=num[number[a1]];
  282. delay(1);
  283. wei_1=1;

  284. wei_2=0;
  285. P0=num[number[a2]];
  286. delay(1);
  287. wei_2=1;

  288. wei_3=0;
  289. P0=num[number[a3]];
  290. delay(1);
  291. wei_3=1;

  292. wei_4=0;
  293. P0=num[number[J]];
  294. delay(1);
  295. wei_4=1;
  296. }
  297. void adc0808()
  298. {
  299. OE=0;
  300.   ST=0;
  301.   ST=1;//清0
  302.   ST=0;//启动
  303.   delay(1);
  304.   while(!EOC);    //eoc等于零的话,在这里等待直到eoc=1,结束循环,向下执行
  305.   OE=1;
  306.   adc_out=P1;
  307.   OE=0;
  308. // adc_display();
  309. // return adc_out;
  310. }
  311. void main()
  312. {
  313. ADC_Init();
  314. Init();
  315. while(1)
  316. {
  317.   adc0808();
  318.   adc_display();
  319.   SBUF=temp;
  320.   while(TI==0);
  321.    TI=0;
  322. }
  323. }
  324. void T0_time()interrupt 1
  325. {
  326. TH0=(65536-2)/256;
  327. TL0=(65536-2)%256;
  328. CLK=~CLK;
  329. }
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表