web123456

Experiment 7: Independent key experiment

  • #include<>
  • sbit But1=P3^1 ;
  • sbit But2=P3^0 ;
  • sbit But3=P3^2 ;
  • sbit But4=P3^3 ;
  • sbit LED1 =P2^0 ;
  • sbit LED2 =P2^1 ;
  • sbit LED3 =P2^2 ;
  • sbit LED4 =P2^3 ;
  • #define PRESS_1 1
  • #define PRESS_2 2
  • #define PRESS_3 3
  • #define PRESS_4 4
  • #define UNPRESS_0 0
  • typedef unsigned int u16;
  • typedef unsigned char u8;
  • void delay_10us(u16 n)
  • {
  • while(n--);
  • }
  • press_scan(u8 mode)
  • {
  • int key=1;
  • if(key=1||(But1==0||But2==0||But3==0||But4==0))
  • {
  • key=0;
  • delay_10us(500) ;
  • if(But1==0)
  • {
  • mode=PRESS_1;
  • }
  • if(But2==0)
  • {
  • mode=PRESS_2;
  • }
  • if(But3==0)
  • {
  • mode=PRESS_3;
  • }
  • if(But4==0)
  • {
  • mode=PRESS_4;
  • }
  • }
  • if(But1==1&&But2==1&&But3==1&&But4==1)
  • {
  • mode=UNPRESS_0;
  • key=1;
  • }
  • return(mode);
  • }
  • void main()
  • { while(1){
  • char mode='0';
  • mode=press_scan(0) ;
  • if(mode==PRESS_1)
  • {
  • LED1=!LED1;
  • }
  • if(mode==PRESS_2)
  • {
  • LED2=!LED2 ;
  • }
  • if(mode==PRESS_3)
  • {
  • LED3=!LED3 ;
  • }
  • if(mode==PRESS_4)
  • {
  • LED4=!LED4 ;
  • }
  • }
  • }