Chat
Danh mục
Mach điều khiển quạt bằng Remote TV Sony RC5 - Dùng PIC 16F877A

Mach điều khiển quạt bằng Remote TV Sony RC5 - Dùng PIC 16F877A

Số lượng:
Thêm vào giỏ
Mach điều khiển quạt bằng Remote TV Sony RC5 - Dùng PIC 16F877A đã được thêm vào giỏ hàng



Code:

/*
 * Project name:
     SONY SIRC decoding
 * Copyright:
      Nicholas Sirirak
  * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       -
     Oscillator:      HS, 104.0000 MHz
     Ext. Modules:    -
     SW:              mikroC PRO V1.65
  */

//#define  LED0  PORTD.F0
#define  LED0  PORTB.F1
#define  LED1  PORTB.F2
#define  LED2  PORTB.F3
#define  LED3  PORTB.F4
#define  LED4  PORTB.F5
#define  LED5  PORTB.F6
#define  LED6  PORTB.F7

unsigned counter = 0;
unsigned input_data, bit_count;
int i,a;
unsigned int mask(unsigned short num) {
  switch (num) {
    case 0 : return 0xc0;
    case 1 : return 0xf9;
    case 2 : return 0xa4;
    case 3 : return 0xb0;
    case 4 : return 0x99;
    case 5 : return 0x92;
    case 6 : return 0x82;
    case 7 : return 0xf8;
    case 8 : return 0x80;
    case 9 : return 0x90;
  } //case end
}//~
void hienthiled(){
     PORTC.F0=1    ;
     PORTD=mask(i);
}

