통신 > SSD1306( dot matrix OLED/PLED 128x64) 아두이노 라이브러리

TODAY221 TOTAL2,696,100
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 통신 | I2C-IIC,TWI | SSD1306( dot matrix OLED/PLED 128x64) 아두이노 라이브러리

페이지 정보

작성자 master 작성일2018-04-26 12:17 조회2,425회 댓글0건

본문

https://github.com/digistump/DigistumpArduino/tree/master/digistump-avr/libraries/DigisparkOLED

이 라이브러리를 c 스타일로 변경 해봤습니다.

 

// DateTime : 2018-04-26 오후 12:19:40
// by Ok-Hyun Park
/**SSD1306xLED-Drivers for SSD1306 controlled dot matrix OLED/PLED 128x64 displays*/
#include <stdlib.h>
#include <stdint.h>
#include <avr/io.h>
#include <Arduino.h>
#include <Wire.h>
#include <util/delay.h>
//
#define U08  uint8_t
#define U16  uint16_t
//
//#define _nofont_8x16 //tBUG
#ifndef   _nofont_8x16 //tBUG
  #define FONT8X16    1
#endif
//
#define FONT6X8    0
//
#include "font6x8.h"
//
#ifndef _nofont_8x16 // tBUG Optional removal to save code space
  #include "font8x16.h"
#endif
//----------------------------------------------------------------------------
#ifndef SSD1306
  #define SSD1306   0x3C // Slave address
#endif
//----------------------------------------------------------------------------
#define start_cmd  0x00
#define start_data 0x40
//----------------------------------------------------------------------------
/**SSD1306xLED-Drivers for SSD1306 controlled dot matrix OLED/PLED 128x64 displays*/
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Some code based on "IIC_wtihout_ACK" by http: //www.14blog.com/archives/1358
U08 ssd1306_init[]={ // Initialization Sequence
  0xAE,     // Display OFF(sleep mode)
  0x20,0b00,// Set Memory Addressing Mode // 00=Horizontal Addressing Mode; 01=Vertical Addressing Mode; // 10=Page Addressing Mode(RESET); 11=Invalid
  0xB0,     // Set Page Start Address for Page Addressing Mode,0-7
  0xC8,     // Set COM Output Scan Direction
  0x00,     //---set low column address
  0x10,     //---set high column address
  0x40,     //--set start line address
  0x81,0x3F,// Set contrast control register
  0xA1,     // Set Segment Re-map.A0=address mapped; A1=address 127 mapped.
  0xA6,     // Set display mode.A6=Normal; A7=Inverse
  0xA8,0x3F,// Set multiplex ratio(1 to 64)
  0xA4,     // Output RAM to Display // 0xA4=Output follows RAM content; 0xA5,Output ignores RAM content
  0xD3,0x00,// Set display offset.00=no offset
  0xD5,     //--set display clock divide ratio/oscillator frequency
  0xF0,     //--set divide ratio
  0xD9,0x22,// Set pre-charge period
  0xDA,0x12,// Set com pins hardware configuration
  0xDB,     //--set vcomh
  0x20,     // 0x20,0.77xVcc
  0x8D,0x14,// Set DC-DC enable
  0xAF      // Display ON in normal mode
};
//
U08 oledFont,oledX,oledY=0;
//
// Program: 5248 bytes
//
void SSD1306_setFont(U08 font){ oledFont=font; }
//
void SSD1306_cmd (U08 c){ Wire.beginTransmission(SSD1306_add); Wire.write(start_cmd); Wire.write(c); Wire.endTransmission(); }
void SSD1306_data(U08 b){ if(Wire.writeAvailable()){ Wire.endTransmission(); Wire.beginTransmission(SSD1306_add); Wire.write(start_data); } Wire.write(b); }
void SSD1306_Cursor(U08 x,U08 y){ Wire.beginTransmission(SSD1306_add); Wire.write(start_cmd); Wire.write(0xb0+y); Wire.write((x>>4)|0x10); Wire.write((x&0x0f)|0x01); Wire.endTransmission(); oledX=xoledY=y; }
//
void SSD1306_fill(U08 f){
  U08 m,n;
  for(m=0;m<8;m++){
    SSD1306_cmd(0xb0+m); SSD1306_cmd(start_cmd); SSD1306_cmd(0x10); // page0-page1 // low column start address // high column start address
    Wire.beginTransmission(SSD1306_add); Wire.write(start_data); for(n=0;n<128;n++)SSD1306_data(f); Wire.endTransmission();
  }
  setCursor(0,0);
}
void SSD1306_clear(){ fill(0); }
//
size_t SSD1306_wr(byte c){
  U08 i=0,ci;
  if(c=='\r')return 1;
  if(c=='\n'){
    if(oledFont==FONT6X8){ oledY++;                      } // if(oledY>7)oledY=7; // tBUG
    else                 { oledY+=2if(oledY>6)oledY=6; } // tBUG  Large Font up by two // tBUG
    setCursor(0,oledY);
    return 1;
  }
  ci=c-32;
  if(oledFont==FONT6X8){
    if(oledX>122){ oledX=0oledY++; if(oledY>7)oledY=7setCursor(oledX,oledY); } // tBUG
    Wire.beginTransmission(SSD1306_add); Wire.write(start_data); for(i=0;i<6;i++)SSD1306_data(pgm_read_byte(&ssd1306xled_font6x8[ci*6+i])); Wire.endTransmission(); setCursor(oledX+6,oledY);
  }
  #ifndef _nofont_8x16 // tBUG
    else{
      if(oledX>120){ oledX=0oledY+=2if(oledY>6)oledY=6setCursor(oledX,oledY); } // oledY++; //tBUG Large Font up by two // tBUG
      Wire.beginTransmission(SSD1306_add); Wire.write(start_data); for(i=0;i<8;i++)Wire.write(pgm_read_byte(&ssd1306xled_font8x16[ci*16+i  ])); Wire.endTransmission(); setCursor(oledX  ,oledY+1);
      Wire.beginTransmission(SSD1306_add); Wire.write(start_data); for(i=0;i<8;i++)Wire.write(pgm_read_byte(&ssd1306xled_font8x16[ci*16+i+8])); Wire.endTransmission(); setCursor(oledX+8,oledY-1);
    }
  #endif
  return 1;
}
//
void SSD1306_bitmap(U08 x0,U08 y0,U08 x1,U08 y1,const U08 bitmap[]){
  U16 j=0U08 y,x;
  // if(y1%8==0)y=y1/8; // else y=y1/8+1; // tBUG:: this does nothing as y is initialized below
  // THIS PARAM rule on y makes any adjustment here WRONG //usage oled.bitmap(START X IN PIXELS,START Y IN ROWS OF 8 PIXELS,END X IN PIXELS,END Y IN ROWS OF 8 PIXELS,IMAGE ARRAY);
  for(y=y0;y<y1;y++){ setCursor(x0,y); Wire.beginTransmission(SSD1306_add); Wire.write(start_data); for(x=x0;x<x1;x++)SSD1306_data(pgm_read_byte(&bitmap[j++])); Wire.endTransmission(); }
  setCursor(0,0);
}
//
void SSD1306_begin(){
  Wire.begin(); for(U08 i=0;i<sizeof(ssd1306_init);i++)SSD1306_cmd(ssd1306_init[i]); clear();
}
//

 

댓글 0

조회수 2,425

등록된 댓글이 없습니다.

통신HOME > 통신 > 전체 목록

게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기