找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

51单片机变量定义为unsigned long时8位数码管显示乱码

查看数: 2717 | 评论数: 6 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2020-10-31 10:47

正文摘要:

程序如下:#include <reg51.h> #define uchar unsigned char #define uint unsigned int unsigned  long  code DUAN[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //ucha ...

回复

ID:287899 发表于 2020-11-1 22:29
wulin 发表于 2020-11-1 16:40
仿真数码管显示乱码的原因不是数组用错类型,而是显示代码顺序有错和延时时间不当。

感谢,
ID:213173 发表于 2020-11-1 16:40
zycs 发表于 2020-11-1 15:11
对不起描述的太模糊了,我想实现的功能是八位数管显示从一加到99999999,本来数组定义的是unsigned char ...

仿真数码管显示乱码的原因不是数组用错类型,而是显示代码顺序有错和延时时间不当。
  1. #include <reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. unsigned  long  code DUAN[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  5. //uchar DUAN2[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  6. //uchar WEI[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  7. //uchar WEI2[8]={0xc7,0xcf,0xd7,0xdf,0xe7,0xef,0xf7,0xff};
  8. void delay(uint g)
  9. {
  10.         uint x,y;
  11.         for(x=g;x>0;x--)
  12.                 for(y=110;y>0;y--);
  13. }        
  14. void main()
  15. {
  16.         unsigned  long i;
  17.         while(1)
  18.         {
  19.                 for(i=0;i<10000000;i++)
  20.                 {
  21.                         P3=0X00;
  22.                         P2=DUAN[i%10];
  23.                         P3=0X01;
  24.                         delay(1);

  25.                         P3=0X00;
  26.                         P2=DUAN[i/10%10];
  27.                         P3=0X02;
  28.                         delay(1);

  29.                         P3=0X00;                       
  30.                         P2=DUAN[i/100%10];
  31.                         P3=0X04;
  32.                         delay(1);

  33.                         P3=0X00;                       
  34.                         P2=DUAN[i/1000%10];
  35.                         P3=0X08;
  36.                         delay(1);

  37.                         P3=0X00;
  38.                         P2=DUAN[i/10000%10];
  39.                         P3=0X10;
  40.                         delay(1);
  41.                        
  42.                         P3=0X00;
  43.                         P2=DUAN[i/100000%10];
  44.                         P3=0X20;
  45.                         delay(1);

  46.                         P3=0X00;
  47.                         P2=DUAN[i/1000000%10];
  48.                         P3=0X40;
  49.                         delay(1);

  50.                         P3=0X00;
  51.                         P2=DUAN[i/10000000%10];
  52.                         P3=0X80;
  53.                         delay(1);
  54.                 }
  55.         }
  56. }
复制代码

ID:287899 发表于 2020-11-1 16:37
不用了各位,问题解决了
ID:287899 发表于 2020-11-1 15:11
robinsonlin 发表于 2020-10-31 12:49
你这个问题的模糊概念不止一个哦。
首先,先弄清楚段码在低层的真实用途。段码的数组定义成unsigned char ...

对不起描述的太模糊了,我想实现的功能是八位数管显示从一加到99999999,本来数组定义的是unsigned char类型,主函数里的变量i定义的unsigned int类型,但是这样只能加到3万多再往后加就乱码了,所以我想着是不是unsigned int类型的变量i范围太小了,所以就把i改成了unsigned long类型的,但是这样就直接乱码了,数组我已经改回unsigned char 类型的了。请各位帮忙看看代码哪里出了问题
ID:287899 发表于 2020-11-1 13:32
本帖最后由 zycs 于 2020-11-1 13:35 编辑
robinsonlin 发表于 2020-10-31 12:49
你这个问题的模糊概念不止一个哦。
首先,先弄清楚段码在低层的真实用途。段码的数组定义成unsigned char ...

想从1加到99999999,数组定义的unsigned char 。本来在主函数里定义的变量i是unsigned int类型的,但是从1加到2万多就乱码了,所以我就想是不是unsigned int类型的变量范围太小了就改成unsigned long类型的,但还是乱码。想请给位师傅帮忙看看问题出在哪里
ID:451718 发表于 2020-10-31 12:49
你这个问题的模糊概念不止一个哦。
首先,先弄清楚段码在低层的真实用途。段码的数组定义成unsigned char类型就够了,不需要定义成long。
其次,数组的成员概念。你用long型量去做数组的角码,你是准备用多大的内存来存数组啊?

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

Powered by 单片机教程网

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