display_test.ino 5.3 KB

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