單片機C語言程序設(shè)計實訓(xùn)99例(共77頁)_第1頁
單片機C語言程序設(shè)計實訓(xùn)99例(共77頁)_第2頁
單片機C語言程序設(shè)計實訓(xùn)99例(共77頁)_第3頁
單片機C語言程序設(shè)計實訓(xùn)99例(共77頁)_第4頁
單片機C語言程序設(shè)計實訓(xùn)99例(共77頁)_第5頁
已閱讀5頁,還剩72頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上單片機C語言程序設(shè)計實訓(xùn)99例案例第 01 篇 基礎(chǔ)程序設(shè)計09 555定時器實驗/*名稱:555定時器實驗說明:調(diào)節(jié)外部電阻RV1可改變延時值,從而影響燈點亮延時和發(fā)聲延時。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit Signal=P10;sbit BEEP=P37;/延時void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<120;i+);/主程序void main()while(1)if(Si

2、gnal)BEEP=BEEP;DelayMS(3);10 BCD譯碼數(shù)碼管顯示數(shù)字/*名稱:BCD譯碼數(shù)碼管顯示數(shù)字說明:BCD碼經(jīng)4511譯碼后輸出數(shù)碼管段碼,實現(xiàn)數(shù)碼管顯示(4511驅(qū)動數(shù)碼管)。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/數(shù)碼管位碼uchar code DSY_Index=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;/待顯示數(shù)字(10為不顯示)uchar code BCD_CODE=2,0,1,0,10,3,10,5;/延時v

3、oid DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<120;i+);/主程序void main()uchar k;while(1)for(k=0;k<8;k+)P2=DSY_Indexk;P1=BCD_CODEk;DelayMS(1);11 MAX7221控制數(shù)碼管動態(tài)顯示/*名稱:MAX7221控制數(shù)碼管動態(tài)顯示說明:本例用MAX7221控制8只數(shù)碼管動態(tài)顯示,這樣大大減少了單片機引腳和機器時間的占用。*/#include<reg51.h>#include<intrins.h>#define uchar uns

4、igned char#define uint unsigned intsbit DIN=P20;sbit CSB=P21;sbit CLK=P22;uchar Disp_Buffer=2,0,1,5,10,5,10,9;/顯示緩沖,10為“-”/延時void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<120;i+);/寫數(shù)據(jù)void Write(uchar Addr,uchar Dat)uchar i;CSB=0;for(i=0;i<8;i+)CLK=0;Addr<<=1;DIN=CY;CLK=1;_nop_();_nop

5、_();CLK=0;for(i=0;i<8;i+)CLK=0;Dat<<=1;DIN=CY;CLK=1;_nop_();_nop_();CLK=0;CSB=1;/MAX7221初始化void Initialise()Write(0x09,0xff);/編碼模式地址0x09 0x000xff,為1的則位選通Write(0x0a,0x07);/亮度地址0x0a 0x000x0f,0x0f最亮Write(0x0b,0x07);/掃描數(shù)碼管個數(shù)地址0x0b,最多掃描8只數(shù)碼管Write(0x0c,0x01);/工作模式地址0x0c 0x00:關(guān)閉;0x01:正常/主程序void ma

6、in()uchar i;Initialise();/初始化DelayMS(1);for(i=0;i<8;i+)/顯示8個數(shù)碼管Write(i+1,Disp_Bufferi);while(1);12 LCD1602字符液晶滾動演示程序/main.c/*名稱:LCD1602字符液晶滾動演示程序說明:K1K3按鈕分別實現(xiàn)液晶垂直或水平滾動顯示及暫停與繼續(xù)控制。*/#include<reg51.h>#include<string.h>#define uchar unsigned char #define uint unsigned intvoid Initialize_L

7、CD();void DelayMS(uint ms);void ShowString(uchar,uchar,uchar *);sbit K1=P30;sbit K2=P31;sbit K3=P32;uchar code Prompt="Press K1 - K3 To Start Demo Prog"/待滾動顯示的信息段落,每行不超過80個字符,共6行uchar const Line_Count=6;uchar code Msg80="Many CAD users dismiss schematic capture as a necessary evil in

8、the ","process of creating PCB layout but we have always disputed this point ","of view. With PCB layout now offering automation of both component ","can often be the most time consuming element of the exercise.","And if you use circuit simulation to develop y

9、our ideas, ","you are going to spend even more time working on the schematic."/顯示緩沖(2行)uchar Disp_Buffer32;/垂直滾動顯示void V_Scroll_Display()uchar i,j,k=0;uchar *p=Msg0;uchar *q=MsgLine_Count+strlen(MsgLine_Count);/以下僅使用顯示緩沖的前16字節(jié)空間while(p<q)for(i=0;i<16&&p<q;i+)/消除顯示緩沖中待

