Advertisement
Guest User

i2c_oled

a guest
Feb 1st, 2014
188
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //==========================================================//
  2. //-------------------------WWW.WIDE.HK----------------------//
  3. //-----i2c OLED example-------------------------------------//
  4. //-----Function : show 128x64 Graphic , Word and animation--//
  5. //-----SCL = A5 , SDA = A4 , VCC = 3.3V-5V , GND----------//
  6. //==========================================================//
  7.  
  8.  
  9. #include <Wire.h>
  10. //---------------FONT + GRAPHIC-----------------------------//
  11. #include <M:\i2c OLED\data.c>
  12. //==========================================================//
  13.  
  14.  
  15. #define OLED_address 0x3c
  16.  
  17. unsigned char fill_OLED=0x55;
  18. unsigned char fill_string1[]="WWW.WIDE.HK";
  19. unsigned char fill_string2[]="OLED 128*64";
  20. unsigned char fill_string3[]="0123456789ABCDEF";
  21. unsigned char fill_string4[]="Arduino 328";
  22. extern unsigned char myFont[][8];
  23. extern unsigned char logo[];
  24. extern unsigned char ip[];
  25.  
  26. //==========================================================//
  27. void print_a_char(unsigned char ascii=0)
  28. {
  29. unsigned char i=0;
  30. for(i=0;i<8;i++)
  31. {
  32. SendChar(myFont[ascii-0x20][i]);
  33. }
  34. }
  35.  
  36. //==========================================================//
  37. void setup()
  38. {
  39.  
  40. Wire.begin();
  41. init_OLED();
  42. delay(10);
  43. clear_display();
  44. delay(50);
  45. }
  46.  
  47. //==========================================================//
  48. void loop()
  49. {
  50. clear_display();
  51. delay(50);
  52.  
  53. sendcommand(0x20); //Set Memory Addressing Mode
  54. sendcommand(0x02); //Set Memory Addressing Mode ab Page addressing mode(RESET)
  55.  
  56. sendcommand(0xa6); //Set Normal Display (default)
  57. //====================SHOW FONTS ARRAY[1-4]=======================//
  58. setXY(2,3);
  59. sendStr(fill_string1);
  60. setXY(3,3);
  61. sendStr(fill_string2);
  62. setXY(4,3);
  63. sendStr(fill_string3);
  64. setXY(5,3);
  65. sendStr(fill_string4);
  66.  
  67. delay(2000);
  68. sendcommand(0xa7); //Set Inverse Display
  69. delay(2000);
  70.  
  71. clear_display();
  72. delay(50);
  73.  
  74. sendcommand(0xa6); //Set Normal Display
  75.  
  76. sendcommand(0xae); //display off
  77. sendcommand(0x20); //Set Memory Addressing Mode
  78. sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode
  79.  
  80.  
  81. //==========================================================//
  82. for(int i=0;i<128*8;i++) // show 128* 64 picture
  83. {
  84. SendChar(logo[i]);
  85. }
  86. //==========================================================//
  87.  
  88.  
  89. sendcommand(0xaf);
  90. delay(2000);
  91. sendcommand(0xa7); //Set Inverse Display
  92. delay(2000);
  93.  
  94. while(1)
  95. {
  96. //sendcommand(0xa6); Set Normal Display
  97. sendcommand(0x29); //Vertical and Horizontal Scroll Setup
  98. sendcommand(0x00); //dummy byte
  99. sendcommand(0x00); //define page0 as startpage address
  100. sendcommand(0x00); //set time interval between each scroll ste as 6 frames
  101. sendcommand(0x07); //define page7 as endpage address
  102. sendcommand(0x01); //set vertical scrolling offset as 1 row
  103. sendcommand(0x2f); //active scrolling
  104. delay(3000);
  105.  
  106. };
  107.  
  108. }
  109.  
  110. //==========================================================//
  111. void sendcommand(unsigned char com)
  112. {
  113. Wire.beginTransmission(OLED_address); //begin transmitting
  114. Wire.send(0x80); //command mode
  115. Wire.send(com);
  116. Wire.endTransmission(); // stop transmitting
  117. }
  118.  
  119. //==========================================================//
  120. void clear_display(void)
  121. {
  122. unsigned char i,k;
  123. for(k=0;k<8;k++)
  124. {
  125. setXY(k,0);
  126. {
  127. for(i=0;i<128;i++) //clear all COL
  128. {
  129. SendChar(0); //clear all COL
  130. //delay(10);
  131. }
  132. }
  133. }
  134. }
  135.  
  136. //==========================================================//
  137. void SendChar(unsigned char data)
  138. {
  139. Wire.beginTransmission(OLED_address); // begin transmitting
  140. Wire.send(0x40);//data mode
  141. Wire.send(data);
  142. Wire.endTransmission(); // stop transmitting
  143. }
  144.  
  145. //==========================================================//
  146. void setXY(unsigned char row,unsigned char col)
  147. {
  148. sendcommand(0xb0+row); //set page address
  149. sendcommand(0x00+(8*col&0x0f)); //set low col address
  150. sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address
  151. }
  152.  
  153.  
  154. //==========================================================//
  155. void sendStr(unsigned char *string)
  156. {
  157. unsigned char i=0;
  158. //setXY(0,0);
  159. while(*string)
  160. {
  161. for(i=0;i<8;i++)
  162. {
  163. SendChar(myFont[*string-0x20][i]);
  164.  
  165. // SendChar(*string);
  166. delay(10);
  167. }
  168. *string++;
  169. }
  170. }
  171.  
  172. //==========================================================//
  173. void init_OLED(void)
  174. {
  175.  
  176. sendcommand(0xae); //display off
  177. delay(50);
  178. //----------------------------REVERSE comments----------------------------//
  179. // sendcommand(0xa0); //seg re-map 0->127(default)
  180. // sendcommand(0xa1); //seg re-map 127->0
  181. // sendcommand(0xc8);
  182. // delay(1000);
  183. //----------------------------REVERSE comments----------------------------//
  184.  
  185. sendcommand(0xaf); //display on
  186. delay(50);
  187.  
  188. }
Advertisement
RAW Paste Data Copied
Advertisement