display_test.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <SoftI2C.h>
  2. // Address of the I2C OLED
  3. #define OLED_I2C_ADDRESS 0x3C
  4. // Commands for SSD1306
  5. #define SSD1306_DISPLAYOFF 0xAE
  6. #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
  7. #define SSD1306_SETMULTIPLEX 0xA8
  8. #define SSD1306_SETDISPLAYOFFSET 0xD3
  9. #define SSD1306_SETSTARTLINE 0x40
  10. #define SSD1306_CHARGEPUMP 0x8D
  11. #define SSD1306_MEMORYMODE 0x20
  12. #define SSD1306_SEGREMAP 0xA1
  13. #define SSD1306_COMSCANDEC 0xC8
  14. #define SSD1306_SETCOMPINS 0xDA
  15. #define SSD1306_SETCONTRAST 0x81
  16. #define SSD1306_SETPRECHARGE 0xD9
  17. #define SSD1306_SETVCOMDETECT 0xDB
  18. #define SSD1306_DISPLAYALLON_RESUME 0xA4
  19. #define SSD1306_NORMALDISPLAY 0xA6
  20. #define SSD1306_DISPLAYON 0xAF
  21. void I2CWriteCommand(uint8_t command) {
  22. I2CStart();
  23. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  24. I2CSend(0x00); // Co = 0, D/C# = 0
  25. I2CSend(command);
  26. I2CStop();
  27. }
  28. void I2CWriteData(uint8_t data) {
  29. I2CStart();
  30. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  31. I2CSend(0x40); // Co = 0, D/C# = 1
  32. I2CSend(data);
  33. I2CStop();
  34. }
  35. void initializeOLED() {
  36. I2CWriteCommand(SSD1306_DISPLAYOFF);
  37. I2CWriteCommand(SSD1306_SETDISPLAYCLOCKDIV);
  38. I2CWriteCommand(0x80); // Suggested value
  39. I2CWriteCommand(SSD1306_SETMULTIPLEX);
  40. I2CWriteCommand(0x3F);
  41. I2CWriteCommand(SSD1306_SETDISPLAYOFFSET);
  42. I2CWriteCommand(0x00);
  43. I2CWriteCommand(SSD1306_SETSTARTLINE | 0x0);
  44. I2CWriteCommand(SSD1306_CHARGEPUMP);
  45. I2CWriteCommand(0x14);
  46. I2CWriteCommand(SSD1306_MEMORYMODE);
  47. I2CWriteCommand(0x00);
  48. I2CWriteCommand(SSD1306_SEGREMAP | 0x1);
  49. I2CWriteCommand(SSD1306_COMSCANDEC);
  50. I2CWriteCommand(SSD1306_SETCOMPINS);
  51. I2CWriteCommand(0x12);
  52. I2CWriteCommand(SSD1306_SETCONTRAST);
  53. I2CWriteCommand(0xCF);
  54. I2CWriteCommand(SSD1306_SETPRECHARGE);
  55. I2CWriteCommand(0xF1);
  56. I2CWriteCommand(SSD1306_SETVCOMDETECT);
  57. I2CWriteCommand(0x40);
  58. I2CWriteCommand(SSD1306_DISPLAYALLON_RESUME);
  59. I2CWriteCommand(SSD1306_NORMALDISPLAY);
  60. I2CWriteCommand(SSD1306_DISPLAYON);
  61. }
  62. void clearScreen(){
  63. for (uint8_t i = 0; i < 8; i++) {
  64. I2CWriteCommand(0xB0 + i); // Set page address
  65. I2CWriteCommand(0x00); // Set lower column address
  66. I2CWriteCommand(0x10); // Set higher column address
  67. for (uint8_t j = 0; j < 128; j++) {
  68. I2CWriteData(0x00);
  69. }
  70. }
  71. }
  72. void setup() {
  73. // Initialize I2C pins
  74. scl_pin = 30; // P3.0
  75. sda_pin = 31; // P3.1
  76. I2CInit();
  77. initializeOLED();
  78. }
  79. void loop() {
  80. // Clear the display
  81. clearScreen();
  82. delay(1000);
  83. }