單片機(jī)程序源代碼[共43頁]_第1頁
單片機(jī)程序源代碼[共43頁]_第2頁
單片機(jī)程序源代碼[共43頁]_第3頁
單片機(jī)程序源代碼[共43頁]_第4頁
單片機(jī)程序源代碼[共43頁]_第5頁
已閱讀5頁,還剩38頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、 LED (D0 和 D1 ),編寫程序,實(shí)現(xiàn)兩個(gè)#define uint unsigned int#define uchar unsigned char#define uchar unsigned char D0 和 D7 滅,其次是 D1 和 D6 滅,再是 D2#define uchar unsigned char uint i,j,xx2=0xaa,0xff,yy2=0xf0,0x0f,zz2=0xb6,0xff; LED 數(shù)碼管,編寫程序,使數(shù)碼管循環(huán)顯示從#define uchar unsigned char #define uint unsigned int#define uch

2、ar unsigned chardelay(ms) #define uchar unsigned charcase 0xfe:case 0xfd:case 0xfb:case 0xf7:case 0xef:case 0xdf:case 0xbf:case 0x7f:#define uchar unsigned char uchar i,j;P2=0xf0;temp=P2;temp&=0xf0;switch(temp)case 0xe0:num=1;break;case 0xd0:num=2;break;case 0xb0:num=3;break;case 0x70:num=4;switch(t

3、emp)case 0xe0:num=5;break;case 0xd0:num=6;break;case 0xb0:num=7;break;case 0x70:num=8;case 0xe0:num=9;break; if(num=0)P0=0xbf;#define uchar unsigned char#define uint unsigned intsbit LED1=P20;sbit LED2=P21; uchar code LED_code=uchar i,j;uchar temp;P1=0xfe;/key1-key4 第一行按下temp=P1;switch(temp) switch(

4、temp) switch(temp)temp=P1;switch(temp) 時(shí),8 位數(shù)碼管均顯示 “-”,每按下一個(gè)號碼后,原位。8 位 LED 數(shù)碼管顯示的內(nèi)容向左移動#define uint unsigned int#define uchar unsigned charuchar keycount=0,num;uchar code LED1_code= uchar LED2_wei8= 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01;uchar i,j;switch(temp)case 0xee:keycount+;num=0;break;case 0x

5、de:keycount+;num=1;break;case 0xbe:keycount+;num=2;break;case 0x7e:keycount+;num=3;break; temp=P3;switch(temp)case 0xed:keycount+;num=4;break;case 0xdd:keycount+;num=5;break;case 0xbd:keycount+;num=6;break;case 0x7d:keycount+;num=7;break; switch(temp)case 0xeb:keycount+;num=8;break;temp=P3;switch(te

6、mp) for(i=0;i7;i+)LED2_duani=LED2_duani+1;后,8 只 LED 暫停默認(rèn)顯示,而進(jìn)行閃爍顯示,當(dāng)顯示達(dá)到5 次后,繼續(xù)按默認(rèn)規(guī)律顯示。#define uint unsigned int#define uchar unsigned charvoid LED_shan() interrupt 0 P0 端口作為輸出口,外接條形2 次按下按鍵 K 后,LED /將 led 值映射到 LED 燈delayms(500); /延時(shí) 500msvoid delayms(unsigned int ms)EX0 = 1; / 打開外部中斷 0EA = 1; /打開總中斷

7、t = 0; #include/ 頭文件#define uchar unsigned char#define uint unsigned intuchar temp,key_count;uchar code tab= case 0:loop_leftmove();break;case 1:loop_rightmove();break;case 2:open_close();break;default:break;void key_interrupt() interrupt 0/ 外部中斷 INT0 中斷服務(wù)程序key_count+;key_count%=3;/*if(key_count=3)k

8、ey_count=0;*/P0 端口作為輸出口,外接#define uchar unsigned char#define uint unsigned intsbit key1=P32;sbit key2=P33;sbit beep=P10; EX0=1;/ 打開外部中斷 INT0EX1=1;/ 打開外部中斷 INT1 switch(key_count%2)case 1:left_right_move();break;/奇數(shù)次按鍵左移右移顯示case 0:break;/偶數(shù)次按鍵拉幕式顯示 P0 端口作為輸出口,外接#define uchar unsigned char #define ucha

9、r unsigned charuchar key1=0,key2=0,m,n,k;uchar table=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f; 使用單片機(jī) P0、P2 端口作為輸出口,外接一個(gè)#include/ 頭文件shi=count/10;/ 分離十位ge=count%10;/ 分離個(gè)位P2=tablege;/ 顯示個(gè)位P0=tableshi;/ 顯示十位 #include/ 頭文件void delay(uint ms)/ 延時(shí)子函數(shù) 使用單片機(jī) P0、P2 端口作為輸出口,外接一個(gè)K2 時(shí),暫停加 1 計(jì)數(shù),按鍵 K13 次按下按鍵 K2 時(shí),

10、LED 數(shù)碼管顯示為 0;第 4 TMOD=0x05;/ 設(shè)置工作方式為 1 選擇計(jì)數(shù)效用TH0=0xff; #define uchar unsigned char #define uchar unsigned char任務(wù)三:用 T2 實(shí)現(xiàn)流水燈控制#include/ 頭文件 ET2=1;/ 打開定時(shí)器 2 中斷TR2=1;/ 啟動定時(shí)器 2init();/ 調(diào)用初始化函數(shù)while(1);void Timer2() interrupt 5 /T2 定時(shí)器的中斷服務(wù)子程序TF2=0; /溢出標(biāo)志位軟件清零#include#define uchar unsigned char#define u

11、int unsigned intuchar code table= ET0=1;/ 打開定時(shí)器 0 中斷TR0=1; / 啟動定時(shí)器 0while(1) /循環(huán)display(); / 掃描數(shù)碼管 任務(wù)五: LED 滾動閃爍控制#define uchar unsigned charuchar table15=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe;uint count,i,num;void int_int()EX0=1;/ 允許外部中斷EX1=1;void key1_1() inter

12、rupt 0num=num-2000;if(num=2000)num=2000;void key2_2() interrupt 2void timer_0() interrupt 1 #include#define uchar unsigned char#define uint unsigned int#define a2#define e2#define T 1000sbit key=P37;/ 880.000Hz a2/ 659.255Hz e2/定義音符結(jié)構(gòu)/發(fā)聲頻率(對應(yīng)的定時(shí)器初值)/發(fā)聲時(shí)值uchar ReloadH;uchar ReloadL;TMOD=0x01;ET0=1; v

13、oid sound(CNote *note) SPEAKER=1;void inter() interrupt 1#include/ 頭文件#define uint unsigned intTMOD=0x05;/ 設(shè)置計(jì)數(shù)器 0 工作方式 1ET0=1; / 打開計(jì)數(shù)器 0 中斷TR0=1;/ 啟動定時(shí)器 0void beep_interrupt() interrupt 1 / 計(jì)數(shù)器 0 中斷服務(wù)子程序 #include#define uint unsigned int#define uchar unsigned charuchar table15=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe;uint count,i,num;void int_int()EX0=1;/ 允許外部中斷EX1=1;void key1_1() interrupt 0

溫馨提示

  • 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論