pd-psu_v1.ino 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. PD Power Supply
  3. Author:
  4. This is the firmware for the PD power supply
  5. that reads the voltage and current of the supply and display the
  6. power information on the SSD1306 OLED display.
  7. This firmware is designed for CH552G with CH55xduino board definations
  8. Recommend config
  9. 24Mhz (Internal), 5V
  10. */
  11. #include <Serial.h>
  12. /* Hardware Definations */
  13. #define CURRENT_PIN 14 //0 - 5A
  14. #define VOLTAGE_PIN 32 //0 - 20V
  15. #define CCCV_PIN 15 //CC = LOW, CV = HIGH
  16. #define OLED_SCL 30
  17. #define OLED_SDA 31
  18. /* Global Variables */
  19. uint16_t voltageReading;
  20. uint16_t currentReading;
  21. bool isCCMode;
  22. /* Function Prototypes */
  23. void setupOLED();
  24. void initializeOLED();
  25. void clearScreen();
  26. void PS_Screen(uint16_t, uint16_t, bool);
  27. void setup() {
  28. //Disable internal pull-up resistors for input pins
  29. pinMode(CURRENT_PIN, INPUT);
  30. pinMode(VOLTAGE_PIN, INPUT);
  31. pinMode(CCCV_PIN, INPUT);
  32. // Initialize I2C pins for OLED
  33. setupOLED();
  34. initializeOLED();
  35. //Clear the OLED screen
  36. clearScreen();
  37. }
  38. void loop() {
  39. //Read the analog reading of both pins
  40. voltageReading = analogRead(VOLTAGE_PIN);
  41. currentReading = analogRead(CURRENT_PIN);
  42. isCCMode = !digitalRead(CCCV_PIN);
  43. //Convert them to actual voltage / current value
  44. voltageReading = map(voltageReading, 0, 255, 0, 500) * 4; //30k - 10k voltage divider
  45. currentReading = map(currentReading, 0, 255, 0, 500); //Max range 0 to 5A
  46. //Cap the range of digits to make sure it won't overflow
  47. voltageReading = min(voltageReading, 9999);
  48. currentReading = min(currentReading, 999);
  49. //Print result on USB Serial
  50. USBSerial_println((float)(voltageReading/100));
  51. USBSerial_println(",");
  52. USBSerial_println((float)(currentReading/100));
  53. USBSerial_println("\n");
  54. //Update OLED display
  55. PS_Screen(voltageReading, currentReading, isCCMode);
  56. }