display_test.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <SoftI2C.h>
  2. #include "display_char.h"
  3. // Address of the I2C OLED
  4. #define OLED_I2C_ADDRESS 0x3C
  5. // Commands for SSD1306
  6. #define SSD1306_DISPLAYOFF 0xAE
  7. #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
  8. #define SSD1306_SETMULTIPLEX 0xA8
  9. #define SSD1306_SETDISPLAYOFFSET 0xD3
  10. #define SSD1306_SETSTARTLINE 0x40
  11. #define SSD1306_CHARGEPUMP 0x8D
  12. #define SSD1306_MEMORYMODE 0x20
  13. #define SSD1306_SEGREMAP 0xA1
  14. #define SSD1306_COMSCANDEC 0xC8
  15. #define SSD1306_SETCOMPINS 0xDA
  16. #define SSD1306_SETCONTRAST 0x81
  17. #define SSD1306_SETPRECHARGE 0xD9
  18. #define SSD1306_SETVCOMDETECT 0xDB
  19. #define SSD1306_DISPLAYALLON_RESUME 0xA4
  20. #define SSD1306_NORMALDISPLAY 0xA6
  21. #define SSD1306_DISPLAYON 0xAF
  22. static uint16_t display_num[10][16] = display16X16_number;
  23. static uint16_t display_A[16] = display16X16_A;
  24. static uint16_t display_C[16] = display16X16_C;
  25. static uint16_t display_V[16] = display16X16_V;
  26. static uint16_t display_blank[16] = display16X16_blank;
  27. uint8_t voltage_dec_3;
  28. uint8_t voltage_dec_2;
  29. uint8_t voltage_dec_1;
  30. uint8_t voltage_dec_0;
  31. uint8_t current_dec_2;
  32. uint8_t current_dec_1;
  33. uint8_t current_dec_0;
  34. uint16_t* display_addr_list[16];
  35. void I2CWriteCommand(uint8_t command) {
  36. I2CStart();
  37. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  38. I2CSend(0x00); // Co = 0, D/C# = 0
  39. I2CSend(command);
  40. I2CStop();
  41. }
  42. void I2CWriteData(uint8_t data) {
  43. I2CStart();
  44. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  45. I2CSend(0x40); // Co = 0, D/C# = 1
  46. I2CSend(data);
  47. I2CStop();
  48. }
  49. void initializeOLED() {
  50. I2CWriteCommand(SSD1306_DISPLAYOFF);
  51. I2CWriteCommand(SSD1306_SETDISPLAYCLOCKDIV);
  52. I2CWriteCommand(0x80); // Suggested value
  53. I2CWriteCommand(SSD1306_SETMULTIPLEX);
  54. I2CWriteCommand(0x3F);
  55. I2CWriteCommand(SSD1306_SETDISPLAYOFFSET);
  56. I2CWriteCommand(0x00);
  57. I2CWriteCommand(SSD1306_SETSTARTLINE | 0x0);
  58. I2CWriteCommand(SSD1306_CHARGEPUMP);
  59. I2CWriteCommand(0x14);
  60. I2CWriteCommand(SSD1306_MEMORYMODE);
  61. I2CWriteCommand(0x00);
  62. I2CWriteCommand(SSD1306_SEGREMAP | 0x1);
  63. I2CWriteCommand(SSD1306_COMSCANDEC);
  64. I2CWriteCommand(SSD1306_SETCOMPINS);
  65. I2CWriteCommand(0x12);
  66. I2CWriteCommand(SSD1306_SETCONTRAST);
  67. I2CWriteCommand(0xCF);
  68. I2CWriteCommand(SSD1306_SETPRECHARGE);
  69. I2CWriteCommand(0xF1);
  70. I2CWriteCommand(SSD1306_SETVCOMDETECT);
  71. I2CWriteCommand(0x40);
  72. I2CWriteCommand(SSD1306_DISPLAYALLON_RESUME);
  73. I2CWriteCommand(SSD1306_NORMALDISPLAY);
  74. I2CWriteCommand(SSD1306_DISPLAYON);
  75. }
  76. void clearScreen() {
  77. for (uint8_t i = 0; i < 8; i++) {
  78. I2CWriteCommand(0xB0 + i); // Set page address
  79. I2CWriteCommand(0x00); // Set lower column address
  80. I2CWriteCommand(0x10); // Set higher column address
  81. for (uint8_t j = 0; j < 128; j++) {
  82. I2CWriteData(0x00);
  83. }
  84. }
  85. }
  86. void nextRow() {
  87. }
  88. void lightScreen() {
  89. for (uint8_t i = 0; i < 8; i++) {
  90. I2CWriteCommand(0xB0 + i); // Set page address
  91. I2CWriteCommand(0x00); // Set lower column address
  92. I2CWriteCommand(0x10); // Set higher column address
  93. for (uint8_t j = 0; j < 128; j++) {
  94. if (j % 4 == 0) {
  95. I2CWriteData(0xAA);
  96. } else {
  97. I2CWriteData(0x80);
  98. }
  99. }
  100. }
  101. }
  102. void PS_Screen(uint16_t voltage_num, uint16_t current_num, bool CC_mode) {
  103. voltage_dec_3 = (voltage_num / 1000) % 10;
  104. voltage_dec_2 = (voltage_num / 100) % 10;
  105. voltage_dec_1 = (voltage_num / 10) % 10;
  106. voltage_dec_0 = voltage_num % 10;
  107. current_dec_2 = (current_num / 100) % 10;
  108. current_dec_1 = (current_num / 10) % 10;
  109. current_dec_0 = current_num % 10;
  110. display_addr_list[0] = (!CC_mode) ? display_C : display_blank;
  111. display_addr_list[1] = (!CC_mode) ? display_V : display_blank;
  112. display_addr_list[2] = display_blank;
  113. display_addr_list[3] = display_num[voltage_dec_3];
  114. display_addr_list[4] = display_num[voltage_dec_2];
  115. display_addr_list[5] = display_num[voltage_dec_1];
  116. display_addr_list[6] = display_num[voltage_dec_0];
  117. display_addr_list[7] = display_V;
  118. display_addr_list[8] = (!CC_mode) ? display_blank : display_C;
  119. display_addr_list[9] = (!CC_mode) ? display_blank : display_C;
  120. display_addr_list[10] = display_blank;
  121. display_addr_list[11] = display_blank;
  122. display_addr_list[12] = display_num[current_dec_2];
  123. display_addr_list[13] = display_num[current_dec_1];
  124. display_addr_list[14] = display_num[current_dec_0];
  125. display_addr_list[15] = display_A;
  126. for (uint8_t i = 0; i < 8; i++) {
  127. I2CWriteCommand(0xB0 + i); // Set page address
  128. I2CWriteCommand(0x00); // Set lower column address
  129. I2CWriteCommand(0x10); // Set higher column address
  130. for (uint8_t j = 0; j < 128; j++) {
  131. if (i % 4 == 3 && (j == 79 || j == 80)) {
  132. I2CWriteData(0xA0);
  133. }
  134. else {
  135. uint16_t push_temp = 0;
  136. uint8_t sel_col = 15 - j % 16;
  137. for (uint8_t k = 0; k < 4; k++) {
  138. uint8_t bit = ((display_addr_list[(j >> 4) + (i / 4 * 8)])[((i % 4) << 2) + k] >> (sel_col)) & 0x0001;
  139. push_temp |= (bit << (2 * k + 1));
  140. }
  141. I2CWriteData(push_temp);
  142. }
  143. }
  144. }
  145. }
  146. void drawRectangle(uint8_t, uint8_t, uint8_t, uint8_t, bool);
  147. void setup() {
  148. // Initialize I2C pins
  149. scl_pin = 30; // P3.0
  150. sda_pin = 31; // P3.1
  151. I2CInit();
  152. initializeOLED();
  153. }
  154. void loop() {
  155. // Clear the display
  156. clearScreen();
  157. delay(500);
  158. PS_Screen(8888, 888, 1);
  159. delay(500);
  160. }