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

下載本文檔

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

文檔簡(jiǎn)介

1、頁眉內(nèi)容第二章任務(wù)一:閃爍廣告燈的設(shè)計(jì)利用89c51單片機(jī)的端口控制兩個(gè)LED(D0和D1),編寫程序,實(shí)現(xiàn)兩個(gè)LED互閃。#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharsbitLED1=P0A0;sbitLED2=P0A1;voiddelayms(uintms)uinti;while(ms-)for(i=0;i<120;i+);voidmain()while(1)LED1=0;LED2=1;delayms(500);LED1=1;LED2=0;delayms(500);任務(wù)二:流水廣告燈的設(shè)計(jì)利

2、用89c51單片機(jī)的端口控制8個(gè)LED(D0D7)循環(huán)點(diǎn)亮,剛開始時(shí)D0點(diǎn)亮,延時(shí)片刻后,接著D1點(diǎn)亮,然后依次點(diǎn)亮D2->D3->D4->D5->D6->D7,然后再點(diǎn)亮D7->D6->D5->D4->D3->D2->D1->D0,重復(fù)循環(huán)。#include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedcharuinti;uchartemp;uinta8=0xfe,0xfd,0xfb,0xf7,0xef,

3、0xdf,0xbf,0x7f;voiddelayms(uintms)while(ms-)uintj;for(j=0;j<120;j+);voidmain()while(1)P0=0xfe;while(P0!=0x7f)/P1=temp;/delayms(500);P0=_crol_(P0,1);delayms(250);temp=0x7f;while(P0!=0xfe)P0=temp;delayms(500);temp=_cror_(temp,1);任務(wù)三:拉幕式與閉幕式廣告燈的設(shè)計(jì)利用89c51單片機(jī)的P0端口實(shí)現(xiàn)8個(gè)LEDD0D7的拉幕式與閉幕式廣告燈設(shè)計(jì)。拉幕式:開始D0D7全滅,

4、延時(shí)片刻后首先D3和D4亮,其次是D2和D5亮,再是D1和D6亮,最后是D0和D7亮。閉幕式:開始D0D7全亮,延時(shí)片刻后首先D0和D7滅,其次是D1和D6滅,再是D2和D5滅,最后是D3和D4滅。#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharucharcodetable=0xe7,0xc3,0x81,0x00;voiddelay(uintx)uinti,j;for(i=x;i>0;i-)for(j=120;j>0;j-);voidmain()uinti;while(1)P1=0xff;de

5、lay(500);for(i=0;i<4;i+)P1=tablei;delay(500);for(i=3;i>0;i-)P1=tablei-1;delay(500);任務(wù)四:復(fù)雜廣告燈的設(shè)計(jì)利用89c51單片機(jī)的端口實(shí)現(xiàn)8個(gè)LED(D0D7)復(fù)雜廣告燈的控制,要求顯示規(guī)律為:正向流水->反向流水->隔燈閃爍3次->高四盞、低四盞閃爍2次->隔兩盞閃爍3次,再重復(fù)循環(huán)。#include<reg52.h>#defineuintunsignedint#include<intrins.h>voiddelayms(uintms)uinti;wh

6、ile(ms-)for(i=0;i<110;i+);voidmain()uinti,j,xx2=0xaa,0xff,yy2=0xf0,0x0f,zz2=0xb6,0xff;unsignedcharaa;while(1)aa=0xfe;for(i=0;i<7;i+)P0=aa;aa=_crol_(aa,1);delayms(500);for(i=0;i<8;i+)P0=aa;aa=_cror_(aa,1);delayms(500);for(i=0;i<3;i+)for(j=0;j<2;j+)P0=xxj;delayms(500);for(i=0;i<2;i+)

7、for(j=0;j<2;j+)P0=yyj;delayms(500);for(i=0;i<3;i+)for(j=0;j<2;j+)P0=zzj;delayms(500);任務(wù)五:?jiǎn)蝹€(gè)LED數(shù)碼管顯示設(shè)計(jì)09使用89c51的P0口作為輸出口,外接LED數(shù)碼管,編寫程序,使數(shù)碼管循環(huán)顯示從的加1計(jì)數(shù)。#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharuchartable10=0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90;uintkey=0;

