找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2853|回復(fù): 14
打印 上一主題 下一主題
收起左側(cè)

新手學(xué)習(xí)1602 顯示函數(shù) 然后這是網(wǎng)上找的程序 是有錯(cuò)誤的 希望得到討論一下

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
  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. 函數(shù)名稱(chēng):DispStr
  27. 功    能:讓液晶從某個(gè)位置起連續(xù)顯示一個(gè)字符串
  28. 參    數(shù):x--位置的列坐標(biāo)
  29.           y--位置的行坐標(biāo)
  30.           ptr--指向字符串存放位置的指針
  31. 返回值  :無(wú)
  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++;    //計(jì)算字符串有效字符的個(gè)數(shù)
  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. 函數(shù)名稱(chēng):DispNchar
  53. 功    能:讓液晶從某個(gè)位置起連續(xù)顯示N個(gè)字符
  54. 參    數(shù):x--位置的列坐標(biāo)
  55.           y--位置的行坐標(biāo)
  56.           n--字符個(gè)數(shù)
  57.           ptr--指向字符存放位置的指針
  58. 返回值  :無(wú)
  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. 函數(shù)名稱(chēng):LocateXY
  76. 功    能:向液晶輸入顯示字符位置的坐標(biāo)信息
  77. 參    數(shù):x--位置的列坐標(biāo)
  78.           y--位置的行坐標(biāo)
  79. 返回值  :無(wú)
  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. 函數(shù)名稱(chēng):Disp1Char
  92. 功    能:在某個(gè)位置顯示一個(gè)字符
  93. 參    數(shù):x--位置的列坐標(biāo)
  94.           y--位置的行坐標(biāo)
  95.           data--顯示的字符數(shù)據(jù)
  96. 返回值  :無(wú)
  97. ********************************************/
  98. void Disp1Char(uchar x,uchar y,uchar data1)
  99. {
  100.     LocateXY( x, y );                        
  101.     LcdWriteData( data1 );               
  102. }

  103. /*******************************************
  104. 函數(shù)名稱(chēng):LcdReset
  105. 功    能:對(duì)1602液晶模塊進(jìn)行復(fù)位操作
  106. 參    數(shù):無(wú)
  107. 返回值  :無(wú)
  108. ********************************************/
  109. void LcdReset(void)
  110. {
  111.     DataDir  = 0xFF;                 //數(shù)據(jù)端口設(shè)為輸出狀態(tài)
  112.     LcdWriteCommand(0x38, 0);            //規(guī)定的復(fù)位操作
  113.     DelayMs(5);
  114.     LcdWriteCommand(0x38, 0);               
  115.     DelayMs(5);
  116.     LcdWriteCommand(0x38, 0);
  117.     DelayMs(5);

  118.     LcdWriteCommand(0x38, 1);                //顯示模式設(shè)置
  119.     LcdWriteCommand(0x08, 1);                //顯示關(guān)閉
  120.     LcdWriteCommand(0x01, 1);            //顯示清屏
  121.     LcdWriteCommand(0x06, 1);                //寫(xiě)字符時(shí)整體不移動(dòng)
  122.     LcdWriteCommand(0x0c, 1);                //顯示開(kāi),不開(kāi)游標(biāo),不閃爍
  123. }

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

  132. /*******************************************
  133. 函數(shù)名稱(chēng):LcdWriteCommand
  134. 功    能:向液晶模塊寫(xiě)入命令
  135. 參    數(shù):cmd--命令,
  136.           chk--是否判忙的標(biāo)志,1:判忙,0:不判
  137. 返回值  :無(wú)
  138. ********************************************/
  139. void LcdWriteCommand(uchar cmd,uchar chk)
  140. {

  141.     if (chk) WaitForEnable();   // 檢測(cè)忙信號(hào)?
  142.    
  143.     CLR_RS;        
  144.     CLR_RW;
  145.     _NOP();

  146.     DataPort = cmd;             //將命令字寫(xiě)入數(shù)據(jù)端口
  147.     _NOP();                                       
  148.    
  149.     SET_EN;                     //產(chǎn)生使能脈沖信號(hào)
  150.     _NOP();
  151.     _NOP();
  152.     CLR_EN;                        
  153. }

  154. /*******************************************
  155. 函數(shù)名稱(chēng):LcdWriteData
  156. 功    能:向液晶顯示的當(dāng)前地址寫(xiě)入顯示數(shù)據(jù)
  157. 參    數(shù):data--顯示字符數(shù)據(jù)
  158. 返回值  :無(wú)
  159. ********************************************/
  160. void LcdWriteData( uchar data1 )
  161. {
  162.     WaitForEnable();        //等待液晶不忙
  163.     SET_RS;
  164.     CLR_RW;

  165.     SET_EN;
  166.         
  167.         _NOP();
  168.     DataPort = data1;        //將顯示數(shù)據(jù)寫(xiě)入數(shù)據(jù)端口
  169.     _NOP();
  170.                 //產(chǎn)生使能脈沖信號(hào)
  171.     _NOP();
  172.     _NOP();
  173.     CLR_EN;               
  174. }

  175. /*******************************************
  176. 函數(shù)名稱(chēng):WaitForEnable
  177. 功    能:等待1602液晶完成內(nèi)部操作
  178. 參    數(shù):無(wú)
  179. 返回值  :無(wú)
  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))  //檢測(cè)忙標(biāo)志
  193.     {
  194.       DelayUs2x(2);
  195.       later++;        
  196.     }
  197.     CLR_EN;
  198.     DataDir|=0xFF;  //將P4口切換為輸出狀態(tài)
  199. }               

復(fù)制代碼
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).

我現(xiàn)在在學(xué)習(xí)這個(gè)18b20測(cè)溫 1602顯示 然后這是網(wǎng)上找的程序  
錯(cuò)誤這么多 怎么搞啊
這是缺了聲明函數(shù)還是啥?
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:824490 發(fā)表于 2021-5-24 00:34 | 只看該作者
認(rèn)真檢查代碼中的符號(hào):
 “;”
 “()”
   “,”
  “{}”
等等。。。
因?yàn)榫W(wǎng)上來(lái)的代碼,編碼變了,可能更改了全角/半角.
回復(fù)

使用道具 舉報(bào)

板凳
ID:213173 發(fā)表于 2021-5-24 06:19 | 只看該作者
這段代碼只是LCD1602的驅(qū)動(dòng)模塊,不一定有錯(cuò)誤。其只能由主程序調(diào)用,不可以單獨(dú)編譯。只算是整個(gè)物體上的一個(gè)構(gòu)件。
回復(fù)

使用道具 舉報(bào)

地板
ID:189235 發(fā)表于 2021-5-24 08:21 | 只看該作者
回復(fù)

使用道具 舉報(bào)

5#
ID:277550 發(fā)表于 2021-5-24 08:23 | 只看該作者
在lcd1602.h中,使用 #ifndef   #define 來(lái)防止重復(fù)加載
回復(fù)

使用道具 舉報(bào)

6#
ID:57657 發(fā)表于 2021-5-24 09:55 | 只看該作者
main函數(shù)去哪了?
回復(fù)

使用道具 舉報(bào)

7#
ID:666991 發(fā)表于 2021-5-24 10:52 | 只看該作者
名字不是重點(diǎn) 發(fā)表于 2021-5-24 00:34
認(rèn)真檢查代碼中的符號(hào):
 “;”
 “()”

應(yīng)該不是這個(gè)問(wèn)題 我復(fù)制來(lái)都特意編譯過(guò) 沒(méi)有問(wèn)題的
回復(fù)

使用道具 舉報(bào)

8#
ID:666991 發(fā)表于 2021-5-24 10:54 | 只看該作者
npn 發(fā)表于 2021-5-24 09:55
main函數(shù)去哪了?

這個(gè)是1602.c main 沒(méi)有貼出來(lái)  main函數(shù)沒(méi)有問(wèn)題
回復(fù)

使用道具 舉報(bào)

9#
ID:824490 發(fā)表于 2021-5-24 11:32 | 只看該作者
compiling 1602.c...
1602.C(25): error C231: 'LcdReset': redefinition          ==>LcdReset重復(fù)定義
1602.C(26): error C141: syntax error near '10'            ==>語(yǔ)法錯(cuò)誤,在字符‘10’附近
1602.C(26): error C231: '_DelayMs': redefinition         ==>_DelayMs重復(fù)定義
1602.C(27): error C141: syntax error near '<string>'  ==>語(yǔ)法錯(cuò)誤,在字符‘<string>’附近
1602.C(28): error C141: syntax error near '0'            ==>語(yǔ)法錯(cuò)誤

。
。
回復(fù)

使用道具 舉報(bào)

10#
ID:824490 發(fā)表于 2021-5-24 11:39 | 只看該作者
低級(jí)錯(cuò)誤:

聲明完了直接進(jìn)入代碼,連個(gè)函數(shù)的基本格式都沒(méi)有??

#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)        
//這里應(yīng)該要有void  函數(shù)名()
//這里還要有“{"

        LcdReset();               //LCD1602初始化
        DelayMs(10);        
        sprintf(temp,"1111111111111111");//更新顯示
        DispStr(0,0,(unsigned char *)temp);//打印顯示
        sprintf(temp,"1111111111111111");//更新顯示
        DispStr(0,1,(unsigned char *)temp);//打印顯示
//這里還要有”}"
/***********************************************
函數(shù)名稱(chēng):DispStr
功    能:讓液晶從某個(gè)位置起連續(xù)顯示一個(gè)字符串
參    數(shù):x--位置的列坐標(biāo)
          y--位置的行坐標(biāo)

再認(rèn)真檢查一下吧。
回復(fù)

使用道具 舉報(bào)

11#
ID:390416 發(fā)表于 2021-5-24 18:21 | 只看該作者
LCD1602顯示℃ 這種自定義符號(hào) http://www.torrancerestoration.com/bbs/dpj-200685-1.html
LCD1602液晶最新資料 說(shuō)明了具體的時(shí)間要求 http://www.torrancerestoration.com/bbs/dpj-205768-1.html
回復(fù)

使用道具 舉報(bào)

12#
ID:666991 發(fā)表于 2021-5-24 18:35 | 只看該作者
名字不是重點(diǎn) 發(fā)表于 2021-5-24 11:39
低級(jí)錯(cuò)誤:

聲明完了直接進(jìn)入代碼,連個(gè)函數(shù)的基本格式都沒(méi)有??

這一段話什么意思啊 ,我在.h 文件中看了聲明函數(shù) 所有的聲明函數(shù)都在.c中出現(xiàn)了 就是已經(jīng)一對(duì)一了  所以我就不知道這段代表啥了
回復(fù)

使用道具 舉報(bào)

13#
ID:824490 發(fā)表于 2021-5-25 09:13 | 只看該作者
本帖最后由 名字不是重點(diǎn) 于 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);//打印顯示
}
改成這樣,再編譯一下
回復(fù)

使用道具 舉報(bào)

14#
ID:592807 發(fā)表于 2021-5-25 10:15 | 只看該作者
zhangyu111 發(fā)表于 2021-5-24 18:35
這一段話什么意思啊 ,我在.h 文件中看了聲明函數(shù) 所有的聲明函數(shù)都在.c中出現(xiàn)了 就是已經(jīng)一對(duì)一了  所以 ...



        LcdReset();               //LCD1602初始化
        DelayMs(10);        
        sprintf(temp,"1111111111111111");//更新顯示
        DispStr(0,0,(unsigned char *)temp);//打印顯示
        sprintf(temp,"1111111111111111");//更新顯示
        DispStr(0,1,(unsigned char *)temp);//打印顯示
這些是函數(shù)聲明嗎?
不是聲明你放文件開(kāi)頭還不注釋?zhuān)植皇呛瘮?shù)內(nèi)容。想干嘛?
回復(fù)

使用道具 舉報(bào)

15#
ID:666991 發(fā)表于 2021-5-27 11:52 | 只看該作者
本帖最后由 zhangyu111 于 2021-5-27 11:55 編輯
名字不是重點(diǎn) 發(fā)表于 2021-5-25 09:13
void Test_display(){
        LcdReset();               //LCD1602初始化
        DelayMs(10);        ...

C:\Users\Administrator\Desktop

6IM6LWF[%V[35A51D__]~MG.png (32.58 KB, 下載次數(shù): 61)

6IM6LWF[%V[35A51D__]~MG.png
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表