Advertisement
Guest User

Untitled

a guest
Mar 19th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. const byte pinRF = 21; // Pin where RF Module is connected. If necessary, change this for your project
  2.  
  3. struct rfControl //Struct for RF Remote Controls
  4. {
  5. unsigned long addr; //ADDRESS CODE
  6. boolean btn1; //BUTTON 1
  7. boolean btn2; //BUTTON 2
  8. };
  9.  
  10. boolean ACT_HT6P20B_RX(struct rfControl &_rfControl){
  11.  
  12. static boolean startbit; //checks if start bit was identified
  13. static int counter; //received bits counter: 22 of Address + 2 of Data + 4 of EndCode (Anti-Code)
  14. static unsigned long buffer; //buffer for received data storage
  15.  
  16. int lambda; // on pulse clock width (if fosc = 2KHz than lambda = 500 us)
  17. int dur0, dur1; // pulses durations (auxiliary)
  18.  
  19. if (!startbit)
  20. {// Check the PILOT CODE until START BIT;
  21. dur0 = pulseIn(pinRF, LOW); //Check how long DOUT was "0" (ZERO) (refers to PILOT CODE)
  22.  
  23. //If time at "0" is between 9200 us (23 cycles of 400us) and 13800 us (23 cycles of 600 us).
  24. if((dur0 > 9200) && (dur0 < 13800) && !startbit)
  25. {
  26. //calculate wave length - lambda
  27. lambda = dur0 / 23;
  28.  
  29. //Reset variables
  30. dur0 = 0;
  31. buffer = 0;
  32. counter = 0;
  33.  
  34. startbit = true;
  35. }
  36. }
  37.  
  38. //If Start Bit is OK, then starts measure os how long the signal is level "1" and check is value is into acceptable range.
  39. if (startbit && counter < 28)
  40. {
  41. ++counter;
  42.  
  43. dur1 = pulseIn(pinRF, HIGH);
  44.  
  45. if((dur1 > 0.5 * lambda) && (dur1 < (1.5 * lambda))) //If pulse width at "1" is between "0.5 and 1.5 lambda", means that pulse is only one lambda, so the data é "1".
  46. {
  47. buffer = (buffer << 1) + 1; // add "1" on data buffer
  48. }
  49. else if((dur1 > 1.5 * lambda) && (dur1 < (2.5 * lambda))) //If pulse width at "1" is between "1.5 and 2.5 lambda", means that pulse is two lambdas, so the data é "0".
  50. {
  51. buffer = (buffer << 1); // add "0" on data buffer
  52. }
  53. else
  54. {
  55. //Reset the loop
  56. startbit = false;
  57. }
  58. }
  59.  
  60. //Check if all 28 bits were received (22 of Address + 2 of Data + 4 of Anti-Code)
  61. if (counter==28)
  62. {
  63. // Check if Anti-Code is OK (last 4 bits of buffer equal "0101")
  64. if ((bitRead(buffer, 0) == 1) && (bitRead(buffer, 1) == 0) && (bitRead(buffer, 2) == 1) && (bitRead(buffer, 3) == 0))
  65. {
  66. counter = 0;
  67. startbit = false;
  68.  
  69. //Get ADDRESS CODE from Buffer
  70. _rfControl.addr = buffer >> 6;
  71.  
  72. //Get Buttons from Buffer
  73. _rfControl.btn1 = bitRead(buffer,4);
  74. _rfControl.btn2 = bitRead(buffer,5);
  75.  
  76. //Serial.print("Address: "); Serial.println(_rfControl.addr, HEX);
  77. //Serial.print("Button1: "); Serial.println(_rfControl.btn1, BIN);
  78. //Serial.print("Button2: "); Serial.println(_rfControl.btn2, BIN);
  79. //Serial.println();
  80.  
  81. //If a valid data is received, return OK
  82. return true;
  83. }
  84. else
  85. {
  86. //Reset the loop
  87. startbit = false;
  88. }
  89. }
  90.  
  91. //If none valid data is received, return NULL and FALSE values
  92. _rfControl.addr = NULL;
  93. _rfControl.btn1 = NULL;
  94. _rfControl.btn2 = NULL;
  95.  
  96. return false;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement