




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、01/*/單片機(jī) C 語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn) 100例基于 8051+Proteus 仿真案例第 01 篇 基礎(chǔ)程序設(shè)計(jì)閃爍的 LED名稱:閃爍的 LED說明: LED 按設(shè)定的時(shí)間間隔閃爍#include#define uchar unsigned char #define uint unsigned int sbit LED=P10;/延時(shí)void DelayMS(uint x)uchar i; while(x-) for(i=0;i120;i+); /主程序 void main() 02/*while(1)LED=LED;DelayMS(150);從左到右的流水燈名稱:從左到右的流水燈說明:接
2、在 P0口的 8個(gè) LED 從左到右循環(huán)依次點(diǎn)亮, 產(chǎn)生走 馬燈效果*/#include #include #define uchar unsigned char #define uint unsigned int /延時(shí)1 / 76void DelayMS(uint x) uchar i;while(x-) for(i=0;i120;i+); /主程序 void main() P0=0xfe; while(1) P0=_crol_(P0,1); /P0 的值向左循環(huán)移動(dòng) DelayMS(150); 03 8 只 LED 左右來(lái)回點(diǎn)亮/* 名稱: 8 只 LED 左右來(lái)回點(diǎn)亮說明:程序利用循環(huán)
3、移位函數(shù) _crol_ 和_cror_形成來(lái)回滾動(dòng)的效果 */ #include #include #define uchar unsigned char #define uint unsigned int /延時(shí) void DelayMS(uint x) uchar i; while(x-) for(i=0;i120;i+); /主程序 void main() uchar i; P2=0x01; while(1) for(i=0;i7;i+)2 / 76P2=_crol_(P2,1); /P2 的值向左循環(huán)移動(dòng)DelayMS(150);for(i=0;i7;i+)P2=_cror_(P2,1
4、); /P2 的值向右循環(huán)移動(dòng)DelayMS(150);04 花樣流水燈/* 名稱:花樣流水燈 說明: 16 只 LED 分兩組 按預(yù)設(shè)的多種花樣變換顯示 */#include#define uchar unsigned char #define uint unsigned int uchar code Pattern_P0= 0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81
5、,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,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,
6、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,0x00,0xff,0x00,0xff;uchar code Pattern_P2=0xff,0xff,0xff,0xff,0xff,
7、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,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0
8、xff,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,0x80,0x00, 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xf
9、f,0xff,0xff,0xff,0xff, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延時(shí)3 / 76void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()uchar i;while(1)P0 和 P2 口顯示/ 從數(shù)組中讀取數(shù)據(jù)送至for(i=0;i136;i+) P0=Pattern_P0i;P2=Pattern_P2i;DelayMS(100);05 LED 模擬交通燈/* 名稱: LED 模擬交通燈 說明:東西向綠燈亮若干秒,黃 燈閃爍 5 次后紅燈亮, 紅燈亮
10、后, 南 北向由紅燈變?yōu)榫G燈,若干秒后南北 向黃燈閃爍 5 此后變紅燈,東西向變 綠燈,如此重復(fù)。*/#include #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_Count=0,Operation_Type=1; / 閃爍次數(shù),操作類型變量 /延時(shí)void DelayMS(uint x)uch
11、ar i;4 / 76while(x-) for(i=0;i120;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);YELLOW_A=YELLOW_A;GREEN_A=1;if(+Flash_Count!=10) return; / 閃爍
12、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;Flash_Count=0;Operation_Type=1;/主程序void main()while(1
13、) Traffic_Light();06 單只數(shù)碼管循環(huán)顯示 0909 循環(huán)顯示/* 名稱:?jiǎn)沃粩?shù)碼管循環(huán)顯示 09 說明:主程序中的循環(huán)語(yǔ)句反復(fù)將 09 的段碼送至 P0 口,使數(shù)字 */#include5 / 76#include #define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/延時(shí)void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t
14、+);/主程序void main()uchar i=0;P0=0x00;while(1) /* for(;i11;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#include#define uchar unsigned char#define uint unsigned int uchar code DSY_C
15、ODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延時(shí) void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t+); /主程序void main()6 / 76uchar i,wei=0x80;while(1)for(i=0;i8;i+)P2=0xff; / 關(guān)閉顯示 wei=_crol_(wei,1);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è)不同
16、字符 說明:數(shù)碼管動(dòng)態(tài)掃描顯示07。*/#include#include#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;t120;t+);/主程序void main()uchar i,wei=0x80;while(1)for(i=0;i8;i+)P2=0xff;P0=DSY_CODEi; / 發(fā)送段碼
17、wei=_crol_(wei,1);P2=wei;/發(fā)送位碼DelayMS(2);7 / 7609 8 只數(shù)碼管閃爍顯示數(shù)字串電路如上圖/* 名稱: 8 只數(shù)碼管閃爍顯示數(shù)字串 說明:數(shù)碼管閃爍顯示由 07 構(gòu)成的一串?dāng)?shù)字 本例用動(dòng)態(tài)刷新法顯示一串?dāng)?shù)字,在停止刷新時(shí)所有數(shù)字顯示消失。*/#include#define uchar unsigned char#define uint unsigned int/段碼表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /位碼表uchar code DSY_ID
18、X=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;/延時(shí)void DelayMS(uint x)uchar t; while(x-) for(t=0;t120;t+);/主程序void main()uchar i,j; while(1) for(i=0;i30;i+) for(j=0;j8;j+) 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)顯示
19、數(shù)字串 說明:數(shù)碼管向左滾動(dòng)顯示 3 個(gè)字符構(gòu)成的數(shù)字串*/8 / 76#include#include#define uchar unsigned char#define uint unsigned int/段碼表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,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-)
20、 for(t=0;t120;t+);/主程序void main()uchar i,j,k=0,m=0x80;while(1) / 刷新若干次,保持一段時(shí)間的穩(wěn)定顯示for(i=0;i15;i+)for(j=0;j8;j+) / 發(fā)送段碼,采用環(huán)形取法,從第 k 個(gè)開始取第 j 個(gè)P0=0xff;P0=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 移位說明:按
21、下 K1 時(shí), P0 口 LED 上移一位;按下 K2 時(shí), P0 口 LED 下移一位;按下 K3 時(shí), P2 口 LED 上移一位;按下 K4 時(shí), P2 口 LED 下移一位;9 / 76*/#include#include#define uchar unsigned char#define uint unsigned int /延時(shí)void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/ 根據(jù) P1 口的按鍵移動(dòng)LEDvoid Move_LED()if (P1&0x10)=0) P0=_cror_(P0,1); /K1 else i
22、f(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; / 最近按鍵 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)顯
23、示 說明: K1、K2 按下時(shí) LED 點(diǎn)亮,松開時(shí)熄滅,K3、K4 按下并釋放時(shí) LED 點(diǎn)亮,再次按下并釋放時(shí)熄滅;10 / 76*/#include#define uchar unsigned char #define uint unsigned int sbit LED1=P00;sbit LED2=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;i120;i+);/主程序voi
24、d 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 時(shí)遞增點(diǎn)亮一只 LED ,全亮?xí)r再次按下則再次循環(huán)開始,K2 按下后點(diǎn)亮上面 4只LED, K3按下后點(diǎn)亮下面 4只 LED ,K4 按下后關(guān)閉所有 LED11 / 76*/#include#define uchar unsigned char #define
25、 uint unsigned int /延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i4;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; case 4: P0=0xff;12 / 7614 K1-K4 控制數(shù)碼管移位顯示/* 名稱
26、: K1-K4 控制數(shù)碼管移位顯示 說明:按下 K1 時(shí)加 1 計(jì)數(shù)并增加顯示位, 按下 K2 時(shí)減 1 計(jì)數(shù)并減少顯示位, 按下 K3 時(shí)清零。*/#include#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
27、位顯示 0,其他黑屏)uchar Display_Buffer=0,10,10,10,10,10,10,10; /延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);void Show_Count_ON_DSY()uchar i;for(i=0;i8;i+)P0=0xff;P0=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)
28、 Show_Count_ON_DSY();13 / 76/* 名稱: K1-K4 控制數(shù)碼管加減演示 說明:按下 K1 后加 1 計(jì)數(shù),按下 后減 1計(jì)數(shù),按下 K3 后清零。*/#include#include#define uchar unsigned char#define uint unsigned int/段碼K2P1=0xff;Key_NO=P1;/P1 口按鍵狀態(tài)分別為 K1-0xfe , K2-0xfd,K3-0xfbswitch(Key_NO)case 0xfe:Key_Counts+;if(Key_Counts8) Key_Counts=8;Display_BufferKe
29、y_Counts-1=Key_Counts;break;case 0xfd:if(Key_Counts0)Display_Buffer-Key_Counts=10;break;case 0xfb:Display_Buffer0=0; for(i=1;i8;i+) Display_Bufferi=10;Key_Counts=0;/若鍵未釋放則僅刷新顯示,不進(jìn)行鍵掃描while(P1!=0xff) Show_Count_ON_DSY();15 K1-K4 控制數(shù)碼管加減演示uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x8
30、0,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;i120;i+);/顯示函數(shù) void Show_Counts_ON_DSY()uchar i,j=0x01;14 / 76Num_Buffer2=Key_Counts/100;Num_Buffer1=Key_Counts/10%10;Num_Buffer0=Key_Counts%10;for(i=0;i3;i+)j=_cro
31、r_(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;i30;i+) Show_Counts_ON_DSY();switch(Key_Code)case 0xfe:if(Key_Counts0) Key_
32、Counts-; break;case 0xfb:Key_Counts=0;Key_Code=0xff;16 4X4 矩陣鍵盤控制條 形 LED 顯示/* 名稱: 4X4 矩陣鍵盤控 制條形 LED 顯示說明:運(yùn)行本例時(shí),按15 /7615 /76下的按鍵值越大點(diǎn)亮的 LED 越多。*/#include#include#define uchar unsigned char#define uint unsigned int/矩陣鍵盤按鍵特征碼表uchar code KeyCodeTable=0x11,0x12,0x14,0x18,0x21, 0x22,0x24,0x28,0x41,0x42,0x
33、44,0x48,0x81,0x82,0x84,0x88;/延時(shí)void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;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;k4;k+) / 對(duì) 4 行分別進(jìn)行掃描 P1=sCode;if(P1&0xf0)!=0xf0)kCode
34、=P1;for(i=0;i16;i+)/查表得到按鍵序號(hào)并返回if(kCode=KeyCodeTablei)return(i);elsesCode=_crol_(sCode,1);return(-1);/主程序16 / 76void main()uchar i,P2_LED,P3_LED;uchar KeyNo=-1;/ 按鍵序號(hào), -1 表示無(wú)按鍵while(1)KeyNo=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
35、=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 #define uchar unsigned char #define uint unsigned int /段碼 uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
36、, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00;sbit BEEP=P37;/ 上次按鍵和當(dāng)前按鍵的序號(hào),該矩陣中序號(hào)范圍015,16 表示無(wú)按鍵uchar Pre_KeyNo=16,KeyNo=16;/延時(shí) void DelayMS(uint x)17 / 76uchar i;while(x-) for(i=0;i40x0f;/ 按鍵后 f0 變成 XXXX0000 , 異或得到改變的值switch(Tmp)case 1:/ 對(duì) 03 行分別附加起始值 0,4,8,12case 2case 4case 8/蜂鳴器 void Beep() uchar i; for(i
37、=0;i100;i+)DelayMS(1);BEEP=BEEP;BEEP=0;/主程序 void main()KeyNo+=0;break;KeyNo+=4;break;KeyNo+=8;break;KeyNo+=12;18 / 76P0=0x00;BEEP=0;while(1)P1=0xf0;if(P1!=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分別控 制 L
38、ED1 和 LED2 。*/#include 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)亮, 再次 按下時(shí)燈熄滅*/#include#define uchar unsigned char #define uint unsigned int19 / 76sbit K1=P10;sbit RELAY=P24;/延時(shí)void DelayMS(uint ms)uchar t;
39、while(ms-)for(t=0;t120;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è)置的編碼 000255*/#include#include#define uchar unsigned char#define uint unsigned int/各數(shù)字的數(shù)碼管段碼(共陰)uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0
40、x7d,0x07,0x7f,0x6f; /顯示緩沖uchar DSY_Buffer3=0,0,0;/延時(shí)void DelayMS(uint ms)uchar t;while(ms-)for(t=0;t120;t+);/主程序void main()20 / 76uchar 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;i3;i+)/ 刷新顯示在數(shù)碼管上 m=_crol_(m,1); P2=m;
41、P0=DSY_CODEDSY_Bufferi; DelayMS(10);21 開關(guān)控制報(bào)警器/* 名稱:開關(guān)控制報(bào)警器 說明:用 K1 開關(guān)控制報(bào)警器,*/#include#define uchar unsigned char#define uint unsigned intsbit SPK=P10;sbit K1=P17;/發(fā)聲函數(shù) void Alarm(uchar t)uchar i,j;for(i=0;i200;i+)SPK=SPK;for(j=0;jt;j+);由參數(shù) t 行成不同的頻率void main()SPK=0;while(1)程序控制 P1.0 輸出兩種不同頻率的聲音,模擬很
42、逼真的報(bào)警效果21 /if(K1=1)Alarm(90);Alarm(120);22 按鍵發(fā)音/* 名稱:按鍵發(fā)音說明: 按下不同的按鍵會(huì)是 輸出,以后也可使用定時(shí)器 */#include#define uchar unsigned char #define uint unsigned int sbit 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;t120;t+); /按周期 t 發(fā)音void Play(uchar t)ucha
43、r i; for(i=0;i100;i+)BEEP=BEEP; DelayMS(t);BEEP=0;void main()P1=0xff;BEEP=0;while(1)SOUNDER 發(fā)出不同頻率的聲音。 本例使用延時(shí)函數(shù)實(shí)現(xiàn)不同頻率的聲音22 / 76if(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#define uchar unsigned char#define uint unsigned intsbit BEEP=P37;/生日快樂歌的音符頻率表,不同頻率由不同的延時(shí)來(lái)決定uchar c
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 課題申報(bào)書 周以華
- 初中課題立項(xiàng)申報(bào)書
- 廠房委托招商合同范本
- 省級(jí)高校教改課題申報(bào)書
- 醫(yī)療維修托管合同范本
- 咨詢產(chǎn)品服務(wù)合同范本
- 美學(xué)課題申報(bào)書格式要求
- 研究生課題申報(bào)書分工
- 出境加工合同范例
- 關(guān)于郵寄合同范本
- 《特種設(shè)備安全管理員》考試通關(guān)題庫(kù)(600題 含參考答案)
- 醫(yī)院意識(shí)形態(tài)培訓(xùn)課件
- 2025年山東核電有限公司招聘筆試參考題庫(kù)含答案解析
- 2025年醫(yī)院科教工作計(jì)劃
- 《亞洲概況及東亞》課件
- 河北交投物流有限公司所屬公司招聘筆試沖刺題2025
- 2025年上半年江西宜春市事業(yè)單位招聘工作人員651人重點(diǎn)基礎(chǔ)提升(共500題)附帶答案詳解
- 2024-2025學(xué)年高一上學(xué)期期末數(shù)學(xué)試卷(新題型:19題)(基礎(chǔ)篇)(含答案)
- 初三心理健康 教育課件
- 高血壓課件教學(xué)課件
- 《鐵道概論(第二版)》 課件 第8章 鐵路運(yùn)輸組織
評(píng)論
0/150
提交評(píng)論