uuu000

oled_ssd1306.c

Nov 5th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. /*
  2. * Created: 19.06.2016 11:38:00
  3. * Author: Ulrich
  4. */
  5.  
  6. #include "oled_ssd1306.h"
  7. #include "font.h"
  8.  
  9. static uint8_t oled_x = 0, oled_y = 0, font_size = 0;
  10.  
  11. const uint8_t ssd1306_init_cmd [] PROGMEM = {
  12. SET_DISPLAY_OFF,
  13. COMMAND_NO_OPERATION,
  14. SET_MEMORY_ADDR_MODE, HORIZONTAL_ADDRESSING_MODE,
  15. SET_PAGE_START,
  16. SET_COM_OUTPUT_REMAPPED,
  17. SET_LOWER_COLUMN,
  18. SET_HIGHER_COLUMN,
  19. SET_DISPLAY_START_LINE,
  20. ENTIRE_DISPLAY_ON,
  21. SET_SEGMENT_REMAP_O2,
  22. SET_DISPLAY_NORMAL,
  23. SET_CONTRAST_CONTROL, 0xFF,
  24. SET_MULTIPLEX_RATIO, 0x3F,
  25. SET_DISPLAY_OFFSET, 0x00,
  26. SET_DISPLAY_CLOCK, 0xF0,
  27. SET_PRECHARGE_PERIOD, 0x22,
  28. SET_COM_PINS, 0x12,
  29. SET_VCOMH_DESELECT, 0x20,
  30. CHARGE_BUMB_SETTING, 0x14,
  31. SET_DISPLAY_ON };
  32.  
  33. //***************************************************************************************
  34. void i2c_init(void){
  35. I2C_PORT |= (1 << SDA_PIN | 1 << SCL_PIN); //Port Pullup
  36. TWCR = 0;
  37. TWSR = 0;
  38. TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
  39. _delay_ms(50);
  40. }
  41.  
  42. //***************************************************************************************
  43. uint8_t i2c_start (void){
  44. uint8_t timeout = 0;
  45. TWCR = (1 << TWINT | 1 << TWSTA | 1 << TWEN);
  46. while(!(TWCR & (1<<TWINT))){
  47. if((timeout++) > 100) return 1;
  48. }
  49. TWDR = OLED_I2C_ADDR;
  50. TWCR = (1 << TWINT | 1 << TWEN);
  51. timeout = 0;
  52. while(!(TWCR & (1<<TWINT))){
  53. if((timeout++) > 100) return 1;
  54. }
  55. return 0;
  56. }
  57.  
  58. //***************************************************************************************
  59. uint8_t i2c_byte (uint8_t byte){
  60. uint8_t timeout = 0;
  61. TWDR = byte;
  62. TWCR = (1 << TWINT | 1 << TWEN);
  63. while(!(TWCR & (1<<TWINT))){
  64. if((timeout++) > 100) return 1;
  65. }
  66. return 0;
  67. }
  68.  
  69. //***************************************************************************************
  70. void i2c_stop (void){
  71. TWCR = (1 << TWINT | 1 << TWSTO | 1 << TWEN);
  72. TWCR = 0;
  73. }
  74.  
  75. //***************************************************************************************
  76. void oled_init (void){
  77. i2c_init();
  78. i2c_start();
  79. i2c_byte(COMMAND);
  80. for (uint8_t tmp = 0; tmp< sizeof(ssd1306_init_cmd); tmp++) {
  81. i2c_byte(pgm_read_byte(&ssd1306_init_cmd[tmp]));
  82. }
  83. i2c_stop();
  84. oled_clear_screen();
  85. }
  86.  
  87. //***************************************************************************************
  88. void oled_clear_screen (void){
  89. oled_gotoxy(0,0);
  90. i2c_start();
  91. i2c_byte(DATA);
  92. for (uint16_t i = 0; i < 128<<4 ; i++) { // 128 * (64 / Byte)
  93. i2c_byte(0);
  94. }
  95. i2c_stop();
  96. oled_gotoxy(0,0);
  97. }
  98.  
  99. //***************************************************************************************
  100. void oled_gotoxy (uint8_t x, uint8_t y){
  101. oled_x = x;
  102. oled_y = y;
  103. i2c_start();
  104. i2c_byte(COMMAND);
  105. i2c_byte(SET_PAGE_START|y);
  106. i2c_byte(SET_COLUMN_ADDRESS);
  107. i2c_byte(x * 8);
  108. i2c_byte(SET_DISPLAY_START_LINE|0x3F);
  109. i2c_stop();
  110. }
  111.  
  112. //***************************************************************************************
  113. void oled_font_size (uint8_t byte){
  114. font_size = byte;
  115. }
  116.  
  117. //***************************************************************************************
  118. void oled_write_char (char c){
  119. if(font_size != 0){
  120. uint8_t tmp = 0;
  121. oled_gotoxy(oled_x,oled_y);
  122. i2c_start();
  123. i2c_byte(DATA);
  124. for (uint8_t count = 0; count < 8; count++){
  125. tmp = pgm_read_byte(&font[(unsigned char)c][count]);
  126. tmp = tmp & 0x0f;
  127. tmp = ((tmp&0x01)*3)+(((tmp&0x02)<<1)*3)+(((tmp&0x04)<<2)*3)+(((tmp&0x08)<<3)*3);
  128. i2c_byte(tmp);
  129. i2c_byte(tmp);
  130. }
  131. i2c_stop();
  132. oled_gotoxy(oled_x,oled_y + 1);
  133. i2c_start();
  134. i2c_byte(DATA);
  135. for (uint8_t count = 0; count < 8; count++){
  136. tmp = pgm_read_byte(&font[(unsigned char)c][count]);
  137. tmp = (tmp & 0xf0)>>4;
  138. tmp = ((tmp&0x01)*3)+(((tmp&0x02)<<1)*3)+(((tmp&0x04)<<2)*3)+(((tmp&0x08)<<3)*3);
  139. i2c_byte(tmp);
  140. i2c_byte(tmp);
  141. }
  142. i2c_stop();
  143. oled_x +=2;
  144. oled_y -=1;
  145. }else{
  146. oled_gotoxy(oled_x,oled_y);
  147. i2c_start();
  148. i2c_byte(DATA);
  149. for (uint8_t count = 0; count < 8; count++){
  150. i2c_byte(pgm_read_byte(&font[(unsigned char)c][count]));
  151. }
  152. i2c_stop();
  153. oled_x +=1;
  154. }
  155. }
  156.  
  157. //***************************************************************************************
  158. void oled_write_str (char* str){
  159. while (*str) {
  160. oled_write_char(*str++);
  161. }
  162. }
  163.  
  164. //***************************************************************************************
  165. void oled_write_P (const char *Buffer,...)
  166. {
  167. va_list ap;
  168. va_start (ap, Buffer);
  169.  
  170. int format_flag;
  171. char str_buffer[10];
  172. char str_null_buffer[10];
  173. char move = 0;
  174. char Base = 0;
  175. int tmp = 0;
  176. char by;
  177. char *ptr;
  178.  
  179. //Ausgabe der Zeichen
  180. for(;;){
  181. by = pgm_read_byte(Buffer++);
  182. if(by==0) break; // end of format string
  183.  
  184. if (by == '%'){
  185. by = pgm_read_byte(Buffer++);
  186. if (isdigit(by)>0){
  187. str_null_buffer[0] = by;
  188. str_null_buffer[1] = '\0';
  189. move = atoi(str_null_buffer);
  190. by = pgm_read_byte(Buffer++);
  191. }
  192.  
  193. switch (by){
  194. case 's':
  195. ptr = va_arg(ap,char *);
  196. while(*ptr) { oled_write_char(*ptr++); }
  197. break;
  198. case 'b':
  199. Base = 2;
  200. goto ConversionLoop;
  201. case 'c':
  202. //Int to char
  203. format_flag = va_arg(ap,int);
  204. oled_write_char (format_flag++);
  205. break;
  206. case 'i':
  207. Base = 10;
  208. goto ConversionLoop;
  209. case 'o':
  210. Base = 8;
  211. goto ConversionLoop;
  212. case 'x':
  213. Base = 16;
  214. //****************************
  215. ConversionLoop:
  216. //****************************
  217. itoa(va_arg(ap,int),str_buffer,Base);
  218. int b=0;
  219. while (str_buffer[b++] != 0){};
  220. b--;
  221. if (b<move){
  222. move -=b;
  223. for (tmp = 0;tmp<move;tmp++){
  224. str_null_buffer[tmp] = '0';
  225. }
  226. //tmp ++;
  227. str_null_buffer[tmp] = '\0';
  228. strcat(str_null_buffer,str_buffer);
  229. strcpy(str_buffer,str_null_buffer);
  230. }
  231. oled_write_str (str_buffer);
  232. move =0;
  233. break;
  234. }
  235. }else{
  236. oled_write_char (by);
  237. }
  238. }
  239. va_end(ap);
  240. }
  241.  
  242.  
Advertisement
Add Comment
Please, Sign In to add comment