8、voidint_int()EA=1;IT0=1;EX0=1;voidcount_num()interrupt0if(key=9)key=0;elsekey+;voidmain()int_int();while(1)P0=tablekey;任務(wù)六:多個(gè)LED數(shù)碼管顯示設(shè)計(jì)使用單片機(jī)P0和P2口作為輸出口,外接一個(gè)8位LED數(shù)碼管,編寫程序,使數(shù)碼管顯示“872AF635?!?include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedchardelay(ms)while(ms-)

9、inti;for(i=0;i<110;i+);main()uchara16=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;ucharb8=0x08,0x07,0x02,0x0a,0x0f,0x06,0x03,0x05,j;uinti;while(1)j=0x01;for(i=0;i<8;i+)P2=j;P0=abi;j=_crol_(j,1);delay(3);任務(wù)七:查詢式按鍵設(shè)計(jì)將8個(gè)按鍵從18進(jìn)行編號(hào),如果其中一個(gè)鍵被按下,則在LED數(shù)碼管上顯示相應(yīng)的鍵值。#in

10、clude<reg52.h>#defineuintunsignedint#defineucharunsignedcharmain()uchara16=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;P0=0xff;while(P0!=0xff)switch(P0)case0xfe:P2=a1;break;case0xfd:P2=a2;break;case0xfb:P2=a3;break;case0xf7:P2=a4;break;case0xef:P2=a5;break;c

11、ase0xdf:P2=a6;break;case0xbf:P2=a7;break;case0x7f:P2=a8;任務(wù)八:LED數(shù)碼管顯示矩陣鍵盤按鍵的設(shè)計(jì)設(shè)計(jì)一個(gè)4*4的矩陣鍵盤,以P1.0P1.3作為行線,以P1.4P1.7作為列線。要求:未按下按鍵時(shí),LED數(shù)碼管顯示“-”,按下按鍵時(shí),在數(shù)碼管上顯示相應(yīng)的鍵值。#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintuchartable17=0xbf,0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88

12、,0x83,0xc6,0xa1,0x86,0x8e;uintnum=0;voiddelay(uintx)uchari,j;for(i=x;i>0;i-)for(j=120;j>0;j-);keyscan()uinttemp;temp=P2;temp&=0x0f;if(temp=0x0e)P2=0xf0;temp=P2;temp&=0xf0;switch(temp)case0xe0:num=1;break;case0xd0:num=2;break;case0xb0:num=3;break;case0x70:num=4;if(temp=0x0d)P2=0xf0;temp

13、=P2;temp&=0xf0;switch(temp)case0xe0:num=5;break;case0xd0:num=6;break;case0xb0:num=7;break;case0x70:num=8;if(temp=0x0b)P2=0xf0;temp=P2;temp&=0xf0;switch(temp)case0xe0:num=9;break;case0xd0:num=10;break;case0xb0:num=11;break;case0x70:num=12;if(temp=0x07)P2=0xf0;temp=P2;temp&=0xf0;switch(tem

14、p)case0xe0:num=13;break;case0xd0:num=14;break;case0xb0:num=15;break;case0x70:num=16;returnnum;voiddisplay(uintnum)if(num=0)P0=0xbf;P0=tablenum;main()uinttemp;while(1)P2=0x0f;temp=P2;if(temp!=0x0f)keyscan();display(num);(2)#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintsbitLED1=

15、P2A0;sbitLED2=P2A1;ucharnum;ucharcodeLED_code=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;/數(shù)碼管字形碼編碼voiddelay(uintx)uchari,j;for(i=x;i>0;i-)for(j=120;j>0;j-);/延時(shí)函數(shù)voiddisplay(ucharnum)P0=LED_codenum;/數(shù)碼管顯示函數(shù)voidkeyscan()/鍵盤掃描函數(shù)uchartemp;P1=0xfe;/key1-key4第一行按