10、顯示行首尾可能出現(xiàn)的空格if(i=0|i=15)&&*p=' ') p+;if(*p!='0')Disp_Bufferi=*p+;elseif(+k>Line_Count) break;p=Msgk;/p指向下一串的首地址Disp_Bufferi=*p+;/不足16個字符時空格補充for(j=i;j<16;j+) Disp_Bufferj=' '/垂直滾動顯示while(F0) DelayMS(5);ShowString(0,0," ");DelayMS(150);while(F0) DelayMS

11、(5);ShowString(0,1,Disp_Buffer);DelayMS(150);while(F0) DelayMS(5);ShowString(0,0,Disp_Buffer);ShowString(0,1," ");DelayMS(150);/最后清屏ShowString(0,0," ");ShowString(0,1," ");/水平滾動顯示void H_Scroll_Display()uchar i,j,k=0,L=0;uchar *p=Msg0;uchar *q=MsgLine_Count+strlen(MsgLin

12、e_Count);/將32個字符的顯示緩沖前16個字符設(shè)為空格for(i=0;i<16;i+) Disp_Bufferi=' 'while(p<q)/忽略緩沖中首尾可能出現(xiàn)的空格if(i=16|i=31)&&*p=' ') p+;for(i=16;i<32&&p<q;i+)if(*p!='0')Disp_Bufferi=*p+;elseif(+k>Line_Count) break;p=Msgk;/p指向下一串的首地址Disp_Bufferi=*p+;/不足32個字符時空格補充for(

13、j=i;j<32;j+) Disp_Bufferj=' '/水平滾動顯示for(i=0;i<=16;i+)while(F0) DelayMS(5);ShowString(0,L,Disp_Buffer+i);while(F0) DelayMS(5);DelayMS(20);L=(L=0)?1:0;/行號在0,1間交替DelayMS(300);/如果顯示結(jié)束時停留在第0行,則清除第1行的內(nèi)容if(L=1) ShowString(0,1," ");/外部中斷0,由K3控制暫停與繼續(xù)顯示void EX_INT0() interrupt 0F0=!F0;

14、/暫停與繼續(xù)顯示控制標志位/主程序void main()uint Count=0;IE=0x81;/允許外部中斷0IT0=1;/下降沿觸發(fā)F0=0;/暫停與繼續(xù)顯示控制標志位Initialize_LCD();ShowString(0,0,Prompt);ShowString(0,1,Prompt+16);while(1)if(K1=0)V_Scroll_Display();DelayMS(300);elseif(K2=0)H_Scroll_Display();DelayMS(300);/LCD1602.c/*名稱:液晶控制與顯示程序說明:本程序是通用的1602液晶控制程序。*/#include

15、<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intsbit RS=P20;sbit RW=P21;sbit EN=P22;/延時void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<120;i+);/忙檢查uchar Busy_Check()uchar LCD_Status;RS=0;/寄存器選擇RW=1;/讀狀態(tài)寄存器EN=1;/開始讀DelayMS(1);LCD_Status=P0;EN=0;return

16、LCD_Status;/寫LCD命令void Write_LCD_Command(uchar cmd)while(Busy_Check()&0x80)=0x80);/忙等待RS=0;/選擇命令寄存器RW=0;/寫EN=0;P0=cmd;EN=1;DelayMS(1);EN=0;/發(fā)送數(shù)據(jù)void Write_LCD_Data(uchar dat)while(Busy_Check()&0x80)=0x80);/忙等待RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0;/LCD初始化void Initialize_LCD()Write_LCD_Co

