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.

127 lines
2.7 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;
  13. } bitfieldA;
  14. void setup() {
  15. // put your setup code here, to run once:
  16. pinMode(hzclk, INPUT);
  17. pinMode(hzbinary, INPUT);
  18. pinMode(sigLED, OUTPUT);
  19. Serial.begin(115200);
  20. }
  21. void loop() {
  22. if(digitalRead(hzbinary) == LOW){
  23. //this handles one clk cycle each
  24. //need to read 20 cycles
  25. clkval = 0;
  26. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  27. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  28. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  29. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  30. checkCLKandBIN();checkCLKandBIN();checkCLKandBIN();
  31. checkCLKandBIN();
  32. checkCLKandBIN();
  33. checkCLKandBIN();
  34. checkCLKandBIN();
  35. checkCLKandBIN();
  36. checkCLKandBIN();
  37. checkCLKandBIN();
  38. //Serial.println(clkval); //outputs 19 or 20
  39. ConvertArraytoBin();
  40. mainsfreq = 0;
  41. bitfieldA.mainstwo = 0;
  42. }
  43. delayMicroseconds(100); //must be lower than 1 millisecond, so mic
  44. /*if hzbinary is low
  45. *
  46. * while clk high, do nothing
  47. * clk++
  48. * sample hzbinary
  49. * while clk low do nothing
  50. */
  51. /*
  52. * //serial print is very slow, and breaks timing
  53. * the below is not usable
  54. * (DMA anyone? second processor anyone?)
  55. Serial.println("hzbinval 1 - 20:");
  56. for(x=0;x<21;x++){
  57. Serial.println(hzbinval[x]);
  58. }
  59. Serial.print("\n\n\n\n");
  60. */
  61. //sample on falling edge (middle of bit)
  62. }
  63. void checkCLKandBIN(void){
  64. /*
  65. * while clk high, do nothing
  66. * clk++
  67. * sample hzbinary
  68. * while clk low do nothing
  69. */
  70. //milliscompare = millis();
  71. while((hzclkval = digitalRead(hzclk)) == HIGH){
  72. if(readyet == 0){
  73. hzbinval[clkval] = digitalRead(hzbinary);
  74. clkval++;
  75. readyet = 1;
  76. }
  77. delayMicroseconds(10);
  78. }
  79. while((hzclkval = digitalRead(hzclk)) == LOW){
  80. delayMicroseconds(1);
  81. readyet = 0;
  82. }
  83. //digitalWrite(sigLED, HIGH);
  84. //delayMicroseconds(1);
  85. //digitalWrite(sigLED, LOW);
  86. //would be nice to add a picture to this source code (waveform)
  87. //or oscope picture
  88. //why isn't this possible?
  89. }
  90. void ConvertArraytoBin (void){
  91. for(x=3;x<21;x++){
  92. if(hzbinval[x] == 1){
  93. bitfieldA.mainstwo = bitfieldA.mainstwo | (0b0000000000000000000001 << (x - 3));
  94. }
  95. }
  96. Serial.println(bitfieldA.mainstwo,BIN);
  97. /*mainsfreq = mainsfreq >> 3;
  98. mainsfreq = mainsfreq & 0b0111111111111;
  99. Serial.println(mainsfreq,DEC);
  100. //111011011101
  101. //1011101101111
  102. //111011011101
  103. //11101101111*/
  104. }