prjbrook

T1PollingF - Arduino

Aug 10th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. //Tue Aug 6 11:05:12 NZST 2019. Having no lusjk with interrupts for Timer1
  2. //..so am trying polling.
  3. //11:36. waitForSecondPulse(void) works OK. Now going to incorporate that into main serial routine.
  4. //Thu Aug 8 13:41:20 NZST 2019. Version D OK if a bit jumpy with MacroExperimentH. Quite fast 122 bps.
  5.  
  6. //Fri Aug 9 09:54:18 NZST 2019 Version E ok but sometimes didn;t start well.Trying INPUT_PULLUP with low in leading tone.
  7. unsigned int big = 60000;
  8. unsigned int big2;
  9. int bitNumber=0;
  10. byte bitLevel, byteResult;
  11. byte byteArray[32];
  12. void setup() {
  13.  
  14. TCCR1B = 0x00; //Disable Timer1 while we set it up
  15. TCCR1A = 0x00; //Timer1 Control Reg A: Normal port operation
  16. TCNT1 = 0; //Reset Timer Count to 0
  17. //TIFR1 |= (1<<TOV1); //Make sure TOV1 flag starts off down
  18. pinMode(8, INPUT_PULLUP);
  19. pinMode(LED_BUILTIN, OUTPUT); //for flash signals eg LongLeadinTone
  20. Serial.begin(115200);
  21. //Serial.flush(); //take out if doesn't work
  22. //Serial.println("Starting"); // To start on new line if other stuff there.
  23. delay(2000);
  24. seekLeadingTone3(); //This is sought only once.
  25. }
  26.  
  27.  
  28.  
  29. void loop() {
  30.  
  31.  
  32. for (int j=0;j<32;j++) {
  33. // Serial.print("[");
  34. for (int i =0;i<8;i++) {
  35.  
  36. inputOneBitFrame();
  37. }
  38. Serial.print("] ");
  39. // Serial.println(big);
  40. // Serial.println(byteResult,HEX);
  41. byteArray[j]=byteResult;
  42. // Serial.println(byteArray[j],HEX);
  43. // Serial.flush();
  44. } //end of outer j loop
  45. Serial.println(big); //just once per byte stream to get idea of speed.
  46. showByteArray();
  47. blinkLEDForever();
  48. }
  49.  
  50. void inputOneBitFrame(void) { //wait for long pulse followed by optional short one. Record result in byteResult
  51. getFirstLowPulse(); //measure time taken and put into "big"
  52. //Serial.print("L");
  53. waitForSecondPulse(); //wait big/2 for middle of second pulse, if any
  54. testBitLevel();
  55. recordBitLevel();
  56. }
  57.  
  58. void waitForSecondPulse(void) { //set up timer1 to mark time for half length of big initial low pulse
  59.  
  60. // Serial.println("T");
  61. TIFR1 |= (1<<TOV1); //Clear Timer Overflow Flag
  62. big2 = 0xffff-big/2;
  63. TCNT1 = big2; // count up for half initial low pulse
  64. // Serial.println(TCNT1);
  65. TCCR1B |= (1 << CS12)|(1<<CS10); //start timer with /1024 prescale
  66. //Now wait for Timer1 to roll over ffff->0
  67. while((TIFR1 & (1<<TOV1))==0); //loop til TOV1 goes high
  68. //------------------if here TOV1 gone high. Indicates rollover.
  69. TCCR1B=0; //stop timer 1 (plus kill other flags that don'r matter
  70.  
  71. // Serial.println(" G2 ");
  72. }
  73. void getFirstLowPulse(void){ // wait for first part of bit frame to pass Hi-LO-Hi.Tme it if it's first pulse of byte.
  74. //Serial.print("v");
  75. waitForPinToGoLow();
  76. //Serial.print("w");
  77. TCNT1=0;
  78. startTimer1();
  79. waitForPinToGoHigh();
  80. stopTimer1();
  81.  
  82. big=TCNT1;
  83. }
  84.  
  85. void testBitLevel(void) { //look at second short pulse. Could be Hi=1 or Low =0
  86. bitLevel=0;
  87. if (digitalRead(8)==HIGH) bitLevel=1;
  88. waitForPinToGoHigh(); //in case i'ts a short low. Have to get ready for nex read of long pulse
  89. }
  90.  
  91. void recordBitLevel(void) { //write the 1 or zero into shifted result to build up byteResult in 8 long pulses (+ possible short ones)
  92. byteResult =(byteResult<<1) | bitLevel;
  93.  
  94. }
  95. //-----------------------------usual routines for egges from t15 stream....................
  96. void waitForPinToGoLow(void) { //Pin 8
  97. while (digitalRead(8) == HIGH) {}
  98. // Serial.println("pin8 now low...");
  99. }
  100. void waitForPinToGoHigh(void) { //Pin 8
  101. while (digitalRead(8) == LOW) {}
  102. //Serial.println("pin8 now high...");
  103. }
  104.  
  105. void startTimer1(void) { //101 in CS0..CS2. Gives /1024
  106. TCCR1B |= (1 << CS12) | (1 << CS10);
  107. }
  108. void stopTimer1(void) {
  109. TCCR1B &= 0B11111000; //CS0:2 = 0
  110. }
  111.  
  112. void seekLeadingTone2(void) {
  113. digitalWrite(LED_BUILTIN, HIGH);
  114. startTimer1();
  115. while(TCNT1<30000){ //10000 too low for /1024
  116. if (digitalRead(8) == LOW) {
  117. TCNT1=0;}
  118. }
  119. stopTimer1();
  120. digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
  121. TCNT1=0;
  122.  
  123. //--------------------------end of usual routines for t15 edges----------------------------
  124.  
  125. }
  126. void blinkLEDForever(void) { //flashing LED incates all bytes, cullently 4, have been received
  127. while(1){
  128. digitalWrite(LED_BUILTIN, HIGH);
  129. delay(200);
  130. digitalWrite(LED_BUILTIN, LOW);
  131. delay(200);
  132. }
  133. }
  134. void showByteArray(void) { //output 32 bytes from tiny15's eeprom
  135. for (int i=0;i<32;i++) {
  136. Serial.print(i);
  137. Serial.print("-->");
  138. Serial.println(byteArray[i],HEX);
  139. }
  140. }
  141. void seekLeadingTone3(void) {
  142. digitalWrite(LED_BUILTIN, HIGH);
  143. startTimer1();
  144. while(TCNT1<30000){
  145. if (digitalRead(8) == HIGH) {
  146. //if (digitalRead(8) == LOW) {
  147. TCNT1=0;}
  148. }
  149. stopTimer1();
  150. digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
  151. TCNT1=0;
  152. delay(200); //little blip between long LOW-HIGH
  153.  
  154. seekLeadingTone2(); //look for long HI (about 4 seconds)
  155. }
Advertisement
Add Comment
Please, Sign In to add comment