16、下temp=P1;temp&=0xf0;if(temp!=0xf0)delay(10);/消除抖動(dòng)temp=P1;temp&=0xf0;if(temp!=0xf0)temp=P1;switch(temp)case0xee:num=1;break;case0xde:num=2;break;case0xbe:num=3;break;case0x7e:num=4;break;while(temp!=0xf0)/等待按鍵釋放temp=P1;temp&=0xf0;P1=0xfd;/key5-key8第二行按下temp=P1;temp&=0xf0;if(temp!=0xf0

17、)delay(10);/消除抖動(dòng)temp=P1;temp&=0xf0;if(temp!=0xf0)temp=P1;switch(temp)case0xed:num=5;break;case0xdd:num=6;break;case0xbd:num=7;break;case0x7d:num=8;break;while(temp!=0xf0)/等待按鍵釋放temp=P1;temp&=0xf0;P1=0xfb;/key9-key12第三行按下temp=P1;temp&=0xf0;if(temp!=0xf0)delay(10);/消除抖動(dòng)temp=P1;temp&=0x

18、f0;if(temp!=0xf0)temp=P1;switch(temp)case0xeb:num=9;break;case0xdb:num=10;break;case0xbb:num=11;break;case0x7b:num=12;break;while(temp!=0xf0)/等待按鍵釋放temp=P1;temp&=0xf0;P1=0xf7;/key13-key16第四行按下temp=P1;temp&=0xf0;if(temp!=0xf0)delay(10);/消除抖動(dòng)temp=P1;temp&=0xf0;if(temp!=0xf0)temp=P1;switch(

19、temp)case0xe7:num=13;break;case0xd7:num=14;break;case0xb7:num=15;break;case0x77:num=16;break;while(temp!=0xf0)/等待按鍵釋放temp=P1;temp&=0xf0;voidmain()while(1)keyscan();if(num=0)P0=0xbf;elsedisplay(num-1);任務(wù)九:簡(jiǎn)單撥號(hào)鍵盤的設(shè)計(jì)使用4*4的矩陣式鍵盤及8位共陽極LED數(shù)碼管設(shè)計(jì)一個(gè)簡(jiǎn)單撥號(hào)鍵盤,要求在初始狀態(tài)時(shí),8位數(shù)碼管均顯示“-”,每按下一個(gè)號(hào)碼后,原8位LED數(shù)碼管顯示的內(nèi)容向左移動(dòng)位

20、。#include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedcharucharkeycount=0,num;ucharcodeLED1_code=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;/數(shù)碼管字形碼編碼ucharLED2_duan8=0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf;/8段共陽數(shù)碼管編碼ucharLED2_w

21、ei8=0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01;voiddelay(uintx)uchari,j;for(i=x;i>0;i-)for(j=120;j>0;j-);voiddisplay()uchari;/P2=0x80;for(i=0;i<8;i+)P2=LED2_weii;P0=LED2_duan7-i;delay(1);voidkeyscan()uchartemp;P3=0xfe;temp=P3;temp&=0xf0;if(temp!=0xf0)delay(10);temp=P3;temp&=0xf0;if(tem

22、p!=0xf0)temp=P3;switch(temp)case0xee:keycount+;num=0;break;case0xde:keycount+;num=1;break;case0xbe:keycount+;num=2;break;case0x7e:keycount+;num=3;break;while(temp!=0xf0)temp=P3;temp&=0xf0;P3=0xfd;temp=P3;temp&=0xf0;if(temp!=0xf0)delay(10);temp=P3;temp&=0xf0;if(temp!=0xf0)temp=P3;switch(t

23、emp)case0xed:keycount+;num=4;break;case0xdd:keycount+;num=5;break;case0xbd:keycount+;num=6;break;case0x7d:keycount+;num=7;break;while(temp!=0xf0)temp=P3;temp&=0xf0;P3=0xfb;temp=P3;temp&=0xf0;if(temp!=0xf0)delay(10);temp=P3;temp&=0xf0;if(temp!=0xf0)temp=P3;switch(temp)case0xeb:keycount+;n

