 ;********WAVE-E6000/T************************** ;*MCU: AT89C51 * ;*MCU-crystal: 6M * ;*Version: 00 * ;*Last Updata: * ;*Author: www.picavr.com * ;*Description: 將鍵盤獲取的字符由串口發(fā)送出去 * ;********************************************** #include <reg51.h> unsigned char key; unsigned int i,j; unsigned char led[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; sbit bb=P3^7; void delay(i) {while(i--) {j=7650;while(j--);} } /* 鍵盤掃描碼轉換為字符顯示碼 */ unsigned char keyc(n) unsigned char n; {switch(n) {case 0x11:return(0);break; case 0x12:return(1);break; case 0x14:return(2);break; case 0x18:return(3);break; case 0x21:return(4);break; case 0x22:return(5);break; case 0x24:return(6);break; case 0x28:return(7);break; case 0x41:return(8);break; case 0x42:return(9);break; case 0x44:return(10);break; case 0x48:return(11);break; case 0x81:return(12);break; case 0x82:return(13);break; case 0x84:return(14);break; case 0x88:return(15);break; } } /* 延時 10ms */ void dlms(void) { unsigned int i; for(i=0;i<765;i++);} /* 鍵盤掃描函數(shù) */ unsigned char kbscan(void) { unsigned char sccode,recode; P1=0xf0; if((P1&0xf0)!=0xf0) { dlms(); if((P1&0xf0)!=0xf0) { sccode=0xfe; while((sccode&0x10)!=0) { P1=sccode; if((P1&0xf0)!=0xf0) { recode=(P1&0xf0)|0x0f; return((~sccode)+(~recode)); } else sccode=(sccode<<1)|0x01; } } } return(0); } void main(void) {P0=0xf9;P2=0x92;delay(10);P0=0xff;P2=0xff; TMOD=0x20; /* 為串口設T/C1為定時方式2 */ TH1=0xfd;TL1=0xfd; /* 設定波特率9600,fosc=11.0592MHz */ SCON=0x50; /* 串行口工作方式1 */ TR1=1;
while(1) { key=kbscan(); /* 去鍵盤掃描碼 */ if(key!=0) { key=keyc(key); /* 或取鍵盤字符 */ bb=0;delay(1);bb=1; P0=led[key>>4]; /* 顯示鍵盤字符 */ P2=led[key&0x0f]; SBUF=key; /* 發(fā)送鍵盤字符 */ while(TI==0); TI=0; delay(5); } } } |