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.

151 lines
3.3 KiB

3 years ago
  1. #define hzclk 7
  2. #define hzbinary 6
  3. #define sigLED 11
  4. uint8_t hzclkval = 0;
  5. uint8_t hzbinval[30];
  6. uint8_t clkval = 0;
  7. uint32_t milliscompare = 0;
  8. uint8_t readyet = 0;
  9. uint8_t x = 0;
  10. uint16_t mainsfreq = 0;
  11. struct {
  12. unsigned int mainstwo : 14; //pesky binary wants a struct of
  13. } bitfieldA; //only exact amt of bits
  14. int temp = 0;
  15. void setup() {
  16. // put your setup code here, to run once:
  17. pinMode(hzclk, INPUT);
  18. pinMode(hzbinary, INPUT);
  19. pinMode(sigLED, OUTPUT);
  20. Serial.begin(115200);
  21. }
  22. void loop() {
  23. if(digitalRead(hzbinary) == LOW){
  24. //this handles one clk cycle each
  25. //need to read 20 cycles
  26. clkval = 0;
  27. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  28. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  29. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  30. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  31. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  32. checkCLKandBIN();
  33. checkCLKandBIN();
  34. checkCLKandBIN();
  35. checkCLKandBIN();
  36. checkCLKandBIN();
  37. checkCLKandBIN();
  38. checkCLKandBIN();
  39. //Serial.println(clkval); //outputs 19 or 20
  40. ConvertArraytoBin();
  41. mainsfreq = 0;
  42. bitfieldA.mainstwo = 0;
  43. }
  44. delayMicroseconds(100); //must be lower than 1 millisecond, so mic
  45. /*if hzbinary is low
  46. *
  47. * while clk high, do nothing
  48. * clk++
  49. * sample hzbinary
  50. * while clk low do nothing
  51. */
  52. /*
  53. * //serial print is very slow, and breaks timing
  54. * the below is not usable
  55. * (DMA anyone? second processor anyone?)
  56. Serial.println("hzbinval 1 - 20:");
  57. for(x=0;x<21;x++){
  58. Serial.println(hzbinval[x]);
  59. }
  60. Serial.print("\n\n\n\n");
  61. */
  62. //sample on falling edge (middle of bit)
  63. }
  64. void checkCLKandBIN(void){
  65. /*
  66. * while clk high, do nothing
  67. * clk++
  68. * sample hzbinary
  69. * while clk low do nothing
  70. */
  71. //milliscompare = millis();
  72. while((hzclkval = digitalRead(hzclk)) == HIGH){
  73. if(readyet == 0){
  74. hzbinval[clkval] = digitalRead(hzbinary);
  75. clkval++;
  76. readyet = 1;
  77. }
  78. delayMicroseconds(10);
  79. }
  80. while((hzclkval = digitalRead(hzclk)) == LOW){
  81. delayMicroseconds(1);
  82. readyet = 0;
  83. }
  84. //digitalWrite(sigLED, HIGH);
  85. //delayMicroseconds(1);
  86. //digitalWrite(sigLED, LOW);
  87. //would be nice to add a picture to this source code (waveform)
  88. //or oscope picture
  89. //why isn't this possible?
  90. }
  91. void ConvertArraytoBin (void){
  92. for(x=4;x<21;x++){
  93. if(hzbinval[x] == 1){
  94. bitfieldA.mainstwo = bitfieldA.mainstwo | (0b0000000000000000000001 << (x - 3));
  95. }
  96. }
  97. //Serial.println(bitfieldA.mainstwo,BIN);
  98. temp = bitfieldA.mainstwo >> 1;
  99. //almost, but no cigar. has timing issues.
  100. //ugly hack time
  101. temp = temp & 0b1011111111111;
  102. //results in a clock that is right most of the time.
  103. //but wrong every 20 or so counts
  104. //todo: fix timing of duino by removing digital read for
  105. //direct port reads
  106. if(temp == 5047){
  107. temp = 5998; //something, something, premature optimization...
  108. }
  109. if(temp == 5048){
  110. temp = 6000; //LUTs
  111. }
  112. Serial.println(temp,BIN);
  113. Serial.println(temp,DEC);
  114. Serial.print("\n");
  115. /*mainsfreq = mainsfreq >> 3;
  116. mainsfreq = mainsfreq & 0b0111111111111;
  117. Serial.println(mainsfreq,DEC);
  118. //111011011101
  119. //1011101101111
  120. //111011011101
  121. //11101101111*/
  122. }