24、um=8;break;case0xdb:keycount+;num=9;break;case0xbb:keycount+;num=10;break;case0x7b:keycount+;num=11;break;while(temp!=0xf0)temp=P3;temp&=0xf0;P3=0xf7;temp=P3;temp&=0xf0;if(temp!=0xf0)delay(10);temp=P3;temp&=0xf0;if(temp!=0xf0)temp=P3;switch(temp)case0xe7:keycount+;num=12;break;case0xd7:k

25、eycount+;num=13;break;case0xb7:keycount+;num=14;break;case0x77:keycount+;num=15;break;while(temp!=0xf0)temp=P3;temp&=0xf0;voidmain()uchari;while(1)display();keyscan();if(keycount!=0)for(i=0;i<7;i+)LED2_duani=LED2_duani+1;LED2_duan7=LED1_codenum;keycount=0;第五章任務(wù)一:帶中斷控制的LED花樣燈P3.2 外接按鍵 K。 編寫程序,

26、 當(dāng) K 未按下時(shí), 8只 LED->右移流水-> 隔燈閃爍3 次花樣顯示; 如果按下 K 鍵5 次后,繼續(xù)按默認(rèn)規(guī)律顯示。P0端口作為輸出口,外接8只LED,進(jìn)行默認(rèn)顯示,其顯示規(guī)律為左移流水后,8只LED暫停默認(rèn)顯示,而進(jìn)行閃爍顯示,當(dāng)顯示達(dá)到#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharuintLED_table14=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd;voiddelayms(uin

27、tms)uinti;while(ms-)for(i=0;i<120;i+);voidint_int()EA=1;EX0=1;IT0=1;voidLED_shan()interrupt0uintk;for(k=0;k<5;k+)P0=0xaa;delayms(500);P0=0xff;delayms(500);voidmain()int_int();while(1)uinti;for(i=0;i<14;i+)P0=LED_tablei;delayms(500);for(i=0;i<3;i+)P0=0xaa;delayms(500);P0=0xff;delayms(500)

28、;任務(wù)二:采用外中斷控制的條形LED彩燈設(shè)計(jì)P0端口作為輸出口,外接條形LED。編寫程序,當(dāng)按鍵K未按下時(shí),LED進(jìn)行循環(huán)左移顯示。如果第1次按下按鍵K后,LED進(jìn)行循環(huán)右移顯示。如果第2次按下按鍵K后,LED進(jìn)行拉幕式與閉幕式花樣顯示。如果第3次按下按鍵K后,又恢復(fù)循環(huán)左移顯示。#include<reg52.h>unsignedcharflag=0;/狀態(tài)標(biāo)志位unsignedcharled=0;/LED映射位unsignedchartable=/花樣數(shù)組0x00,0x81,0xc3,0xe7,0xff,0xe7,0xc3,0x81;/先閉幕式再開幕式voiddelayms(un

29、signedintms);/延時(shí)函數(shù)voidsysInit(void);/系統(tǒng)初始化voidstep0(void);/狀態(tài)0voidstep1(void);/狀態(tài)1voidstep2(void);/狀態(tài)2voidmain(void)sysInit();/初始化系統(tǒng)while(1)switch(flag)case0:step0();break;case 1: step1();break;case 2: step2();break;default:break;P0=led;/將led值映射到LED燈delayms(500);/延時(shí)500msvoiddelayms(unsignedintms)uns

30、ignedchari;for(;ms;ms-)for(i=110;i;i-);voidsysInit(void)EX0=1;/打開外部中斷0EA=1;/打開總中斷IT0=1;/外部中斷0下降沿觸發(fā)voidstep0(void)led<<=1;/左移if(led=0)/溢出處理led=0x01;voidstep1(void)led>>=1;/右移if(led=0)/溢出處理led=0x80;voidstep2(void)staticchart;led=tablet+;/開幕閉幕花樣顯示if(t=8)t=0;voidEX_0()interrupt0/外部中斷0服務(wù)函數(shù)fla

31、g+;if(flag=3)flag=0;(2)#include<reg52.h>/頭文件#defineucharunsignedchar#defineuintunsignedint/宏定義uchartemp,key_count;ucharcodetab=/開幕式閉幕式編碼0x00,0x81,0xc3,0xe7,0xff,0xe7,0xc3,0x81;voiddelay(uintms)/延時(shí)子函數(shù)uinti,j;for(i=ms;i>0;i-)for(j=0;j<110;j+);voidloop_leftmove()temp=temp<<1;if(temp=0

32、)temp=0x01;voidloop_rightmove()temp=temp>>1;if(temp=0)temp=0x80;voidopen_close()ucharcount;temp=tabcount;count+;if(count>=8)count=0;voidinit()EA=1;EX0=1;IT0=1;temp=0;key_count=0;voidmain()init();while(1)switch(key_count)case0:loop_leftmove();break;case1:loop_rightmove();break;case2:open_clo

33、se();break;default:break;P0=temp;delay(250);voidkey_interrupt()interrupt0/外部中斷INT0中斷服務(wù)程序key_count+;key_count%=3;/*if(key_count=3)key_count=0;*/任務(wù)三:采用兩個(gè)外中斷的LED彩燈控制P0端口作為輸出口,外接8只LED,P3.2外接按鍵K1,P3.3外接按鍵K2。編寫程序,沒有按下任何按鍵時(shí),LED隔燈閃爍,當(dāng)按鍵K1為奇數(shù)次時(shí),LED進(jìn)行左移流水->右移流水顯示。當(dāng)按下按鍵K1為偶數(shù)次時(shí),LED進(jìn)行拉幕式顯示;當(dāng)按下按鍵K2時(shí),LED暫停顯示,蜂鳴

34、器發(fā)出“叮咚”的開門聲音,然后LED繼續(xù)花樣顯示(K2按鍵優(yōu)先)。#include<reg52.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedintsbitkey1=P3A2;sbitkey2=P3A3;sbitbeep=P1A0;ucharcodetab=0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7;/開幕式編碼uchartemp=0x01,key_count,flag,right,beep_FG;voiddelay(uintms)uinti,j;for

35、(i=ms;i>0;i-)for(j=110;j>0;j-);voidinit()EA=1;/打開總中斷EX0=1;/打開外部中斷INT0EX1=1;/打開外部中斷INT1IT0=1;/下降沿觸發(fā)方式IT1=1;/下降沿觸發(fā)方式PX0=0;/設(shè)置INT0為低中斷優(yōu)先級(jí)PX1=1;/設(shè)置INT1為高中斷優(yōu)先級(jí)voidinterval_LED()P0=0xff;delay(500);P0=0xaa;delay(500);voidleft_right_move()if(!right)temp=_crol_(temp,1);elsetemp=_cror_(temp,1);if(temp=0

36、x01)right=0;if(temp=0x80)right=1;voidopen_LED()ucharcount;temp=tabcount+;if(count=8)count=0;voidBeep()uchari,j,k;for(i=100;i;i-)for(j=20;j;j-)for(k=1000;k;k-)beep=beep;/*for(i=100;i;i-)for(j=200;j;j-)for(k=1000;k;k-)beep=beep;*/beep_FG=0;voidmain()init();beep=0;while(1)if(!flag)interval_LED();elseif

37、(beep_FG)Beep();elseswitch(key_count%2)case1:left_right_move();break;奇數(shù)次按鍵左移右移顯示case0:open_LED();break;偶數(shù)次按鍵拉幕式顯示default:break;P0=temp;delay(400);voidkey1_interrupt()interrupt0flag=1;key_count+;voidkey2_interrupt()interrupt2flag=1;beep_FG=1;任務(wù)四:采用兩個(gè)外中斷實(shí)現(xiàn)LED鍵控移位P0端口作為輸出口,外接8只LED,P3.2外接按鍵K1,P3.3外接按鍵K2

