找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 702|回复: 5
收起左侧

51单片机开发板 lcd1602刚开始00000的时候按减一按键,屏幕显示乱码该如何解决?

[复制链接]
ID:1073848 发表于 2023-4-25 20:17 | 显示全部楼层 |阅读模式
请问刚开始00000的时候按减一按键,屏幕显示乱码该如何解决?
2. 按键加到一百会显示“1:0”  一百一十是“1;0”十位没有显示正确该如何解决?


单片机源程序如下:
#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);

        }
}
回复

使用道具 举报

ID:161164 发表于 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. }
复制代码


回复

使用道具 举报

ID:384109 发表于 2023-4-25 22:21 | 显示全部楼层
显示前先判断是否小于0,其次数据显示的代码是错的
回复

使用道具 举报

ID:213173 发表于 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. }
复制代码
回复

使用道具 举报

ID:584814 发表于 2023-4-26 11:44 | 显示全部楼层
定义用INT而不是UINT,然后对负数进行一些处理就可以
回复

使用道具 举报

ID:123289 发表于 2023-4-26 16:03 | 显示全部楼层
学会走单步调试
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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