2019物联网开发者大会 12.06 北京北辰洲际酒店→ 报名参会 ←
电子产品世界 » 论坛首页 » 活动中心 » 板卡试用 » 秃头系列:OLED使用

共3条 1/1 1 跳转至

秃头系列:OLED使用

菜鸟
2018-12-30 17:46:58    评分


第一次用这个东西,我最近一直在上班,加班,没有时间去搞这些,用空闲时间去填补这些,还是小有成就的.

       OLED的驱动芯片是ssd1306,这个是OLED屏幕卖家提供给我的资料.这个芯片用的是SPI的通讯方式,初始化顺序也是参考了厂家的提供的初始化指令.直接移植来过来.

       

void OLED_Init(void)
{
   OLED_RST_Set();
        Delay_ms(100);
        OLED_RST_Clr();
        Delay_ms(100);
        OLED_RST_Set();
       
        OLED_WR_Byte(0xAE,OLED_CMD);//Set display off
        OLED_WR_Byte(0x00,OLED_CMD);//set low column address
        OLED_WR_Byte(0x10,OLED_CMD);//set high column address
        OLED_WR_Byte(0x40,OLED_CMD);//set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
        OLED_WR_Byte(0x81,OLED_CMD);//Set contrast control
        OLED_WR_Byte(0xCF,OLED_CMD);//Select1-256 
        OLED_WR_Byte(0xA4,OLED_CMD);//Disable entire display
        OLED_WR_Byte(0xA6,OLED_CMD);//Set normal display
        OLED_WR_Byte(0xAF,OLED_CMD);//Set display on
        OLED_WR_Byte(0x20,OLED_CMD);//Set memory addressing mode
        OLED_WR_Byte(0x02,OLED_CMD);//Set page addressing mode
        OLED_WR_Byte(0xA1,OLED_CMD);//Disable segment re-map
        OLED_WR_Byte(0xA8,OLED_CMD);//Set multiplex ratio
        OLED_WR_Byte(0x3F,OLED_CMD);//1/64 duty
        OLED_WR_Byte(0xC8,OLED_CMD);//Set normal mode scan
        OLED_WR_Byte(0xD3,OLED_CMD);//Set display offset
        OLED_WR_Byte(0x00,OLED_CMD);//No offset
        OLED_WR_Byte(0xDA,OLED_CMD);//Set COM pins hardware configuration
        OLED_WR_Byte(0x12,OLED_CMD);//Alternative COM pin configuration
        OLED_WR_Byte(0xD5,OLED_CMD);//Set display clock clock divide
        OLED_WR_Byte(0x80,OLED_CMD);//100 frames/s
        OLED_WR_Byte(0xD9,OLED_CMD);//Set pre-charge period
        OLED_WR_Byte(0xF1,OLED_CMD);//Set pre-charge as 15 clocks,discharge as 1 clock
        OLED_WR_Byte(0xDB,OLED_CMD);//Set Vcomh deselect level
        OLED_WR_Byte(0x40,OLED_CMD);//~0.77xVcc
        OLED_WR_Byte(0x8D,OLED_CMD);//Set DCDC
        OLED_WR_Byte(0x14,OLED_CMD);//Enable DCDC (0x10 disable)
       
        OLED_Clear(0);
        OLED_Set_Address(0,0);    
}


 

引脚的操作,我借鉴前人的经验,用宏进行了封装.很值得学习的一个步骤.

#define OLED_SCLK_Set() GPIO_SetBits(GPIOA,GPIO_Pin_0);
#define OLED_SCLK_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_0);
 
#define OLED_SDIN_Set() GPIO_SetBits(GPIOA,GPIO_Pin_1);
#define OLED_SDIN_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_1);
 
#define OLED_RST_Set() GPIO_SetBits(GPIOA,GPIO_Pin_4);
#define OLED_RST_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_4);
 
#define OLED_DC_Set() GPIO_SetBits(GPIOB,GPIO_Pin_0);
#define OLED_DC_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_0);
 
