display_driver.ino 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //Global Variables
  23. static uint16_t display_num[10][16] = display16X16_number_R;
  24. static uint16_t display_A[16] = display16X16_A_R;
  25. static uint16_t display_C[16] = display16X16_C_R;
  26. static uint16_t display_V[16] = display16X16_V_R;
  27. static uint16_t display_blank[16] = display16X16_blank;
  28. uint8_t voltage_dec_3;
  29. uint8_t voltage_dec_2;
  30. uint8_t voltage_dec_1;
  31. uint8_t voltage_dec_0;
  32. uint8_t current_dec_2;
  33. uint8_t current_dec_1;
  34. uint8_t current_dec_0;
  35. uint16_t* display_addr_list[16];
  36. void I2CWriteCommand(uint8_t command) {
  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. //Setup the OLED SoftI2C Config, call to initializeOLED to start connection
  50. void setupOLED() {
  51. scl_pin = OLED_SCL; // Default: P3.0
  52. sda_pin = OLED_SDA; // Default: P3.1
  53. I2CInit();
  54. }
  55. //Start the OLED screen in desired mode
  56. void initializeOLED() {
  57. I2CWriteCommand(SSD1306_DISPLAYOFF);
  58. I2CWriteCommand(SSD1306_SETDISPLAYCLOCKDIV);
  59. I2CWriteCommand(0x80); // Suggested value
  60. I2CWriteCommand(SSD1306_SETMULTIPLEX);
  61. I2CWriteCommand(0x3F);
  62. I2CWriteCommand(SSD1306_SETDISPLAYOFFSET);
  63. I2CWriteCommand(0x00);
  64. I2CWriteCommand(SSD1306_SETSTARTLINE | 0x0);
  65. I2CWriteCommand(SSD1306_CHARGEPUMP);
  66. I2CWriteCommand(0x14);
  67. I2CWriteCommand(SSD1306_MEMORYMODE);
  68. I2CWriteCommand(0x00);
  69. I2CWriteCommand(SSD1306_SEGREMAP | 0x1);
  70. I2CWriteCommand(SSD1306_COMSCANDEC);
  71. I2CWriteCommand(SSD1306_SETCOMPINS);
  72. I2CWriteCommand(0x12);
  73. I2CWriteCommand(SSD1306_SETCONTRAST);
  74. I2CWriteCommand(0xCF);
  75. I2CWriteCommand(SSD1306_SETPRECHARGE);
  76. I2CWriteCommand(0xF1);
  77. I2CWriteCommand(SSD1306_SETVCOMDETECT);
  78. I2CWriteCommand(0x40);
  79. I2CWriteCommand(SSD1306_DISPLAYALLON_RESUME);
  80. I2CWriteCommand(SSD1306_NORMALDISPLAY);
  81. I2CWriteCommand(SSD1306_DISPLAYON);
  82. }
  83. //Clear the screen with setting all bits to 0
  84. void clearScreen() {
  85. for (uint8_t i = 0; i < 8; i++) {
  86. I2CWriteCommand(0xB0 + i); // Set page address
  87. I2CWriteCommand(0x00); // Set lower column address
  88. I2CWriteCommand(0x10); // Set higher column address
  89. for (uint8_t j = 0; j < 128; j++) {
  90. I2CWriteData(0x00);
  91. }
  92. }
  93. }
  94. //Clear the screen with setting all bits to 1
  95. void lightScreen() {
  96. for (uint8_t i = 0; i < 8; i++) {
  97. I2CWriteCommand(0xB0 + i); // Set page address
  98. I2CWriteCommand(0x00); // Set lower column address
  99. I2CWriteCommand(0x10); // Set higher column address
  100. for (uint8_t j = 0; j < 128; j++) {
  101. I2CWriteData(0xFF);
  102. }
  103. }
  104. }
  105. //Print the current power information to the screen
  106. void PS_Screen(uint16_t voltage_num, uint16_t current_num, bool CC_mode) {
  107. voltage_dec_3 = (voltage_num / 1000) % 10;
  108. voltage_dec_2 = (voltage_num / 100) % 10;
  109. voltage_dec_1 = (voltage_num / 10) % 10;
  110. voltage_dec_0 = voltage_num % 10;
  111. current_dec_2 = (current_num / 100) % 10;
  112. current_dec_1 = (current_num / 10) % 10;
  113. current_dec_0 = current_num % 10;
  114. display_addr_list[0] = (!CC_mode) ? display_C : display_blank;
  115. display_addr_list[1] = (!CC_mode) ? display_V : display_blank;
  116. display_addr_list[2] = display_blank;
  117. display_addr_list[3] = display_num[voltage_dec_3];
  118. display_addr_list[4] = display_num[voltage_dec_2];
  119. display_addr_list[5] = display_num[voltage_dec_1];
  120. display_addr_list[6] = display_num[voltage_dec_0];
  121. display_addr_list[7] = display_V;
  122. display_addr_list[8] = (!CC_mode) ? display_blank : display_C;
  123. display_addr_list[9] = (!CC_mode) ? display_blank : display_C;
  124. display_addr_list[10] = display_blank;
  125. display_addr_list[11] = display_blank;
  126. display_addr_list[12] = display_num[current_dec_2];
  127. display_addr_list[13] = display_num[current_dec_1];
  128. display_addr_list[14] = display_num[current_dec_0];
  129. display_addr_list[15] = display_A;
  130. uint8_t push_temp;
  131. uint8_t readed_byte;
  132. for (uint8_t i = 0; i < 8; i++) {
  133. I2CWriteCommand(0xB0 + i); // Set page address
  134. I2CWriteCommand(0x00); // Set lower column address
  135. I2CWriteCommand(0x10); // Set higher column address
  136. for (uint8_t j = 0; j < 128; j++) {
  137. if (i % 4 == 3 && (j == 79 || j == 80)) {
  138. I2CWriteData(0xA0);
  139. } else {
  140. push_temp = 0;
  141. readed_byte = display_addr_list[(j >> 4) + (i / 4 * 8)][j%16] >> (i%4 <<2);
  142. push_temp |= ((readed_byte >> 3) & 0x01) << 7;
  143. push_temp |= ((readed_byte >> 2) & 0x01) << 5;
  144. push_temp |= ((readed_byte >> 1) & 0x01) << 3;
  145. push_temp |= (readed_byte & 0x01) << 1;
  146. I2CWriteData(push_temp);
  147. }
  148. }
  149. }
  150. }