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

QQ登錄

只需一步,快速開始

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

51單片機(jī)開發(fā)板 lcd1602剛開始00000的時(shí)候按減一按鍵,屏幕顯示亂碼該如何解決?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1073848 發(fā)表于 2023-4-25 20:17 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
請(qǐng)問剛開始00000的時(shí)候按減一按鍵,屏幕顯示亂碼該如何解決?
2. 按鍵加到一百會(huì)顯示“1:0”  一百一十是“1;0”十位沒有顯示正確該如何解決?


單片機(jī)源程序如下:
#include <REGX52.H>

typedef unsigned char uchar;
typedef unsigned int uint;
uchar code table[] = {"Welcome!"};
uchar code ren[] = {"RenShu:"};
uchar code dat[2];
sbit RS = P2^6;         //RS?
sbit RW = P2^5;
sbit EN = P2^7;         //EN?
sbit du=P1^6;
sbit we=P1^7;
sbit k1=P3^2;
sbit k2=P3^3;
uint num,i,c;
void delay(uint z)
{
        uint x,y;
        for(x = z; x > 0; x--)
                for(y = 114; y > 0 ; y--);
}
void Read_Busy()
{
        uchar busy;
        P0 = 0xff;
        RS = 0;
        RW = 1;
        do
        {
                EN = 1;
                busy = P0;
                EN = 0;
        }while(busy & 0x80);
}
void Write_Cmd(uchar cmd)
{
        Read_Busy();//???
        RS = 0;
        RW = 0;
        P0 = cmd;
        EN = 1;
        EN = 0;
}

