Schupp

netzfrequenz

Apr 8th, 2022 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define TRUE 1
  2. #define FALSE 0
  3. #define SPI2_NSS_PIN 28
  4. #include "SPI.h"
  5. #include "winbondflash.h"
  6. #include <Adafruit_GFX.h>
  7. #include <ILI9488.h>
  8. #include <Average.h>
  9.  
  10. HardwareTimer pulseTimer(4);
  11. // Use hardware SPI (on Uno, #13, #12, #11) and the above for Enable,CS/DC,Reset
  12. ILI9488 tft = ILI9488(28, PA8 , PB10);
  13. winbondFlashSPI mem;
  14. volatile uint16_t pulse_timer_overflow_counter = 0;
  15. volatile uint32_t lastpulse = 0;
  16. volatile uint32_t interval = 0;
  17. volatile float frequency = 0;
  18. boolean toggle = false;
  19. int mescount = 0;
  20. uint32_t cycleinteval = 0;
  21. Average<float> ave(20);
  22. uint8_t chartnow[480];
  23. uint8_t chartlast[480];
  24. void pulse_isr(void)
  25. {
  26.   uint32_t current_time = (pulse_timer_overflow_counter << 16) | pulseTimer.getCount();
  27.   if (current_time < lastpulse)
  28.   {
  29.     interval = 0xFFFFFFFF - lastpulse + current_time;
  30.   }
  31.   else
  32.   {
  33.     interval = current_time - lastpulse;
  34.   }
  35.   if (toggle) {
  36.  
  37.     lastpulse = current_time;
  38.     frequency = (float)(1000000UL / (float)interval);
  39.     if (frequency > 49 && frequency < 51)
  40.       ave.push(frequency);
  41.     toggle = false;
  42.   } else {
  43.     toggle = true;
  44.   }
  45. }
  46. uint8_t *buf;
  47. uint8_t buffarr[8401];
  48.  
  49. void setup() {
  50.   buf = buffarr;
  51.  
  52.   SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
  53.   SPI.setDataMode(SPI_MODE0); //Set the  SPI_2 data mode 0
  54.   SPI.setClockDivider(SPI_CLOCK_DIV2);
  55.  
  56.   pinMode(2, OUTPUT);
  57.   pinMode(3, OUTPUT);
  58.   digitalWrite(2, HIGH);
  59.   digitalWrite(3, HIGH);
  60.   pinMode(4, OUTPUT);
  61.   Serial1.begin(115200);
  62.   tft.begin();
  63.   //  SPI.setFrequency(SPI_36MHZ)
  64.   Serial1.println("Init Chip...");
  65.   if (mem.begin(_W25Q64, SPI, 4))
  66.     Serial1.println("OK");
  67.   else
  68.   {
  69.     Serial1.println("FAILED");
  70.  
  71.   }
  72.   //flash.begin();
  73.   //flash.setClock(16000000);
  74.  
  75.   pulseTimer.pause();
  76.   pulseTimer.setOverflow(65535);
  77.   pulseTimer.setPrescaleFactor(72);
  78.   pulseTimer.setMode(1, TIMER_OUTPUT_COMPARE);
  79.   pulseTimer.setCompare(1, 65535);
  80.   pulseTimer.attachInterrupt(1, pulse_overflow_isr);
  81.   pulseTimer.refresh();
  82.   pulseTimer.resume();
  83.   pinMode(PB11, INPUT);
  84.   attachInterrupt(PB11, pulse_isr, RISING);
  85.   tft.setRotation(3);
  86.   tft.fillScreen(ILI9488_BLACK);
  87.   uint8_t x = tft.readcommand8(ILI9488_RDMODE);
  88.   //Serial1.print("Display Power Mode: 0x"); //Serial1.println(x, HEX);
  89.   x = tft.readcommand8(ILI9488_RDMADCTL);
  90.   //Serial1.print("MADCTL Mode: 0x"); //Serial1.println(x, HEX);
  91.   x = tft.readcommand8(ILI9488_RDPIXFMT);
  92.   //Serial1.print("Pixel Format: 0x"); //Serial1.println(x, HEX);
  93.   x = tft.readcommand8(ILI9488_RDIMGFMT);
  94.   Serial1.print("Image Format: 0x"); Serial1.println(x, HEX);
  95.   x = tft.readcommand8(ILI9488_RDSELFDIAG);
  96.   String test = "Netzfrequenz";
  97.   drawString(test, 1, 480 / 2 - (12 * 35 / 2), 0);
  98.   drawmyimage(2084300, 0, 0, 480, 320);
  99.  
  100.  
  101. }
  102.  
  103. void pulse_overflow_isr(void)
  104. {
  105.   pulse_timer_overflow_counter++;
  106. }
  107.  
  108. //
  109.  
  110. int z = 0;
  111. int p = 0;
  112. void loop() {
  113.   memcpy(chartlast, chartnow, 480);
  114.   for (int i = 1; i < 480; i++) {
  115.     chartnow[i - 1] = chartnow[i];
  116.   }
  117.   float floattemp = ave.mean();
  118.   chartnow[479] = map((int)floattemp * 1000, 49700, 50300, 0, 100);
  119.   Serial1.println(chartnow[479]);
  120.   String freq = String(floattemp, 3);
  121.   drawString(freq, 0, 480 / 2 - (12 * 35 / 2), 80);
  122.  
  123.   delay(200);
  124. }
  125. //skalierung
  126. String oldstring = "";
  127.  
  128. void drawString(String mystring, uint8_t fontsize,  uint16_t x, uint16_t y  )
  129. {
  130.   int mywidth = 0;
  131.   if (fontsize == 0)
  132.     mywidth = 70;
  133.   else  if (fontsize == 1)
  134.     mywidth = 35;
  135.   for (int i = 0; i < mystring.length(); i++)
  136.   {
  137.     char mychar = (char)mystring.charAt(i);
  138.     if (mychar != (char)oldstring.charAt(i))
  139.       drawChar(fontsize, mychar, x + mywidth * i, y);
  140.   }
  141.   oldstring = mystring;
  142. }
  143.  
  144. void drawChar(uint8_t fontsize, char mychar, uint16_t x, uint16_t y) {
  145.  
  146.   int mywidth, myheight, mysize, frac1, frac2;
  147.   unsigned long startpoint = 0;
  148.   uint8_t ascii = (uint8_t)mychar;
  149.   ascii = ascii - 32;
  150.   //  Serial1.println(ascii);
  151.   if (fontsize == 0 || fontsize == 5)
  152.   {
  153.     startpoint = 0;
  154.     mywidth = 70;
  155.     myheight = 119;
  156.     frac1 = 8400;
  157.     frac2 = 8400;
  158.     //Serial1.println("font0");
  159.  
  160.   }
  161.   else if (fontsize == 1)
  162.   {
  163.     startpoint = 1582700;
  164.     mywidth = 35;
  165.     myheight = 60;
  166.     mem.read(startpoint + (ascii * mywidth * myheight * 2), buf, mywidth * myheight * 2 );
  167.     buf[0] = 0;
  168.     tft.drawImage(buf, x, y, mywidth, myheight );
  169.   } else if (fontsize == 2)
  170.   {
  171.     startpoint = 1981700;
  172.     mywidth = 18;
  173.     myheight = 30;
  174.   }
  175.   mysize = mywidth * myheight;
  176.   uint32_t time1 = 0;
  177.   uint32_t time2 = 0;
  178.   unsigned long timemics = micros();
  179.   if (fontsize == 0)
  180.   {
  181.     mem.read(startpoint + (ascii * mywidth * myheight * 2), buf, frac1 );
  182.     time1 = micros() - timemics;
  183.     buf[0] = 0;
  184.     tft.drawImage(buf, x, y, 70, 60 );
  185.     time2 = micros() - timemics;
  186.     mem.read(startpoint + frac1 + (ascii * mywidth * myheight * 2), buf, frac2);
  187.     buf[0] = 0;
  188.     tft.drawImage(buf, x, y + 59, 70, 59 );
  189.  
  190.   } else if (fontsize == 5)
  191.   {
  192.     int steps = 10;
  193.     for (int i = 0; i < myheight; i += steps)
  194.     {
  195.       mem.read(startpoint + ((ascii * mywidth * myheight * 2) + i * mywidth * 2), buf, steps * 2 * mywidth );
  196.       buf[0] = 0;
  197.       tft.drawNewImage(buf, x, y + i, 70, steps );
  198.     }
  199.  
  200.   }
  201.   // Serial1.print(time1);
  202.   // Serial1.print("-");
  203.   // Serial1.println(time2 - time1);
  204.  
  205. }
  206. uint8_t *bufimg = new uint8_t[961];
  207. void drawmyimage(int startpoint, int x, int y, int picwidth, int picheight) {
  208.   for (int i = 0; i < picheight; i++)
  209.   {
  210.     mem.read(startpoint + (i * picwidth * 2), bufimg, 960);
  211.     buf[0] = 0;
  212.     tft.drawImage(bufimg, 0, y + i, picwidth, 1 );
  213.   }
  214. }
Add Comment
Please, Sign In to add comment