版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、單片機(jī)C語言程序設(shè)計(jì)實(shí)訓(xùn)100例-基于8051和PROTEUS仿真單片機(jī)C語言程序設(shè)計(jì)實(shí)訓(xùn)100例基于8051+Proteus仿真案例第 01 篇 基礎(chǔ)程序設(shè)計(jì)01閃爍的LED/* 名稱:閃爍的LED說明:LED按設(shè)定的時(shí)間間隔閃爍*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P10;/延時(shí)void DelayMS(uint x)uchar i;while(x-)for(i=0;i<120;i+);/主程序void main()while(1)LED=LED;De
2、layMS(150);02 從左到右的流水燈/* 名稱:從左到右的流水燈說明:接在P0口的8個(gè)LED從左到右循環(huán)依次點(diǎn)亮,產(chǎn)生走馬燈效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/延時(shí)void DelayMS(uint x)uchar i;while(x-)for(i=0;i<120;i+);/主程序void main()P0=0xfe;while(1)P0=_crol_(P0,1); /P0的值向左循環(huán)移動(dòng)DelayMS(15
3、0);03 8只LED左右來回點(diǎn)亮/* 名稱:8只LED左右來回點(diǎn)亮說明:程序利用循環(huán)移位函數(shù)_crol_和_cror_形成來回滾動(dòng)的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/延時(shí)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_(P
4、2,1); /P2的值向左循環(huán)移動(dòng)DelayMS(150);for(i=0;i<7;i+)P2=_cror_(P2,1); /P2的值向右循環(huán)移動(dòng)DelayMS(150);04 花樣流水燈/* 名稱:花樣流水燈說明:16只LED分兩組按預(yù)設(shè)的多種花樣變換顯示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar code Pattern_P0= 0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xf
5、f,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,0xdf,0xbf,0x7f,0xff
6、,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,0xff,0x00,0xff,
7、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, 0x7f,0x3f,0x1f
8、,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,0xe0,0xc0,0
9、x80,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;/延時(shí)void DelayMS(uint x)uchar i;while(x-)for(i=0;i<120;i+);/主程序void main()uchar i;while(1)/從數(shù)組中讀取數(shù)據(jù)送至P0和P2口顯示for(i=0;i<136;i+)P0=Pattern_P0i;P2=Pattern_P2i;DelayMS(
10、100);05 LED模擬交通燈/*名稱:LED模擬交通燈說明:東西向綠燈亮若干秒,黃燈閃爍5次后紅燈亮, 紅燈亮后,南北向由紅燈變?yōu)榫G燈,若干秒后南北向黃燈閃爍5此后變紅燈,東西向變綠燈,如此重復(fù)。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit RED_A=P00; /東西向燈sbit YELLOW_A=P01;sbit GREEN_A=P02;sbit RED_B=P03; /南北向燈sbit YELLOW_B=P04;sbit GREEN_B=P05;uchar Flash
11、_Count=0,Operation_Type=1; /閃爍次數(shù),操作類型變量/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i<120;i+);/交通燈切換void Traffic_Light()switch(Operation_Type)case 1:/東西向綠燈與南北向紅燈亮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);YEL
12、LOW_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_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;Fla
13、sh_Count=0;Operation_Type=1;/主程序void main()while(1) Traffic_Light();06 單只數(shù)碼管循環(huán)顯示09/*名稱:單只數(shù)碼管循環(huán)顯示09說明:主程序中的循環(huán)語句反復(fù)將09的段碼送至P0口,使數(shù)字09循環(huán)顯示*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
14、0x90,0xff;/延時(shí)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(300); /注:另一方案 */P0=DSY_CODEi;i=(i+1)%10;DelayMS(300);07 8只數(shù)碼管滾動(dòng)顯示單個(gè)數(shù)字/*名稱:8只數(shù)碼管滾動(dòng)顯示單個(gè)數(shù)字說明:數(shù)碼管從左到右依次滾動(dòng)顯示07,程序通過每次僅循環(huán)選通一只數(shù)碼管*/#include<reg51.h&
15、gt;#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延時(shí)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
16、);P0=DSY_CODEi; /發(fā)送數(shù)字段碼P2=wei; /發(fā)送位碼DelayMS(300);08 8只數(shù)碼管動(dòng)態(tài)顯示多個(gè)不同字符電路如上圖/*名稱:8只數(shù)碼管動(dòng)態(tài)顯示多個(gè)不同字符說明:數(shù)碼管動(dòng)態(tài)掃描顯示07。*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延時(shí)void DelayMS(uint
17、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ù)碼管閃爍顯示由07構(gòu)成的一串?dāng)?shù)字 本例用動(dòng)態(tài)刷新法顯示一串?dāng)?shù)字,在停止刷新時(shí)所有數(shù)字顯示消失。*/#include<reg51.h>#define uchar unsig
18、ned 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;/延時(shí)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+)for(j=0;j<8;j
19、+)P0=0xff;P0=DSY_CODEj; /發(fā)送段碼P2=DSY_IDXj; /發(fā)送位碼DelayMS(2); P2=0x00; /關(guān)閉所有數(shù)碼管并延時(shí)DelayMS(1000);10 8只數(shù)碼管滾動(dòng)顯示數(shù)字串電路如上圖/*名稱:8只數(shù)碼管滾動(dòng)顯示數(shù)字串說明:數(shù)碼管向左滾動(dòng)顯示3個(gè)字符構(gòu)成的數(shù)字串*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/段碼表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x
20、99,0x92,0x82,0xf8,0x80,0x90,0xff;/下面數(shù)組看作環(huán)形隊(duì)列,顯示從某個(gè)數(shù)開始的8個(gè)數(shù)(10表示黑屏)uchar Num=10,10,10,10,10,10,10,10,2,9,8;/延時(shí)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)/刷新若干次,保持一段時(shí)間的穩(wěn)定顯示for(i=0;i<15;i+)for(j=0;j<8;j+)/發(fā)送段碼,采用環(huán)形取法,從第k個(gè)開始取第j個(gè)P0=0xff;P0=
21、DSY_CODENum(k+j)%11; m=_crol_(m,1);P2=m; /發(fā)送位碼DelayMS(2); k=(k+1)%11;/環(huán)形隊(duì)列首支針k遞增,Num下標(biāo)范圍010,故對(duì)11取余11 K1-K4 控制LED移位/*名稱:K1-K4 控制LED移位說明:按下K1時(shí),P0口LED上移一位; 按下K2時(shí),P0口LED下移一位; 按下K3時(shí),P2口LED上移一位; 按下K4時(shí),P2口LED下移一位;*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsi
22、gned int/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i<120;i+);/根據(jù)P1口的按鍵移動(dòng)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)=0) P2=_crol_(P2,1);/K4/主程序void main()uchar Recent_Key
23、;/最近按鍵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按下時(shí)LED點(diǎn)亮,松開時(shí)熄滅, K3、K4按下并釋放時(shí)LED點(diǎn)亮,再次按下并釋放時(shí)熄滅;*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED1=P00;sbit LED
24、2=P01;sbit LED3=P02;sbit LED4=P03;sbit K1=P10;sbit K2=P11;sbit K3=P12;sbit K4=P13;/延時(shí)void DelayMS(uint x)uchar i;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 分組
25、控制LED說明:每次按下K1時(shí)遞增點(diǎn)亮一只LED,全亮?xí)r再次按下則再次循環(huán)開始, K2按下后點(diǎn)亮上面4只LED,K3按下后點(diǎn)亮下面4只LED,K4按下后關(guān)閉所有LED*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/延時(shí)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)Del
26、ayMS(10);if(t!=P1) continue;/取得4位按鍵值,由模式XXXX1111(X中有一位為0,其他均為1)/變?yōu)槟J?000XXXX(X中有一位為1,其他均為0)Key_State=t>>4;k=0;/檢查1所在位置,累加獲取按鍵號(hào)kwhile(Key_State!=0)k+;Key_State>>=1;/根據(jù)按鍵號(hào)k進(jìn)行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;ca
27、se 4:P0=0xff;14 K1-K4 控制數(shù)碼管移位顯示/*名稱:K1-K4 控制數(shù)碼管移位顯示說明:按下K1時(shí)加1計(jì)數(shù)并增加顯示位, 按下K2時(shí)減1計(jì)數(shù)并減少顯示位, 按下K3時(shí)清零。*/#include<reg51.h>#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,
28、0x04,0x02,0x01;/待顯示到各數(shù)碼管的數(shù)字緩沖(開始僅在0位顯示0,其他黑屏)uchar Display_Buffer=0,10,10,10,10,10,10,10;/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i<120;i+);void Show_Count_ON_DSY()uchar i;for(i=0;i<8;i+)P0=0xff;P0=DSY_CODEDisplay_Bufferi;P2=DSY_Indexi;DelayMS(2);/主程序void main()uchar i,Key_NO,Key_Count
29、s=0;P0=0xff;P1=0xff;P2=0x00;while(1)Show_Count_ON_DSY();P1=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;break;case 0xfb:Displ
30、ay_Buffer0=0;for(i=1;i<8;i+) Display_Bufferi=10;Key_Counts=0;/若鍵未釋放則僅刷新顯示,不進(jìn)行鍵掃描while(P1!=0xff) Show_Count_ON_DSY();15 K1-K4 控制數(shù)碼管加減演示/*名稱:K1-K4 控制數(shù)碼管加減演示說明:按下K1后加1計(jì)數(shù),按下K2后減1計(jì)數(shù),按下K3后清零。*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/段碼uchar c
31、ode DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/待顯示的3位緩沖uchar Num_Buffer=0,0,0;/按鍵代碼,按鍵計(jì)數(shù)uchar Key_Code,Key_Counts=0;/延時(shí)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_Buffer1=Key_Counts/10%10
32、;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í),數(shù)碼管刷新顯示30次,該行代碼同時(shí)起到延時(shí)作用if(Key_Code!=0xff)for(i=0;i<30;i+) Show_Counts_ON_D
33、SY();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顯示/*名稱:4X4矩陣鍵盤控制條形LED顯示說明:運(yùn)行本例時(shí),按下的按鍵值越大點(diǎn)亮的LED越多。*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#
34、define uint unsigned int/矩陣鍵盤按鍵特征碼表uchar code KeyCodeTable=0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88;/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(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)
35、DelayMS(2);if(P1&0xf0)!=0xf0)sCode=0xfe;/行掃描碼初值for(k=0;k<4;k+)/對(duì)4行分別進(jìn)行掃描P1=sCode;if(P1&0xf0)!=0xf0)kCode=P1;for(i=0;i<16;i+)/查表得到按鍵序號(hào)并返回if(kCode=KeyCodeTablei)return(i);elsesCode=_crol_(sCode,1); return(-1);/主程序void main()uchar i,P2_LED,P3_LED;uchar KeyNo=-1;/按鍵序號(hào),-1表示無按鍵while(1)KeyNo=
36、Keys_Scan();/掃描鍵盤獲取按鍵序號(hào)KeyNo if(KeyNo!=-1)P2_LED=0xff;P3_LED=0xff;for(i=0;i<=KeyNo;i+)/鍵值越大,點(diǎn)亮的LED越多if(i<8)P3_LED>>=1;elseP2_LED>>=1;P3=P3_LED; /點(diǎn)亮條形LEDP2=P2_LED;17 數(shù)碼管顯示4X4矩陣鍵盤按鍵號(hào)/*名稱:數(shù)碼管顯示4X4矩陣鍵盤按鍵號(hào)說明:按下任意鍵時(shí),數(shù)碼管都會(huì)顯示其鍵的序號(hào),掃描程序首先判斷按鍵發(fā)生在哪一列,然后根據(jù)所發(fā)生的行附加不同的值,從而得到按鍵的序號(hào)。*/#include<re
37、g51.h>#define uchar unsigned char#define uint unsigned int/段碼uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00;sbit BEEP=P37;/上次按鍵和當(dāng)前按鍵的序號(hào),該矩陣中序號(hào)范圍015,16表示無按鍵uchar Pre_KeyNo=16,KeyNo=16;/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i<12
38、0;i+);/矩陣鍵盤掃描void Keys_Scan()uchar Tmp;P1=0x0f;/高4位置0,放入4行DelayMS(1);Tmp=P10x0f;/按鍵后0f變成0000XXXX,X中一個(gè)為0,3個(gè)仍為1,通過異或把3個(gè)1變?yōu)?,唯一的0變?yōu)?switch(Tmp)/判斷按鍵發(fā)生于03列的哪一列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&
39、gt;>40x0f;/按鍵后f0變成XXXX0000,X中有1個(gè)為0,三個(gè)仍為1;高4位轉(zhuǎn)移到低4位并異或得到改變的值switch(Tmp)/對(duì)03行分別附加起始值0,4,8,12case 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!=
40、0xf0) Keys_Scan(); /獲取鍵序號(hào)if(Pre_KeyNo!=KeyNo)P0=DSY_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=P10;sbit S2=P11;sbit LED1=P00;sbit LED2=P01;/主程序void main()while(1)LED1=S1;LED2=S2;19 繼電器控制照明設(shè)備/*名稱:繼電器控制照明設(shè)備說明:按下K1燈點(diǎn)亮,再次按
41、下時(shí)燈熄滅*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit K1=P10;sbit RELAY=P24;/延時(shí)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ù)碼管顯示撥碼開關(guān)編碼/*名稱:數(shù)碼管顯示撥碼開關(guān)編碼說明:系統(tǒng)顯示撥碼開關(guān)所設(shè)置的
42、編碼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;/顯示緩沖uchar DSY_Buffer3=0,0,0;/延時(shí)void DelayMS(uint ms)uchar t;while(ms-)for(t=0;t<120;t+);/主程序void main(
43、)uchar 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)控制報(bào)警器/*名稱:開關(guān)控制報(bào)警器說明:用K1開關(guān)控制報(bào)警器,程序控制P1.0輸出兩種不同頻率的聲音,模擬很逼真的報(bào)警效果*/#include<reg51.h>#def
44、ine uchar unsigned char#define uint unsigned intsbit SPK=P10;sbit K1=P17;/發(fā)聲函數(shù)void Alarm(uchar t)uchar i,j;for(i=0;i<200;i+)SPK=SPK;for(j=0;j<t;j+);/由參數(shù)t行成不同的頻率void main()SPK=0;while(1)if(K1=1)Alarm(90);Alarm(120);22 按鍵發(fā)音/*名稱:按鍵發(fā)音說明:按下不同的按鍵會(huì)是SOUNDER發(fā)出不同頻率的聲音。本例使用延時(shí)函數(shù)實(shí)現(xiàn)不同頻率的聲音輸出,以后也可使用定時(shí)器*/#inc
45、lude<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P37;sbit K1=P14;sbit K2=P15;sbit K3=P16;sbit K4=P17;/延時(shí)void DelayMS(uint x)uchar t;while(x-) for(t=0;t<120;t+);/按周期t發(fā)音void Play(uchar t)uchar i;for(i=0;i<100;i+)BEEP=BEEP;DelayMS(t);BEEP=0;void main()P1=0xff;BEEP=
46、0;while(1)if(K1=0)Play(1);if(K2=0)Play(2);if(K3=0)Play(3);if(K4=0)Play(4);23 播放音樂/*名稱:播放音樂說明:程序運(yùn)行時(shí)播放生日快樂歌, 未使用定時(shí)器中斷,所有頻率完全用延時(shí)實(shí)現(xiàn)*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P37;/生日快樂歌的音符頻率表,不同頻率由不同的延時(shí)來決定uchar code SONG_TONE=212,212,190,212,159,169,212,212,190
47、,212,142,159,212,212,106,126,159,169,190,119,119,126,159,142,159,0;/生日快樂歌節(jié)拍表,節(jié)拍決定每個(gè)音符的演奏長短uchar code SONG_LONG=9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0;/延時(shí)void DelayMS(uint x)uchar t;while(x-) for(t=0;t<120;t+);/播放函數(shù)void PlayMusic()uint i=0,j,k;while(SONG_LONGi!=0|SONG
48、_TONEi!=0)/播放各個(gè)音符,SONG_LONG為拍子長度for(j=0;j<SONG_LONGi*20;j+)BEEP=BEEP;/SONG_TONE延時(shí)表決定了每個(gè)音符的頻率for(k=0;k<SONG_TONEi/3;k+);DelayMS(10);i+;void main()BEEP=0;while(1)PlayMusic();/播放生日快樂DelayMS(500);/播放完后暫停一段時(shí)間24 INT0中斷計(jì)數(shù)/*名稱:INT0中斷計(jì)數(shù)說明:每次按下計(jì)數(shù)鍵時(shí)觸發(fā)INT0中斷,中斷程序累加計(jì)數(shù), 計(jì)數(shù)值顯示在3只數(shù)碼管上,按下清零鍵時(shí)數(shù)碼管清零*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/09的段碼uc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年初中體育教案設(shè)計(jì)要點(diǎn)
- 南京信息工程大學(xué)《中國現(xiàn)代文學(xué)史Ⅰ》2021-2022學(xué)年第一學(xué)期期末試卷
- 健康促進(jìn)健康教育計(jì)劃
- 2024年機(jī)械員(安裝維修檢測等)專業(yè)技術(shù)知識(shí)考試題庫與答案
- 融入現(xiàn)代技術(shù)的2024版《鐵杵成針》教學(xué)課件
- 污水處理中的廢物排放與減量技術(shù)考核試卷
- 建筑物拆除流程與步驟考核試卷
- 教師招聘考試題庫《教育公共基礎(chǔ)知識(shí)》考點(diǎn)強(qiáng)化練習(xí)(二)-35
- 客戶服務(wù)中的跨文化溝通技巧考核試卷
- 油炸食品制造業(yè)的產(chǎn)品創(chuàng)新與品牌建設(shè)考核試卷
- 貨車車輛定點(diǎn)維修合同協(xié)議書
- 惡性心律失常及常見心律失常識(shí)別與急診處理課件
- 2024 年春國家開放大學(xué)《思想道德與法治》 形考作業(yè)參考答案
- 高標(biāo)準(zhǔn)農(nóng)田項(xiàng)目施工部冬季施工已有設(shè)施和管線的加固保護(hù)等特殊情況下的施工措施
- 填埋場工藝流程設(shè)計(jì)
- 體量與力量雕塑的美感課件高中美術(shù)人美版美術(shù)鑒賞
- 水災(zāi)期間的食品安全措施
- 上下班安全交通培訓(xùn)
- 股骨頭置換術(shù)后護(hù)理查房
- 《招商招租方案》課件
- 第六單元中國特色社會(huì)主義生態(tài)文明建設(shè)及結(jié)語練習(xí)-2023-2024學(xué)年中職高教版(2023)中國特色社會(huì)主義
評(píng)論
0/150
提交評(píng)論