Advertisement
Electgpl

PIC - Transmisor NEC

Sep 6th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. /* This RF transmitter is based on NEC protocol
  2.    Internal oscillator used @ 8MHz
  3. */
  4.  
  5. #include <16F883.h>
  6. #fuses NOMCLR, NOBROWNOUT, NOLVP, INTRC_IO
  7. #use delay(clock = 8MHz)
  8. #use fast_io(B)
  9.  
  10. void send_signal(unsigned int32 number){
  11.   int8 i;
  12.   // Send 9ms pulse
  13.   output_high(PIN_B4);
  14.   delay_ms(9);
  15.   // Send 4.5ms space
  16.   output_low(PIN_B4);
  17.   delay_us(4500);
  18.   // Send data (32 bits)
  19.   for(i = 0; i < 32; i++){
  20.     // If bit is 1 send 560us pulse and 1680us space
  21.     if(bit_test(number, 31 - i)){
  22.       output_high(PIN_B4);
  23.       delay_us(560);
  24.       output_low(PIN_B4);
  25.       delay_us(1680);
  26.     }
  27.     // If bit is 0 send 560us pulse and 560us space
  28.     else{
  29.       output_high(PIN_B4);
  30.       delay_us(560);
  31.       output_low(PIN_B4);
  32.       delay_us(560);
  33.     }
  34.   }
  35.   // Send end bit
  36.   output_high(PIN_B4);
  37.   delay_us(560);
  38.   output_low(PIN_B4);
  39.   delay_us(560);
  40. }
  41. void main() {
  42.   setup_oscillator(OSC_8MHZ);                    // Set internal oscillator to 8MHz
  43.   output_b(0);
  44.   set_tris_b(0x0F);                              // Configure RB0, RB1, RB2 and RB3 as inputs
  45.   port_b_pullups(0x0F);                          // Enable internal pull-ups for pins RB0,RB1,RB2 and RB3
  46.   while(TRUE){
  47.     if(!input(PIN_B0)){                          // If RB0 button is pressed
  48.       send_signal(0x00FF00FF);
  49.       delay_ms(500);
  50.     }
  51.     if(!input(PIN_B1)){                          // If RB1 button is pressed
  52.       send_signal(0x00FF807F);
  53.       delay_ms(500);
  54.     }
  55.     if(!input(PIN_B2)){                          // If RB2 button is pressed
  56.       send_signal(0x00FF40BF);
  57.       delay_ms(500);
  58.     }
  59.     if(!input(PIN_B3)){                          // If RB3 button is pressed
  60.       send_signal(0x00FF20DF);
  61.       delay_ms(500);
  62.     }
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement