This is the OLED library for 128x64 display

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oled.cpp Source File

oled.cpp

00001 #include "mbed.h"
00002 #include "oled.h"
00003 #include "oledfont.h"    
00004 #include "stdlib.h"
00005 
00006 
00007 //initialize OLED                       
00008 oled::oled(PinName oled_SCL, PinName oled_SDA, PinName oled_RES, PinName oled_DC):_oled_SCL(oled_SCL), _oled_SDA(oled_SDA), _oled_RES(oled_RES), _oled_DC(oled_DC)
00009 {   
00010     OLED_RST_Clr();
00011     wait_ms(100);
00012     OLED_RST_Set(); 
00013                       
00014     OLED_WR_Byte(0xAE,OLED_CMD); //Shut down display
00015     OLED_WR_Byte(0xD5,OLED_CMD); //Set clock division,oscillation frequency
00016     OLED_WR_Byte(80,OLED_CMD);   //[3:0],clock division;[7:4],oscillation frequency
00017     OLED_WR_Byte(0xA8,OLED_CMD); //Set driving channel
00018     OLED_WR_Byte(0X3F,OLED_CMD); //default 0X3F(1/64) 
00019     OLED_WR_Byte(0xD3,OLED_CMD); //Set display shift
00020     OLED_WR_Byte(0X00,OLED_CMD); //default=0
00021 
00022     OLED_WR_Byte(0x40,OLED_CMD); //Set display start coloum [5:0], coloum number.
00023                                                         
00024     OLED_WR_Byte(0x8D,OLED_CMD); //Set charge pump
00025     OLED_WR_Byte(0x14,OLED_CMD); //Start/Shut down
00026     OLED_WR_Byte(0x20,OLED_CMD); //Set flash address mode
00027     OLED_WR_Byte(0x02,OLED_CMD); //[1:0],00,row address mode;01,coloum address mode;10,page address mode;default=10;
00028     OLED_WR_Byte(0xA1,OLED_CMD); //Redefine Set,bit0: 0,0->0;1,0->127;
00029     OLED_WR_Byte(0xC0,OLED_CMD); //Set COM scan direction;bit3:0,Normal mode;1,redefine mode COM[N-1]->COM0;N: driving channel
00030     OLED_WR_Byte(0xDA,OLED_CMD); //Set COM pin assignment
00031     OLED_WR_Byte(0x12,OLED_CMD); //[5:4]Setup
00032          
00033     OLED_WR_Byte(0x81,OLED_CMD); //Set contrast
00034     OLED_WR_Byte(0xEF,OLED_CMD); //1~255;default 0X7F (Set brightness)
00035     OLED_WR_Byte(0xD9,OLED_CMD); //Set pre-charge period
00036     OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;
00037     OLED_WR_Byte(0xDB,OLED_CMD); //Set VCOMH voltage rate
00038     OLED_WR_Byte(0x30,OLED_CMD); //[6:4]
00039 
00040     OLED_WR_Byte(0xA4,OLED_CMD); //Whole display start; bit0: 1=start, 0=Shut down;
00041     OLED_WR_Byte(0xA6,OLED_CMD); //Set display mode; bit0: 1=inverted display,0=normal display                                 
00042     OLED_WR_Byte(0xAF,OLED_CMD); //Open display  
00043     OLED_Clear();
00044 }
00045 
00046 //turn on OLED display    
00047 void oled::OLED_Display_On(void)
00048 {
00049     OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC command
00050     OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
00051     OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
00052 }
00053 
00054 //Shutdown OLED display     
00055 void oled::OLED_Display_Off(void)
00056 {
00057     OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC command
00058     OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
00059     OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
00060 }   
00061 
00062 //Draw a point 
00063 //x:0~127
00064 //y:0~63
00065 //t:1=Fill, 0=Clear                
00066 void oled::OLED_DrawPoint(uint8_t x,uint8_t y,bool t)
00067 {
00068     uint8_t pos,bx,temp=0;
00069     if(x>127||y>63)return;//Out of range
00070     pos=7-y/8;
00071     bx=y%8;
00072     temp=1<<(7-bx);
00073     if(t)_OLED_GRAM[x][pos]|=temp;
00074     else _OLED_GRAM[x][pos]&=~temp;     
00075 }
00076 
00077 //m^n Function
00078 uint32_t oled::oled_pow(uint8_t m,uint8_t n)
00079 {
00080     uint32_t result=1;   
00081     while(n--)result*=m;    
00082     return result;
00083 }
00084 
00085 // display 2 numbers
00086 //x,y : Start co-ordinate    
00087 //len : length of the number
00088 //size: Font size 16/12 
00089 //mode: 0=overtype mode; 1=insert mode
00090 //num: value(0~4294967295);           
00091 void oled::OLED_ShowNumber(uint8_t x,uint8_t y,uint8_t size, uint32_t num)
00092 {           
00093     uint8_t t,temp;
00094     uint8_t enshow=0;                          
00095     uint8_t len=0;
00096     uint32_t temp_num = num;
00097     
00098     while(temp_num !=0)
00099     {
00100         temp_num /= 10;
00101         len++;
00102     }
00103     
00104     for(t=0;t<len;t++)
00105     {
00106         temp=(num/oled_pow(10,len-t-1))%10;
00107         if(enshow==0&&t<(len-1))
00108         {
00109             if(temp==0)
00110             {
00111                 OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
00112                 continue;
00113             }else enshow=1; 
00114              
00115         }
00116         OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1); 
00117     }
00118 }
00119 
00120 
00121 //Display a charcter at specific position
00122 //x:0~127
00123 //y:0~63
00124 //mode:0=inverted display;1=normal display               
00125 //size:Choose font 16/12 
00126 void oled::OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,bool mode)
00127 {                   
00128     uint8_t temp,t,t1;
00129     uint8_t y0=y;
00130     chr=chr-' ';                   
00131     for(t=0;t<size;t++)
00132     {   
00133         if(size==12)temp=oled_asc2_1206[chr][t];  //1206 Font size
00134         else temp=oled_asc2_1608[chr][t];        //1608 Font size                             
00135         for(t1=0;t1<8;t1++)
00136         {
00137             if(temp&0x80)OLED_DrawPoint(x,y,mode);
00138             else OLED_DrawPoint(x,y,!mode);
00139             temp<<=1;
00140             y++;
00141             if((y-y0)==size)
00142             {
00143                 y=y0;
00144                 x++;
00145                 break;
00146             }
00147         }    
00148     }          
00149 }
00150 
00151 // display string
00152 //x,y: start co-ordinate  
00153 //*p: String address
00154 //Font size = 16
00155 void oled::OLED_ShowString(uint8_t x,uint8_t y,uint8_t size,const uint8_t *p)
00156 {
00157 #define MAX_CHAR_POSX 122
00158 #define MAX_CHAR_POSY 58          
00159     while(*p!='\0')
00160     {       
00161         if(x>MAX_CHAR_POSX){x=0;y+=16;}
00162         if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}
00163         if(size == 12){OLED_ShowChar(x,y,*p,12,1);}
00164         if(size == 16){OLED_ShowChar(x,y,*p,12,1);}
00165         x+=8;
00166         p++;
00167     }  
00168 }    
00169 
00170 //Send a byte to OLED
00171 //dat: data / command
00172 //cmd: data type 0 = command, 1 = data
00173 void oled::OLED_WR_Byte(uint8_t dat,uint8_t cmd)
00174 {   
00175     uint8_t i;            
00176     if(cmd)
00177       OLED_RS_Set();
00178     else 
00179       OLED_RS_Clr();          
00180     for(i=0;i<8;i++)
00181     {             
00182         OLED_SCLK_Clr();
00183         if(dat&0x80)
00184            OLED_SDIN_Set();
00185         else 
00186            OLED_SDIN_Clr();
00187         OLED_SCLK_Set();
00188         dat<<=1;   
00189     }                         
00190     OLED_RS_Set();        
00191 }
00192 
00193 void oled::OLED_Refresh(void)
00194 {
00195     uint8_t i,n;            
00196     for(i=0;i<8;i++)  
00197     {  
00198         OLED_WR_Byte (0xb0+i,OLED_CMD);    //Set page address
00199         OLED_WR_Byte (0x00,OLED_CMD);      //Set displace position - Row (Address byte 1)
00200         OLED_WR_Byte (0x10,OLED_CMD);      //Set displace position - Row (Address byte 2)
00201         for(n=0;n<128;n++)OLED_WR_Byte(_OLED_GRAM[n][i],OLED_DATA); 
00202     }   
00203 }
00204 
00205 //Clear screen (Black)    
00206 void oled::OLED_Clear(void)  
00207 {  
00208     uint8_t i,n;  
00209     for(i=0;i<8;i++)for(n=0;n<128;n++)_OLED_GRAM[n][i]=0X00;  
00210     OLED_Refresh();//Refresh display
00211 }
00212 
00213 
00214 void oled::OLED_RST_Clr(void) 
00215 {
00216     _oled_RES=0;
00217 }
00218 
00219 void oled::OLED_RST_Set(void) 
00220 {
00221     _oled_RES=1;   //RST
00222 }
00223 
00224 void oled::OLED_RS_Clr(void) 
00225 {
00226     _oled_DC=0;    //DC
00227 }
00228 
00229 void oled::OLED_RS_Set(void)
00230 {
00231     _oled_DC=1;    //DC
00232 }
00233 
00234 void oled::OLED_SCLK_Clr(void)
00235 {
00236     _oled_SCL=0;  //SCL
00237 }
00238 
00239 void oled::OLED_SCLK_Set(void)
00240 {
00241     _oled_SCL=1;   //SCL
00242 }
00243 
00244 void oled::OLED_SDIN_Clr(void)
00245 {
00246     _oled_SDA=0;   //SDA
00247 }
00248 
00249 void oled::OLED_SDIN_Set(void)
00250 {
00251     _oled_SDA=1;   //SDA
00252 }  
00253 
00254 
00255 
00256