38、。編寫程序,要求實(shí)現(xiàn)LED鍵控移位,按下按鍵K1時(shí),8只LED左移1位;按下按鍵K2時(shí),8只LED右移1位。#include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedcharucharkey1=0,key2=0,temp1=0,temp2=0,temp11=0,temp12=0;/*voiddelayms(uintms)uinti;while(ms-)for(i=0;i<120;i+);*/voidint_int()EA=1;EX0=1;EX1=1;IT0=1;IT1

39、=1;voidint0()interrupt0key1+;voidint1()interrupt2key2+;voidmain()uchara;int_int();a=0xfe;while(1)if(temp1!=key1)P0=a;a=_crol_(a,1);temp1=key1;if(temp2!=key2)P0=a;a=_cror_(a,1);key2=temp2;(2)#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharucharkey1=0,key2=0,m,n,k;uchartable=0xfe,

40、0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;voidint_int()EA=1;EX0=1;IT0=1;EX1=1;IT1=1;voidint0()interrupt0key1+;m=key1%8;voidint2()interrupt2key2+;n=key2%8;voidmain()int_int();while(1)k=(m-n+8)%8;P0=tablek;任務(wù)五:采用外中斷實(shí)現(xiàn)計(jì)數(shù)使用單片機(jī)P0、P2端口作為輸出口,外接一個(gè)2位LED數(shù)碼管,P3.2外接按鍵K1,P3.3頁眉內(nèi)容K2外接按鍵K2。編寫程序,每次按下K1時(shí),LED數(shù)碼管進(jìn)行加1顯示;每次按下按

41、鍵時(shí),LED數(shù)碼管進(jìn)行減1顯示,計(jì)數(shù)范圍為099。#include<reg52.h>/頭文件#defineuintunsignedint/宏定義#defineucharunsignedchar/宏定義uchartable=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/0-9共陽字型碼編碼charcount=0;ucharshi,ge;voidLED_scan()/數(shù)碼管掃描子函數(shù)shi=count/10;/分離十位ge=count%10;/分離個(gè)位P2=tablege;/顯示個(gè)位P0=tableshi;/顯示十位voidint

42、_int()/初始化EA=1;/打開總中斷EX0=1;/打開外部中斷0EX1=1;/打開外部中斷1IT0=1;/設(shè)置下降沿觸發(fā)方式IT1=1;/設(shè)置下降沿觸發(fā)方式voidmain()/主函數(shù)int_int();/調(diào)用初始化函數(shù)while(1)LED_scan();/調(diào)用數(shù)碼管掃描函數(shù)voidint0()interrupt0/外部中斷0服務(wù)程序count+;if(count=100)count=0;voidint2()interrupt2count-;if(count<0)count=99;/計(jì)數(shù)加1/溢出操作/外部中斷1服務(wù)程序/計(jì)數(shù)減1/溢出操作頁眉內(nèi)容(2)#include<r

43、eg52.h>/頭文件#defineuintunsignedint/宏定義#defineucharunsignedchar/宏定義uchartable=0xc0,0xf9,0xa4,0xb0,0x99,共陽字型碼編碼0x92,0x82,0xf8,0x80,0x90;/0-9charcount=0;ucharshi,ge;voiddelay(uintms)/延時(shí)子函數(shù)uchari;for(;ms;ms-)for(i=120;i;i-);voidLED_scan()/數(shù)碼管掃描子函數(shù)shi=count/10;/分離十位ge=count%10;/分離個(gè)位P2=0x01;/個(gè)位對(duì)應(yīng)的數(shù)碼管點(diǎn)亮P

