Advertisement
Electgpl

PIC - Receptor RC5

Sep 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. //LCD module connections
  2. #define LCD_RS_PIN PIN_B1
  3. #define LCD_RW_PIN PIN_B2
  4. #define LCD_ENABLE_PIN PIN_B3
  5. #define LCD_DATA4 PIN_B4
  6. #define LCD_DATA5 PIN_B5
  7. #define LCD_DATA6 PIN_B6
  8. #define LCD_DATA7 PIN_B7
  9. //End LCD module connections
  10.  
  11. #include <16F84A.h>
  12. #fuses HS,NOWDT,PUT,NOPROTECT
  13. #use delay(crystal=8000000)
  14. #include <lcd.c>
  15. #use fast_io(B)
  16.  
  17. int1 toggle;
  18. unsigned int8 count, i, address, command;
  19. void main(){
  20.   port_b_pullups(TRUE);                  // Enable PORTB pull-ups
  21.   output_b(0);                           // PORTB initial state
  22.   set_tris_b(1);
  23.   lcd_init();                            // Initialize LCD module
  24.   lcd_putc('\f');                        // LCD clear
  25.   lcd_gotoxy(3, 1);                      // Go to column 3 row 1
  26.   lcd_putc("RC5 protocol");
  27.   lcd_gotoxy(5, 2);                      // Go to column 5 row 2
  28.   lcd_putc("decoder");
  29.   while(TRUE){
  30.     ret:
  31.     count = 0;
  32.     while(input(PIN_B0));                // Wait until RB0 pin falls
  33.     // Check if the received signal uses RC5 protocol
  34.     while((input(PIN_B0) == 0) && (count < 20)){
  35.       count++;
  36.       delay_us(50);}
  37.     if( (count > 20) || (count < 14))      // Signal doesn't use RC5 protocol
  38.       goto ret;                            // Return
  39.     count = 0;
  40.     while((input(PIN_B0)) && (count < 20)){
  41.       count++;
  42.       delay_us(50);}
  43.     if( (count > 20) || (count < 14))      // Signal doesn't use RC5 protocol
  44.       goto ret;                            // Return
  45.     count = 0;
  46.     while((input(PIN_B0) == 0) && (count < 40)){
  47.       count++;
  48.       delay_us(50);}
  49.     if( (count > 40) || (count < 14))      // Signal doesn't use RC5 protocol
  50.       goto ret;                            // Return
  51.     // End check (The received signal uses RC5 protocol)
  52.     if(count > 30)
  53.       delay_us(400);
  54.     else
  55.       delay_us(1300);
  56.     for(i = 0; i < 12; i++){
  57.       if(i == 0){
  58.         if(input(PIN_B0) == 1)   toggle = 0;
  59.         else                     toggle = 1;
  60.         }
  61.       else {
  62.         if(i < 6){                                //Read address bits
  63.           if(input(PIN_B0) == 1)
  64.             bit_clear(address, (5 - i));          //Clear bit (5-i)
  65.           else
  66.             bit_set(address, (5 - i));           //Set bit (5-i)
  67.           }
  68.         else {                                 //Read command bits
  69.           if(input(PIN_B0) == 1)
  70.             bit_clear(command, (11 - i));        //Clear bit (11-i)
  71.           else
  72.             bit_set(command, (11 - i));          //Set bit (11-i)
  73.           }
  74.         }
  75.       delay_us(1778);
  76.       }
  77.                      // Display results
  78.     address &= 0x1F;
  79.     command &= 0x3F;
  80.     lcd_putc('\f');
  81.     lcd_gotoxy(2, 1);
  82.     lcd_putc("Toggle bit:");
  83.     lcd_gotoxy(1, 2);
  84.     lcd_putc("Adds:");
  85.     lcd_gotoxy(9, 2);
  86.     lcd_putc("Cmd:");
  87.     lcd_gotoxy(14, 1);
  88.     printf(lcd_putc,"%1u",toggle);
  89.     lcd_gotoxy(6, 2);
  90.     printf(lcd_putc,"%2u",address);
  91.     lcd_gotoxy(14, 2);
  92.     printf(lcd_putc,"%2u",command);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement