鱼C论坛

 找回密码
 立即注册
查看: 4184|回复: 4

请问有懂单片机的吗?请问为什么数码管数字一到8就显示不了

[复制链接]
发表于 2020-4-22 13:28:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <reg51.h>
  2. #include <intrins.h>                            //μ&#188;è&#235;&#209;óê±1us3ìDò

  3. #define  Disdata P0                                               //&#182;¨ò&#229;P0&#182;&#203;&#191;ú
  4. #define  discan  P2                             //&#182;¨ò&#229;P2&#182;&#203;&#191;ú
  5. #define  uchar unsigned char
  6. #define  uint  unsigned int

  7. sbit     DQ=P3^7;                               //&#189;óè&#235;DS18B20×ü&#207;&#223;
  8. sbit     DIN=P0^7;                              //&#191;&#216;&#214;&#198;D&#161;êyμ&#227;áá&#195;e
  9. uint     h;
  10. uint     temp;

  11. uchar code ditab[16]={0x00,0x01,0x01,0x02,0x02,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x07,0x08,0x09,0x09};
  12. uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
  13. //êy&#194;&#235;1ü&#207;&#212;ê&#190;                       "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9" "2&#187;áá" "-"              
  14. uchar code scan_con[4]={0x01,0x02,0x04,0x08};                    //&#203;&#196;&#206;&#187;êy&#194;&#235;1üμ&#196;&#206;&#187;&#209;&#161;±à&#194;&#235;
  15. uchar data temp_data[2]={0x00,0x00};                             //′&#230;′¢&#206;&#194;&#182;èμ&#196;êy×é
  16. uchar data display[5]={0x00,0x00,0x00,0x00,0x00};    //′&#230;′¢êy&#194;&#235;1ü&#207;&#212;ê&#190;êy×&#214;μ&#196;êy×é


  17. void Delayus(uint us)
  18. {
  19.         do
  20.         {
  21.                 _nop_();
  22.                 _nop_();
  23.                 _nop_();
  24.                 _nop_();
  25.                 _nop_();
  26.                 _nop_();
  27.         }
  28.         while(--us);
  29. }

  30. void scan(void)
  31. {
  32.   char k;
  33.   for(k=0;k<4;k++)                     //òà′&#206;μ&#227;áá&#203;&#196;&#184;&#246;êy&#194;&#235;1ü
  34.         {
  35.                 Disdata = 0xff;                                                                                        //&#207;&#251;3yé&#207;ò&#187;&#184;&#246;êy&#194;&#235;1ü&#207;&#212;ê&#190;êy×&#214;μ&#196;ó°&#207;ì
  36.                 discan=scan_con[k];  
  37.                 Disdata=dis_7[display[k]];         
  38.                 if (k==2)                                                                                                                //&#207;&#212;ê&#190;D&#161;êyμ&#227;
  39.                 {
  40.                         DIN=0;
  41.                 }                  
  42.                 Delayus(2800);
  43.         }
  44. }

  45. void init(void)                  //3&#245;ê&#188;&#187;ˉ3ìDò
  46. {
  47.         char presence=1;
  48.         while(presence)
  49.         {
  50.                 while(presence)
  51.                 {
  52.                         DQ=1;
  53.                         Delayus(1);Delayus(1);//×ü&#207;&#223;à-&#184;&#223;&#214;&#174;oó&#212;úà-μí
  54.                         DQ=0;                                       
  55.                         Delayus(500);           //550 us&#209;ó3ù
  56.                         DQ=1;                                       
  57.                         Delayus(60);            //66 us&#209;ó3ù
  58.                         presence=DQ;         //presence=0 è&#244;3&#245;ê&#188;&#187;ˉ3é1|£&#172;&#212;ò&#189;&#248;DD&#207;&#194;ò&#187;2&#189;
  59.                 }
  60.                 Delayus(450);            //500 us&#209;ó3ù
  61.                 presence=~DQ;         
  62.         }
  63.         DQ=1;                   //3&#245;ê&#188;&#187;ˉ3é1|
  64. }

  65. void write_byte(uchar dat)       //DS18B20D′è&#235;×&#214;&#189;ú3ìDò
  66. {
  67.   uchar i;
  68.   for(i=8;i>0;i--)
  69.   {
  70.                 DQ=1;
  71.                 Delayus(1);Delayus(1);                  //×ü&#207;&#223;à-&#184;&#223;2us
  72.                 DQ=0;
  73.                 Delayus(1);Delayus(1);Delayus(1);Delayus(1);  //×ü&#207;&#223;à-μí5 us
  74.                 DQ=dat&0x01;                           //&#195;&#191;′&#206;&#214;&#187;&#182;á×&#238;oóò&#187;&#206;&#187;
  75.                 Delayus(60);                              //&#209;óê±66usoóêy&#190;Yóòò&#198;ò&#187;&#206;&#187;
  76.                 dat=dat>>1;                             
  77.    }
  78.    DQ=1;
  79.    Delayus(10);
  80. }

  81. uchar read_byte(void)     //DS18B20&#182;á3&#246;×&#214;&#189;ú3ìDò
  82. {
  83.         uchar i;
  84.         uchar dat=0;
  85.         for(i=8;i>0;i--)
  86.         {
  87.                 DQ=1;
  88.                 Delayus(1);Delayus(1);
  89.                 dat>>=1;
  90.                 DQ=0;
  91.                 Delayus(1);Delayus(1);Delayus(1);Delayus(1);         
  92.                 DQ=1;
  93.                 Delayus(1);Delayus(1);Delayus(1);Delayus(1);      
  94.                 if(DQ)
  95.                 {
  96.                         dat|=0x80;
  97.                 }
  98.                 Delayus(60);                                    
  99.         }
  100.         DQ=1;
  101.         return(dat);
  102. }

  103. read_temp()
  104. {
  105.   init();                  
  106.   Delayus(2000);
  107.   write_byte(0xcc);            //ì&#248;1yrom2ù×÷
  108.   write_byte(0x44);            //&#206;&#194;&#182;è×a&#187;&#187;
  109.   init();
  110.   Delayus(10);
  111.   write_byte(0xcc);            
  112.   write_byte(0xbe);                                                 //&#182;áè&#161;&#206;&#194;&#182;è
  113.   temp_data[0]=read_byte();    //&#182;áè&#161;&#206;&#194;&#182;èμí×&#214;&#189;ú
  114.   temp_data[1]=read_byte();    //&#182;áè&#161;&#206;&#194;&#182;è&#184;&#223;×&#214;&#189;ú
  115.   temp=temp_data[1];
  116.   temp<<=8;                                               
  117.   temp=temp|temp_data[0];      //&#184;&#223;μí×&#214;&#189;ú×éo&#207;3é&#206;&#194;&#182;è
  118.   return temp;                 
  119. }

  120. void work_temp(uint tem)
  121. {
  122.         uchar n=0;
  123.         if(tem>6348)                        // &#197;D&#182;&#207;&#206;&#194;&#182;è&#213;y&#184;o
  124.   {
  125.                 tem=65536-tem;n=1;
  126.         }                                                                                                            //&#184;o&#206;&#194;&#182;èè&#161;21&#194;&#235;
  127.   display[4]=tem&0x0f;                                   //′&#230;·&#197;D&#161;êy&#214;μ
  128.   display[3]=ditab[display[4]];                          // &#207;&#212;ê&#190;D&#161;êy&#214;μ
  129.   display[4]=tem>>4;                              // ′&#230;·&#197;&#213;&#251;êy&#214;μ
  130.   display[0]=display[4]/100;                       // &#207;&#212;ê&#190;°ù&#206;&#187;êy
  131.   display[2]=display[4]%100;                      // ′&#230;·&#197;ê&#174;&#206;&#187;êyoí&#184;&#246;&#206;&#187;êy
  132.   display[1]=display[2]/10;                       // &#207;&#212;ê&#190;ê&#174;&#206;&#187;êy
  133.   display[2]=display[2]%10;                                           // &#207;&#212;ê&#190;&#184;&#246;&#206;&#187;êy
  134.   if(!display[0])
  135.   {
  136.     display[0]=10;                                                   //°ù&#206;&#187;êy&#206;a0&#212;ò2&#187;&#207;&#212;ê&#190;
  137.     if(!display[1])
  138.                 {
  139.                         display[1]=10;                                                //°ù&#206;&#187;êy&#206;a0μ&#196;í&#172;ê±ê&#174;&#206;&#187;êyò2&#206;a0&#212;ò&#182;&#188;2&#187;&#207;&#212;ê&#190;
  140.                 }
  141.   }
  142.   if(n)
  143.         {
  144.                 display[0]=11;
  145.         }                                                                                                                              //&#184;o&#206;&#194;&#182;è&#212;ú°ù&#206;&#187;&#207;&#212;ê&#190;"-"
  146. }

  147. void main(void)
  148. {
  149.   Disdata=0x00;                 //3&#245;ê&#188;&#187;ˉP0P2&#191;ú
  150.   discan=0x00;
  151.   for(h=0;h<4;h++)              //&#191;a&#187;ú&#207;&#212;ê&#190;"----"
  152.   {
  153.                 display[h]=11;
  154.         }
  155.   init();                                   //3&#245;ê&#188;&#187;ˉDS18B20
  156.   write_byte(0xcc);            
  157.   write_byte(0x44);            
  158.   for(h=0;h<100;h++)            
  159.   {
  160.                 scan();
  161.         }
  162.   while(1)
  163.   {
  164.                 work_temp(read_temp());     
  165.                 scan();                     
  166.   }
  167. }
复制代码
微信图片_20200422132748.jpg
微信图片_20200422132741.jpg
微信图片_20200422132752.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-22 13:30:52 | 显示全部楼层
如题 (不知为何程序注释是乱码的 )     数码管别的数字都能正常显示,唯独8不能显示,而且当个位数为8的时候,连小数点也不显示了,请问有大神知道问题出在哪里吗?我尝试将P0初始化为 0x80,想让数码管一开始就显示四个8,但是他也不能正常显示
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-22 13:31:59 | 显示全部楼层
单片机型号是1T的 stc12c5a60s2    晶振用的是 11.0592Mhz  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-27 22:14:52 | 显示全部楼层
你将P0初始化为 0x80都没有显示,你确定数码管硬件没有问题嘛?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-2 10:26:27 | 显示全部楼层
帖子都一年多了不知道你还玩不玩单片机。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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