Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. // gcc ssd1306.c -lwiringPi -o ssd1306
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <wiringPiI2C.h>
  6.  
  7. #define WIDTH 128
  8. #define HEIGHT 64
  9.  
  10. int buffer[ WIDTH * HEIGHT / 8 ];
  11. int i2cd;
  12.  
  13. void ssd1306_command(unsigned int c)
  14. {
  15. unsigned int control = 0x00;
  16. wiringPiI2CWriteReg8( i2cd, control, c );
  17. }
  18.  
  19. void ssd1306_byte(unsigned int c)
  20. {
  21. unsigned int control = 0x40;
  22. wiringPiI2CWriteReg8( i2cd, control, c );
  23. }
  24.  
  25. void drawPixel( int x, int y, unsigned int color )
  26. {
  27. switch (color)
  28. {
  29. case 1: // white
  30. buffer[x + ( y / 8 ) * WIDTH ] = 1;
  31.  
  32. break;
  33. case 0: // black
  34. buffer[x + ( y / 8 ) * WIDTH ] = 0;
  35. break;
  36. }
  37. }
  38.  
  39. void init()
  40. {
  41. i2cd = wiringPiI2CSetup( 0x3C ); // address
  42.  
  43. ssd1306_command(0xAE); // 0xAE // display off
  44. ssd1306_command(0xD5); // 0xD5 // set display clock division
  45. ssd1306_command(0x80); // the suggested ratio 0x80
  46. ssd1306_command(0xA8); // 0xA8 set multiplex
  47. ssd1306_command(63); // set height
  48. ssd1306_command(0xD3); // set display offset
  49. ssd1306_command(0x0); // no offset
  50. ssd1306_command(64); // line #0 setstartline
  51. ssd1306_command(0x8D); // 0x8D // chargepump
  52. ssd1306_command(0x14);
  53. ssd1306_command(0x20); // memory mode
  54. ssd1306_command(0x00); // 0x0 act like ks0108
  55. ssd1306_command(161); // segremap
  56. ssd1306_command(0xC8); // comscandec
  57. ssd1306_command(0xDA); // 0xDA set com pins
  58. ssd1306_command(0x12);
  59. ssd1306_command(0x81); // 0x81 // set contract
  60. ssd1306_command(0xCF);
  61. ssd1306_command(0xD9); // 0xd9 set pre-charge
  62. ssd1306_command(0xF1);
  63. ssd1306_command(0xDB); // SSD1306_SETVCOMDETECT
  64. ssd1306_command(0x40);
  65. ssd1306_command(0xA4); // 0xA4 // display all on resume
  66. ssd1306_command(0xA6); // 0xA6 // normal display
  67. ssd1306_command(0x2E); // deactivate scroll
  68. ssd1306_command(0xAF); // --turn on oled panel
  69. }
  70.  
  71. void renderBuffer(void)
  72. {
  73. ssd1306_command(0x21); // column address
  74. ssd1306_command(0); // Column start address (0 = reset)
  75. ssd1306_command(127); // Column end address (127
  76. ssd1306_command(0x22); // page address
  77. ssd1306_command(0x00); // Page start address (0 = reset)
  78. ssd1306_command(7); // Page end address
  79.  
  80. int i;
  81.  
  82. for (i = 0; i < ( 128 * 64 / 8 ); i++)
  83. {
  84. ssd1306_byte( buffer[i] );
  85. }
  86. }
  87.  
  88. void clearBuffer(void)
  89. {
  90. memset( buffer, 0, ( 128 * 64 / 8 ) * sizeof( int ) );
  91. }
  92.  
  93. void main()
  94. {
  95. init();
  96. clearBuffer();
  97. drawPixel( 10, 10, 1 );
  98. renderBuffer();
  99. }
  100.  
  101. #ifndef F_CPU
  102. #define F_CPU 8000000UL
  103. #endif
  104.  
  105. #include <avr/io.h>
  106. #include <util/delay.h>
  107. #include <stdlib.h>
  108. #include "i2c_master.c"
  109. #include "i2c_master.h"
  110.  
  111. #define SSD1306_ADDRESS 0x3C
  112.  
  113. void initDisplay()
  114. {
  115. i2c_start(SSD1306_ADDRESS);
  116.  
  117. i2c_write(0xAE); // 0xAE // display off
  118. i2c_write(0xD5); // 0xD5 // set display clock division
  119. i2c_write(0x80); // the suggested ratio 0x80
  120. i2c_write(0xA8); // 0xA8 set multiplex
  121. i2c_write(63); // set height
  122. i2c_write(0xD3); // set display offset
  123. i2c_write(0x0); // no offset
  124. i2c_write(64); // line #0 setstartline
  125. i2c_write(0x8D); // 0x8D // chargepump
  126. i2c_write(0x14);
  127. i2c_write(0x20); // memory mode
  128. i2c_write(0x00); // 0x0 act like ks0108
  129. i2c_write(161); // segremap
  130. i2c_write(0xC8); // comscandec
  131. i2c_write(0xDA); // 0xDA set com pins
  132. i2c_write(0x12);
  133. i2c_write(0x81); // 0x81 // set contract
  134. i2c_write(0xCF);
  135. i2c_write(0xD9); // 0xd9 set pre-charge
  136. i2c_write(0xF1);
  137. i2c_write(0xDB); // SSD1306_SETVCOMDETECT
  138. i2c_write(0x40);
  139. i2c_write(0xA4); // 0xA4 // display all on resume
  140. i2c_write(0xA6); // 0xA6 // normal display
  141. i2c_write(0x2E); // deactivate scroll
  142. i2c_write(0xAF); // --turn on oled panel
  143.  
  144. i2c_stop();
  145. }
  146.  
  147. void drawPixel()
  148. {
  149. i2c_start( SSD1306_ADDRESS );
  150.  
  151. i2c_write(0x21); // column address
  152. i2c_write(0); // Column start address (0 = reset)
  153. i2c_write(127); // Column end address (127
  154. i2c_write(0x22); // page address
  155. i2c_write(0x00); // Page start address (0 = reset)
  156. i2c_write(7); // Page end address
  157.  
  158. int i;
  159.  
  160. int z=0;
  161.  
  162. for ( i = 0; i < ( 128 * 64 / 8 ); i++ )
  163. {
  164. if ( z == 0 )
  165. {
  166. i2c_write( 0xff );
  167. z = 1;
  168. }
  169. else
  170. {
  171. i2c_write( 0x00 );
  172. z = 0;
  173. }
  174. }
  175.  
  176. i2c_stop();
  177. }
  178.  
  179. int main(void){
  180.  
  181. i2c_init();
  182. initDisplay();
  183. drawPixel();
  184.  
  185. return 0;
  186. }
  187.  
  188. -ATMEGA328P connected to a 4.0v power source
  189. -ADC5 (SCL) connected to OLED's SCL (with additional line to VCC with 15k resistor in between)
  190. -ADC4 (SDA) connected to OLED's SDA (with additional line to VCC with 15k resistor in between)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement