数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 790|回复: 3

[Arduino] 一款老外制使用ARDUINO制作的电压源,搬来分享给大家

[复制链接]
发表于 2026-7-27 15:45:42 | 显示全部楼层 |阅读模式
本帖最后由 icespirit 于 2026-7-27 15:49 编辑

看到一老外制作的DC电压源,不知道是否有人感兴趣,顺手搬来,给需要的、动手能力强的人玩玩

个人认为不好的地方是使用了触摸屏调节操作,感觉比较费电,要是那种传统机械式按钮调节或编码器操作的可能会更好,更省电
还有ARDUINO nano的处理速度担心有些拉跨,触摸屏操作的话,有些担心那颗CPU有些慢的感觉,不知道实际情况和我预想的有多差别







  1. //SCULLCOM HOBBY ELECTRONICS
  2. //DC VOLTAGE CALIBRATOR
  3. //Using TFT Display with Touch Screen
  4. //30th November 2017

  5. //Version 1.0

  6. #include <Adafruit_GFX.h>       //https://github.com/adafruit/Adafruit-GFX-Library/archive/master.zip
  7. #include <Adafruit_ILI9341.h>   //https://github.com/adafruit/Adafruit_ILI9341/archive/master.zip
  8. #include <URTouch.h>            //http://www.rinkydinkelectronics.com/download.php?f=URTouch.zip
  9. #include <MCP4922.h>            //https://github.com/helgenodland/MCP4922-Arduino-SPI-Library/archive/master.zip
  10. #include <SPI.h>

  11. // Pins for TFT
  12. #define TFT_DC 9              // Pin connection D / C display
  13. #define TFT_CS 10             // Pin of CS display output connection
  14. #define TFT_RST 8             // Pin of output connection RESET (If connected to power or button, then comment out this line, and uncomment the next one)
  15. // #define TFT_RST -1         // If the display of the RESET is connected to the power supply or the RESET button on the Arduino
  16. #define TFT_MISO 12           // Pin of display output connection SDO(MISO)
  17. #define TFT_MOSI 11           // Pin of display output connection SDI(MOSI)
  18. #define TFT_CLK 13            // Pin of display output connection SCK

  19. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);  // Create a display object and inform the library of the pinout for working with graphics

  20. // Pins for Touch Screen
  21. #define t_SCK 3               // Pin of display output connection T_CLK
  22. #define t_CS 4                // Pin of display output connection T_CS
  23. #define t_MOSI 5              // Pin of display output connection T_DIN
  24. #define t_MISO 6              // Pin of display output connection T_DOUT
  25. #define t_IRQ 7               // Pin of display output connection T_IRQ   

  26. URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ); // Create an object of the sensor module and inform the library of the pinout for working with it

  27. MCP4922 DAC(11, 13, 2, 14); // (MOSI,SCK,CS,LDAC) define Connections for UNO_board,
  28. float volts = 0.000;        //set initial voltage
  29. float voltsU = 0.000;

  30. int x, y;                   //TFT screen cooridinates

  31. char myInput[6];   //store number from keypad  - was [5]
  32. int n = 0;        //index for myInput array
  33. char key;
  34. int keyDelay = 500;  //was 1000

  35. void setup()
  36. {
  37.   //volts=0;            //reset DAC output to zero at statup
  38.   //dacOutput();
  39.   Serial.begin(9600);
  40.   pinMode(A0, OUTPUT);
  41.   digitalWrite(A0, LOW);
  42.   pinMode(A1, OUTPUT);
  43.   digitalWrite(A1, LOW);
  44.   pinMode(A2, OUTPUT);
  45.   digitalWrite(A2, LOW);
  46.   pinMode(A3, OUTPUT);
  47.   digitalWrite(A3, LOW);
  48.   pinMode(A4, OUTPUT);
  49.   digitalWrite(A4, LOW);


  50.   volts = 0;          //reset DAC output to zero at statup
  51.   dacOutput();        //set DAC output to volts
  52.   voltsU = 1;         //sets voltsU to be different to volts so first voltage entry is accepted

  53.   tft.begin();                      // Initialize the start of work with the graphic display
  54.   tft.setRotation(1);               // We translate the display into landscape orientation

  55.   ts.InitTouch();                   // Initialize the touchscreen display module
  56.   ts.setPrecision(PREC_MEDIUM);    // Determine the necessary accuracy of the processing of the pressures: PREC_LOW - low, PREC_MEDIUM - medium, PREC_HI - high, PREC_EXTREME - maximum

  57.   tft.fillScreen(ILI9341_BLACK);

  58.   setCommonDisplay();
  59.   mainInputDisplay();
  60.   displayVoltage();
  61. }

  62. void loop()
  63. {

  64.   while (true)
  65.   {

  66.     readTouchScreen();                //check for input fron touch screen
  67.     {
  68.       if ((y >= 180) && (y <= 200)) //keypad top row
  69.       {
  70.         if ((x >= 10) && (x <= 90)) //SET button
  71.         {
  72.           tft.fillRect(10, 50, 305, 160, ILI9341_BLACK);
  73.           setVoltageKeypad();   //
  74.           keyPadEntry();
  75.         }
  76.       }

  77.       if ((y >= 180) && (y <= 200)) //keypad top row
  78.       {
  79.         if ((x >= 110) && (x <= 180)) //Output ON button
  80.         {
  81.           digitalWrite(A2, HIGH);
  82.         }
  83.       }

  84.       if ((y >= 180) && (y <= 200)) //keypad top row
  85.       {
  86.         if ((x >= 200) && (x <= 280)) //Output OFF button
  87.         {
  88.           digitalWrite(A2, LOW);
  89.         }
  90.       }

  91.     }

  92.     Serial.print("voltsU = ");
  93.     Serial.println(voltsU);
  94.     Serial.print("volts = ");
  95.     Serial.println(volts);

  96.     if (voltsU != volts) {
  97.       displayVoltage();                           //print reference voltage on TFT main display
  98.     }

  99.     if (voltsU != volts) {
  100.       dacOutput();
  101.     }
  102.     //else{
  103.     //  voltsU = volts;
  104.     //}

  105.     Serial.println(volts, 3);                   //test only
  106.   }
  107. }

  108. void setVoltageKeypad()
  109. {
  110.   tft.fillRect(70, 90, 180, 120, ILI9341_RED);        //fill keypad area with RED

  111.   for (int i = 70; i <= 190; i = i + 60) {            //coordinates for drawing keypad outline
  112.     for (int p = 90; p <= 180; p = p + 30) {            //coordinates for drawing keypad outline
  113.       tft.drawRect(i, p, 60, 30, ILI9341_WHITE);          //draw keypad outline in WHITE
  114.     }
  115.   }

  116.   tft.setTextColor(ILI9341_YELLOW);  // Determine the color of text for display
  117.   tft.setTextSize(2);               // Determine the font size for display
  118.   int t = 1;
  119.   for (int p = 100; p <= 160; p = p + 30) { //routine to print keypad numbers 1 to 9
  120.     for (int i = 95; i <= 215; i = i + 60) { //
  121.       tft.setCursor(i, p);
  122.       tft.print(t);
  123.       t = t + 1;
  124.     }
  125.   }

  126.   tft.setCursor(95, 185);               // Determine the coordinates for decimal point on keypad
  127.   tft.print(".");                       // Print decimal point on keypad

  128.   tft.setCursor(155, 190);              // Determine the coordinates for 0 on keypad
  129.   tft.print("0");                       // Print 0 on keypad

  130.   tft.setCursor(205, 190);              // Determine the coordinates for Delete (back space)on keypad
  131.   tft.print("Del");                     // Print Del on keypad


  132.   tft.fillRect(255, 180, 60, 30, ILI9341_RED);    //Draw RED button for SET Voltage on keypad
  133.   tft.drawRect(255, 180, 60, 30, ILI9341_WHITE);  //Draw WHITE outline on SET button on keypad
  134.   tft.setTextColor(ILI9341_YELLOW);               // Determine the color of text for display
  135.   tft.setTextSize(2);                             // Determine the font size for display
  136.   tft.setCursor(270, 190);                        // Determine the coordinates for printing SET on keypad
  137.   tft.print("SET");                               // Print SET on keypad

  138. }

  139. void setCommonDisplay()
  140. {
  141.   tft.drawRect(0, 0, 320, 240, ILI9341_MAGENTA);
  142.   tft.setTextSize(3);
  143.   tft.setTextColor(ILI9341_GREEN);  // Determine the color of text for display
  144.   tft.setCursor(10, 10);             // Determine the coordinates of the upper-left corner of the output area
  145.   tft.print("Voltage Reference");    // Display the text


  146.   tft.setTextColor(ILI9341_WHITE);  // Determine the color of text for display
  147.   tft.setTextSize(2);               // Determine the font size for display
  148.   tft.setCursor(5, 220);           // Determine the coordinates of the upper-left corner of the output area
  149.   tft.print("Scullcom Hobby Electronics");            // Display the text
  150. }

  151. void mainInputDisplay()
  152. {
  153.   // make the color selection boxes
  154.   //tft.fillRect(Hpos, Vpos, width, Height, Colour);
  155.   //colours Black, White, Red, Yellow, Green, Cyan, Blue, Magenta
  156.   for (int i = 20; i <= 220; i = i + 100) {
  157.     tft.drawRect(i, 180, 80, 30, ILI9341_RED);      // draw rectangle around box
  158.     tft.fillRect(i, 180, 80, 30, ILI9341_WHITE);     // fill rectangle
  159.   }

  160.   tft.setTextColor(ILI9341_BLUE);     // Determine the color of text for display
  161.   tft.setTextSize(2);                 // Determine the font size for display
  162.   tft.setCursor(30, 187);             // Determine the coordinates of the upper-left corner of the output area
  163.   tft.print("SET V");                 // Display the text
  164.   tft.setCursor(150, 187);            // Determine the coordinates of the upper-left corner of the output area
  165.   tft.print("ON");                  // Display the text
  166.   tft.setCursor(245, 187);            // Determine the coordinates of the upper-left corner of the output area
  167.   tft.print("OFF");                   // Display the text
  168. }

  169. void readTouchScreen()                            //touch screen check routine
  170. {
  171.   if (ts.dataAvailable())
  172.   {
  173.     ts.read();
  174.     x = ts.getX();
  175.     y = ts.getY();

  176.     Serial.println("  ");
  177.     Serial.print("X = "); Serial.print(x);
  178.     Serial.print("    Y = "); Serial.print(y);
  179.   }
  180. }

  181. void keyPadEntry()
  182. {
  183.   while (true)
  184.   {
  185.     readTouchScreen();

  186.     tft.setTextSize(3);               // Determine the font size for display
  187.     tft.setCursor(180, 50);           // Determine the coordinates of the upper-left corner of the output area
  188.     tft.print("Volt");

  189.     if ((y >= 90) && (y <= 110))
  190.     {
  191.       if ((x >= 60) && (x <= 110))
  192.       {
  193.         Serial.println("KEY 1 pressed");
  194.         key = '1';
  195.         addNewNumber();
  196.       }
  197.     }

  198.     if ((y >= 90) && (y <= 110)) //keypad 1st row
  199.     {
  200.       if ((x >= 120) && (x <= 170)) //Keypad 2 return button
  201.       {
  202.         Serial.println("KEY 2 pressed");
  203.         key = '2';
  204.         addNewNumber();
  205.       }
  206.     }

  207.     if ((y >= 90) && (y <= 110)) //keypad 1st row
  208.     {
  209.       if ((x >= 180) && (x <= 230)) //Keypad 3 return button
  210.       {
  211.         Serial.println("KEY 3 pressed");
  212.         key = '3';
  213.         addNewNumber();
  214.       }
  215.     }

  216.     if ((y >= 120) && (y <= 140)) //keypad 2nd row
  217.     {
  218.       if ((x >= 60) && (x <= 110)) //Keypad 4 return button
  219.       {
  220.         Serial.println("KEY 4 pressed");
  221.         key = '4';
  222.         addNewNumber();
  223.       }
  224.     }

  225.     if ((y >= 120) && (y <= 140)) //keypad 2nd row
  226.     {
  227.       if ((x >= 120) && (x <= 170)) //Keypad 5 return button
  228.       {
  229.         Serial.println("KEY 5 pressed");
  230.         key = '5';
  231.         addNewNumber();
  232.       }
  233.     }

  234.     if ((y >= 120) && (y <= 140)) //keypad 2nd row
  235.     {
  236.       if ((x >= 180) && (x <= 230)) //Keypad 6 return button
  237.       {
  238.         Serial.println("KEY 6 pressed");
  239.         key = '6';
  240.         addNewNumber();
  241.       }
  242.     }

  243.     if ((y >= 150) && (y <= 170)) //keypad 3rd row
  244.     {
  245.       if ((x >= 60) && (x <= 110)) //Keypad 7 return button
  246.       {
  247.         Serial.println("KEY 7 pressed");
  248.         key = '7';
  249.         addNewNumber();
  250.       }
  251.     }

  252.     if ((y >= 150) && (y <= 170)) //keypad 3rd row
  253.     {
  254.       if ((x >= 120) && (x <= 170)) //Keypad 8 return button
  255.       {
  256.         Serial.println("KEY 8 pressed");
  257.         key = '8';
  258.         addNewNumber();
  259.       }
  260.     }

  261.     if ((y >= 150) && (y <= 170)) //keypad 3rd row
  262.     {
  263.       if ((x >= 180) && (x <= 230)) //Keypad 9 return button
  264.       {
  265.         Serial.println("KEY 9 pressed");
  266.         key = '9';
  267.         addNewNumber();
  268.       }
  269.     }


  270.     if ((y >= 180) && (y <= 200)) //keypad 4th row
  271.     {
  272.       if ((x >= 60) && (x <= 110)) //Keypad . return button
  273.       {
  274.         Serial.println("KEY . pressed");
  275.         key = '.';
  276.         addNewNumber();
  277.       }
  278.     }

  279.     if ((y >= 180) && (y <= 200)) //keypad 4th row
  280.     {
  281.       if ((x >= 120) && (x <= 170)) //Keypad 0 return button
  282.       {
  283.         Serial.println("KEY 0 pressed");
  284.         key = '0';
  285.         addNewNumber();
  286.       }
  287.     }

  288.     if ((y >= 180) && (y <= 200))         //keypad 4th row
  289.     {
  290.       if ((x >= 180) && (x <= 230))    //Keypad Del return button
  291.       {
  292.         Serial.println("KEY Del pressed");

  293.         key = ' ';
  294.         n = n - 1;
  295.         if (n <= 0) {
  296.           n = 0;
  297.         }
  298.         myInput[n] = key;
  299.         myInput[n + 1] = '\0';

  300.         Serial.print(n);  //test only

  301.         tft.setTextSize(3);               // Determine the font size for display
  302.         tft.setCursor(70, 50);           // Determine the coordinates of the upper-left corner of the output area
  303.         tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);  // Determine the color of text and background for display
  304.         tft.print(myInput);             // Display the text
  305.         delay(keyDelay);
  306.         x = 0;
  307.         y = 0;
  308.       }
  309.     }

  310.     if ((y >= 180) && (y <= 200))                   //
  311.     {
  312.       if ((x >= 240) && (x <= 310))               //Keypad SET return button
  313.       {
  314.         Serial.println("volt set");
  315.         volts = atof(myInput);                  //convert voltage string to float
  316.         x = 0;
  317.         y = 0;
  318.         tft.fillRect(10, 50, 305, 160, ILI9341_BLACK);
  319.         mainInputDisplay();

  320.         for ( int i = 0; i < sizeof(myInput);  ++i )  //clear myInput array
  321.           myInput[i] = (char)0;                         //clear myInput array

  322.         n = 0;
  323.         return;
  324.       }
  325.     }
  326.   }
  327. }

  328. void dacOutput()
  329. {

  330.   if (volts > 8.001) {
  331.     volts = volts / 4;
  332.     digitalWrite(A1, HIGH);       //set INA105 gain switch to x2
  333.     digitalWrite(A3, HIGH);       //set INA105 gain switch to x2
  334.     SPI.begin();
  335.     DAC.Set((volts * 1000), 0);
  336.     //delay(100);
  337.     SPI.end();
  338.     volts = volts * 4;   //set TFT display reading to input reading

  339.   } else if (volts > 4.000 && volts < 8.001) {
  340.     volts = volts / 2;
  341.     digitalWrite(A1, LOW);       //set INA105 gain switch to x2
  342.     digitalWrite(A3, HIGH);
  343.     SPI.begin();
  344.     DAC.Set((volts * 1000), 0);
  345.     //delay(100);
  346.     SPI.end();
  347.     volts = volts * 2;   //set TFT display reading to input reading

  348.   } else {
  349.     digitalWrite(A1, LOW);
  350.     digitalWrite(A3, LOW);

  351.     SPI.begin();
  352.     DAC.Set((volts * 1000), 0);
  353.     //delay(100);
  354.     SPI.end();
  355.   }
  356.   voltsU = volts;
  357. }

  358. void displayVoltage()
  359. {
  360.   tft.setTextSize(4);               // Determine the font size for display (text size was 4)
  361.   tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);  // Determine the color of text for display
  362.   tft.setCursor(20, 100);           // Determine the coordinates of the upper-left corner of the output area
  363.   tft.print("VOLTS ");    // Display the text
  364.   tft.print(volts, 3);
  365. }

  366. void checkMainSelection()
  367. {
  368.   while (true)
  369.   {
  370.     readTouchScreen();

  371.     if ((y >= 180) && (y <= 255)) //keypad top row
  372.     {
  373.       if ((x >= 10) && (x <= 90)) //SET button
  374.       {
  375.         setVoltageKeypad();   //
  376.         keyPadEntry();
  377.         mainInputDisplay();
  378.       }
  379.     }
  380.   }
  381. }

  382. void addNewNumber()
  383. {
  384.   if (n <= 4)
  385.   {
  386.     myInput[n] = key;
  387.     myInput[n + 1] = '\0';
  388.     n++;
  389.   } else {
  390.     n = 4;

  391.     myInput[n + 1] = '\0';
  392.   }
  393.   tft.setTextSize(3);               // Determine the font size for display
  394.   tft.setCursor(70, 50);            // Determine the coordinates of the upper-left corner of the output area
  395.   tft.print(myInput);               // Display the text
  396.   delay(keyDelay);
  397.   x = 0;
  398.   y = 0;
  399. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2026-7-27 15:55:18 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|数码之家-技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-9-23 16:22 , Processed in 0.218401 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

快速回复 返回顶部 返回列表