Last active
September 26, 2015 13:04
-
-
Save edwios/47612f02476fca2d66fd to your computer and use it in GitHub Desktop.
OLED Driver for Particle-Photon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
***************************************************************************** | |
**************************** OLED Driver ********************************* | |
*** 1.3" 12864 OLED with SH1106 and Simplified Chinese IC for Spark Core *** | |
***************************************************************************** | |
****************************************************************************/ | |
int Rom_CS = A2; | |
unsigned long fontaddr=0; | |
char dispCS[32]; | |
/***************************************************************************** | |
Funtion : OLED_WrtData | |
Description: Write Data to OLED | |
Input : byte8 ucCmd | |
Output : NONE | |
Return : NONE | |
*****************************************************************************/ | |
void transfer_data_lcd(byte ucData) | |
{ | |
Wire.beginTransmission(0x78 >> 1); | |
Wire.write(0x40); //write data | |
Wire.write(ucData); | |
Wire.endTransmission(); | |
} | |
/***************************************************************************** | |
Funtion : OLED_WrCmd | |
Description: Write Command to OLED | |
Input : byte8 ucCmd | |
Output : NONE | |
Return : NONE | |
*****************************************************************************/ | |
void transfer_command_lcd(byte ucCmd) | |
{ | |
Wire.beginTransmission(0x78 >> 1); //Slave address,SA0=0 | |
Wire.write(0x00); //write command | |
Wire.write(ucCmd); | |
Wire.endTransmission(); | |
} | |
/* OLED Initialization */ | |
void initial_lcd() | |
{ | |
digitalWrite(Rom_CS, HIGH); | |
Wire.begin(); | |
delay(20); | |
transfer_command_lcd(0xAE); //display off | |
transfer_command_lcd(0x20); //Set Memory Addressing Mode | |
transfer_command_lcd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid | |
transfer_command_lcd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7 | |
transfer_command_lcd(0xc8); //Set COM Output Scan Direction | |
transfer_command_lcd(0x00);//---set low column address | |
transfer_command_lcd(0x10);//---set high column address | |
transfer_command_lcd(0x40);//--set start line address | |
transfer_command_lcd(0x81);//--set contrast control register | |
transfer_command_lcd(0x7f); | |
transfer_command_lcd(0xa1);//--set segment re-map 0 to 127 | |
transfer_command_lcd(0xa6);//--set normal display | |
transfer_command_lcd(0xa8);//--set multiplex ratio(1 to 64) | |
transfer_command_lcd(0x3F);// | |
transfer_command_lcd(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content | |
transfer_command_lcd(0xd3);//-set display offset | |
transfer_command_lcd(0x00);//-not offset | |
transfer_command_lcd(0xd5);//--set display clock divide ratio/oscillator frequency | |
transfer_command_lcd(0xf0);//--set divide ratio | |
transfer_command_lcd(0xd9);//--set pre-charge period | |
transfer_command_lcd(0x22); // | |
transfer_command_lcd(0xda);//--set com pins hardware configuration | |
transfer_command_lcd(0x12); | |
transfer_command_lcd(0xdb);//--set vcomh | |
transfer_command_lcd(0x20);//0x20,0.77xVcc | |
transfer_command_lcd(0x8d);//--set DC-DC enable | |
transfer_command_lcd(0x14);// | |
transfer_command_lcd(0xaf);//--turn on oled panel | |
} | |
void lcd_address(byte page,byte column) | |
{ | |
transfer_command_lcd(0xb0 + column); /* Page address */ | |
transfer_command_lcd((((page + 1) & 0xf0) >> 4) | 0x10); /* 4 bit MSB */ | |
transfer_command_lcd(((page + 1) & 0x0f) | 0x00); /* 4 bit LSB */ | |
} | |
void clear_screen() | |
{ | |
unsigned char i,j; | |
digitalWrite(Rom_CS, HIGH); | |
for(i=0;i<8;i++) | |
{ | |
transfer_command_lcd(0xb0 + i); | |
transfer_command_lcd(0x00); | |
transfer_command_lcd(0x10); | |
for(j=0;j<132;j++) | |
{ | |
transfer_data_lcd(0x00); | |
} | |
} | |
} | |
void display_128x64(byte *dp) | |
{ | |
unsigned int i,j; | |
for(j=0;j<8;j++) | |
{ | |
lcd_address(0,j); | |
for (i=0;i<132;i++) | |
{ | |
if(i>=2&&i<130) | |
{ | |
// Write data to OLED, increase address by 1 after each byte written | |
transfer_data_lcd(*dp); | |
dp++; | |
} | |
} | |
} | |
} | |
void display_graphic_16x16(unsigned int page,unsigned int column,byte *dp) | |
{ | |
unsigned int i,j; | |
digitalWrite(Rom_CS, HIGH); | |
for(j=2;j>0;j--) | |
{ | |
lcd_address(column,page); | |
for (i=0;i<16;i++) | |
{ | |
transfer_data_lcd(*dp); | |
dp++; | |
} | |
page++; | |
} | |
} | |
void display_graphic_8x16(unsigned int page,byte column,byte *dp) | |
{ | |
unsigned int i,j; | |
for(j=2;j>0;j--) | |
{ | |
lcd_address(column,page); | |
for (i=0;i<8;i++) | |
{ | |
// Write data to OLED, increase address by 1 after each byte written | |
transfer_data_lcd(*dp); | |
dp++; | |
} | |
page++; | |
} | |
} | |
/* | |
Display a 5x7 dot matrix, ASCII or a 5x7 custom font, glyph, etc. | |
*/ | |
void display_graphic_5x7(unsigned int page,byte column,byte *dp) | |
{ | |
unsigned int col_cnt; | |
byte page_address; | |
byte column_address_L,column_address_H; | |
page_address = 0xb0 + page - 1;// | |
column_address_L =(column&0x0f); // -1 | |
column_address_H =((column>>4)&0x0f)+0x10; | |
transfer_command_lcd(page_address); /*Set Page Address*/ | |
transfer_command_lcd(column_address_H); /*Set MSB of column Address*/ | |
transfer_command_lcd(column_address_L); /*Set LSB of column Address*/ | |
for (col_cnt=0;col_cnt<6;col_cnt++) | |
{ | |
transfer_data_lcd(*dp); | |
dp++; | |
} | |
} | |
/**** Send command to Character ROM ***/ | |
void send_command_to_ROM( byte datu ) | |
{ | |
SPI.transfer(datu); | |
} | |
/**** Read a byte from the Character ROM ***/ | |
byte get_data_from_ROM( ) | |
{ | |
byte ret_data=0; | |
ret_data = SPI.transfer(255); | |
return(ret_data); | |
} | |
/* | |
* Read continuously from ROM DataLen's bytes and | |
* put them into pointer pointed to by pBuff | |
*/ | |
void get_n_bytes_data_from_ROM(byte addrHigh,byte addrMid,byte addrLow,byte *pBuff,byte DataLen ) | |
{ | |
byte i; | |
digitalWrite(Rom_CS, LOW); | |
delayMicroseconds(100); | |
send_command_to_ROM(0x03); | |
send_command_to_ROM(addrHigh); | |
send_command_to_ROM(addrMid); | |
send_command_to_ROM(addrLow); | |
for(i = 0; i < DataLen; i++ ) { | |
*(pBuff+i) =get_data_from_ROM(); | |
} | |
digitalWrite(Rom_CS, HIGH); | |
} | |
/******************************************************************/ | |
void display_string_5x7(byte y,byte x,const char *text) | |
{ | |
unsigned char i= 0; | |
unsigned char addrHigh,addrMid,addrLow ; | |
while((text[i]>0x00)) | |
{ | |
if((text[i]>=0x20) &&(text[i]<=0x7e)) | |
{ | |
unsigned char fontbuf[8]; | |
fontaddr = (text[i]- 0x20); | |
fontaddr = (unsigned long)(fontaddr*8); | |
fontaddr = (unsigned long)(fontaddr+0x3bfc0); | |
addrHigh = (fontaddr&0xff0000)>>16; | |
addrMid = (fontaddr&0xff00)>>8; | |
addrLow = fontaddr&0xff; | |
get_n_bytes_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,8);/*取8个字节的数据,存到"fontbuf[32]"*/ | |
display_graphic_5x7(y,x+1,fontbuf);/*显示5x7的ASCII字到LCD上,y为页地址,x为列地址,fontbuf[]为数据*/ | |
i+=1; | |
x+=6; | |
} | |
else | |
i++; | |
} | |
} | |
// Showing the time at the upper part of the screen | |
void printTime(byte y) | |
{ | |
char dispCS[23]; | |
noInterrupts(); | |
String s = Time.timeStr().substring(4,19); | |
char v[17]; | |
s.toCharArray(v, 16); | |
sprintf(dispCS, "%s ", v); | |
display_string_5x7(y,20,dispCS); | |
interrupts(); | |
} | |
void setup() { | |
Time.zone(8); | |
SPI.begin(); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode(SPI_MODE3); | |
SPI.setClockDivider(SPI_CLOCK_DIV8); | |
digitalWrite(Rom_CS, HIGH); | |
initial_lcd(); | |
clear_screen(); //clear all dots | |
digitalWrite(led, LOW); | |
display_string_5x7(4,1,"(c)2014 ioStation"); | |
} | |
void loop() { | |
printTime(0); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment