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

QQ登錄

只需一步,快速開始

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

單片機(jī)數(shù)碼管滾動(dòng)顯示1-9程序 數(shù)組下標(biāo)在改變過程中出現(xiàn)運(yùn)算錯(cuò)誤?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
寫的數(shù)碼管滾動(dòng)顯示1-9,但是顯示過程中這個(gè)5和6總是出現(xiàn)兩次,
調(diào)試發(fā)現(xiàn)是數(shù)組下標(biāo)在改變過程中出現(xiàn)運(yùn)算錯(cuò)誤,但是實(shí)在想不通這是為什么

單片機(jī)源代碼和仿真圖在壓縮包里,請(qǐng)高手指點(diǎn)指點(diǎn),多謝了.


  1. #include <reg51.h>
  2. #include "delay.h"
  3. #include "display.h"

  4. void main()
  5. {  
  6.         uint i;
  7.         uchar num = 0;
  8.         
  9.         while(1){
  10.                 for( num = 0; num <11; num++){
  11.                         dBuf[0] = dBuf[num+0];
  12.                         dBuf[1] = dBuf[num+1];
  13.                         dBuf[2] = dBuf[num+2];
  14.                         dBuf[3] = dBuf[num+3];
  15.                         Delay(1);
  16.                         for( i = 0; i < 2000; i++){
  17.                                 display();
  18.                         }
  19.                
  20.                 }
  21.         }
  22. }
復(fù)制代碼
  1. #include <reg51.h>
  2. #include "delay.h"
  3. #include "display.h"

  4. unsigned char code table[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x40,0x00 };
  5. unsigned char dBuf[] = { 1,2,3,4,5,6,7,8,9,10,1,2,3,4 };  //顯示數(shù)據(jù)緩沖區(qū)
  6. unsigned char code wCode[] = { 0xfe, 0xfd, 0xfb, 0xf7 }; //位選信號(hào)
  7. unsigned char num ;

  8. void display(){
  9.         
  10.                 uchar i;  //不要符初值,keil會(huì)自動(dòng)初始化為0,且只做一次初始化
  11.                 //static uchar i = 0;
  12.                 //4 消隱
  13.                 DPORT = 0;
  14.                 //1 送段碼         
  15.                 DPORT = table[dBuf[i]];
  16.                 //2 送位選
  17.                 WPORT = wCode[i];
  18.                 //3 延時(shí) (所有數(shù)碼管一輪的時(shí)間合不要大于10ms
  19.                 Delay(1);
  20.                 DPORT = 0;
  21.                 i++;
  22.                 if( 4 == i ){
  23.                         i = 0;
  24.                 }
  25.         
  26. }
復(fù)制代碼

smggdxs.zip

17.94 KB, 下載次數(shù): 2

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

使用道具 舉報(bào)

沙發(fā)
ID:404160 發(fā)表于 2022-4-18 11:45 | 只看該作者
主循環(huán)中的11,改10即可。for( num = 0; num <10; num++){
回復(fù)

使用道具 舉報(bào)

板凳
ID:884109 發(fā)表于 2022-4-18 12:06 | 只看該作者
飛云居士 發(fā)表于 2022-4-18 11:45
主循環(huán)中的11,改10即可。for( num = 0; num

試過,不管用,關(guān)鍵是第三次和第四次循環(huán)時(shí),就是當(dāng)num為2還有3時(shí),num+1和num+2出現(xiàn)異常,數(shù)組下標(biāo)出現(xiàn)重復(fù)值,但后面的又正常了. 懷疑是變量名沖突可又沒看出來
回復(fù)

使用道具 舉報(bào)

地板
ID:123289 發(fā)表于 2022-4-18 16:52 | 只看該作者
1、DISPALY中 i 改 j 。
2、所有變量改為全局,含數(shù)組。
試試。
回復(fù)

使用道具 舉報(bào)

5#
ID:332444 發(fā)表于 2022-4-18 17:55 | 只看該作者
4位數(shù)碼管不用數(shù)組也可以
回復(fù)

使用道具 舉報(bào)

6#
ID:884109 發(fā)表于 2022-4-18 18:44 | 只看該作者
問題終于解決了,錯(cuò)誤出現(xiàn)的原因找到了,
                        dBuf[0] = dBuf[num+0];
                        dBuf[1] = dBuf[num+1];
                        dBuf[2] = dBuf[num+2];
                        dBuf[3] = dBuf[num+3];
是這幾句修改了原始數(shù)組的緣故,又定義了一個(gè)數(shù)組,把原始數(shù)組和顯示用數(shù)組分開后,正常了.
謝謝大家的幫助.
回復(fù)

使用道具 舉報(bào)

7#
ID:213173 發(fā)表于 2022-4-18 20:30 | 只看該作者
renwxzy 發(fā)表于 2022-4-18 12:06
試過,不管用,關(guān)鍵是第三次和第四次循環(huán)時(shí),就是當(dāng)num為2還有3時(shí),num+1和num+2出現(xiàn)異常,數(shù)組下標(biāo)出現(xiàn)重復(fù)值 ...


  1. #include <reg51.h>
  2. //#include "delay.h"
  3. //#include "display.h"
  4. #define DPORT P3  //段碼IO
  5. #define WPORT P0  //位碼IO
  6. #define N          4   //數(shù)碼管個(gè)數(shù)
  7. //unsigned char code table[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x40,0x00 };
  8. unsigned char dBuf[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x40,0x00 };//顯示數(shù)據(jù)緩沖區(qū)
  9. unsigned char code wCode[] = { 0xfe, 0xfd, 0xfb, 0xf7 }; //位選信號(hào)
  10. unsigned char num ;

  11. void Delay(unsigned int ms )
  12. {
  13.         unsigned int i,j;
  14.         for( i = ms; i > 0; i-- )
  15.                 for( j = 114; j > 0; j-- ) ;
  16. }


  17. void display()
  18. {       
  19.         static unsigned char i = 0;
  20.         DPORT = 0x00;                // 消隱
  21.         WPORT = wCode[i];        // 送位選       
  22.         DPORT = dBuf[i];        // 送段碼
  23.         i=++i%4;
  24.         Delay(1);
  25. }
  26.        
  27. void main()
  28. {  
  29.         unsigned int i;       
  30.         unsigned char j;

  31.         while(1)
  32.         {       
  33.                 if(++i>=500)
  34.                 {
  35.                         i=0;
  36.                         dBuf[11]=dBuf[0];
  37.                         for(j=0;j<11;j++)
  38.                                 dBuf[j]=dBuf[j+1];
  39.                 }
  40.                 display();
  41.         }
  42. }
復(fù)制代碼



回復(fù)

使用道具 舉報(bào)

8#
ID:884109 發(fā)表于 2022-4-19 14:39 | 只看該作者

又一種思路 受教 多謝了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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