|
出這個(gè)題的目的是讓學(xué)生掌握IO口作輸出口的同時(shí)也做輸入口,編程時(shí)按分時(shí)輪換改變IO口狀態(tài),在實(shí)際電路中檢測(cè)按鍵時(shí)短暫關(guān)閉LED是觀察不到的。仿真的效果不如實(shí)際電路好。
無標(biāo)題.jpg (173.97 KB, 下載次數(shù): 26)
下載附件
2019-11-25 12:43 上傳
- #include <REG51.H>
- #define uint unsigned int
- #define uchar unsigned char
- sbit a=P3^0;
- sbit b=P3^6;
- sbit c=P3^7;
- uchar code table1[]={0xfe,0xfd,0xfb,0xf7};
- uchar code table2[]={0xf7,0xfb,0xfd,0xfe};
- uchar key=0x0f;
- void delay(uint k)//延時(shí)程序
- {
- uint i,j;
- for(i=k;i>0;i--)
- for(j=125;j>0;j--);
- }
- void keyscan()//按鍵掃描程序
- {
- uchar i;
- P0=0xff;
- i=P0&0x0f;
- if(i!=0x0f)
- {
- key=i;
- }
- }
- void display()//顯示程序
- {
- static uint i=0,j=0;
- if(key==0x0e)
- {
- if(i<250)
- P0&=0x00;
- else P0=0x0f;
- }
- if(key==0x0d)
- {
- P0=0x00;
- }
- if(key==0x0b)
- {
- P0=table1[j];
- }
- if(key==0x07)
- {
- P0=table2[j];
- }
- i++;
- if(i>=500)
- {
- i=0;
- j++;
- j%=4;
- }
- }
- void main(void)
- {
- b=0;
- c=0;
- while(1)
- {
- a=0;
- keyscan();
- a=1;
- display();
- delay(1);
- }
- }
復(fù)制代碼
|
|