PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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
4.2KB

  1. //example adapted from somewhere but cannot remember!
  2. //If the author recognize it drop me a note!
  3. #include <SPI.h>
  4. #include <Adafruit_GFX.h>
  5. #include <TFT_ILI9163C.h>
  6. // Color definitions
  7. #define BLACK 0x0000
  8. #define BLUE 0x001F
  9. #define RED 0xF800
  10. #define GREEN 0x07E0
  11. #define CYAN 0x07FF
  12. #define MAGENTA 0xF81F
  13. #define YELLOW 0xFFE0
  14. #define WHITE 0xFFFF
  15. #define NBINS 8
  16. const uint8_t bar_Width = 7;
  17. uint32_t avrg_TmrF = 0;
  18. uint16_t t_b[NBINS];
  19. /*
  20. Teensy3.x and Arduino's
  21. You are using 4 wire SPI here, so:
  22. MOSI: 11//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  23. MISO: 12//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  24. SCK: 13//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  25. the rest of pin below:
  26. */
  27. #define __CS 10
  28. #define __DC 9
  29. /*
  30. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  31. Arduino's 8 bit: any
  32. DUE: check arduino site
  33. If you do not use reset, tie it to +3V3
  34. */
  35. TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC);
  36. void setup(void) {
  37. Serial.begin(38400);
  38. tft.begin();
  39. tft.setRotation(1);
  40. tft.fillScreen(BLACK);
  41. tft.setTextWrap(true);
  42. tft.setTextColor(WHITE,BLACK);
  43. tft.setCursor(0,0);
  44. Draw_Table();
  45. }
  46. void loop(){
  47. for (int i=0;i<NBINS;i++){
  48. t_b[i] = random(0,4096);
  49. }
  50. Print_Data();
  51. delay(100);
  52. }
  53. void Draw_Table(void)
  54. {
  55. tft.drawFastVLine( 22, 0, 128, WHITE); // Draw a Scale
  56. tft.drawFastHLine( 20, 0, 4, WHITE);
  57. for ( int i = 10; i < 128; i += 10 ) {
  58. tft.drawFastHLine( 20, i, 4, WHITE);
  59. tft.setCursor( 0, i - 3);
  60. //if ( i < 60 )
  61. if ( i < 120 )
  62. tft.print( i * 1.2,0);
  63. else
  64. tft.print(" dB");
  65. }
  66. tft.setCursor( 96, 1); // Digital display on right side
  67. tft.print("T H D");
  68. tft.setCursor( 96, 23);
  69. tft.print("F R Q");
  70. tft.setCursor( 96, 45);
  71. tft.print("R M S");
  72. }
  73. void Print_Data(void)
  74. {
  75. float frequency = 0.0;
  76. float total_thd = 0.0;
  77. float voltag_ac = 0.0;
  78. avrg_TmrF >>= 4;
  79. if (avrg_TmrF != 0) frequency = (8.0 * 16000000.0) / avrg_TmrF;
  80. avrg_TmrF = 0;
  81. //--------------------- FREQ ---------------
  82. tft.setCursor(96,33);
  83. if (frequency < 99) {
  84. tft.print(frequency,2);
  85. }
  86. else{
  87. tft.print("...");
  88. }
  89. /*THD: Total Harmonic Distortion. The harmonic distortion characterises the ratio of the sum of the
  90. harmonics to the fundamental signal. Normally there are the first 6 harmonics used for the
  91. characterisation.
  92. THD = 20 * log (SQRT (SUM (SQR ([Harmonics]))) / [Fundamental])*/
  93. // ---------------- Vertical VU's ------------------------------------------
  94. uint32_t total1 = 0; // ALL
  95. uint32_t total2 = 0; // All, Except Fundamental (1).
  96. uint16_t fnd = 0; // Fundamental
  97. for (int i = 1; i < NBINS; i++) {
  98. int st1 = (i * 10) + 15; // k = 70 / (NBINS -1)
  99. tft.drawRect((st1-1),0,bar_Width,128,WHITE); // Volume
  100. uint32_t vremn1 = t_b[i] >> 4; // V(i) / updt_Rate
  101. uint32_t vremn2 = vremn1 * vremn1; // V(i) ^ 2.
  102. total1 += vremn2; // Total1 = V1^2 + V2^2 + V3^2 + V4^2 + V5^2
  103. if (i != 1)
  104. total2 += vremn2; // Total2 = V2^2 + V3^2 + V4^2 + V5^2
  105. else
  106. fnd = vremn1; // Fundamental = V1
  107. vremn2 = 20 * log10(vremn1+1); // !!! +1 MUST,
  108. int st2 = map(vremn2,0,73,(128-2),0); // 73 dB
  109. tft.fillRect(st1,1,(bar_Width-2),st2,BLACK); // Empty
  110. tft.fillRect(st1,(st2 + 2),(bar_Width-2),(128-2-st2),GREEN); // Fill Up
  111. t_b[i] = 0;
  112. }
  113. voltag_ac = sqrt(total1) / 20.27; // Hardware Calibration Coefficient /0.39752907
  114. //-------------RMS--------------------
  115. tft.setCursor(96,55);
  116. if (voltag_ac < 999) {
  117. tft.print(voltag_ac,1);
  118. }
  119. else{
  120. tft.print( "...");
  121. }
  122. total_thd = 100.0 * sqrt(total2) / fnd;
  123. //-------------THD ------------------------
  124. tft.setCursor(96,11);
  125. if (total_thd < 9) {
  126. tft.print(total_thd,3);
  127. }
  128. else{
  129. tft.print( "...");
  130. }
  131. }