17、mmand(0x38);DelayMS(1);Write_LCD_Command(0x01);DelayMS(1);/清屏Write_LCD_Command(0x06);DelayMS(1);/字符進入模式:屏幕不動,字符后移Write_LCD_Command(0x0c);DelayMS(1);/顯示開,光標關(guān)/顯示字符串void ShowString(uchar x,uchar y,uchar *str)uchar i=0;if(y=0) Write_LCD_Command(0x80|x);/設(shè)置顯示起始位置if(y=1) Write_LCD_Command(0xc0|x);for(i=0;

18、i<16;i+)/輸出字符串Write_LCD_Data(stri);1319 用ADC0808控制PWM輸出/*名稱:用ADC0808控制PWM輸出說明:使用數(shù)模轉(zhuǎn)換芯片ADC0808,通過調(diào)節(jié)可變電阻RV1來調(diào)節(jié)脈沖寬度,運行程序時,通過虛擬示波器觀察占空比的變化。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit CLK=P24;/時鐘信號sbit ST=P25;/啟動信號sbit EOC=P26;/轉(zhuǎn)換結(jié)束信號sbit OE=P27;/輸出使能sbit PWM=P30;

19、/PWM輸出/延時void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<40;i+);/主程序void main()uchar Val;TMOD=0x02;/T1工作模式2TH0=0x14;TL0=0x00;IE=0x82;TR0=1;while(1)ST=0;ST=1;ST=0;/啟動A/D轉(zhuǎn)換while(!EOC);/等待轉(zhuǎn)換完成OE=1;Val=P1;/讀轉(zhuǎn)換值OE=0;if(Val=0)/PWM輸出(占空比為0%)PWM=0;DelayMS(0xff);continue;if(Val=0xff) /PWM輸出(占空比為100%)PWM

20、=1;DelayMS(0xff);continue;PWM=1; /PWM輸出(占空比為0%100%)DelayMS(Val);PWM=0;DelayMS(0xff-Val);/T0定時器中斷給ADC0808提供時鐘信號void Timer0_INT() interrupt 1CLK=CLK;20 ADC0809數(shù)模轉(zhuǎn)換與顯示/*名稱:ADC0809數(shù)模轉(zhuǎn)換與顯示說明:ADC0809采樣通道3輸入的模擬量,轉(zhuǎn)換后的結(jié)果顯示在數(shù)碼管上。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/各數(shù)字

21、的數(shù)碼管段碼(共陰)uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;sbit CLK=P13;/時鐘信號sbit ST=P12;/啟動信號sbit EOC=P11;/轉(zhuǎn)換結(jié)束信號sbit OE=P10;/輸出使能/延時void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i<120;i+);/顯示轉(zhuǎn)換結(jié)果void Display_Result(uchar d)P2=0xf7;/第4個數(shù)碼管顯示個位數(shù)P0=DSY_CODEd%10;DelayMS(5);P2

22、=0xfb;/第3個數(shù)碼管顯示十位數(shù)P0=DSY_CODEd%100/10;DelayMS(5);P2=0xfd;/第2個數(shù)碼管顯示百位數(shù)P0=DSY_CODEd/100;DelayMS(5);/主程序void main()TMOD=0x02;/T1工作模式2TH0=0x14;TL0=0x00;IE=0x82;TR0=1;P1=0x3f;/選擇ADC0809的通道3(0111)(P1.4P1.6)while(1)ST=0;ST=1;ST=0;/啟動A/D轉(zhuǎn)換while(EOC=0);/等待轉(zhuǎn)換完成OE=1;Display_Result(P3);OE=0;/T0定時器中斷給ADC0808提供時鐘

23、信號void Timer0_INT() interrupt 1CLK=CLK;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;DelayMS(150);02 從左到右的流水燈/* 名稱:從左到右的流水燈說明:接在

24、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(150);03 8只LED左右來回點亮/* 名稱:8只LED左右來回點亮說明:程序利用

25、循環(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_(P2,1); /P2的值向左循環(huán)移動DelayMS(150);for(i=0;i&l

26、t;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,0xff,0xff,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xd

27、b,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,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff

28、,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,0x00,0xff,0x00,0xff;uchar code Pattern_P2

29、= 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,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,

30、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,0x80,0x00, 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0

31、xfc,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(100);05 LED模擬交通燈/*名稱:LED模擬交通燈說明:東西向綠燈亮若干秒

32、,黃燈閃爍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_Count=0,Operation_Type=1; /閃爍次數(shù),操作類型變量/延

33、時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);YELLOW_A=YELLOW_A;GREEN_A=1;if(+Flash_Count!

34、=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;Flash_Count=0;Operation_Type=1;/主程序void main

35、()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,0x90,0xff;/延時void DelayMS(uint x)uchar t;

36、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>#include<intrins.h>#define uchar

37、 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);P0=DSY_CODEi; /發(fā)送數(shù)字段碼P2=wei; /發(fā)送位碼Delay

38、MS(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 x)uchar t;while(x-) for(t=0;t<120;t+);

39、/主程序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)成的一串數(shù)字 本例用動態(tài)刷新法顯示一串數(shù)字,在停止刷新時所有數(shù)字顯示消失。*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int/段碼表ucha

40、r 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+)P0=0xff;P0=DSY_CODEj; /發(fā)送段碼P2=DSY_IDXj;

41、 /發(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,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/下面數(shù)組看作環(huán)

42、形隊列,顯示從某個數(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=DSY_CODENum(k+j)%11; m=_crol_(m,1);P2=m;

43、/發(fā)送位碼DelayMS(2); k=(k+1)%11;/環(huán)形隊列首支針k遞增,Num下標范圍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 unsigned int/延時void DelayMS(uint x)uchar i;wh

44、ile(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;/最近按鍵P0=0xfe;P2=0xfe;P1=0xff;Recent_Key=

45、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 LED2=P01;sbit LED3=P02;sbit LED4=P03;sbit K1

46、=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 分組控制LED說明:每次按下K1時遞增點亮一只LED,全亮?xí)r再次按下則再次循環(huán)開始,

47、K2按下后點亮上面4只LED,K3按下后點亮下面4只LED,K4按下后關(guān)閉所有LED*/#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)DelayMS(10);if(t!=P1) continue;/取得4位按鍵值,由模式X

48、XXX1111(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進行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;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,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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論