void Write_Dat(uchar dat)
{
        Read_Busy();
        RS = 1;
        RW = 0;
        P0 = dat;
        EN = 1;
        EN = 0;
}
void Init_LCD1602()
{
        du=0;
        we=0;
        EN=0;
        Write_Cmd(0x38);
        Write_Cmd(0x0c);        
        Write_Cmd(0x06);
        Write_Cmd(0x01);
}
void key()
{
         if(k1==0)
                   {
                                   delay(5);
                                if(k1==0)
                                {                        
                c++;
                                }
                                while(!k1);
                   }
        if(k2==0)
                   {
                                   delay(5);
                                if(k2==0)
                                {
                c--;
                                }
                                while(!k2);
                   }
}
void main()
{
Init_LCD1602();//1602???
        c=0;        
while(1)
{
        Write_Cmd(0x80 | 0x00);//????
        for(num=0;num<8;num++)
        {
                 Write_Dat(table[num]);
                 delay(5);        
        }
        Write_Cmd(0x80 | 0x40);//????
        for(i=0;i<7;i++)
        {
                 Write_Dat(ren[ i]);
                 delay(5);        
        }
    Write_Cmd(0x80 | 0x48);
        key();
                        
                                                Write_Dat(c/10000+0x30);
                                                Write_Dat(c/1000+0x30);
                                                Write_Dat(c/100+0x30);
                                                Write_Dat(c/10+0x30);
                                                Write_Dat(c%10+0x30);

        }
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:161164 發(fā)表于 2023-4-25 21:33 | 只看該作者
  1. #include <REG52.H>

  2. typedef unsigned char uchar;
  3. typedef unsigned int uint;
  4. uchar code table[] = {"Welcome!"};
  5. uchar code renshu[] = {"RenShu:"};
  6. uchar code dat[2];
  7. sbit RS = P2^6;         //RS?
  8. sbit RW = P2^5;
  9. sbit EN = P2^7;         //EN?
  10. sbit du=P1^6;
  11. sbit we=P1^7;
  12. sbit k1=P3^2;
  13. sbit k2=P3^3;
  14. uint num,i,c;
  15. void delay(uint z)
  16. {
  17.     uint x,y;
  18.     for(x = z; x > 0; x--)
  19.         for(y = 114; y > 0 ; y--);
  20. }
  21. void Read_Busy()
  22. {
  23.     uchar busy;
  24.     P0 = 0xff;
  25.     RS = 0;
  26.     RW = 1;
  27.     do
  28.     {
  29.         EN = 1;
  30.         busy = P0;
  31.         EN = 0;
  32.     }
  33.     while(busy & 0x80);
  34. }
  35. void Delay40us()                //@12.000MHz
  36. {
  37.         unsigned char i;

  38.         i = 17;
  39.         while (--i);
  40. }
  41. void Write_Cmd(uchar cmd)
  42. {
  43. //    Read_Busy();//???
  44.     RS = 0;
  45.     RW = 0;
  46.     P0 = cmd;
  47.     EN = 1;
  48.     EN = 0;
  49.         Delay40us();
  50. }

  51. void Write_Dat(uchar dat)
  52. {
  53. //    Read_Busy();
  54.     RS = 1;
  55.     RW = 0;
  56.     P0 = dat;
  57.     EN = 1;
  58.     EN = 0;
  59.         Delay40us();
  60. }
  61. void Init_LCD1602()
  62. {
  63.     du=0;
  64.     we=0;
  65.     EN=0;
  66.         delay(15);
  67.     Write_Cmd(0x38);
  68.     Write_Cmd(0x0c);
  69.     Write_Cmd(0x06);
  70.     Write_Cmd(0x01);
  71.         delay(2);
  72. }
  73. void key()
  74. {
  75.     if(k1==0)
  76.     {
  77.         delay(5);
  78.         if(k1==0)
  79.         {
  80.             c++;
  81.         }
  82.         while(!k1);
  83.     }
  84.     if(k2==0)
  85.     {
  86.         delay(5);
  87.         if(k2==0)
  88.         {
  89.             c--;
  90.         }
  91.         while(!k2);
  92.     }
  93. }
  94. void main()
  95. {
  96.     Init_LCD1602();//1602???
  97.     c=0;
  98.     while(1)
  99.     {
  100.         Write_Cmd(0x80 | 0x00);//????
  101.         for(num=0; num<8; num++)
  102.         {
  103.             Write_Dat(table[num]);
  104.             delay(5);
  105.         }
  106.         Write_Cmd(0x80 | 0x40);//????
  107.         for(i=0; i<7; i++)
  108.         {
  109.             Write_Dat(renshu[ i]);
  110.             delay(5);
  111.         }
  112.         Write_Cmd(0x80 | 0x48);
  113.         key();

  114.         Write_Dat(c/10000+0x30);
  115.         Write_Dat(c/1000+0x30);
  116.         Write_Dat(c/100+0x30);
  117.         Write_Dat(c/10+0x30);
  118.         Write_Dat(c%10+0x30);

  119.     }
  120. }
復(fù)制代碼


回復(fù)

使用道具 舉報(bào)

板凳
ID:384109 發(fā)表于 2023-4-25 22:21 | 只看該作者
顯示前先判斷是否小于0,其次數(shù)據(jù)顯示的代碼是錯(cuò)的
回復(fù)

使用道具 舉報(bào)

地板
ID:213173 發(fā)表于 2023-4-26 11:18 | 只看該作者
  1. #include <REG52.H>

  2. typedef unsigned char uchar;
  3. typedef unsigned int uint;
  4. uchar code table[] = {"Welcome!"};
  5. uchar code ren[] = {"RenShu:"};
  6. uchar code dat[2];
  7. sbit RS = P2^6;         //RS?
  8. sbit RW = P2^5;
  9. sbit EN = P2^7;         //EN?
  10. sbit du=P1^6;
  11. sbit we=P1^7;
  12. sbit k1=P3^2;
  13. sbit k2=P3^3;
  14. uint num,i,c;
  15. void delay(uint z)
  16. {
  17.         uint x,y;
  18.         for(x = z; x > 0; x--)
  19.                 for(y = 114; y > 0 ; y--);
  20. }
  21. void Read_Busy()
  22. {
  23.         uchar busy;
  24.         P0 = 0xff;
  25.         RS = 0;
  26.         RW = 1;
  27.         do
  28.         {
  29.                 EN = 1;
  30.                 busy = P0;
  31.                 EN = 0;
  32.         }while(busy & 0x80);
  33. }
  34. void Write_Cmd(uchar cmd)
  35. {
  36.         Read_Busy();//???
  37.         RS = 0;
  38.         RW = 0;
  39.         P0 = cmd;
  40.         EN = 1;
  41.         EN = 0;
  42. }

  43. void Write_Dat(uchar dat)
  44. {
  45.         Read_Busy();
  46.         RS = 1;
  47.         RW = 0;
  48.         P0 = dat;
  49.         EN = 1;
  50.         EN = 0;
  51. }
  52. void Init_LCD1602()
  53. {
  54.     du=0;
  55.     we=0;
  56.     EN=0;
  57.     Write_Cmd(0x38);
  58.     Write_Cmd(0x0c);        
  59.     Write_Cmd(0x06);
  60.     Write_Cmd(0x01);
  61. }
  62. void key()
  63. {
  64.         static uchar time=0;
  65.         static bit sign=0;
  66.         if(!k1 || !k2)
  67.         {
  68.                 if(++time>=10 && sign==0)
  69.                 {
  70.                         sign=1;
  71.                         if(!k1)
  72.                         {
  73.                                 if(c<50000)c++;
  74.                         }
  75.                         if(!k2)
  76.                         {
  77.                                 if(c>0)c--;
  78.                         }
  79.                 }
  80.         }
  81.         else
  82.         {
  83.                 time=0;
  84.                 sign=0;
  85.         }
  86. }

  87. void main()
  88. {
  89.         c=0;
  90.         Init_LCD1602();
  91.         Write_Cmd(0x80 | 0x00);
  92.         for(num=0;num<8;num++)
  93.         {
  94.                 Write_Dat(table[num]);
  95.                 delay(1);        
  96.         }
  97.         Write_Cmd(0x80 | 0x40);
  98.         for(i=0;i<7;i++)
  99.         {
  100.                 Write_Dat(ren[ i]);
  101.                 delay(1);        
  102.         }
  103.         while(1)
  104.         {
  105.                 key();
  106.                 Write_Cmd(0x80 | 0x48);
  107.                 Write_Dat(c/10000%10+0x30);
  108.                 Write_Dat(c/1000%10+0x30);
  109.                 Write_Dat(c/100%10+0x30);
  110.                 Write_Dat(c/10%10+0x30);
  111.                 Write_Dat(c%10+0x30);       
  112.                 delay(1);
  113.         }
  114. }
復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

5#
ID:584814 發(fā)表于 2023-4-26 11:44 | 只看該作者
定義用INT而不是UINT,然后對(duì)負(fù)數(shù)進(jìn)行一些處理就可以
回復(fù)

使用道具 舉報(bào)

6#
ID:123289 發(fā)表于 2023-4-26 16:03 | 只看該作者
學(xué)會(huì)走單步調(diào)試
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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