ssd1306.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <Arduino.h>
  2. #include <SPI.h>
  3. #include "ssd1306.h"
  4. #include "codetab.h"
  5. #define HEIGHT 64
  6. #define WIDTH 128
  7. #define SEGMENTS 8
  8. static unsigned char _dc;
  9. static unsigned char _rst;
  10. void ssd1306_data(unsigned char data)
  11. {
  12. digitalWrite(_dc, HIGH);
  13. SPI.transfer(data);
  14. }
  15. void ssd1306_command(unsigned char command)
  16. {
  17. digitalWrite(_dc, LOW);
  18. SPI.transfer(command);
  19. }
  20. void ssd1306_set_coordinate(unsigned char x, unsigned char y)
  21. {
  22. ssd1306_command(0xB0 + y);
  23. ssd1306_command(((x & 0xF0) >> 4) | 0x10);
  24. ssd1306_command((x & 0x0F) | 0x01);
  25. }
  26. void ssd1306_fill(unsigned char color)
  27. {
  28. unsigned char x;
  29. unsigned char y;
  30. for (y = 0; y < SEGMENTS; y++) {
  31. ssd1306_command(0xB0 + y);
  32. ssd1306_command(0x01);
  33. ssd1306_command(0x10);
  34. for (x = 0; x < WIDTH; x++) {
  35. ssd1306_data(color);
  36. }
  37. }
  38. }
  39. void ssd1306_clear(void)
  40. {
  41. ssd1306_fill(0x00);
  42. }
  43. void ssd1306_init(const unsigned char dc, const unsigned char rst)
  44. {
  45. _dc = dc;
  46. _rst = rst;
  47. pinMode(_dc, OUTPUT);
  48. pinMode(_rst, OUTPUT);
  49. digitalWrite(_rst, LOW);
  50. delay(100);
  51. digitalWrite(_rst, HIGH);
  52. ssd1306_command(0xAE); // turn off the screen
  53. ssd1306_command(0xD5); // set display clock divide ratio/oscillator frequency
  54. ssd1306_command(0x80); // suggested ratio: 0x80
  55. ssd1306_command(0xA8); // set multiplex
  56. ssd1306_command(0x3F); // height: 64
  57. ssd1306_command(0xD3); // set display offset
  58. ssd1306_command(0x00); // offset: 0
  59. ssd1306_command(0x40 | 0x0); // set start line
  60. ssd1306_command(0x8D); // set charge pump
  61. ssd1306_command(0x14); // charge pump: disable
  62. ssd1306_command(0x20); // set memory mode
  63. ssd1306_command(0x00); // mode: 0
  64. ssd1306_command(0xA0 | 0x1); // set seg/column mapping
  65. ssd1306_command(0xC8); // set com/row scan direction
  66. ssd1306_command(0xDA); // set com pins hardware configuration
  67. ssd1306_command(0x12); // compins: 0x12
  68. ssd1306_command(0x81); // set contrast control register
  69. ssd1306_command(0xCF); // brightness: 0xCF
  70. ssd1306_command(0xD9); // set pre-charge period
  71. ssd1306_command(0xF1); // pre-charge: 15 clocks / discharge: 1 clock
  72. ssd1306_command(0XDB); // set vcom detect
  73. ssd1306_command(0x40); // vcom deselect level: 0x40
  74. ssd1306_command(0xA4); // disable entire display
  75. ssd1306_command(0xA6); // disable inverse display (normal display)
  76. ssd1306_command(0xAF); // turn on the screen
  77. ssd1306_clear();
  78. ssd1306_set_coordinate(0, 0);
  79. }
  80. void ssd1306_draw_6x8_string(unsigned char x, unsigned char y, const char *string)
  81. {
  82. unsigned char i;
  83. const char *chr;
  84. char printable_char;
  85. for (chr = string; *chr != '\0'; chr++) {
  86. printable_char = *chr - ' ';
  87. if (x > 126) {
  88. x = 0;
  89. y++;
  90. }
  91. ssd1306_set_coordinate(x, y);
  92. for (i = 0; i < 6; i++) {
  93. ssd1306_data(pgm_read_byte(&F6x8[printable_char][i]));
  94. }
  95. x += 6;
  96. }
  97. }
  98. void ssd1306_draw_8x16_string(unsigned char x, unsigned char y, const char *string)
  99. {
  100. unsigned char i;
  101. const char *chr;
  102. char printable_char;
  103. for (chr = string; *chr != '\0'; chr++) {
  104. printable_char = *chr - ' ';
  105. if (x > 120) {
  106. x = 0;
  107. y++;
  108. }
  109. ssd1306_set_coordinate(x, y);
  110. for (i = 0; i < SEGMENTS; i++) {
  111. ssd1306_data(pgm_read_byte(&F8X16[printable_char * 16 + i]));
  112. }
  113. ssd1306_set_coordinate(x, y + 1);
  114. for (i = 0; i < SEGMENTS; i++) {
  115. ssd1306_data(pgm_read_byte(&F8X16[printable_char * 16 + i + 8]));
  116. }
  117. x += 8;
  118. }
  119. }