You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
4.5 KiB

3 years ago
  1. #define hzclk 7
  2. #define hzbinary 6
  3. #define sigLED 11
  4. #include "LedControl.h"
  5. LedControl lc=LedControl(12,11,10,1);
  6. /* we always wait a bit between updates of the display */
  7. unsigned long delaytime=250;
  8. uint8_t hzclkval = 0;
  9. uint8_t hzbinval[30];
  10. uint8_t clkval = 0;
  11. uint32_t milliscompare = 0;
  12. uint8_t readyet = 0;
  13. uint8_t x = 0;
  14. uint16_t mainsfreq = 0;
  15. struct {
  16. unsigned int mainstwo : 14; //pesky binary wants a struct of
  17. } bitfieldA; //only exact amt of bits
  18. int temp = 0;
  19. void setup() {
  20. // put your setup code here, to run once:
  21. pinMode(hzclk, INPUT);
  22. pinMode(hzbinary, INPUT);
  23. pinMode(sigLED, OUTPUT);
  24. Serial.begin(115200);
  25. /*
  26. The MAX72XX is in power-saving mode on startup,
  27. we have to do a wakeup call
  28. */
  29. lc.shutdown(0,false);
  30. /* Set the brightness to a medium values */
  31. lc.setIntensity(0,8);
  32. /* and clear the display */
  33. lc.clearDisplay(0);
  34. }
  35. void loop() {
  36. if(digitalRead(hzbinary) == LOW){
  37. //this handles one clk cycle each
  38. //need to read 20 cycles
  39. clkval = 0;
  40. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  41. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  42. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  43. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  44. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  45. checkCLKandBIN();
  46. checkCLKandBIN();
  47. checkCLKandBIN();
  48. checkCLKandBIN();
  49. checkCLKandBIN();
  50. checkCLKandBIN();
  51. checkCLKandBIN();
  52. //Serial.println(clkval); //outputs 19 or 20
  53. ConvertArraytoBin();
  54. mainsfreq = 0;
  55. bitfieldA.mainstwo = 0;
  56. }
  57. delayMicroseconds(100); //must be lower than 1 millisecond, so mic
  58. /*if hzbinary is low
  59. *
  60. * while clk high, do nothing
  61. * clk++
  62. * sample hzbinary
  63. * while clk low do nothing
  64. */
  65. /*
  66. * //serial print is very slow, and breaks timing
  67. * the below is not usable
  68. * (DMA anyone? second processor anyone?)
  69. Serial.println("hzbinval 1 - 20:");
  70. for(x=0;x<21;x++){
  71. Serial.println(hzbinval[x]);
  72. }
  73. Serial.print("\n\n\n\n");
  74. */
  75. //sample on falling edge (middle of bit)
  76. }
  77. void checkCLKandBIN(void){
  78. /*
  79. * while clk high, do nothing
  80. * clk++
  81. * sample hzbinary
  82. * while clk low do nothing
  83. */
  84. //milliscompare = millis();
  85. while((hzclkval = digitalRead(hzclk)) == HIGH){
  86. if(readyet == 0){
  87. hzbinval[clkval] = digitalRead(hzbinary);
  88. clkval++;
  89. readyet = 1;
  90. }
  91. delayMicroseconds(10);
  92. }
  93. while((hzclkval = digitalRead(hzclk)) == LOW){
  94. delayMicroseconds(1);
  95. readyet = 0;
  96. }
  97. //digitalWrite(sigLED, HIGH);
  98. //delayMicroseconds(1);
  99. //digitalWrite(sigLED, LOW);
  100. //would be nice to add a picture to this source code (waveform)
  101. //or oscope picture
  102. //why isn't this possible?
  103. }
  104. //https://playground.arduino.cc/Main/LedControl/#Seg7Control
  105. void printNumber(int v) {
  106. int ones;
  107. int tens;
  108. int hundreds;
  109. int thousands;
  110. boolean negative;
  111. if(v < -9999 || v > 9999)
  112. return;
  113. if(v<0) {
  114. negative=true;
  115. v=v*-1;
  116. }
  117. ones=v%10;
  118. v=v/10;
  119. tens=v%10;
  120. v=v/10;
  121. hundreds=v%10;
  122. v=v/10;
  123. thousands=v;
  124. /*if(negative) {
  125. //print character '-' in the leftmost column
  126. lc.setChar(0,4,'-',false);
  127. }
  128. else {
  129. //print a blank in the sign column
  130. lc.setChar(0,4,' ',false);
  131. }*/
  132. //Now print the number digit by digit
  133. lc.setDigit(0,3,(byte)thousands,false);
  134. lc.setDigit(0,2,(byte)hundreds,false);
  135. lc.setDigit(0,1,(byte)tens,false);
  136. lc.setDigit(0,0,(byte)ones,false);
  137. }
  138. void ConvertArraytoBin (void){
  139. for(x=4;x<21;x++){
  140. if(hzbinval[x] == 1){
  141. bitfieldA.mainstwo = bitfieldA.mainstwo | (0b0000000000000000000001 << (x - 3));
  142. }
  143. }
  144. //Serial.println(bitfieldA.mainstwo,BIN);
  145. temp = bitfieldA.mainstwo >> 1;
  146. //almost, but no cigar. has timing issues.
  147. //ugly hack time
  148. temp = temp & 0b1011111111111;
  149. //results in a clock that is right most of the time.
  150. //but wrong every 20 or so counts
  151. //todo: fix timing of duino by removing digital read for
  152. //direct port reads
  153. if(temp == 5047){
  154. temp = 5998; //something, something, premature optimization...
  155. }
  156. if(temp == 5048){
  157. temp = 6000; //LUTs
  158. }
  159. Serial.println(temp,BIN);
  160. Serial.println(temp,DEC);
  161. Serial.print("\n");
  162. lc.clearDisplay(0);
  163. printNumber(temp);
  164. delay(delaytime);
  165. /*mainsfreq = mainsfreq >> 3;
  166. mainsfreq = mainsfreq & 0b0111111111111;
  167. Serial.println(mainsfreq,DEC);
  168. //111011011101
  169. //1011101101111
  170. //111011011101
  171. //11101101111*/
  172. }