找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2815|回复: 14
收起左侧

新手学习1602 显示函数 然后这是网上找的程序 是有错误的 希望得到讨论一下

[复制链接]
ID:666991 发表于 2021-5-23 21:41 | 显示全部楼层 |阅读模式
  1. #include "1602.h"
  2. #include "delay.h"
  3. #include <intrins.h>

  4. #define uchar unsigned char
  5. #define uint unsigned int

  6. #define _NOP() _nop_()
  7. sbit RS = P2^4;   //定义端口
  8. sbit RW = P2^5;
  9. sbit EN = P2^6;

  10. #define DataPort    P0                                
  11. #define DataPIN     P0
  12. #define DataDir                P0

  13. #define CLR_RS (RS=0)
  14. #define SET_RS (RS=1)        
  15. #define CLR_RW (RW=0)        
  16. #define SET_RW (RW=1)
  17. #define CLR_EN (EN=0)
  18. #define SET_EN (EN=1)        


  19.         LcdReset();               //LCD1602初始化
  20.         DelayMs(10);        
  21.         sprintf(temp,"1111111111111111");//更新显示
  22.         DispStr(0,0,(unsigned char *)temp);//打印显示
  23.         sprintf(temp,"1111111111111111");//更新显示
  24.         DispStr(0,1,(unsigned char *)temp);//打印显示

  25. /***********************************************
  26. 函数名称:DispStr
  27. 功    能:让液晶从某个位置起连续显示一个字符串
  28. 参    数:x--位置的列坐标
  29.           y--位置的行坐标
  30.           ptr--指向字符串存放位置的指针
  31. 返回值  :无
  32. ***********************************************/
  33. void DispStr(uchar x,uchar y,uchar *ptr)
  34. {
  35.     uchar *temp;
  36.     uchar i,n = 0;
  37.    
  38.     temp = ptr;
  39.     while(*ptr++ != '\0')   n++;    //计算字符串有效字符的个数
  40.    
  41.     for (i=0;i<n;i++)
  42.     {
  43.                 if((temp[i]&0x30)==0x30)temp[i]=temp[i]&0x36;//**All notes can be deleted and modified**//                        
  44.         Disp1Char(x++,y,temp[i]);
  45.         if (x == 0x10)
  46.         {
  47.             break;
  48.         }
  49.     }
  50. }

  51. /*******************************************
  52. 函数名称:DispNchar
  53. 功    能:让液晶从某个位置起连续显示N个字符
  54. 参    数:x--位置的列坐标
  55.           y--位置的行坐标
  56.           n--字符个数
  57.           ptr--指向字符存放位置的指针
  58. 返回值  :无
  59. *******************************************/
  60. void DispNChar(uchar x,uchar y, uchar n,uchar *ptr)
  61. {
  62.     uchar i;
  63.    
  64.     for (i=0;i<n;i++)
  65.     {
  66.         Disp1Char(x++,y,ptr[i]);
  67.         if (x == 0x10)
  68.         {
  69.            x = 0;
  70.             y ^= 1;                          //异或操作 换行
  71.         }
  72.     }
  73. }

  74. /*******************************************
  75. 函数名称:LocateXY
  76. 功    能:向液晶输入显示字符位置的坐标信息
  77. 参    数:x--位置的列坐标
  78.           y--位置的行坐标
  79. 返回值  :无
  80. ********************************************/
  81. void LocateXY(uchar x,uchar y)
  82. {
  83.     uchar temp;

  84.     temp = x&0x0f;
  85.     y &= 0x01;
  86.     if(y)   temp |= 0x40;  //如果在第2行
  87.     temp |= 0x80;

  88.     LcdWriteCommand(temp,1);
  89. }

  90. /*******************************************
  91. 函数名称:Disp1Char
  92. 功    能:在某个位置显示一个字符
  93. 参    数:x--位置的列坐标
  94.           y--位置的行坐标
  95.           data--显示的字符数据
  96. 返回值  :无
  97. ********************************************/
  98. void Disp1Char(uchar x,uchar y,uchar data1)
  99. {
  100.     LocateXY( x, y );                        
  101.     LcdWriteData( data1 );               
  102. }

  103. /*******************************************
  104. 函数名称:LcdReset
  105. 功    能:对1602液晶模块进行复位操作
  106. 参    数:无
  107. 返回值  :无
  108. ********************************************/
  109. void LcdReset(void)
  110. {
  111.     DataDir  = 0xFF;                 //数据端口设为输出状态
  112.     LcdWriteCommand(0x38, 0);            //规定的复位操作
  113.     DelayMs(5);
  114.     LcdWriteCommand(0x38, 0);               
  115.     DelayMs(5);
  116.     LcdWriteCommand(0x38, 0);
  117.     DelayMs(5);

  118.     LcdWriteCommand(0x38, 1);                //显示模式设置
  119.     LcdWriteCommand(0x08, 1);                //显示关闭
  120.     LcdWriteCommand(0x01, 1);            //显示清屏
  121.     LcdWriteCommand(0x06, 1);                //写字符时整体不移动
  122.     LcdWriteCommand(0x0c, 1);                //显示开,不开游标,不闪烁
  123. }

  124. /*------------------------------------------------
  125.                 清屏函数
  126. ------------------------------------------------*/
  127. void LcdClear(void)
  128. {
  129.         LcdWriteCommand(0x01,1);
  130.         DelayMs(5);
  131. }

  132. /*******************************************
  133. 函数名称:LcdWriteCommand
  134. 功    能:向液晶模块写入命令
  135. 参    数:cmd--命令,
  136.           chk--是否判忙的标志,1:判忙,0:不判
  137. 返回值  :无
  138. ********************************************/
  139. void LcdWriteCommand(uchar cmd,uchar chk)
  140. {

  141.     if (chk) WaitForEnable();   // 检测忙信号?
  142.    
  143.     CLR_RS;        
  144.     CLR_RW;
  145.     _NOP();

  146.     DataPort = cmd;             //将命令字写入数据端口
  147.     _NOP();                                       
  148.    
  149.     SET_EN;                     //产生使能脉冲信号
  150.     _NOP();
  151.     _NOP();
  152.     CLR_EN;                        
  153. }

  154. /*******************************************
  155. 函数名称:LcdWriteData
  156. 功    能:向液晶显示的当前地址写入显示数据
  157. 参    数:data--显示字符数据
  158. 返回值  :无
  159. ********************************************/
  160. void LcdWriteData( uchar data1 )
  161. {
  162.     WaitForEnable();        //等待液晶不忙
  163.     SET_RS;
  164.     CLR_RW;

  165.     SET_EN;
  166.         
  167.         _NOP();
  168.     DataPort = data1;        //将显示数据写入数据端口
  169.     _NOP();
  170.                 //产生使能脉冲信号
  171.     _NOP();
  172.     _NOP();
  173.     CLR_EN;               
  174. }

  175. /*******************************************
  176. 函数名称:WaitForEnable
  177. 功    能:等待1602液晶完成内部操作
  178. 参    数:无
  179. 返回值  :无
  180. ********************************************/
  181. void WaitForEnable(void)
  182. {
  183.           unsigned int later=0;
  184.         DataPort=0xff;
  185.     CLR_RS;
  186.     SET_RW;
  187.     _NOP();
  188.     SET_EN;
  189.     _NOP();
  190.     _NOP();
  191.     while((DataPIN&Busy)!=0);   
  192.     while(((DataPIN&0x80)!=0)&&(later<1000))  //检测忙标志
  193.     {
  194.       DelayUs2x(2);
  195.       later++;        
  196.     }
  197.     CLR_EN;
  198.     DataDir|=0xFF;  //将P4口切换为输出状态
  199. }               

复制代码
compiling 1602.c...
1602.C(25): error C231: 'LcdReset': redefinition
1602.C(26): error C141: syntax error near '10'
1602.C(26): error C231: '_DelayMs': redefinition
1602.C(27): error C141: syntax error near '<string>'
1602.C(28): error C141: syntax error near '0'
1602.C(28): error C132: 'temp': not in formal parameter list
1602.C(28): error C141: syntax error near ')'
1602.C(29): error C141: syntax error near '<string>'
1602.C(29): error C132: '_sprintf': not in formal parameter list
1602.C(30): error C141: syntax error near '0'
1602.C(30): error C132: 'DispStr': not in formal parameter list
1602.C(30): error C141: syntax error near 'temp'
1602.C(30): error C132: 'temp': not in formal parameter list
1602.C(41): error C132: '_DispStr': not in formal parameter list
1602.C(41): error C141: syntax error near '{'
1602.C(42): error C132: 'temp': not in formal parameter list
1602.C(43): error C132: 'i': not in formal parameter list
1602.C(43): error C244: 'n': can't initialize, bad type or class
1602.C(43): error C132: 'n': not in formal parameter list
1602.C(45): error C244: 'temp': can't initialize, bad type or class
1602.C(45): error C202: 'ptr': undefined identifier
1602.C(45): error C132: 'temp': not in formal parameter list
1602.C(46): error C141: syntax error near 'while'
1602.C(46): error C141: syntax error near '++', expected ')'
1602.C(46): error C129: missing ';' before '++'
1602.c - 25 Error(s), 0 Warning(s).

我现在在学习这个18b20测温 1602显示 然后这是网上找的程序  
错误这么多 怎么搞啊
这是缺了声明函数还是啥?
回复

使用道具 举报

ID:824490 发表于 2021-5-24 00:34 | 显示全部楼层
认真检查代码中的符号:
 “;”
 “()”
   “,”
  “{}”
等等。。。
因为网上来的代码,编码变了,可能更改了全角/半角.
回复

使用道具 举报

