transmit.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. void switch2transmit() {
  2. disableRx = true;
  3. // ------------------ GO TO TRANSMIT STATE -------------------------
  4. digitalWrite(txEnablePin, HIGH);
  5. delayMicroseconds( 400 );
  6. // ------------------ GO TO TRANSMIT STATE -------------------------
  7. }
  8. void switch2receive() {
  9. // ------------------ GO TO RECEIVE STATE -------------------------
  10. // From Transmission Mode to Receiver Mode follow the procedure:
  11. // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
  12. //After 200 us the device is ready for reception.
  13. digitalWrite(txEnablePin, LOW);
  14. delayMicroseconds( 40 );
  15. digitalWrite(enablePin, LOW);
  16. delayMicroseconds( 20 );
  17. digitalWrite(enablePin, HIGH);
  18. delayMicroseconds( 200 );
  19. // ------------------ GO TO RECEIVE STATE -------------------------
  20. disableRx = false;
  21. }
  22. void sendPulse(int high, int low) {
  23. digitalWrite(txPin, HIGH);
  24. delayMicroseconds( high );
  25. digitalWrite(txPin, LOW);
  26. delayMicroseconds( low );
  27. }
  28. // Nexa : http://elektronikforumet.com/wiki/index.php/RF_Protokoll_-_Nexa_sj%C3%A4lvl%C3%A4rande
  29. // "1" = 295 µs hög och 170 µs låg
  30. // "0" = 295 µs hög och 920 µs låg
  31. //
  32. // Etta skickas som 10 och Nolla som 01
  33. //
  34. // Dessa siffror från web-sidan stämmer inte med den Nexa-kontroll som jag har.
  35. // Jag har rättat dessa med siffror från verkligheten. Finns i define's nedan.
  36. // <NT01995080>
  37. // <NT01995090>
  38. // Timing for NEXA Remote controls
  39. #define HIGH_PULSE 240
  40. #define LOW_PULSE_0 300
  41. #define LOW_PULSE_1 1300
  42. #define SYNC_PULSE 2700
  43. #define STOP_PULSE 10000
  44. void send1() {
  45. sendPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  46. sendPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  47. }
  48. void send0() {
  49. sendPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  50. sendPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  51. }
  52. void sendSync() {
  53. sendPulse( HIGH_PULSE, SYNC_PULSE );
  54. }
  55. void sendStop() {
  56. sendPulse( HIGH_PULSE, STOP_PULSE );
  57. }
  58. void sendNexaCode(unsigned long int txCode) {
  59. unsigned long int code=0;
  60. unsigned long int orig_code=txCode;
  61. int i;
  62. int loop;
  63. if( 1 == 1 ) {
  64. // Turn off interrupts from the timer
  65. TIMSK1 = 0;
  66. switch2transmit();
  67. for( loop=6; loop>0; loop--) {
  68. code = orig_code;
  69. sendSync();
  70. for(i=0; i < 32; i++) {
  71. if( code & 0x80000000 ) {
  72. send1();
  73. }
  74. else {
  75. send0();
  76. }
  77. code = (code << 1);
  78. }
  79. sendStop();
  80. }
  81. switch2receive();
  82. // Enable interrupts again
  83. TIMSK1 |= (1 << OCIE1A);
  84. }
  85. }