prjbrook

Arduino code for below Tiny 15 serial

Aug 6th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 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.  
  5. unsigned int big = 60000;
  6. unsigned int big2;
  7. int bitNumber=0;
  8. byte bitLevel, byteResult;
  9. void setup() {
  10.  
  11. TCCR1B = 0x00; //Disable Timer1 while we set it up
  12. TCCR1A = 0x00; //Timer1 Control Reg A: Normal port operation
  13. TCNT1 = 0; //Reset Timer Count to 0
  14. //TIFR1 |= (1<<TOV1); //Make sure TOV1 flag starts off down
  15. pinMode(8, INPUT);
  16. pinMode(LED_BUILTIN, OUTPUT); //for flash signals eg LongLeadinTone
  17. Serial.begin(115200);
  18. //delay(3000);
  19. }
  20.  
  21.  
  22.  
  23. void loop() {
  24. Serial.println("TopL");
  25. seekLeadingTone2();
  26. for (int i =0;i<8;i++) {
  27. Serial.print(i);
  28. inputOneBitFrame();
  29.  
  30. }
  31. Serial.println(byteResult);
  32.  
  33. Serial.println("GotL");
  34. }
  35.  
  36. void inputOneBitFrame(void) { //wait for long pulse followed by optional short one. Record result in byteResult
  37. getFirstLowPulse(); //measure time taken and put into "big"
  38. waitForSecondPulse(); //wait big/2 for middle of second pulse, if any
  39. testBitLevel();
  40. recordBitLevel();
  41. }
  42.  
  43. void waitForSecondPulse(void) { //set up timer1 to mark time for half length of big initial low pulse
  44.  
  45. Serial.println("T");
  46. TIFR1 |= (1<<TOV1); //Clear Timer Overflow Flag
  47. big2 = 0xffff-big/2;
  48. TCNT1 = big2; // count up for half initial low pulse
  49. Serial.println(TCNT1);
  50. TCCR1B |= (1 << CS12)|(1<<CS10); //start timer with /1024 prescale
  51. //Now wait for Timer1 to roll over ffff->0
  52. while((TIFR1 & (1<<TOV1))==0); //loop til TOV1 goes high
  53. //------------------if here TOV1 gone high. Indicates rollover.
  54. TCCR1B=0; //stop timer 1 (plus kill other flags that don'r matter
  55.  
  56. Serial.println("G");
  57. }
  58. void getFirstLowPulse(void){ // wait for first part of bit frame to pass Hi-LO-Hi.Tme it if it's first pulse of byte.
  59. waitForPinToGoLow();
  60. TCNT1=0;
  61. startTimer1();
  62. waitForPinToGoHigh();
  63. stopTimer1();
  64.  
  65. big=TCNT1;
  66. }
  67.  
  68. void testBitLevel(void) { //look at second short pulse. Could be Hi=1 or Low =0
  69. bitLevel=0;
  70. if (digitalRead(8)==HIGH) bitLevel=1;
  71. waitForPinToGoHigh(); //in case i'ts a short low. Have to get ready for nex read of long pulse
  72. }
  73.  
  74. void recordBitLevel(void) { //write the 1 or zero into shifted result to build up byteResult in 8 long pulses (+ possible short ones)
  75. byteResult =(byteResult<<1) | bitLevel;
  76.  
  77. }
  78. //-----------------------------usual routines for egges from t15 stream....................
  79. void waitForPinToGoLow(void) { //Pin 8
  80. while (digitalRead(8) == HIGH) {}
  81. // Serial.println("pin8 now low...");
  82. }
  83. void waitForPinToGoHigh(void) { //Pin 8
  84. while (digitalRead(8) == LOW) {}
  85. //Serial.println("pin8 now high...");
  86. }
  87.  
  88. void startTimer1(void) { //101 in CS0..CS2. Gives /1024
  89. TCCR1B |= (1 << CS12) | (1 << CS10);
  90. }
  91. void stopTimer1(void) {
  92. TCCR1B &= 0B11111000; //CS0:2 = 0
  93. }
  94.  
  95. void seekLeadingTone2(void) {
  96. digitalWrite(LED_BUILTIN, HIGH);
  97. startTimer1();
  98. while(TCNT1<30000){ //10000 to low for /1024
  99. if (digitalRead(8) == LOW) {
  100. TCNT1=0;}
  101. }
  102. stopTimer1();
  103. digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
  104. TCNT1=0;
  105.  
  106. //--------------------------end of usual routines for t15 edges----------------------------
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment