Naze32 clone with Frysky receiver
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

frsky_arduino_rx_complete.ino 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Frsky RX 2-way
  3. * By Midelic
  4. * on RCGroups
  5. * an adaptation from Kyrre Aalerud(Kreature)
  6. * 2012 Frsky rx demo code
  7. * http://www.rcgroups.com/forums/showthread.php?t=1667453
  8. * Thanks also to Phracturedblue and his deviation firmware
  9. **********************************
  10. */
  11. #include <avr/interrupt.h>
  12. #include <EEPROM.h>
  13. #include "iface_cc2500.h"
  14. //#define DEBUG
  15. //#define DEBUG_RSSI
  16. //#define DEBUG0
  17. //#define DEBUG1
  18. //#define DEBUG2
  19. //#define DEBUG3
  20. //#define DEBUG4
  21. //#define DEBUG5
  22. //#define RSSI_OVER_PPM 7
  23. #define FAILSAFE
  24. #define SPIBB
  25. //#define SPIHW
  26. #if defined SPIHW
  27. #include <SPI.h>
  28. #endif
  29. // Used for RSSI_OVER_PPM
  30. int rssi;
  31. const int rssi_offset = 71;
  32. const int rssi_min = -103;
  33. const int rssi_max = -96;
  34. #define chanel_number 8 //set the number of chanels
  35. #define SEEK_CHANSKIP 13
  36. #define MAX_MISSING_PKT 20
  37. #define PPM_FrLen 22500
  38. #define PPM_PulseLen 300
  39. #define default_servo_value 1500
  40. #define onState 0 //set polarity of the pulses: 1 is positive, 0 is negative
  41. #define sigPin 10
  42. #if defined(SPIBB)
  43. #define MO_pin 5 //D5
  44. #define MI_pin 6 //D6
  45. #define SCLK_pin 4 //D4
  46. #define CS 2 //D2
  47. #define GDO_pin 3 //D3 GDO0 pin
  48. #define SCK_on PORTD |= 0x10 //D4
  49. #define SCK_off PORTD &= 0xEF //D4
  50. #define MO_on PORTD |= 0x20 //D5
  51. #define MO_off PORTD &= 0xDF //D5
  52. #define MI_1 (PIND & 0x40) == 0x40 //D6 input
  53. #define MI_0 (PIND & 0x40) == 0x00 //D6
  54. #define CS_on PORTD |= 0x04 //D2
  55. #define CS_off PORTD &= 0xFB //D2
  56. #define GDO_1 (PIND & 0x08) == 0x08 //D3 input
  57. #define GDO_0 (PIND & 0x08) == 0x00 //D3
  58. #endif
  59. #define bind_pin A0 //C0 bind plug also servo8
  60. #define Servo1_OUT 7 //Servo1(D7)
  61. #define Servo2_OUT 8 //Servo2(B0)
  62. #define Servo3_OUT 9 //Servo3(B1)
  63. #define Servo4_OUT 10 //Servo4(B2)//PPM pin
  64. #define Servo5_OUT 11 //Servo5(B3)
  65. #define Servo6_OUT 12 //Servo6(B4)
  66. #define Servo7_OUT 13 //Servo7(B5)
  67. #define Servo8_OUT A0 //Servo8(C0)
  68. #define Servo1_OUT_HIGH PORTD |= _BV(7) //Servo1(D7)
  69. #define Servo2_OUT_HIGH PORTB |= _BV(0) //Servo2(B0)
  70. #define Servo3_OUT_HIGH PORTB |= _BV(1) //Servo3(B1)
  71. #define Servo4_OUT_HIGH PORTB |= _BV(2) //Servo4(B2)
  72. #define Servo5_OUT_HIGH PORTB |= _BV(3) //Servo5(B3)
  73. #define Servo6_OUT_HIGH PORTB |= _BV(4) //Servo6(B4)
  74. #define Servo7_OUT_HIGH PORTB |= _BV(5) //Servo7(B5)
  75. #define Servo8_OUT_HIGH PORTC |= _BV(0) //Servo8(C0)
  76. #define Servo_Ports_LOW PORTD &= 0x7F ; PORTB &= 0xc0; PORTC &=0xFE //all servos low
  77. #define LED_pin A3
  78. #define LED_ON PORTC |= _BV(3)
  79. #define LED_OFF PORTC &= ~_BV(3)
  80. #define NOP() __asm__ __volatile__("nop")
  81. // Globals:
  82. static uint8_t ccData[27];
  83. static uint8_t ccLen;
  84. static boolean packet = false;
  85. //static uint16_t time;
  86. static uint8_t channr;
  87. static uint8_t missingPackets = 0;
  88. uint8_t calData[60];
  89. uint8_t hopData[60];
  90. uint8_t listLength;
  91. uint8_t txid[2];
  92. static uint8_t counter = 0;
  93. volatile uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
  94. volatile byte scale;
  95. static byte jumper1 = 0;
  96. static byte jumper2 = 0;
  97. volatile int ppm[chanel_number];
  98. static uint16_t total_servo_time = 0;
  99. static byte cur_chan_numb = 0;
  100. boolean debug = false;
  101. int count = 0;
  102. uint16_t c[8];
  103. boolean debug2 = true;
  104. boolean debug3 = false;
  105. void setup()
  106. {
  107. #if defined(SPIBB)
  108. pinMode(MO_pin, OUTPUT); //SI
  109. pinMode(MI_pin, INPUT); //SO
  110. pinMode(SCLK_pin, OUTPUT); //SCLK
  111. pinMode(CS, OUTPUT); //CS output
  112. pinMode(GDO_pin, INPUT); //GDO0 pin
  113. SCK_off; //start sck low
  114. MO_off; //low
  115. #endif
  116. pinMode(LED_pin, OUTPUT);
  117. CS_on;
  118. #if defined(SPIHW)
  119. pinMode(CS, OUTPUT);
  120. pinMode(GDO_pin, INPUT);
  121. SPI.setClockDivider(SPI_CLOCK_DIV2);
  122. SPI.setBitOrder( MSBFIRST);
  123. SPI.begin();
  124. #endif
  125. pinMode(Servo1_OUT, OUTPUT); //Servo1
  126. pinMode(Servo2_OUT, OUTPUT); //Servo2
  127. pinMode(Servo3_OUT, OUTPUT); //Servo3
  128. pinMode(Servo4_OUT, OUTPUT); //Servo4
  129. //
  130. pinMode(Servo6_OUT, OUTPUT); //Servo6
  131. pinMode(Servo7_OUT, OUTPUT); //Servo7
  132. pinMode(Servo8_OUT, OUTPUT); //Servo8
  133. //Servo8_OUT_HIGH;//bindpin pullup
  134. #if defined DEBUG
  135. Serial.begin(115200);
  136. int8_t i;
  137. Serial.print("PartNum ");
  138. i = cc2500_readReg(CC2500_30_PARTNUM + CC2500_READ_BURST);
  139. Serial.println(i);
  140. delay(10);
  141. Serial.print("Version ");
  142. i = cc2500_readReg(CC2500_31_VERSION + CC2500_READ_BURST);
  143. Serial.println(i);
  144. #endif
  145. #if F_CPU == 16000000
  146. scale = 2;
  147. #elif F_CPU == 8000000
  148. scale = 1;
  149. #else
  150. #error // 8 or 16MHz only !
  151. #endif
  152. initialize(1); //binding
  153. binding();
  154. pinMode(Servo8_OUT, OUTPUT); //Servo8.bind pin is set to output again.
  155. initialize(0); //data
  156. jumper1 = PPM_jumper(); // Check the possible jumper positions for changing the receiver mode.
  157. if (jumper1 == 1) {
  158. //initiallize default ppm values
  159. for (int i = 0; i < chanel_number; i++) {
  160. ppm[i] = default_servo_value;
  161. }
  162. pinMode(sigPin, OUTPUT);
  163. digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off)
  164. }
  165. config_timer();
  166. channr = 0;
  167. cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);//0A-hop
  168. cc2500_writeReg(CC2500_23_FSCAL3, 0x89); //23-89
  169. cc2500_strobe(CC2500_SRX);
  170. }
  171. void updateRSSI() {
  172. #if defined(RSSI_OVER_PPM)
  173. int rssi_dec = cc2500_readReg(CC2500_34_RSSI | CC2500_READ_BURST);
  174. if (rssi_dec < 128) {
  175. rssi = ((rssi_dec / 2) - rssi_offset) & 0x7f;
  176. } else {
  177. rssi = (((rssi_dec - 256) / 2)) - rssi_offset;
  178. }
  179. #if defined(DEBUG_RSSI2)
  180. Serial.print(millis());
  181. Serial.print("\t");
  182. Serial.println(rssi);
  183. #endif
  184. rssi = constrain(rssi, rssi_min, rssi_max);
  185. #endif
  186. }
  187. void loop()
  188. {
  189. unsigned long time = micros();
  190. #if defined(FAILSAFE)
  191. if (missingPackets > 170) {
  192. //**************************************
  193. //noInterrupts();//
  194. //digitalWrite(sigPin, LOW);
  195. //Servo_Ports_LOW;
  196. //**********************************************
  197. missingPackets = 0;
  198. int i;
  199. for (i = 0; i < 8; i++) {
  200. Servo_data[i] = 1000;
  201. ppm[i] = 1000;
  202. }
  203. }
  204. #endif
  205. while (1) {
  206. if ((micros() - time) > 9000) {
  207. missingPackets++;
  208. cc2500_strobe(CC2500_SIDLE);
  209. if (missingPackets > MAX_MISSING_PKT) {
  210. nextChannel(SEEK_CHANSKIP);
  211. LED_OFF;
  212. counter++;
  213. if (counter > (MAX_MISSING_PKT << 1))
  214. LED_ON;
  215. if (counter == (MAX_MISSING_PKT << 2)) counter = 0;
  216. break;
  217. } else
  218. nextChannel(1);
  219. break;
  220. }
  221. if (GDO_1) {
  222. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  223. if (ccLen > 20)
  224. ccLen = 20;//
  225. if (ccLen) {
  226. cc2500_readFifo((uint8_t *)ccData, ccLen);
  227. if (ccData[ccLen - 1] & 0x80) { // Only if correct CRC
  228. missingPackets = 0;
  229. if (ccData[0] == 0x11) { // Correct length
  230. if ((ccData[1] == txid[0]) && (ccData[2] == txid[1])) { // Only if correct txid
  231. packet = true;
  232. //sei(); ///////////////////////////////////////////////////////////////////////////////////////
  233. updateRSSI();
  234. cc2500_strobe(CC2500_SIDLE);
  235. nextChannel(1);
  236. LED_ON;
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. if (packet == true) {
  245. packet = false;
  246. debug = true;
  247. //cli();
  248. c[0] = (uint16_t)(ccData[10] & 0x0F) << 8 | ccData[6];
  249. c[1] = (uint16_t)(ccData[10] & 0xF0) << 4 | ccData[7];
  250. c[2] = (uint16_t)(ccData[11] & 0x0F) << 8 | ccData[8];
  251. c[3] = (uint16_t)(ccData[11] & 0xF0) << 4 | ccData[9];
  252. c[4] = (uint16_t)(ccData[16] & 0x0F) << 8 | ccData[12];
  253. c[5] = (uint16_t)(ccData[16] & 0xF0) << 4 | ccData[13];
  254. c[6] = (uint16_t)(ccData[17] & 0x0F) << 8 | ccData[14];
  255. c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
  256. //sei();
  257. for (int i = 0; i < 8; i++) {
  258. Servo_data[i] = 0.67 * c[i];
  259. if (Servo_data[i] < 900) { //added new
  260. Servo_data[i] = 1500; //added new
  261. Servo_data[2] = 1000;
  262. }
  263. ppm[i] = Servo_data[i];
  264. }
  265. #if defined(RSSI_OVER_PPM)
  266. ppm[RSSI_OVER_PPM] = map(rssi, rssi_min, rssi_max, 1000, 2000);
  267. #endif
  268. #if defined(DEBUG5)
  269. //Serial.println(rssi);
  270. #endif
  271. #if defined(DEBUG0)
  272. for (int i = 0; i < 8; i++) {
  273. Serial.print(" ");
  274. Serial.print(Servo_data[i]);
  275. Serial.print(" ");
  276. }
  277. Serial.println(" ");
  278. #endif
  279. }
  280. cc2500_strobe(CC2500_SRX);
  281. if (debug == true) {
  282. debug = false;
  283. #if defined(DEBUG2)
  284. Serial.println(ccData[3], HEX);
  285. #endif
  286. }
  287. }
  288. void initialize(int bind)
  289. {
  290. cc2500_resetChip();
  291. cc2500_writeReg(CC2500_02_IOCFG0, 0x01); // reg 0x02: RX complete interrupt(GDO0)
  292. cc2500_writeReg(CC2500_17_MCSM1, 0x0C); // reg 0x17:
  293. cc2500_writeReg(CC2500_18_MCSM0, 0x18); // reg 0x18:
  294. cc2500_writeReg(CC2500_06_PKTLEN, 0x19); // Leave room for appended status bytes
  295. cc2500_writeReg(CC2500_08_PKTCTRL0, 0x05); // reg 0x08:
  296. cc2500_writeReg(CC2500_3E_PATABLE, 0xFF); //
  297. cc2500_writeReg(CC2500_0B_FSCTRL1, 0x08); // reg 0x0B:
  298. cc2500_writeReg(CC2500_0C_FSCTRL0, 0x00); // reg 0x0C
  299. cc2500_writeReg(CC2500_0D_FREQ2, 0x5C); // reg 0x0D
  300. cc2500_writeReg(CC2500_0E_FREQ1, 0x76); // reg 0x0E
  301. cc2500_writeReg(CC2500_0F_FREQ0, 0x27); // reg 0x0F
  302. cc2500_writeReg(CC2500_10_MDMCFG4, 0xAA); // reg 0x10
  303. cc2500_writeReg(CC2500_11_MDMCFG3, 0x39); // reg 0x11
  304. cc2500_writeReg(CC2500_12_MDMCFG2, 0x11); // reg 0x12
  305. cc2500_writeReg(CC2500_13_MDMCFG1, 0x23); // reg 0x13
  306. cc2500_writeReg(CC2500_14_MDMCFG0, 0x7A); // reg 0x14
  307. cc2500_writeReg(CC2500_15_DEVIATN, 0x42); // reg 0x15
  308. cc2500_writeReg(CC2500_19_FOCCFG, 0x16); // reg 0x16
  309. cc2500_writeReg(CC2500_1A_BSCFG, 0x6C); // reg 0x1A
  310. cc2500_writeReg(CC2500_1B_AGCCTRL2, 0x03); // reg 0x1B
  311. cc2500_writeReg(CC2500_1C_AGCCTRL1, 0x40); // reg 0x1C
  312. cc2500_writeReg(CC2500_1D_AGCCTRL0, 0x91); // reg 0x1D
  313. cc2500_writeReg(CC2500_21_FREND1, 0x56); // reg 0x21:
  314. cc2500_writeReg(CC2500_22_FREND0, 0x10); // reg 0x22:
  315. cc2500_writeReg(CC2500_23_FSCAL3, 0xA9); // reg 0x23:
  316. cc2500_writeReg(CC2500_24_FSCAL2, 0x05); // reg 0x24:
  317. cc2500_writeReg(CC2500_25_FSCAL1, 0x00); // reg 0x25
  318. cc2500_writeReg(CC2500_26_FSCAL0, 0x11); // reg 0x26
  319. cc2500_writeReg(CC2500_29_FSTEST, 0x59); // reg 0x29
  320. cc2500_writeReg(CC2500_2C_TEST2, 0x88); // reg 0x2C
  321. cc2500_writeReg(CC2500_2D_TEST1, 0x31); // reg 0x2D
  322. cc2500_writeReg(CC2500_2E_TEST0, 0x0B); // reg 0x2E
  323. cc2500_writeReg(CC2500_03_FIFOTHR, 0x0F); // reg 0x03:
  324. cc2500_writeReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
  325. cc2500_strobe(CC2500_SIDLE); // Go to idle...
  326. cc2500_writeReg(CC2500_07_PKTCTRL1, 0x0D); // reg 0x07 hack: Append status, filter by address, auto-flush on bad crc, PQT=0
  327. //cc2500_writeReg(CC2500_0C_FSCTRL0, 0); // Frequency offset...
  328. cc2500_writeReg(CC2500_0C_FSCTRL0, bind ? 0x00 : count); // Frequency offset hack
  329. cc2500_writeReg(CC2500_0A_CHANNR, 0x00);
  330. }
  331. // Receives complete bind setup
  332. void getBind(void)
  333. {
  334. cc2500_strobe(CC2500_SRX);//enter in rx mode
  335. listLength = 0;
  336. boolean eol = false;
  337. // len|bind |tx id|idx|h0|h1|h2|h3|h4|00|00|00|00|00|00|01
  338. // Start by getting bind packet 0 and the txid
  339. // 0 1 2 txid0(3) txid1()4 5 6 7 8 9 10 11 12 13 14 15 16 17
  340. //ccdata //11 03 01 d7 2d 00 00 1e 3c 5b 78 00 00 00 00 00 00 01
  341. //11 03 01 19 3e 00 02 8e 2f bb 5c 00 00 00 00 00 00 01
  342. while (1) {
  343. if (GDO_1) {
  344. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  345. if (ccLen) {
  346. cc2500_readFifo((uint8_t *)ccData, ccLen);
  347. if (ccData[ccLen - 1] & 0x80) {
  348. if (ccData[2] == 0x01) {
  349. if (ccData[5] == 0x00) {
  350. txid[0] = ccData[3];
  351. txid[1] = ccData[4];
  352. for (uint8_t n = 0; n < 5; n++) {
  353. hopData[ccData[5] + n] = ccData[6 + n];
  354. }
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. #if defined(DEBUG)
  363. Serial.print(txid[0], HEX);
  364. Serial.println(txid[1], HEX);
  365. #endif
  366. for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
  367. while (1) {
  368. if (GDO_1) {
  369. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  370. if (ccLen) {
  371. cc2500_readFifo((uint8_t *)ccData, ccLen);
  372. if (ccData[ccLen - 1] & 0x80) {
  373. if (ccData[2] == 0x01) {
  374. if(debug3) {
  375. Serial.print("ccLen = ");
  376. Serial.println(ccLen);
  377. }
  378. if ((ccData[3] == txid[0]) && (ccData[4] == txid[1])) {
  379. if(debug3)
  380. {
  381. Serial.print("ccData[5] = ");
  382. Serial.println(ccData[5]);
  383. Serial.print("bindIdx = ");
  384. Serial.println(bindIdx);
  385. }
  386. if (ccData[5] == bindIdx) {
  387. for (uint8_t n = 0; n < 5; n++) {
  388. if(debug3)
  389. {
  390. Serial.print("ccData[6 + n] = ");
  391. Serial.println(ccData[6 + n]);
  392. Serial.print("ccData[ccLen - 3] = ");
  393. Serial.println(ccData[ccLen - 3]);
  394. }
  395. //if (ccData[6 + n] == ccData[ccLen - 3]) {
  396. if (ccData[6 + n] <= 3) {
  397. eol = true;
  398. #if defined(DEBUG)
  399. Serial.print("listLength: ");
  400. Serial.println(listLength);
  401. #endif
  402. listLength = ccData[5] + n;
  403. break;
  404. }
  405. hopData[ccData[5] + n] = ccData[6 + n];
  406. }
  407. break;
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. #if defined(DEBUG)
  416. Serial.println(bindIdx / 5);
  417. #endif
  418. if (eol) break; // End of list found, stop!
  419. }
  420. #if defined(DEBUG)
  421. listLength = 47;
  422. Serial.println("jumpIdx list: ");
  423. for (uint8_t jumpIdx = 0; jumpIdx < (listLength); jumpIdx++) {
  424. Serial.print(" ");
  425. Serial.print(hopData[jumpIdx], HEX);
  426. Serial.print(" ");
  427. }
  428. Serial.println(" ");
  429. #endif
  430. Store_bind();
  431. cc2500_strobe(CC2500_SIDLE); // Back to idle
  432. }
  433. ISR(TIMER1_COMPA_vect)
  434. {
  435. TCNT1 = 0;
  436. if (jumper1 == 0) {
  437. pinMode(Servo5_OUT, OUTPUT);
  438. Servo_Ports_LOW;
  439. //code for servo.
  440. cur_chan_numb++; //next servo
  441. if (cur_chan_numb < chanel_number) {
  442. total_servo_time += Servo_data[cur_chan_numb] * scale;
  443. OCR1A = Servo_data[cur_chan_numb] * scale;
  444. } else {
  445. OCR1A = PPM_FrLen * scale - total_servo_time;
  446. cur_chan_numb = 0xff;
  447. total_servo_time = 0;
  448. }
  449. switch (cur_chan_numb) {
  450. case 0:
  451. Servo1_OUT_HIGH;
  452. break;
  453. case 1:
  454. Servo2_OUT_HIGH;
  455. break;
  456. case 2:
  457. Servo3_OUT_HIGH;
  458. break;
  459. case 3:
  460. Servo4_OUT_HIGH;
  461. break;
  462. case 4:
  463. Servo5_OUT_HIGH;
  464. break;
  465. case 5:
  466. Servo6_OUT_HIGH;
  467. break;
  468. case 6:
  469. Servo7_OUT_HIGH;
  470. break;
  471. case 7:
  472. Servo8_OUT_HIGH;
  473. break;
  474. }
  475. } else {
  476. static boolean state = true;
  477. pinMode(sigPin, OUTPUT);
  478. digitalWrite(sigPin, !onState);
  479. if (state) {
  480. digitalWrite(sigPin, onState);
  481. OCR1A = PPM_PulseLen * scale;
  482. state = false;
  483. } else {
  484. static byte cur_chan_numb;
  485. static unsigned int calc_rest;
  486. // digitalWrite(sigPin, !onState);//PPM on servo4 pin10
  487. state = true;
  488. if (cur_chan_numb >= chanel_number) {
  489. cur_chan_numb = 0;
  490. calc_rest = calc_rest + PPM_PulseLen;//
  491. OCR1A = (PPM_FrLen - calc_rest) * scale;
  492. calc_rest = 0;
  493. } else {
  494. OCR1A = (ppm[cur_chan_numb] - PPM_PulseLen) * scale;
  495. calc_rest = calc_rest + ppm[cur_chan_numb];
  496. cur_chan_numb++;
  497. }
  498. }
  499. }
  500. }
  501. void config_timer()
  502. {
  503. OCR1A = 50 * scale;
  504. cli();
  505. TCCR1A = 0; //
  506. TCCR1B = 0;
  507. TCCR1B |= (1 << WGM12);
  508. TCCR1B |= (1 << CS11);
  509. TIMSK1 |= (1 << OCIE1A);
  510. sei();
  511. }
  512. void nextChannel(uint8_t skip)
  513. {
  514. channr += skip;//
  515. if (channr >= listLength) channr -= listLength;
  516. cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);
  517. cc2500_writeReg(CC2500_23_FSCAL3, 0x89);
  518. }
  519. void binding()
  520. {
  521. jumper2 = bind_jumper();
  522. while (1) {
  523. if (jumper2 == 0) { //bind complete or no bind
  524. uint8_t i;
  525. uint8_t adr = 100;
  526. for (i = 0; i < 2; i++) {
  527. txid[i] = EEPROM.read(adr + i);
  528. }
  529. if (txid[0] == 0xff && txid[1] == 0xff) {
  530. // No valid txid, forcing bind
  531. jumper2 = 1;
  532. continue;
  533. }
  534. for (i = 0; i < sizeof(hopData); i++) {
  535. hopData[i] = EEPROM.read(adr + 10 + i);
  536. }
  537. listLength = EEPROM.read(adr + 100);
  538. count = EEPROM.read(adr + 101);
  539. break;
  540. } else {
  541. LED_ON;
  542. tunning();
  543. //count=0xC8;//for test
  544. cc2500_writeReg(CC2500_0C_FSCTRL0, count);
  545. int adr = 100;
  546. EEPROM.write(adr + 101, count);
  547. getBind();
  548. while (1) {
  549. LED_ON;
  550. delay(500);
  551. LED_OFF;
  552. delay(500);
  553. }
  554. }
  555. }
  556. }
  557. void tunning()
  558. {
  559. cc2500_strobe(CC2500_SRX);//enter in rx mode
  560. int count1 = 0;
  561. while (1) {
  562. count1++;
  563. if (count >= 250) {
  564. count = 0;
  565. }
  566. if (count1 > 3000) {
  567. count1 = 0;
  568. cc2500_writeReg(CC2500_0C_FSCTRL0, count); // Frequency offset hack
  569. count = count + 10;
  570. //cc2500_strobe(CC2500_SRX);//enter in rx mode
  571. }
  572. if (GDO_1) {
  573. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  574. if (ccLen) {
  575. cc2500_readFifo((uint8_t *)ccData, ccLen);
  576. if (ccData[ccLen - 1] & 0x80) {
  577. if (ccData[2] == 0x01) {
  578. if (ccData[5] == 0x00) {
  579. break;
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. #if defined(DEBUG1)
  587. Serial.println(count, HEX);
  588. #endif
  589. }
  590. void Store_bind()
  591. {
  592. uint8_t i;
  593. int adr = 100;
  594. for (i = 0; i < 2; i++) {
  595. EEPROM.write(adr + i, txid[i]);
  596. }
  597. for (i = 0; i < sizeof(hopData); i++) {
  598. EEPROM.write(adr + 10 + i, hopData[i]);
  599. }
  600. EEPROM.write(adr + 100, listLength);
  601. }
  602. unsigned char PPM_jumper(void)
  603. {
  604. // PPM Selection (jumper between Ch1 and ch3)
  605. pinMode(Servo3_OUT, INPUT); //CH3 input
  606. digitalWrite(Servo3_OUT, HIGH); // pull up
  607. digitalWrite(Servo1_OUT, HIGH); // CH1 is HIGH
  608. delayMicroseconds(1);
  609. if ( digitalRead(Servo3_OUT) == HIGH) {
  610. digitalWrite(Servo1_OUT, LOW); // CH1 is LOW
  611. delayMicroseconds(1);
  612. if (digitalRead(Servo3_OUT) == LOW) { // OK jumper plugged
  613. pinMode(Servo3_OUT, OUTPUT);
  614. return 1;
  615. }
  616. }
  617. pinMode(Servo3_OUT, OUTPUT);
  618. return 0; // servo PWM by default
  619. }
  620. //bind jumper
  621. unsigned char bind_jumper(void)
  622. {
  623. pinMode(bind_pin, INPUT_PULLUP);//pull up
  624. if ( digitalRead(bind_pin) == LOW) {
  625. delayMicroseconds(1);
  626. return 1;
  627. }
  628. return 0;
  629. }