transmit.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include "app.h"
  3. #include "main.h"
  4. #include "led_blink.h"
  5. #include "transmit.h"
  6. #include "stm32l1xx_hal.h"
  7. #include "stm32l1xx_hal_gpio.h"
  8. // For debug
  9. __IO uint32_t time_start=0,time_stop=0,time_tot=0;
  10. void switch2transmit() {
  11. disableRx = true;
  12. // ------------------ GO TO TRANSMIT STATE -------------------------
  13. digitalWrite(txEnablePin, HIGH);
  14. delayMicroseconds( 400 );
  15. // ------------------ GO TO TRANSMIT STATE -------------------------
  16. time_start++;
  17. time_stop++;
  18. time_tot++;
  19. }
  20. void switch2receive() {
  21. // ------------------ GO TO RECEIVE STATE -------------------------
  22. // From Transmission Mode to Receiver Mode follow the procedure:
  23. // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
  24. //After 200 us the device is ready for reception.
  25. digitalWrite(txEnablePin, LOW);
  26. delayMicroseconds( 40 );
  27. digitalWrite(enablePin, LOW);
  28. delayMicroseconds( 20 );
  29. digitalWrite(enablePin, HIGH);
  30. delayMicroseconds( 200 );
  31. // ------------------ GO TO RECEIVE STATE -------------------------
  32. disableRx = false;
  33. }
  34. void sendPulse(int high, int low) {
  35. digitalWrite(txPin, HIGH);
  36. delayMicroseconds( high );
  37. digitalWrite(txPin, LOW);
  38. delayMicroseconds( low );
  39. }
  40. // Nexa : http://elektronikforumet.com/wiki/index.php/RF_Protokoll_-_Nexa_sj%C3%A4lvl%C3%A4rande
  41. // "1" = 295 µs hög och 170 µs låg
  42. // "0" = 295 µs hög och 920 µs låg
  43. //
  44. // Etta skickas som 10 och Nolla som 01
  45. //
  46. // Dessa siffror från web-sidan stämmer inte med den Nexa-kontroll som jag har.
  47. // Jag har rättat dessa med siffror från verkligheten. Finns i define's nedan.
  48. // <NT01995080>
  49. // <NT01995090>
  50. // Timing for NEXA Remote controls
  51. #define HIGH_PULSE 240
  52. #define LOW_PULSE_0 300
  53. #define LOW_PULSE_1 1320
  54. #define SYNC_PULSE 2700
  55. #define STOP_PULSE 10000
  56. void send1() {
  57. sendPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  58. sendPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  59. }
  60. void send0() {
  61. sendPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  62. sendPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  63. }
  64. void sendSync() {
  65. sendPulse( HIGH_PULSE, SYNC_PULSE );
  66. }
  67. void sendStop() {
  68. sendPulse( HIGH_PULSE, STOP_PULSE );
  69. }
  70. void sendNexaCodeStr(unsigned char *c) {
  71. unsigned long int orig_code=0;
  72. if( sscanf((const char *)&(c[2]),"%08lX",&orig_code) == 1 ) {
  73. sendNexaCodeNo(orig_code);
  74. }
  75. }
  76. void sendNexaCodeNo(unsigned long orig_code) {
  77. unsigned long code=0;
  78. //unsigned long int orig_code=0;
  79. int i;
  80. int loop;
  81. blinkTheTxLED();
  82. if( 1 ) {
  83. switch2transmit();
  84. for( loop=4; loop>0; loop--) {
  85. code = orig_code;
  86. time_start = micros();
  87. sendSync();
  88. for(i=0; i < 32; i++) {
  89. if( code & 0x80000000 ) {
  90. send1();
  91. }
  92. else {
  93. send0();
  94. }
  95. code = (code << 1);
  96. }
  97. sendStop();
  98. time_stop = micros();
  99. time_tot = time_stop - time_start; // Should be 71580uS
  100. time_start++;
  101. time_stop++;
  102. }
  103. switch2receive();
  104. }
  105. }