PROTEUS仿真100實例_第1頁
PROTEUS仿真100實例_第2頁
PROTEUS仿真100實例_第3頁
PROTEUS仿真100實例_第4頁
PROTEUS仿真100實例_第5頁
已閱讀5頁,還剩72頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、單片機(jī)c語言程序設(shè)計實訓(xùn)100例-基于8051和proteus仿真單片機(jī)c語言程序設(shè)計實訓(xùn)100例基于8051+proteus仿真案例第 01 篇 基礎(chǔ)程序設(shè)計01閃爍的led/* 名稱:閃爍的led說明:led按設(shè)定的時間間隔閃爍*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit led=p10;/延時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個led從左到右循環(huán)依次點亮,產(chǎn)生走馬燈效果*/#include<reg51.h>#include<intrins.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()p0=0xfe;while(1)p0=_crol_(p0,1); /p0的值向左循環(huán)移動delayms(15

3、0);03 8只led左右來回點亮/* 名稱:8只led左右來回點亮說明:程序利用循環(huán)移位函數(shù)_crol_和_cror_形成來回滾動的效果*/#include<reg51.h>#include<intrins.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 i;p2=0x01;while(1)for(i=0;i<7;i+)p2=_crol_(p

4、2,1); /p2的值向左循環(huán)移動delayms(150);for(i=0;i<7;i+)p2=_cror_(p2,1); /p2的值向右循環(huán)移動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;/延時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ù),操作類型變量/延時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;/延時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ù)碼管滾動顯示單個數(shù)字/*名稱:8只數(shù)碼管滾動顯示單個數(shù)字說明:數(shù)碼管從左到右依次滾動顯示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;/延時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ù)碼管動態(tài)顯示多個不同字符電路如上圖/*名稱:8只數(shù)碼管動態(tài)顯示多個不同字符說明:數(shù)碼管動態(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;/延時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ù)字 本例用動態(tài)刷新法顯示一串?dāng)?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;/延時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ù)碼管并延時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,0xf9,0xa4,0xb0,0x

20、99,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)形取法,從第k個開始取第j個p0=0xff;p0=

21、dsy_codenum(k+j)%11; m=_crol_(m,1);p2=m; /發(fā)送位碼delayms(2); k=(k+1)%11;/環(huán)形隊列首支針k遞增,num下標(biāo)范圍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 unsigned char#define uint unsi

22、gned 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)=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按下時led點亮,松開時熄滅, k3、k4按下并釋放時led點亮,再次按下并釋放時熄滅;*/#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;/延時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時遞增點亮一只led,全亮?xí)r再次按下則再次循環(huán)開始, k2按下后點亮上面4只led,k3按下后點亮下面4只led,k4按下后關(guān)閉所有l(wèi)ed*/#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)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所在位置,累加獲取按鍵號kwhile(key_state!=0)k+;key_state>>=1;/根據(jù)按鍵號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時加1計數(shù)并增加顯示位, 按下k2時減1計數(shù)并減少顯示位, 按下k3時清零。*/#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;/延時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計數(shù),按下k2后減1計數(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;/按鍵代碼,按鍵計數(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_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ù)碼管刷新顯示30次,該行代碼同時起到延時作用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)行本例時,按下的按鍵值越大點亮的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;/延時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+)/對4行分別進(jìn)行掃描p1=scode;if(p1&0xf0)!=0xf0)kcode=p1;for(i=0;i<16;i+)/查表得到按鍵序號并返回if(kcode=keycodetablei)return(i);elsescode=_crol_(scode,1); return(-1);/主程序void main()uchar i,p2_led,p3_led;uchar keyno=-1;/按鍵序號,-1表示無按鍵while(1)keyno=

36、keys_scan();/掃描鍵盤獲取按鍵序號keyno if(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; /點亮條形ledp2=p2_led;17 數(shù)碼管顯示4x4矩陣鍵盤按鍵號/*名稱:數(shù)碼管顯示4x4矩陣鍵盤按鍵號說明:按下任意鍵時,數(shù)碼管都會顯示其鍵的序號,掃描程序首先判斷按鍵發(fā)生在哪一列,然后根據(jù)所發(fā)生的行附加不同的值,從而得到按鍵的序號。*/#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)前按鍵的序號,該矩陣中序號范圍015,16表示無按鍵uchar pre_keyno=16,keyno=16;/延時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中一個為0,3個仍為1,通過異或把3個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個為0,三個仍為1;高4位轉(zhuǎn)移到低4位并異或得到改變的值switch(tmp)/對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(); /獲取鍵序號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燈點亮,再次按

41、下時燈熄滅*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit k1=p10;sbit relay=p24;/延時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;/延時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)控制報警器/*名稱:開關(guān)控制報警器說明:用k1開關(guān)控制報警器,程序控制p1.0輸出兩種不同頻率的聲音,模擬很逼真的報警效果*/#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ā)音說明:按下不同的按鍵會是sounder發(fā)出不同頻率的聲音。本例使用延時函數(shù)實現(xiàn)不同頻率的聲音輸出,以后也可使用定時器*/#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;/延時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)行時播放生日快樂歌, 未使用定時器中斷,所有頻率完全用延時實現(xiàn)*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit beep=p37;/生日快樂歌的音符頻率表,不同頻率由不同的延時來決定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é)拍決定每個音符的演奏長短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;/延時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)/播放各個音符,song_long為拍子長度for(j=0;j<song_longi*20;j+)beep=beep;/song_tone延時表決定了每個音符的頻率for(k=0;k<song_tonei/3;k+);delayms(10);i+;void main()beep=0;while(1)playmusic();/播放生日快樂delayms(500);/播放完后暫停一段時間24 int0中斷計數(shù)/*名稱:int0中斷計數(shù)說明:每次按下計數(shù)鍵時觸發(fā)int0中斷,中斷程序累加計數(shù), 計數(shù)值顯示在3只數(shù)碼管上,按下清零鍵時數(shù)碼管清零*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/09的段碼ucha

溫馨提示

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

評論

0/150

提交評論