44、0=tablege;/顯示個(gè)位delay(1);P2=0x02;/十位對(duì)應(yīng)的數(shù)碼管點(diǎn)亮P0=tableshi;/顯示十位delay(1);voidint_int()/初始化EA=1;/打開總中斷EX0=1;/打開外部中斷0EX1=1;/打開外部中斷1IT0=1;/設(shè)置下降沿觸發(fā)方式IT1=1;/設(shè)置下降沿觸發(fā)方式voidmain()/主函數(shù)int_int();/調(diào)用初始化函數(shù)while(1)LED_scan();/調(diào)用數(shù)碼管掃描函數(shù)voidint0()interrupt0/外部中斷0服務(wù)程序count+;/計(jì)數(shù)加1頁眉內(nèi)容/溢出操作/ 外部中斷1 服務(wù)程序/計(jì)數(shù)減1/溢出操作if(count=

45、100)count=0;voidint2()interrupt2count-;if(count<0)count=99;任務(wù)六:采用外中斷進(jìn)行計(jì)數(shù)和清零控制使用單片機(jī)P0、P2端口作為輸出口,外接一個(gè)2位LED數(shù)碼管,P3.2外接按鍵K1,P3.3外接按鍵K2。編寫程序,按鍵K2作為“加1/暫停/清零”控制;按鍵K1作為加1計(jì)數(shù)控制。當(dāng)按鍵K2沒有按下時(shí),按鍵K1按下后LED數(shù)碼管總顯示為0;第1次按下鍵K2時(shí),每次按下K1,LED數(shù)碼管進(jìn)行加1顯示;第二次按下按鍵K2時(shí),暫停加1計(jì)數(shù),按鍵K1按下后LED數(shù)碼管顯示暫停前的數(shù)值;第3次按下按鍵K2時(shí),LED數(shù)碼管顯示為0;第4次按下按鍵K

46、2時(shí),與第1次類似。#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharucharkey1=0,temp1=0,key2=0,temp2=0,key=0;uchartable=0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90;voiddelayms(uintms)while(ms-)uinti=0;for(i;i<120;i+);voidint_int()EA=1;EX0=1;EX1=1;IT0=1;IT1=1;voidint1()interrupt0k

47、ey2+;void int29() interrupt 2頁眉內(nèi)容key1+;voiddisplay()P2=0xfe;P0=tabletemp1;delayms(2);P2=0xfd;P0=tabletemp2;delayms(2);voidmain()int_int();while(1)temp1=key1%10;temp2=key1/10;switch(key2%3)case0:while(1)if(key2%3!=0)break;P2=0xfe;P0=table0;delayms(2);P2=0xfd;P0=table0;delayms(2);key1=0;break;case 1:

48、display();break;case 2: key=key1;while(1)if(key2%3!=2)break;P2=0xfe;P0=tabletemp1;delayms(2);void display()P2=0xfd;P0=tabletemp2;delayms(2);key1=key;break;第六章任務(wù)一:簡(jiǎn)單計(jì)數(shù)器的設(shè)計(jì)使用單片機(jī)P0,P2端口作為輸出口,外接一個(gè)2位LED數(shù)碼管,P3.4外接按鍵K。編寫程序,每次按鍵K按下時(shí)LED數(shù)碼管進(jìn)行加1顯示,計(jì)數(shù)范圍為099。#include<reg52.h>#defineuintunsignedint#defineuc

49、harunsignedcharuchartable=0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90;ucharkey=0;voiddelayms(uintms)uinti;while(ms-)for(i=0;i<120;i+);voidint_int()EA=1;ET0=1;/開定時(shí)器0中斷TR0=1;/啟動(dòng)定時(shí)器0TMOD=0x05;/設(shè)置工作方式為1選擇計(jì)數(shù)功能TH0=0xff;TL0=0xff;/裝初值使得按一下就中斷voidint1()interrupt1TH0=0xff;TL0=0xff;key+;P2=0xfe;P0=tab

50、lekey%10;delayms(3);P2=0xfd;P0=tablekey/10;delayms(3);voidmain()int_int();while(1)display();if(key%10=9&key/10=9)key=0;(2)#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharuchartable=0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90;num1=0;num2=0;voiddelay(uintms)uinti;while(ms-)for(i=0;i<120;i+);voidint_int()EA=1;ET0=1;/開定時(shí)器0中斷TR0=1;/啟動(dòng)定時(shí)器0TMOD=0x05;/設(shè)置工作方式為1TH0=0xff

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論