#define OLED_CS_Set() GPIO_SetBits(GPIOA,GPIO_Pin_15);
#define OLED_CS_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_15);



由于屏幕不是标准的spi接口,所以根据数据手册,所以只能用软件实现屏幕的spi接口

//写入命令或数据
void OLED_WR_Byte(uint8_t DATA,uint8_t CMD)
{
   uint8_t i;
        OLED_CS_Clr();
        if(CMD)
          {OLED_DC_Set();}
        else
                OLED_DC_Clr();
        for(i=0;i<8;i++)
        {
           OLED_SCLK_Clr();
                if(DATA&0x80)
                       {OLED_SDIN_Set();}
      else
         OLED_SDIN_Clr();
                     OLED_SCLK_Set();
                     DATA<<=1;
        }
        OLED_CS_Set();
   OLED_DC_Set();      
}

//设置OLED行列地址
void OLED_Set_Address(uint8_t column,uint8_t page)
{
   OLED_WR_Byte(OLED_PAGE_Address+page,OLED_CMD); 
 
   OLED_WR_Byte(OLED_LINE_Address_L+column%16,OLED_CMD);
   OLED_WR_Byte(OLED_LINE_Address_H+column/16,OLED_CMD);  
}

//功能描述:清屏
void OLED_Clear(uint8_t data)
{
   uint32_t counter_x,counter_y;
        for(counter_y=0;counter_y<8;counter_y++)
        {
           OLED_WR_Byte(OLED_PAGE_Address+counter_y,OLED_CMD);
                OLED_WR_Byte(OLED_LINE_Address_L,OLED_CMD);
                OLED_WR_Byte(OLED_LINE_Address_H,OLED_CMD);
                
                for(counter_x=0;counter_x<128;counter_x++)
                   OLED_WR_Byte(data,OLED_DATA);   
        }
}
 
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示                         
//size:选择字体 16/12 
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr)
{         
       unsigned char c=0,i=0; 
              c=chr-' ';//得到偏移后的值                
              if(x>Max_Column-1){x=0;y=y+2;}
              if(SIZE ==16)
                     {
                     OLED_Set_Address(x,y); 
                     for(i=0;i<8;i++)
                     OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
                     OLED_Set_Address(x,y+1);
                     for(i=0;i<8;i++)
                     OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
                     }
                     else {      
                            OLED_Set_Address(x,y+1);
                            for(i=0;i<6;i++)
                            OLED_WR_Byte(F6x8[c][i],OLED_DATA);
                            
                     }
}
 
//m^n函数
uint32_t oled_pow(uint8_t m,uint8_t n)
{
       uint32_t result=1;   
       while(n--)result*=m;    
       return result;
}                    
 
//显示2个数字
//x,y :起点坐标       
//len :数字的位数
//size:字体大小
//mode:模式  0,填充模式;1,叠加模式
//num:数值(0~4294967295);                       
void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size)
{             
       uint8_t t,temp;
       uint8_t enshow=0;                                      
       for(t=0;t<len;t++)
       {
              temp=(num/oled_pow(10,len-t-1))%10;
              if(enshow==0&&t<(len-1))
              {
                     if(temp==0)
                     {
                            OLED_ShowChar(x+(size/2)*t,y,' ');
                            continue;
                     }else enshow=1; 
                     
              }
             OLED_ShowChar(x+(size/2)*t,y,temp+'0'); 
       }
} 
 
//显示一个字符号串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr)
{
       unsigned char j=0;
       while (chr[j]!='\0')
       {             OLED_ShowChar(x,y,chr[j]);
                     x+=8;
              if(x>120){x=0;y+=2;}
                     j++;
       }
}



      





关键词: 秃头     使用     oled    

管理员
2018-12-31 09:15:22    评分
2楼

谢谢楼主分享


高工
2019-01-03 10:18:20    评分
3楼

来点文字解释一下代码段嘛


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]