找回密码
 立即注册

QQ登录

只需一步,快速开始

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

51单片机水位检测程序 DCS

[复制链接]
跳转到指定楼层
楼主
以51单片机为内核,水位传感器检测水位,电机驱动模块驱动水泵,1602显示,矩阵键盘控制电机转速


单片机源程序如下:
  1. //#ifndef _Use_LCD1602B
  2. //# define _Use_LCD1602B

  3. /***********************
  4. *    系 统 宏 定 义    *
  5. ***********************/

  6. /*---------------------*
  7. *    常 数 宏 定 义    *
  8. *---------------------*/
  9. #ifndef True
  10.     # define True           0x01
  11. #endif
  12. #ifndef False
  13.     # define False          0x00
  14. #endif

  15. #ifndef Enable
  16.     # define Enable         0x01
  17. #endif
  18. #ifndef Disable
  19.     # define Disable        0x00
  20. #endif

  21. #ifndef RunStringSpeed
  22.     # define RunStringSpeed 100
  23. #endif

  24. #ifndef FlashTimeOut
  25.     # define FlashTimeOut   500
  26. #endif
  27. #ifndef FlashGIFTimeOut
  28.     # define FlashGIFTimeOut  1000
  29. #endif

  30. # define LCD_Write          0x00
  31. # define LCD_Read           0x01
  32. # define LCD_Command        0x00
  33. # define LCD_Data           0x01
  34. # define LCD_CMD_Init       0x28
  35. # define LCD_CMD_DispCtr    0x0c
  36. # define LCD_CMD_CLS        0x01
  37. # define LCD_CMD_EnterSet   0x06
  38. # define LCD_CMD_IconShow   0x0f
  39. # define LCD_CMD_IconHide   0x0c
  40. # define LCD_CMD_NotMove    0b00010100
  41. /*---------------------*
  42. *    动 作 宏 定 义    *
  43. *---------------------*/
  44. # define SetReadState       LCD_SetReadData;LCD_RS = LCD_Command;LCD_RW = LCD_Read;
  45. # define SetRead            LCD_SetReadData;LCD_RW = LCD_Read;
  46. # define SetWrite           LCD_SetWriteData;LCD_RW = LCD_Write;
  47. # define SetCommand         LCD_RS = LCD_Command;
  48. # define SetData            LCD_RS = LCD_Data;

  49. # define Print(a)           LCDDisplayString(a);
  50. # define Locate(x,y)        LCDSetXY(x-1,y-1);
  51. # define CLS                LCDWaitForReady();LCDSendCommand(LCD_CMD_CLS);
  52. # define PrintN(a,b)        LCDDisplayNum((unsigned long)a,b);
  53. # define ShowIcon           LCDWaitForReady();LCDSendCommand(LCD_CMD_IconShow);
  54. # define HideIcon           LCDWaitForReady();LCDSendCommand(LCD_CMD_IconHide);
  55. /***********************
  56. *    全局变量声明区    *
  57. ***********************/
  58. const char CHR[16] = {'0','1','2','3','4','5','6','7','8'
  59.                      ,'9','a','b','c','d','e','f'};
  60. extern unsigned int FlashTimeCounter;
  61. extern unsigned int RunTimeCounter;
  62. extern unsigned int FlashGIFStringCounter;
  63. /***********************
  64. *    系统函数声明区    *
  65. ***********************/
  66. void LCDInit(void);
  67. void LCDSendCommand(char Command);
  68. void LCDSendData(char Data);
  69. void LCDWaitForReady(void);
  70. void LCDSetXY(char X,char Y);
  71. void LCDDisplayString(char *String);
  72. void LCDDisplayNum(unsigned long Num,char BitCount);
  73. void LCDDelay(unsigned int Time);
  74. void LCDDelayUs(unsigned int Time);
  75. void RunString(char *String,char Direction,char Y,char StartX,char EndX);
  76. void Flash(char *String,char Icon,char X,char Y);
  77. char StringLength(char *String);
  78. void FlashStringGroup(char String[][17],char StringCounter,char X,char Y);
  79. /********************************************************
  80. *  函数说明:LCD驱动类毫秒延时函数                      *
  81. *  输入:    需要延时的大体毫秒数                       *
  82. ********************************************************/
  83. void LCDDelay(unsigned int Time)
  84. {
  85.     unsigned int TimeCounter = 0;
  86.         
  87.         for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
  88.         {
  89.             LCDDelayUs(255);
  90.         }
  91. }

  92. /********************************************************
  93. *  函数说明:LCD驱动指令周期延时函数                    *
  94. *  输入:    需要大体延时Us数                           *
  95. ********************************************************/
  96. void LCDDelayUs(unsigned int Time)
  97. {
  98.      unsigned int TimeCounter = 0;
  99.          for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
  100.          {
  101.              asm("nop");
  102.          }
  103. }

  104. /********************************************************
  105. *  函数说明:LCD初始化函数                              *
  106. ********************************************************/
  107. void LCDInit(void)
  108. {
  109.     LCDDelay(15);
  110.         
  111.         LCDWaitForReady();
  112.         LCDSendCommand(LCD_CMD_Init);
  113.         
  114.         LCDWaitForReady();
  115.         LCDSendCommand(LCD_CMD_DispCtr);
  116.         
  117.         LCDWaitForReady();
  118.         LCDSendCommand(LCD_CMD_CLS);
  119.         LCDDelay(2);
  120.         LCDSendCommand(LCD_CMD_EnterSet);
  121.         
  122. }

  123. /********************************************************
  124. *  函数说明:向LCD发送指令函数                          *
  125. *  输入:    需要发送的指令                             *
  126. ********************************************************/
  127. void LCDSendCommand(char Command)
  128. {
  129.         SetWrite;
  130.         SetCommand;
  131.         {
  132.             LCD_E = Enable;
  133.                
  134.                 LCD_SendHalfCharHigh(Command);
  135.                
  136.             LCD_E = Disable;
  137.         }
  138.         {
  139.             LCD_E = Enable;
  140.                
  141.         LCD_SendHalfCharLow(Command);        
  142.                
  143.                 LCD_E = Disable;
  144.         }
  145.     SetRead;               
  146.         SetCommand;
  147. }

  148. /********************************************************
  149. *  函数说明:向LCD发送数据函数                          *
  150. ********************************************************/
  151. void LCDSendData(char Data)
  152. {
  153.     SetWrite;
  154.         SetData;
  155.         {
  156.             LCD_E = Enable;
  157.                
  158.                 LCD_SendHalfCharHigh(Data);
  159.                
  160.             LCD_E = Disable;
  161.         }

  162.         {
  163.             LCD_E = Enable;
  164.                
  165.         LCD_SendHalfCharLow(Data);        
  166.                
  167.                 LCD_E = Disable;
  168.         }
  169.     SetRead;               
  170.         SetCommand;
  171. }

  172. /********************************************************
  173. *  函数说明:等待LCD空闲状态函数                        *
  174. ********************************************************/
  175. void LCDWaitForReady(void)
  176. {
  177. #ifdef _Use_LCDOnlyWrite
  178.         LCDDelayUs(30);
  179. #else
  180.         
  181.         SetRead;
  182.         SetCommand;
  183.    
  184.     LCD_E = Enable;   
  185.    
  186.         while (LCD_BF == Enable);   //RW=1,读PD7,为0表示空闲;
  187.    
  188.     LCD_E = Disable;
  189.         
  190. #endif
  191. }

  192. /********************************************************
  193. *  函数说明:设置显示坐标函数                           *
  194. ********************************************************/
  195. void LCDSetXY(char X,char Y)
  196. {
  197.     char Address;
  198.     if (Y == 0)
  199.         {
  200.             Address = 0x80 + X;
  201.         }
  202.     else
  203.         {
  204.         Address = 0xc0 + X;
  205.         }
  206.         
  207.         LCDWaitForReady();
  208.     LCDSendCommand(Address);
  209. }

  210. /********************************************************
  211. *  函数说明:LCD字符串显示函数                          *
  212. ********************************************************/
  213. void LCDDisplayString(char *String)
  214. {
  215.     while(*String)
  216.         {
  217.             LCDWaitForReady();
  218.             LCDSendData(*String);
  219.                 String++;
  220.         }
  221. }

  222. /********************************************************
  223. *  函数说明:数值显示函数(HEX)                          *
  224. *  输入:    需要显示的数字(无符号长整形)             *
  225. ********************************************************/
  226. void LCDDisplayNum(unsigned long Num,char BitCount)
  227. {
  228.     char a = 0;
  229.         for (a = 8-BitCount ;a<8;a++)
  230.         {
  231.             LCDSendData(CHR[(Num<<(a<<2))>>28]);
  232.         }
  233. }

  234. /********************************************************
  235. *  函数说明:滚屏字幕效果                               *
  236. *  输入:    需要滚屏的字符串 长度 位置                 *
  237. ********************************************************/
  238. void RunString(char *String,char Direction,char Y,char StartX,char EndX)
  239. {
  240.     static char StringHead = 0;
  241.         char SCREEN[17];
  242.         char a = 0;
  243.         char Point = StringHead;
  244.         char StringLong = StringLength(String);
  245.         static int RunTimeCounter = 0;
  246.         
  247.         for (a = 0;a<EndX - StartX + 1;a++)
  248.         {
  249.              SCREEN[a] = String[Point];
  250.                  Point ++;
  251.                  if (Point == StringLong)
  252.                  {
  253.                      Point = 0;
  254.                  }
  255.         }
  256.         
  257.         for (;a < 17;a++)
  258.         {
  259.             SCREEN[a] =' ';
  260.         }
  261.         
  262.         RunTimeCounter ++;
  263.         if (RunTimeCounter >RunStringSpeed)
  264.         {
  265.             StringHead ++;
  266.                 RunTimeCounter = 0;
  267.                 if (StringHead == StringLong)
  268.             {
  269.                 StringHead = 0;
  270.             }
  271.         }
  272.         
  273.         Locate(StartX,Y)
  274.         Print(SCREEN)
  275. }

  276. /********************************************************
  277. *  函数说明:字符串长度测试函数                         *
  278. ********************************************************/
  279. char StringLength(char *String)
  280. {
  281.     char n = 0;
  282.         while (*String)
  283.         {
  284.             n++;
  285.                 String ++;
  286.         }
  287.         
  288.         return n;
  289. }

  290. /********************************************************
  291. *  函数说明:闪烁显示函数                               *
  292. ********************************************************/
  293. void Flash(char *String,char Icon,char X,char Y)
  294. {
  295.     char a = 0;
  296.         char StringLong = StringLength(String);
  297.         
  298.     if (FlashTimeCounter % FlashTimeOut > (FlashTimeOut >> 1))
  299.         {
  300.             Locate(X,Y)
  301.                 Print(String)   
  302.         }
  303.         else
  304.         {
  305.             for (a = X ;a < X+StringLong;a++)
  306.                 {
  307.                     Locate(a,Y)
  308.                         LCDWaitForReady();
  309. ……………………

  310. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
源代码.rar (35.93 KB, 下载次数: 37)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2019-4-17 01:22 | 只看该作者
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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