殘影現象:
出現殘影代碼:
#include <REGX52.H>
#include <INTRINS.H>
void Delayxms(unsigned int x) //@11.0592MHz
{while(x){unsigned char i, j;_nop_();i = 2;j = 199; do{while (--j);} while (--i);x--;}
}
void DisplayDigitalNumber(unsigned char location,unsigned char number){// select nth number_lightunsigned char numbers[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};switch(location){case 1 :P2_4 = 0;P2_3 = 0; P2_2 = 0;break;case 2 :P2_4 = 0;P2_3 = 0; P2_2 = 1;break;case 3 :P2_4 = 0;P2_3 = 1; P2_2 = 0;break;case 4 :P2_4 = 0;P2_3 = 1; P2_2 = 1;break;case 5 :P2_4 = 1;P2_3 = 0; P2_2 = 0;break;case 6 :P2_4 = 1;P2_3 = 0; P2_2 = 1;break;case 7 :P2_4 = 1;P2_3 = 1; P2_2 = 0;break;case 8 :P2_4 = 1;P2_3 = 1; P2_2 = 1;break;}P0 = numbers[number];}
void main(){while(1){DisplayDigitalNumber(3,1);DisplayDigitalNumber(2,2);DisplayDigitalNumber(1,3);}
}
原因分析
位選與段選切換不同步(main):
- 原代碼中先設置位選(選擇數碼管)再設置段選(顯示數字)。此時若位選已打開,但段選數據尚未穩定,會導致瞬間顯示錯誤數據,產生殘影。
缺乏消隱處理:
- 在切換數碼管時,沒有關閉所有位選,導致舊段選數據可能殘留在新位選上,形成短暫的重影。
動態掃描時間不足:
- 主循環連續調用顯示函數,但未給每個數碼管足夠的顯示時間。快速切換導致每個數碼管點亮時間過短,人眼感知為殘影。
解決方案,在設置玩顯示以后,直接把數碼管的顯示清空就不會有殘影了,并且延遲顯示一下:
//只需要修改顯示函數即可
void DisplayDigitalNumber(unsigned char location,unsigned char number){// select nth number_lightunsigned char numbers[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};switch(location){case 1 :P2_4 = 0;P2_3 = 0; P2_2 = 0;break;case 2 :P2_4 = 0;P2_3 = 0; P2_2 = 1;break;case 3 :P2_4 = 0;P2_3 = 1; P2_2 = 0;break;case 4 :P2_4 = 0;P2_3 = 1; P2_2 = 1;break;case 5 :P2_4 = 1;P2_3 = 0; P2_2 = 0;break;case 6 :P2_4 = 1;P2_3 = 0; P2_2 = 1;break;case 7 :P2_4 = 1;P2_3 = 1; P2_2 = 0;break;case 8 :P2_4 = 1;P2_3 = 1; P2_2 = 1;break;}P0 = numbers[number];Delayxms(1);P0=0x00;
}