單片機C語言程序設計實訓100例—基于8051Proteus仿真_第1頁
單片機C語言程序設計實訓100例—基于8051Proteus仿真_第2頁
單片機C語言程序設計實訓100例—基于8051Proteus仿真_第3頁
單片機C語言程序設計實訓100例—基于8051Proteus仿真_第4頁
單片機C語言程序設計實訓100例—基于8051Proteus仿真_第5頁
已閱讀5頁,還剩61頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、?基于8051+Proteus仿真?案例第01篇根底程序設計01 閃爍的LED/* 名稱:閃爍的LED說明:LED按設定的時間間隔閃爍*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1A0;/延時void DelayMS(uint x)(uchar i;while(x-)(for(i=0;i<120;i+);)/主程序void main()(while(1)(LED=LED;DelayMS(150);)19>?UALlPanfflJEPDI/AD11日FUM

2、RZAl XLrU :J -1:P0I4/W4FOUADSPD6/MJ6RSTPQJ/AD7.:Fi.De陽詡F2JJA1DP9EMjFALEP2.4JA12由Pj.CMUP? 丁閃 5PI 0a3.omwP1.1PI 2F3.2JlhnD二£Pl.3p3 mlmJPIP3.4maPl.5P3.51T1PI 6PI.7P3.7JFCID- 二 12 蜜 1402從左到右的流水燈/* 名稱:從左到右的流水燈說明:接在P0 口的8個LED 從左到右循環(huán)依次點亮, 產(chǎn)生走 馬燈效果*/#include<reg51.h>#include<intrins.h>#defi

3、ne uchar unsigned char#define uint unsigned int /延時; U1_LII X1I I*WTalIFOOT皿陽 2rM7KT.14J2W&3加川心行而興皿ggR暗&學泗1K4JAIJF7UM1P2tAi.l4 FJ FMI >PI QWPI iF31J_PI ;7 glWFl aPD事而一Fl 4/"TOPi £PI時用鼻血而PI TH TffTvoid DelayMS(uint x)(uchar i;while(x-)(for(i=0;i<120;i+);)/主程序void main()(P0=0xf

4、e;while(1)(P0=_crol_(P0,1);/P0的值向左循環(huán)移動DelayMS(150);)03 8只LED左右往返點亮/* 名稱:8只LED左右往返點亮說明:程序利用循環(huán)移位函數(shù)crol和cror形成往返滾動的效果3TqiRSi叩二心2 IM P 口工心憶 帕MM* PQoykpc叩EWfi一L -Ipj 田m wn村吟E R r iTH FU®直 F3JWTtPi£JTr_ PI閑T P二忖±±±之ME CA*/#include<reg51.h>#include<intrins.h>#define uch

5、ar unsigned char#define uint unsigned int/延時void DelayMS(uint x)(uchar i;while(x-)(for(i=0;i<120;i+);)/主程序void main()(uchar i;P2=0x01;while(1)(for(i=0;i<7;i+) (P2=_crol_(P2,1);/P2的值向左循環(huán)移動 DelayMS(150);for(i=0;i<7;i+)P2=_cror_(P2,1);/P2的值向右循環(huán)移動 DelayMS(150);04把戲流水燈/* 名稱:把戲流水燈說明:16只LED分兩組 按預設

6、的多種把戲變換顯示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int uchar code Pattern_P0= MB舸mu muK>IHpl PDi*Mia 叩mm E4W KiMe5 即WM 叩3?母rFZjCLMB 用11M F7 1W1D PH.11 厲號叫堆 吃*13 用 3.1* 吃5*9MDn iffip色通而F3 3«E I nmF3i.ni 戶J班 pjrrp0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff

7、,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xfe,0xfd,0xfb,0xf7,0xef,0x

8、df,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe, 0x00,

9、0xff,0x00,0xff,0x00,0xff,0x00,0xff;uchar code Pattern_P2=0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x

10、7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,

11、0xe0,0xc0,0x80,0x00, 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延時void DelayMS(uint x) (uchar i;while(x-)for(i=0;i<120;i+); /主程序 void main() (uchar i;while(1)(從數(shù)組中讀取數(shù)據(jù)送至for(i=0;i<136;i+)(P0=Pattern_P0i;P2=Pattern_P2i

12、; DelayMS(100); 05 LED模擬交通燈/* 名稱:LED模擬交通燈說明:東西向綠燈亮假設干秒,黃 燈閃爍5次后紅燈亮,紅燈亮后,北向由紅燈變?yōu)榫G燈,假設干秒后南北 向黃燈閃爍5此后變紅燈,東西向變 綠燈,如此重復.*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int sbit RED_A=P0A0;東西向燈sbit YELLOW_A=P0A1;sbit GREEN_A=P0A2;sbit RED_B=P0A3;南北向燈sbit YELLOW_B=P0A4;sbit GREEN_

13、B=P0A5;uchar Flash_Count=0,Operation_Type /延時P0和P2 口顯示同4HKT*U!+H±主isrP3HVIK>IlTM.- F±.*T工FUiTO mffi ,麻r 用前叼必 冷 aPJ>!AII PiAwrj p冷遇 PieAJH-d P: FIAT;閃爍次數(shù),操作類型變量void DelayMS(uint x) (uchar i;while(x-) for(i=0;i<120;i+);)交通燈切換void Traffic_Light()(switch(Operation_Type)(case 1: 東西向綠燈與

14、南北向紅燈亮RED_A=1;YELLOW_A=1;GREEN_A=0;RED_B=0;YELLOW_B=1;GREEN_B=1;DelayMS(2000);Operation_Type=2;break;case 2: 東西向黃燈閃爍,綠燈關(guān)閉DelayMS(300);YELLOW_A=YELLOW_A;GREEN_A=1;if(+Flash_Count!=10) return; / 閃爍 5 次Flash_Count=0;Operation_Type=3;break;case 3: 東西向紅燈,南北向綠燈亮RED_A=0;YELLOW_A=1;GREEN_A=1;RED_B=1;YELLOW_

15、B=1;GREEN_B=0;DelayMS(2000);Operation_Type=4;break;case 4: 南北向黃燈閃爍 5次DelayMS(300);YELLOW_B=YELLOW_B;GREEN_B=1;if(+Flash_Count!=10) return;Flash_Count=0;Operation_Type=1;)/主程序void main()(while(1) Traffic_Light();)06單只數(shù)碼管循環(huán)顯示 09/* 名稱:單只數(shù)碼管循環(huán)顯示09說明:主程序中的循環(huán)語句反復將09的段碼送至P0 口,使數(shù)字09循環(huán)顯示*/#include<reg51.h

16、>#include<intnns.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/延時void DelayMS(uint x)uchar t;while(x-) for(t=0;t<120;t+);/主程序void main()uchar i=0;P0=0x00;while(1)/*for(;i<11;i+) P0=DSY_CODEi;DelayMS(30

17、0);注:另一方案 */P0=DSY_CODEi;i=(i+1)%10;DelayMS(300); )07 8只數(shù)碼管滾動顯示單個數(shù)字/* 名稱:8只數(shù)碼管滾動顯示單個數(shù)字說明:數(shù)碼管從左到右依次滾動顯示 07,程序通過每次僅循環(huán)選通一只數(shù)碼 管*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int忖業(yè)設Ri 2y02印融皿1 Esygs 卬后山苒 FOTJWDRST他 GALE口工317C faGA絲.眄pg offlnciP1.1pi.-um

18、cnPl JPl JP531MFTP3.4HTiPlipasrriPl 3PH第.:后=x±±il r- Muchar code DSY_CODE尸0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /延時void DelayMS(uint x) uchar t;while(x-) for(t=0;t<120;t+);/主程序void main()uchar i,wei=0x80; while(1) ( for(i=0;i<8;i+) ( P2=0xff; 關(guān)閉顯示 wei=_crol_(wei,1);P0=DS

19、Y_CODEi;/發(fā)送數(shù)字段碼P2=wei;發(fā)送位碼DelayMS(300); 08 8只數(shù)碼管動態(tài)顯示多個不同字符 電路如上圖 /* 名稱:8只數(shù)碼管動態(tài)顯示多個不同字符 說明:數(shù)碼管動態(tài)掃描顯示07.*/ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE尸0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /延時 void DelayMS(uin

20、t x) uchar t;while(x-) for(t=0;t<120;t+); /主程序 void main() uchar i,wei=0x80; while(1) for(i=0;i<8;i+) P2=0xff;P0=DSY_CODEi;發(fā)送段碼wei=_crol_(wei,1); P2=wei;發(fā)送位碼DelayMS(2);09 8只數(shù)碼管閃爍顯示數(shù)字串電路如上圖/* 名稱:8只數(shù)碼管閃爍顯示數(shù)字串 說明:數(shù)碼管I閃爍顯示由07構(gòu)成的一串數(shù)字本例用動態(tài)刷新法顯示一串數(shù)字,在停止刷新時所有數(shù)字顯示消失.*/#include<reg51.h>#define uch

21、ar unsigned char#define uint unsigned int/段碼表uchar code DSY_CODE尸0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /位碼表uchar code DSY_IDX尸0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;/延時 void DelayMS(uint x) uchar t;while(x-) for(t=0;t<120;t+);/主程序 void main() uchar i,j; while(1) for(i=0;i<30;i+)

22、for(j=0;j<8;j+) P0=0xff; P0=DSY_CODEj;發(fā)送段碼P2=DSY_IDXj;發(fā)送位碼DelayMS(2); P2=0x00;關(guān)閉所有數(shù)碼管并延時DelayMS(1000); 10 8只數(shù)碼管滾動顯示數(shù)字串 電路如上圖/* 名稱:8只數(shù)碼管滾動顯示數(shù)字串 說明:數(shù)碼管向左滾動顯示3個字符構(gòu)成的數(shù)字串*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/段碼表uchar code DSY_CODE尸0xc0,0

23、xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/下面數(shù)組看作環(huán)形隊列,顯示從某個數(shù)開始的8個數(shù)(10表示黑屏)uchar Num尸10,10,10,10,10,10,10,10,2,9,8;/延時void DelayMS(uint x) uchar t;while(x-) for(t=0;t<120;t+);/主程序 void main() uchar i,j,k=0,m=0x80;while(1)刷新假設干次,保持一段時間的穩(wěn)定顯示for(i=0;i<15;i+)for(j=0;j<8;j+) 發(fā)送段碼,采用環(huán)形取法,從第

24、 k個開始取第j個P0=0xff;P0=DSY_CODENum(k+j)%11;m=_crol_(m,1);P2=m;發(fā)送位碼DelayMS(2); k=(k+1)%11; 環(huán)形隊列首支針 k遞增,Num下標范圍010,故對11取余 11 K1-K4 限制LED 移位/* 名稱:K1-K4限制LED移位說明:按下 K1時,P0 口 LED上移一位;按下K2時,P0 口 LED下移一位;按下K3時,P2 口 LED上移一位;按下K4時,P2 口 LED下移一位;*/#include<reg51.h>#include<intrins.h>#define uchar unsi

25、gned charXTAL3o J j3口: FlpfEptnpfplm#define uint unsigned int/延時void DelayMS(uint x)(uchar i;while(x-) for(i=0;i<120;i+);/根據(jù)P1 口的按鍵移動LEDvoid Move_LED() (if (P1&0x10)=0) P0=_cror_(P0,1);/K1else if(P1&0x20)=0) P0=_crol_(P0,1);/K2else if(P1&0x40)=0) P2=_cror_(P2,1);/K3else if(P1&0x80

26、)=0) P2=_crol_(P2,1);/K4 /主程序 void main() (uchar Recent_Key; / 最近按鍵P0=0xfe;P2=0xfe;P1=0xff;Recent_Key=0xff;while(1) (if(Recent_Key!=P1) (Recent_Key=P1;保存最近按鍵Move_LED(); DelayMS(10); 12 K1-K4按鍵狀態(tài)顯示/* 名稱:K1-K4按鍵狀態(tài)顯示說明:K1、K2按下時LED點亮,松開時熄滅,K3、K4按下并釋放時LED點亮,再次按下并釋放時熄滅;*/#include<reg51.h>#define uch

27、ar unsigned char#define uint unsigned intsbit LED1=P0A0;sbit LED2=P0A1;sbit LED3=P0A2;sbit LED4=P0A3;sbit K1=P1A0;sbit K2=P1A1;sbit K3=P1A2;sbit K4=P1A3;/延時void DelayMS(uint x)(uchar i;3 ,JT工. KI JL .» . . » .餐 .1 I.A il-POCUA&OxrqfarPOIAflTH叩印5 PCHWTMFtlJJACTP1.1M9Pi2MlD 隙MgP? 他3PHD P

28、l.iFH.4P2SMI3 陛MH 啕的15FUfTjjP 心沌魚瓶中mPH >5叩灑TS由6近MJ初 3H無while(x-) for(i=0;i<120;i+);)/主程序void main()(P0=0xff;P1=0xff;while(1) (LED1=K1;LED2=K2;if(K3=0) (while(K3=0);LED3=LED3;)if(K4=0)(while(K4=0);LED4=LED4;) DelayMS(10);) 13 K1-K4 分組限制 LED /* 名稱:K1-K4分組限制LED說明:每次按下 K1時遞增點亮一只LED,全亮時再次按下那么再次循環(huán)開始

29、,K2按下后點亮上面 4只LED , K3按下后點亮下面 4只LED , K4按下后關(guān)閉所有 LEDC2 nC3UI*/#include<reg51.h>#define uchar unsigned char #define uint unsigned int /延時 void DelayMS(uint x) uchar i;while(x-) for(i=0;i<120;i+);/主程序 void main() uchar k,t,Key_State;P0=0xff;P1=0xff; while(1) t=P1; if(t!=0xff) DelayMS(10); if(t!

30、=P1) continue; 取得4位按鍵值,由模式 XXXX1111(X中有一位為0,其他均為 變?yōu)槟J?000XXXX(X 中有一位為1,其他均為0) Key_State=t>>4; k=0;/檢查1所在位置,累加獲取按鍵號kwhile(Key_State!=0) k+;Key_State>>=1; 根據(jù)按鍵號k進彳T 4種處理 switch(k) case 1:if(P0=0x00) P0=0xff;P0<<=1;DelayMS(200); break; case 2:P0=0xf0;break;case 3: P0=0x0f;break;通用.占品上

31、忸盍屋.iW千TC1T卜KtrHTRL5R0JiW3RDSiM'5 叩日回:W網(wǎng)町卬也劃Q麗rri/"禺if/未對之仄口的行pi cinun疝pi ipa/iJi®Fl 5pi jpi awrTF1 4幡iWflpusF3 5H1M hFw ea3TFil TFIT前I尸 RF".LEI>RE&.1)case 4: P0=0xff;14 K1-K4限制數(shù)碼管移位顯示/* 名稱:K1-K4限制數(shù)碼管移位顯示說明:按下 K1時加1計數(shù)并增加顯示位,按下K2時減1計數(shù)并減少顯示位, 按下K3時清零.*/#include<reg51.h>

32、#define uchar unsigned char#define uint unsigned int/段碼uchar code DSY_CODE尸0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff); /位碼uchar code DSY_Index尸0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01;待顯示到各數(shù)碼管的數(shù)字緩沖(開始僅在 0位顯示0,其他黑屏)uchar Display_Buffer尸0,10,10,10,10,10,10,10;/延時void DelayMS(uint x) uchar

33、i;while(x-) for(i=0;i<120;i+);void Show_Count_ON_DSY() uchar i;for(i=0;i<8;i+) P0=0xff;RN1P0=DSY_CODEDisplay_Bufferi;P2=DSY_Indexi;DelayMS(2);/主程序void main()uchar i,Key_NO,Key_Counts=0;P0=0xff;P1=0xff;P2=0x00;while(1)11M-4Pl«Fl liTTE- Pl J*#f FJ3tf?Tiri»5W 弓皿_ FSEA*口rrShow_Count_ON_D

34、SY();I ,K>1MDrt 2aAl1Kl F4 3MO9P*WH raawBC吟M 問ww R21W.i1 f T 抬*中3*Z7J*btP1=0xff;Key_NO=P1;/P1 口按鍵狀態(tài)分別為K1-0xfe , K2-0xfd,K3-0xfbswitch(Key_NO)case 0xfe:Key_Counts+;if(Key_Counts>8) Key_Counts=8;Display_BufferKey_Counts-1=Key_Counts;break;case 0xfd:if(Key_Counts>0)Display_Buffer-Key_Counts=10

35、;break;case 0xfb:Display_Buffer0=0;for(i=1;i<8;i+) Display_Bufferi=10; Key_Counts=0;假設鍵未釋放那么僅刷新顯示,不進行鍵掃描while(P1!=0xff) Show_Count_ON_DSY();K215 K1-K4限制數(shù)碼管加減演示/* 名稱:K1-K4限制數(shù)碼管加減演示說明:按下 K1后加1計數(shù),按下 后減1計數(shù),按下K3后清零.*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define ui

36、nt unsigned int/段碼 uchar code DSY_CODE尸0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/待顯示的3位緩沖uchar Num_Buffer尸0,0,0;/按鍵代碼,按鍵計數(shù)uchar Key_Code,Key_Counts=0;/延時void DelayMS(uint x)uchar i;while(x-) for(i=0;i<120;i+);顯示函數(shù)void Show_Counts_ON_DSY()uchar i,j=0x01;Num_Buffer2=Key_Counts/100;Num

37、_Buffer1=Key_Counts/10%10;Num_Buffer0=Key_Counts%10;for(i=0;i<3;i+)j=_cror_(j,1);P0=0xff;P0=DSY_CODENum_Bufferi;P2=j;DelayMS(1);)/主程序void main()uchar i;P0=0xff;P1=0xff;P2=0x00;Key_Code=0xff;while(1) Show_Counts_ON_DSY();P1=0xff;Key_Code=P1;有鍵按下時,數(shù)碼管刷新顯示30次,該行代碼同時起到延時作用if(Key_Code!=0xff)for(i=0;i&

38、lt;30;i+) Show_Counts_ON_DSY();switch(Key_Code) case 0xfe: if(Key_Counts<255) Key_Counts+;break;case 0xfd:if(Key_Counts>0) Key_Counts-;break;case 0xfb:Key_Counts=0;)Key_Code=0xff;)16 4X4矩陣鍵盤限制條形LED顯示田黜AL;/* 名稱:4X4矩陣鍵盤控 制條形LED顯示說明:運行本例時,按ri - 口地川:FU混淆麗 晤少工東建解心二 IM二 如£必山口和*附 陽占科4 K;刈 FZJJWi

39、TS胞所11r I'i-n7M下的按鍵值越大點亮的LED越多.*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int /矩陣鍵盤按鍵特征碼表 uchar code KeyCodeTable尸0x11,0x12,0x14,0x18,0x21, 0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88; /延時 void DelayMS(uint x) uchar i;while(x-) f

40、or(i=0;i<120;i+);/鍵盤掃描uchar Keys_Scan() uchar sCode,kCode,i,k;/低4位置0,放入4行P1=0xf0;/假設高4位出現(xiàn)0,那么有鍵按下 if(P1&0xf0)!=0xf0) DelayMS(2); if(P1&0xf0)!=0xf0) sCode=0xfe;行掃描碼初值for(k=0;k<4;k+)/對4行分別進行掃描 P1=sCode; if(P1&0xf0)!=0xf0) kCode=P1; for(i=0;i<16;i+)/查表得到按鍵序號并返回if(kCode=KeyCodeTable

41、i) return(i); else sCode=_crol_(sCode,1); return(-1);/主程序void main() (uchar i,P2_LED,P3_LED;uchar KeyNo=-1;按鍵序號,-1表示無按鍵while(1)(KeyNo=Keys_Scan();掃描鍵盤獲取按鍵序號KeyNoif(KeyNo!=-1) (P2_LED=0xff;P3_LED=0xff;for(i=0;i<=KeyNo;i+)/鍵值越大,點亮的 LED越多(if(i<8)P3_LED>>=1; elseP2_LED>>=1; P3=P3_LED;點

42、亮條形 LEDP2=P2_LED;17數(shù)碼管顯示4X4矩陣鍵盤按 鍵號/* 名稱:數(shù)碼管顯示 4X4矩陣 鍵盤按鍵號說明:按下任意鍵時,數(shù)碼 管都會顯示其鍵的序號,掃描程 序首先判斷按鍵發(fā)生在哪一列, 然后根據(jù)所發(fā)生的行附加不同的 值,從而得到按鍵的序號.EDd WT PTJVUD *3 Jam F2 4A!2 3 i mai WFaifFnnroc:' 石電 , 燈即*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/段碼uchar code DSY_CODE=0xc0,0xf9,

43、0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00;sbit BEEP=P3A7;/上次按鍵和當前按鍵的序號,該矩陣中序號范圍 015, 16表示無按鍵uchar Pre_KeyNo=16,KeyNo=16;/延時void DelayMS(uint x)(uchar i;while(x-) for(i=0;i<120;i+);)/矩陣鍵盤掃描void Keys_Scan()(uchar Tmp;P1=0x0f; 高4位置0,放入4行DelayMS(1);Tmp=P1A0x0f;/按鍵后0f

44、變成0000XXXX , X中一個為0, 3個仍為1,通過異或把 3個1變?yōu)?,唯 一的0變?yōu)?switch(Tmp) 判斷按鍵發(fā)生于 03歹U的哪一列J (case 1: KeyNo=0;break;case 2: KeyNo=1;break;case 4: KeyNo=2;break;case 8: KeyNo=3;break;default:KeyNo=16;/ 無鍵按下)P1=0xf0; 低4位置0,放入4列DelayMS(1);Tmp=P1>>4A0x0f;/按鍵后f0變成XXXX0000 , X中有1個為0,三個仍為1;高4位轉(zhuǎn)移到低4位并 異或得到改變的值switch

45、(Tmp) /對03行分別附加起始值 0, 4, 8, 12 (case 1: KeyNo+=0;break;case 2: KeyNo+=4;break;case 4: KeyNo+=8;break;case 8: KeyNo+=12; )蜂鳴器void Beep()(uchar i;for(i=0;i<100;i+)(DelayMS(1);BEEP=BEEP;)BEEP=0;)/主程序void main()(P0=0x00;BEEP=0;while(1)(P1=0xf0;if(P1!=0xf0) Keys_Scan();/ 獲取鍵序號if(Pre_KeyNo!=KeyNo)(P0=D

46、SY_CODEKeyNo;Beep();Pre_KeyNo=KeyNo;)DelayMS(100);)18 開關(guān)限制LED/* 名稱:開關(guān)限制 LED說明:開關(guān)S1和S2分別控制 LED1 和 LED2.*/#include<reg51.h>sbit S1=P1A0;sbit S2=P1A1;sbit LED1=P0A0;sbit LED2=P0A1;/主程序void main()(while(1)(LED1=S1;LED2=S2;)19繼電器限制照明設備/* 名稱:繼電器限制照明設備說明:按下K1燈點亮,再次按下時燈熄滅*/#include<reg51.h>#defi

47、ne uchar unsigned char#define uint unsigned int Q1.2g 4 客后.7P15"S"P1叫P1叫叫EMM PcniMin po.二 FO.jrAM Ffl Fd.yyxDS PO.WiDfl rn'rA&FPiQ&OV =2TI P2泌19 P2 4居以 Pl&Arl P2 3事 PlfW5P3.T/D© 加麗而" x.miTmP34HO .即P3三殛 PBTJfOAajCuaDQ 5allwm鈍.癡4由anm*3.73/力口maF3 1總守P2 型MO號P2.W1以工

48、63;1 JEfcF2 Ml4P2 T.m.15Ml DpsnkKbFli 1H 1ED5 3FSitfnrp|i Pli嗎P3聞卬0Fl 5F4l ?嚏.生金£,三葺±±三 一±_-tL2NOVIsbit K1=P1A0;sbit RELAY=P2A4;/延時void DelayMS(uint ms)uchar t;while(ms-)for(t=0;t<120;t+);/主程序void main()P1=0xff;RELAY=1;while(1)(if(K1=0)(while(K1=0);RELAY=RELAY;DelayMS(20);)20數(shù)

49、碼管顯示撥碼開關(guān)編碼D(SWlHCT*LIXTjuJ?國I,喝M 爾居口! POWW Ki FQ4JAPI 叫,叫D5 PO FCL7MC7耐/* 名稱:數(shù)碼管顯示撥碼開關(guān)編碼說明:系統(tǒng)顯示撥碼開關(guān)所設置的編碼000255*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/各數(shù)字的數(shù)碼管段碼(共陰)uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f);顯示

50、緩沖uchar DSY_Buffer3=0,0,0;/延時void DelayMS(uint ms) uchar t;while(ms-)for(t=0;t<120;t+);/主程序void main()0注定P2赳威 同期Ml P:桃 1: 心勁d? F !器k /T7-M'Blnz-Bj/Eljwuchar i,m,Num;P0=0xff;P2=0xff;while(1)(m=0xfe;Num=P1; /讀取撥碼開關(guān)的值DSY_Buffer0=Num/100;DSY_Buffer1=Num/10%10;DSY Buffer2=Num%10;for(i=0;i<3;i+)刷新顯示在數(shù)碼官上( m=_crol_(m,1); P2=m;P0=DSY_CODEDSY_Bufferi; DelayMS(10);)21開關(guān)限制報警器/* 名稱:開關(guān)限制報警器±±±J+kNTMl叫FD.-lJttEd時.足心 叫&g

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論