找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2752|回复: 6
打印 上一主题 下一主题
收起左侧

自己写的一个用LCD1602显示风向的单片机程序,无法运行,求大佬们指点

[复制链接]
回帖奖励 10 黑币 回复本帖可获得 5 黑币奖励! 每人限 1 次
跳转到指定楼层
楼主
用风向传感器和单片机连接,用LCD1602显示风向

运行后没有error,但有warning


这是风向传感器的说明书:



无法运行,求各位指点一下,程序哪里有问题,谢谢各位,非常感谢!

这是原理图:


这是源程序:
main.c程序:
  1. #include<stc12c5a.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include<stdlib.h>
  5. #include<math.h>
  6. #include<intrins.h>
  7. #include<LCD.h>
  8. uchar table[24]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  9. uchar wind_direction[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};//风向传感器问询帧
  10. uchar wind_direction1[2]={0x00,0x00};//单片机接收风向数据
  11. void delay(uint z)//延时函数
  12. {
  13. uint x,y;
  14. for(x=z;x>0;x--)
  15.   for(y=110;y>0;y--);
  16. }
  17. void init()  //系统初始化
  18. {
  19. TMOD=0X10;//8位重装定时器
  20. TH1=0xfa;
  21. TL1=0xfa;
  22. TR1=1;//定时器1中断
  23. SCON=0x50;//方式1
  24. TR1=1;//定时器1中断??
  25. IP=0X10;//
  26. EA=1;
  27. ES=1;
  28. }
  29. void main()
  30. {
  31. init();
  32.   while(1)
  33. {
  34.   int i;
  35. for(i=0;i<8;i++)
  36.   {
  37.   SBUF=wind_direction[i];
  38.    while(!TI);  //数据发送结束时TI置1
  39.    TI=0;
  40.   }
  41. }
  42. lcd1602_init();
  43. lcd1602_locate(0x80,wind_direction[0]);
  44. }
  45. void UART_1() interrupt 4
  46. {
  47. int i=0;
  48. //接收完缓冲数据后RI置1
  49. if(RI)//如果RI为1
  50. {
  51.     RI=0,//RI置0
  52.    //风向传感器
  53.   
  54.   table[i++]=SBUF;
  55.   if(i>8)
  56.   {
  57.   wind_direction1[0]=table[3];
  58.   }
  59. }
  60. }


  61. LCD.c程序:
  62. #include<reg52.h>
  63. #include<intrins.h>
  64. #include "LCD.h"
  65. #define uchar unsigned char
  66. #define uint  unsigned int
  67. #define IODATA  P0
  68. sbit E=P2^2;    //1602使能引脚
  69. sbit RW=P2^1;    //1602读写引脚   
  70. sbit RS=P2^0;    //1602数据/命令选择引脚
  71. void delay1602()  
  72. {
  73. _nop_();    //延时1us
  74. _nop_();
  75. }
  76. void DELAY1602(uint ms)
  77. {
  78. uint i,j;
  79. for(i=0;i<ms;i++)

  80.   for(j=0;j<148;j++)
  81. {
  82. }
  83. }
  84. void wcmd(uchar del)//写命令函数
  85. {
  86.   RS=0;//命令选择端
  87. delay1602();
  88. RW=0;//写命令
  89. delay1602();
  90. E=0;
  91. delay1602();
  92. IODATA=del;  //把命令字写入
  93. delay1602();
  94. E=1;//使能
  95. delay1602();
  96. E=0;
  97. }
  98. void lcd1602_writedata(uchar dat)//写数据函数
  99. {
  100. delay1602();
  101. RS=1;//数据选择端
  102. delay1602();
  103. RW=0;//写命令
  104. delay1602();
  105. E=0;
  106. delay1602();
  107. IODATA=dat;  //把要显示的数据写入
  108. delay1602();
  109. E=1;
  110. delay1602();
  111. E=0;
  112. }
  113. void lcd1602_init()//LCD初始化
  114. {
  115. DELAY1602(15);
  116. wcmd(0x38);
  117. DELAY1602(5);
  118. wcmd(0x38);
  119. DELAY1602(5);
  120. wcmd(0x38);
  121. wcmd(0x38);
  122. wcmd(0x08);
  123. wcmd(0x01);
  124. wcmd(0x06);
  125. wcmd(0x0c);
  126. }
  127. void lcd1602_locate(uchar a,char sign)
  128. {
  129. wcmd(a);
  130. lcd1602_writedata(sign);
  131. }
复制代码


LCD.h:
#ifndef _1602_
#define _1602_
#define uchar unsigned char
#define uint  unsigned int
void lcd1602_init();
void lcd1602_writedata(uchar del);
void lcd1602_locate(unsigned char x,unsigned char y);
#endif

编译运行没有错误,但接上传感器完全没有任何现象,球球各位指点,谢谢~
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:155507 发表于 2020-11-4 20:49 | 只看该作者
程序都是贴的图片,程序也不短,光凭眼去分析也是需要花时间的,建议把原程序贴出来,或许会有好心人帮你去调试一下找到问题!

没有原理图、没有实物,程序都是针对特定电路编写的,最好有电路图
回复

使用道具 举报

板凳
ID:451718 发表于 2020-11-4 20:56 | 只看该作者
程序不完整,你的问题能够完整点么?  不运行是不显示,还是不通讯,还是压根编译都过不了?
回复

使用道具 举报

地板
ID:825397 发表于 2020-11-4 21:21 | 只看该作者
赞一个
回复

使用道具 举报

5#
ID:796670 发表于 2020-11-4 21:46 | 只看该作者
angmall 发表于 2020-11-4 20:49
程序都是贴的图片,程序也不短,光凭眼去分析也是需要花时间的,建议把原程序贴出来,或许会有好心人帮你去调试 ...

谢谢指点,已附上原理图和源程序
回复

使用道具 举报

6#
ID:414556 发表于 2020-11-4 23:20 | 只看该作者
你可以先通过串口查看传感器是否有数据,再查看显示程序是否正常.
回复

使用道具 举报

7#
ID:839064 发表于 2020-11-5 00:41 | 只看该作者
warning很可能是子函数没有被调用
回复

使用道具 举报

8#
ID:213173 发表于 2020-11-5 07:47 | 只看该作者

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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