ID:213173 发表于 2021-5-24 06:19 | 显示全部楼层
这段代码只是LCD1602的驱动模块,不一定有错误。其只能由主程序调用,不可以单独编译。只算是整个物体上的一个构件。
回复

使用道具 举报

ID:189235 发表于 2021-5-24 08:21 | 显示全部楼层
回复

使用道具 举报

ID:277550 发表于 2021-5-24 08:23 | 显示全部楼层
在lcd1602.h中,使用 #ifndef   #define 来防止重复加载
回复

使用道具 举报

ID:57657 发表于 2021-5-24 09:55 | 显示全部楼层
main函数去哪了?
回复

使用道具 举报

ID:666991 发表于 2021-5-24 10:52 | 显示全部楼层
名字不是重点 发表于 2021-5-24 00:34
认真检查代码中的符号:
 “;”
 “()”

应该不是这个问题 我复制来都特意编译过 没有问题的
回复

使用道具 举报

ID:666991 发表于 2021-5-24 10:54 | 显示全部楼层
npn 发表于 2021-5-24 09:55
main函数去哪了?

这个是1602.c main 没有贴出来  main函数没有问题
回复

使用道具 举报

ID:824490 发表于 2021-5-24 11:32 | 显示全部楼层
compiling 1602.c...
1602.C(25): error C231: 'LcdReset': redefinition          ==>LcdReset重复定义
1602.C(26): error C141: syntax error near '10'            ==>语法错误,在字符‘10’附近
1602.C(26): error C231: '_DelayMs': redefinition         ==>_DelayMs重复定义
1602.C(27): error C141: syntax error near '<string>'  ==>语法错误,在字符‘<string>’附近
1602.C(28): error C141: syntax error near '0'            ==>语法错误


回复

使用道具 举报

ID:824490 发表于 2021-5-24 11:39 | 显示全部楼层
低级错误:

声明完了直接进入代码,连个函数的基本格式都没有??

#define CLR_RS (RS=0)
#define SET_RS (RS=1)        
#define CLR_RW (RW=0)        
#define SET_RW (RW=1)
#define CLR_EN (EN=0)
#define SET_EN (EN=1)        
//这里应该要有void  函数名()
//这里还要有“{"

        LcdReset();               //LCD1602初始化
        DelayMs(10);        
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,0,(unsigned char *)temp);//打印显示
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,1,(unsigned char *)temp);//打印显示
//这里还要有”}"
/***********************************************
函数名称:DispStr
功    能:让液晶从某个位置起连续显示一个字符串
参    数:x--位置的列坐标
          y--位置的行坐标

再认真检查一下吧。
回复

使用道具 举报

ID:390416 发表于 2021-5-24 18:21 | 显示全部楼层
LCD1602显示℃ 这种自定义符号 http://www.51hei.com/bbs/dpj-200685-1.html
LCD1602液晶最新资料 说明了具体的时间要求 http://www.51hei.com/bbs/dpj-205768-1.html
回复

使用道具 举报

ID:666991 发表于 2021-5-24 18:35 | 显示全部楼层
名字不是重点 发表于 2021-5-24 11:39
低级错误:

声明完了直接进入代码,连个函数的基本格式都没有??

这一段话什么意思啊 ,我在.h 文件中看了声明函数 所有的声明函数都在.c中出现了 就是已经一对一了  所以我就不知道这段代表啥了
回复

使用道具 举报

ID:824490 发表于 2021-5-25 09:13 | 显示全部楼层
本帖最后由 名字不是重点 于 2021-5-25 09:14 编辑

void Test_display(){
        LcdReset();               //LCD1602初始化
        DelayMs(10);        
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,0,(unsigned char *)temp);//打印显示
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,1,(unsigned char *)temp);//打印显示
}
改成这样,再编译一下
回复

使用道具 举报

ID:592807 发表于 2021-5-25 10:15 | 显示全部楼层
zhangyu111 发表于 2021-5-24 18:35
这一段话什么意思啊 ,我在.h 文件中看了声明函数 所有的声明函数都在.c中出现了 就是已经一对一了  所以 ...



        LcdReset();               //LCD1602初始化
        DelayMs(10);        
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,0,(unsigned char *)temp);//打印显示
        sprintf(temp,"1111111111111111");//更新显示
        DispStr(0,1,(unsigned char *)temp);//打印显示
这些是函数声明吗?
不是声明你放文件开头还不注释,又不是函数内容。想干嘛?
回复

使用道具 举报

ID:666991 发表于 2021-5-27 11:52 | 显示全部楼层
本帖最后由 zhangyu111 于 2021-5-27 11:55 编辑
名字不是重点 发表于 2021-5-25 09:13
void Test_display(){
        LcdReset();               //LCD1602初始化
        DelayMs(10);        ...

C:\Users\Administrator\Desktop
6IM6LWF[%V[35A51D__]~MG.png
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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