無標(biāo)題.jpg (213.04 KB, 下載次數(shù): 28)
下載附件
2021-12-11 07:51 上傳
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- //sbit k1=P1^0;
- //sbit k2=P1^1;
- //sbit k3=P1^2;
- unsigned char keyval;
- uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(uint xms)
- {
- uint x,y;
- for(x=xms;x>0;x--)
- for(y=110;y>0;y--);
- }
- void key_scan(void)
- {
- if((P1&0x07)!=0x07)
- {
- delay(10);
- if((P1&0x07)!=0x07)
- {
- switch(P1&0x07)
- {
- case 0x06: keyval=1; break;
- case 0x05: keyval=2; break;
- case 0x03: keyval=3; break;
- }
- while(!((P1&0x07)!=0x07));
- }
- }
- }
- void Putc_to_SerialPort(uchar c)
- {
- SBUF=c;
- while(TI==0);
- TI=0;
- }
- void main()
- {
- P0=0x00;
- SCON=0x50;
- TMOD=0x20;
- PCON=0x00;
- TL1=0xfd;
- TH1=0xfd;
- TR1=1;
- IE=0x90;
- keyval=0;
- while(1)
- {
- key_scan();
- switch(keyval)
- {
- case 1: Putc_to_SerialPort('X');keyval=0;break;
- case 2: Putc_to_SerialPort('A');keyval=0;break;
- case 3: Putc_to_SerialPort('B');keyval=0;break;
- }
- }
- }
- void Serial_INT() interrupt 4
- {
- if(RI)
- {
- RI=0;
- P0=DSY_CODE[SBUF];
- }
- }
復(fù)制代碼- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LED1=P1^0;
- sbit LED2=P1^1;
- sbit K2=P1^7;
- uchar NumX=-1;
- void DelayMS(uint ms)
- {
- uchar i;
- while(ms--)
- {
- for(i=0;i<120;i++);
- }
- }
- void main()
- {
- SCON=0x50;
- TMOD=0x20;
- TH1=0xfd;
- TL1=0xfd;
- PCON=0x00;
- TR1=1;
- IE=0x90;
- while(1)
- {
- if(K2==0)
- {
- DelayMS(10);
- if(K2==0)
- {
- NumX=++NumX%10;
- SBUF=NumX;
- while(TI==0);
- TI=0;
- while(K2==0);
- }
- }
- }
- }
- void Serial_INT() interrupt 4
- {
- RI=0;
- switch(SBUF)
- {
- case 'X': LED1=0;LED2=1;break;
- case 'A': LED2=0;LED1=1;break;
- case 'B': LED1=LED2=0; break;
- }
- }
復(fù)制代碼
|