//Hien thi led 7 seg
void led7seg(){while(portb.f1==0&&portb.f2==0&&portb.f3==0&&portb.f4==0&&portb.f5==0&&portb.f6==0){
i=0;
hienthiled();
break;
}

//portb.f1 =1;
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f2==0){
                                                                                while(portb.f1){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }
//portb.f2=1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f1==0){
                                                                                while(portb.f2){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }
 // portb.f3==1;
 if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f1==0){
                                                                if(portb.f2==0){
                                                                                while(portb.f3){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }
//portb.f4=1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f1==0){
                                                if(portb.f3==0){
                                                                if(portb.f2==0){
                                                                                while(portb.f4){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }
//portb.f5 = 1
if(portb.f6==0){
                if(portb.f1==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f2==0){
                                                                                while(portb.f5){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }
//portb.f6 = 1
if(portb.f1==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f2==0){
                                                                                while(portb.f6){
                                                                                i=1;
                                                                                hienthiled();
                                                                                break;
                                                                                             }
                                                                                }
                                                                }
                                                }
                                }
                }

//portb.f1 va portb.f2 =1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f3 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f2==0){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f4 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f2==0){
                                                if(portb.f3==0){
                                                                if(portb.f4==1){
                                                                while(portb.f1==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// portb.f1 va portb.f5
if(portb.f6==0){
                if(portb.f2==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f5==1){
                                                                while(portb.f1==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f6
if(portb.f2==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f6==1){
                                                                while(portb.f1==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f3 =1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f1==0){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f4 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f1==0){
                                                if(portb.f3==0){
                                                                if(portb.f4==1){
                                                                while(portb.f2==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va fortb.f5 =1
if(portb.f6==0){
                if(portb.f1==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f5==1){
                                                                while(portb.f2==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f6
if(portb.f1==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==0){
                                                                if(portb.f6==1){
                                                                while(portb.f2==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f3 va portb.f4 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f4==1){
                                                                while(portb.f3==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f3 va portb.f5
if(portb.f6==0){
                if(portb.f4==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f5==1){
                                                                while(portb.f3==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f3 va portb.f6 = 1
if(portb.f4==0){
                if(portb.f5==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f6==1){
                                                                while(portb.f3==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// portb.f4 va portb.f5 = 1
if(portb.f6==0){
                if(portb.f3==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f5==1){
                                                                while(portb.f4==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f4 va portb.f6 = 1
if(portb.f3==0){
                if(portb.f5==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f6==1){
                                                                while(portb.f4==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f5 va portb.f6 =1
if(portb.f3==0){
                if(portb.f4==0){
                                if(portb.f2==0){
                                                if(portb.f1==0){
                                                                if(portb.f6==1){
                                                                while(portb.f5==1){
                                                                i=2;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }


// portb.f1 va portb.f2 va portb.f3 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

//portb.f1 portb.f2 portb.f4 =1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f3==0){
                                                if(portb.f4==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f2 portb.f5 =1
if(portb.f6==0){
                if(portb.f3==0){
                                if(portb.f4==0){
                                                if(portb.f5==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f2 portb.f6 = 1
if(portb.f3==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f6==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f3 portb.f4=1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f2==0){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f1 va portb.f3 portb.f5 = 1
if(portb.f6==0){
                if(portb.f2==0){
                                if(portb.f4==0){
                                                if(portb.f5==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// portb.f1 va portb.f3 portb.f6 = 1
if(portb.f2==0){
                if(portb.f5==0){
                                if(portb.f4==0){
                                                if(portb.f6==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

// portb.f1 va portb.f4 portb.f5 = 1
if(portb.f6==0){
                if(portb.f2==0){
                                if(portb.f3==0){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

// portb.f1 va portb.f4 portb.f6 =1
if(portb.f5==0){
                if(portb.f2==0){
                                if(portb.f3==0){
                                                if(portb.f6==1){
                                                                if(portb.f4==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

// portb.f1 va portb.f5 portb.f6
if(portb.f4==0){
                if(portb.f2==0){
                                if(portb.f3==0){
                                                if(portb.f6==1){
                                                                if(portb.f5==1){
                                                                while(portb.f1==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f3 portb.f4 =1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f1==0){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 portb.f3 portb.f5 = 1
if(portb.f6==0){
                if(portb.f1==0){
                                if(portb.f4==0){
                                                if(portb.f5==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f3 portb.f6 =1
if(portb.f5==0){
                if(portb.f1==0){
                                if(portb.f3==0){
                                                if(portb.f6==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f4 portb.f5 =1
if(portb.f6==0){
                if(portb.f3==0){
                                if(portb.f1==0){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f4 portb.f6 = 1
if(portb.f5==0){
                if(portb.f3==0){
                                if(portb.f1==0){
                                                if(portb.f6==1){
                                                                if(portb.f4==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f2 va portb.f5 portb.f6 = 1
if(portb.f4==0){
                if(portb.f3==0){
                                if(portb.f1==0){
                                                if(portb.f6==1){
                                                                if(portb.f5==1){
                                                                while(portb.f2==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// portb.f3 va portb.f4 portb.f5 = 1
if(portb.f6==0){
                if(portb.f2==0){
                                if(portb.f1==0){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f3==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//portb.f3 va portb.f4 portb.f6 = 1
if(portb.f5==0){
                if(portb.f2==0){
                                if(portb.f1==0){
                                                if(portb.f6==1){
                                                                if(portb.f4==1){
                                                                while(portb.f3==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f3 f5 f6
if(portb.f4==0){
                if(portb.f2==0){
                                if(portb.f1==0){
                                                if(portb.f6==1){
                                                                if(portb.f5==1){
                                                                while(portb.f3==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

//portb.f4 va portb.f5 portb.f6 = 1
if(portb.f3==0){
                if(portb.f2==0){
                                if(portb.f1==0){
                                                if(portb.f6==1){
                                                                if(portb.f5==1){
                                                                while(portb.f4==1){
                                                                i=3;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f3 f4 = 1
if(portb.f6==0){
                if(portb.f5==0){
                                if(portb.f4==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f3 f5 =1
if(portb.f6==0){
                if(portb.f4==0){
                                if(portb.f5==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f3 f6 =1
if(portb.f5==0){
                if(portb.f4==0){
                                if(portb.f6==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// f1 f2 f4 f5 =1
if(portb.f6==0){
                if(portb.f3==0){
                                if(portb.f5==1){
                                                if(portb.f4==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f4 f6  =1
if(portb.f5==0){
                if(portb.f3==0){
                                if(portb.f6==1){
                                                if(portb.f4==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f5 f6  =1
if(portb.f4==0){
                if(portb.f3==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f3 f4 f5
if(portb.f6==0){
                if(portb.f2==0){
                                if(portb.f5==1){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f3 f4 f6
if(portb.f5==0){
                if(portb.f2==0){
                                if(portb.f6==1){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f3 f5 f6
 if(portb.f4==0){
                if(portb.f2==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f3==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f4 f5 f6
if(portb.f2==0){
                if(portb.f3==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f1==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f2 f3 f4 f5  =1
if(portb.f6==0){
                if(portb.f1==0){
                                if(portb.f5==1){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f2 f3 f4 f6  =1
 if(portb.f5==0){
                if(portb.f1==0){
                                if(portb.f6==1){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
// f2 f3 f5 f6
 if(portb.f4==0){
                if(portb.f1==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f2 f4 f5 f6
 if(portb.f3==0){
                if(portb.f1==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f2==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

//f3 f4 f5 f6    =1
 if(portb.f2==0){
                if(portb.f1==0){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f3==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f4 f5 f6 f1      =1
 if(portb.f3==0){
                if(portb.f2==0){
                                if(portb.f1==1){
                                                if(portb.f6==1){
                                                                if(portb.f5==1){
                                                                while(portb.f4==1){
                                                                i=4;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

//f1 f2 f3 f4 f5
if(portb.f6==0){
                if(portb.f5==1){
                                if(portb.f4==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f3 f4 f6
if(portb.f5==0){
                if(portb.f6==1){
                                if(portb.f4==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f1 f2 f3 f5 f6
if(portb.f4==0){
                if(portb.f6==1){
                                if(portb.f5==1){
                                                if(portb.f3==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }

//f1 f2 f4 f5 f6
if(portb.f3==0){
                if(portb.f6==1){
                                if(portb.f5==1){
                                                if(portb.f4==1){
                                                                if(portb.f2==1){
                                                                while(portb.f1==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f2 f3 f4 f5 f6
if(portb.f1==0){
                if(portb.f6==1){
                                if(portb.f5==1){
                                                if(portb.f4==1){
                                                                if(portb.f3==1){
                                                                while(portb.f2==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }
//f3 f4 f5 f6 f1
if(portb.f2==0){
                if(portb.f1==1){
                                if(portb.f6==1){
                                                if(portb.f5==1){
                                                                if(portb.f4==1){
                                                                while(portb.f3==1){
                                                                i=5;
                                                                hienthiled();
                                                                break;

                                                                                   }
                                                                               }
                                                                }
                                               }
                                 }
                 }



//ca 6 port cung chay
while(portb.f1&&portb.f2&&portb.f3&&portb.f4&&portb.f5&&portb.f6){
i=6;
hienthiled();
break;
      }
}
//Ket thuc hien thi led 7 seg


enum {
        Idle,
        Start_bit,
        Capture_bit
};

char Current_state = Idle;
char got_data = 0;
int Command_code, Device_code;
void interrupt(){
     if(INTCON.INTF){
        switch (Current_state){
           case Idle:
                      OPTION_REG.INTEDG = 1;        //interrupt on rising edge.
                      counter = 0;
                      Current_state = Start_bit;
                      break;
           //found the rising edge, check lenght for 2.4ms
           case Start_bit:
                      //correct signal, move on to next state
                      if(counter == 4) {
                         counter = 0;
                         bit_count = 0;
                         input_data = 0;
                         Current_state = Capture_bit;
                         } else {
                            //fault signal, reset to Idle
                            Current_state = Idle;
                         }
                      break;
           case Capture_bit:
                      //check plus length for 0 or 1
                      if(counter == 2){
                         input_data >>= 1;         // add 0 to received data
                         bit_count++;
                      }else {
                         if(counter == 3){
                            input_data >>= 1;
                            input_data |= 0x8000;     //add 1 to received data
                            bit_count++;
                         } else {
                            //error occurs, reset to Idle state
                            OPTION_REG.INTEDG = 0;    //interrupt on falling edge.
                            Current_state = Idle;
                         }
                       }
                      //compleat 12 bit
                      if(bit_count >= 12){
                         got_data = 1;
                         input_data >>= 4;
                         OPTION_REG.INTEDG = 0;      //interrupt on falling edge.
                         Current_state = Idle;
                      }
                      counter = 0;
                      break;
            default: Current_state = Idle;
        }
     INTCON.INTF = 0;       //clear interrupt flag.
     }
     if(PIR1.TMR2IF){
        counter++;
        if(counter > 5) {
            Current_state = Idle;
            counter = 0;
            OPTION_REG.INTEDG = 0;        //interrupt on falling edge.
        }
        PIR1.TMR2IF = 0;          //clear interrupt flag
     }
}

//******************************************************************************
// MAIN  MAIN  MAIN MAIN
//******************************************************************************
void main() {
     TRISD = 0X00;                  //portc is output
     TRISB = 0X01;                  //portb is output, Rb0 input
     TRISC=0X00;
     PORTB=0X00;
     PORTD=0X00;

//******************************************************************************
// RB0 interrupt set up
//******************************************************************************
     INTCON.INTE = 1;          //enable RB0 interrupt.
     OPTION_REG.INTEDG = 0;        //interrupt on falling edge.
     //TRISB.F0 = 1;               //RB0 = input.
    // ADCON1 = 0x0F;              //all digital I/O
//******************************************************************************
// Timer2 interrupt set up, interrupt every 600us
//******************************************************************************
     T2CON = 0X01;                  //timer off, prescaler 1:16
     PR2 = 149;                  //preload timer2 comparator value.
     TMR2 = 0;                   //reset value timer2
    INTCON.INTF=0;
    PIR1.TMR2IF = 0;            //clear interrupt flag.
    PIE1.TMR2IE = 1;            //enable timer2 interrupt.
    PIR1.TMR2IF = 1;            //timer2 interrupt high priority
//******************************************************************************
// Global interrupt enable
//******************************************************************************
     INTCON.PEIE = 1;            //enable interrupt
     INTCON.GIE = 1;             //enable global interrupt
     T2CON.TMR2ON = 1;           //timer2 is on
     i=0;
     hienthiled();
     while(1){
          if(got_data){
            Command_code = input_data & 0x7d;     //d duoc 4 kenh
            Device_code = input_data >> 7;
            got_data = 0;

            if(Device_code == 1){
               switch (Command_code){
                  case 0: LED0 = ~LED0; break; //BUTON1
                  case 1: LED1 = ~LED1; break; //2
                  case 4: LED2 = ~LED2; break; //3
                  case 5: LED3 = ~LED3; break; //4
                  case 13: LED4 = ~LED4; break; //5
                  //case 13: LED5 = ~LED5; break;
                  //case 2: LED6 = ~LED6; break;
                  //case 3: LED7 = ~LED7; break;
               }

               PIE1.TMR2IE = 0;            //disable timer2 interrupt.
               Delay_ms(500);
               TMR2 = 0;                   //reset value timer2
               PIE1.TMR2IE = 1;            //enable timer2 interrupt.
            }

          }
          led7seg();
     }

}

Theo dientudieukhien.net

( Nguyên lý + Mô phỏng + Code Compiler)