找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4259|回复: 1
收起左侧

51单片机模拟驱动IIC显示OLED

[复制链接]
ID:486575 发表于 2019-3-7 21:44 | 显示全部楼层 |阅读模式
主要核心代码如下:

#include"oled.h"
#include"codetable.h"
/*********************OLED写数据************************************/
void delay(unsigned int z)
{
        unsigned int x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
   SCL = high;       
   SDA = high;
   _nop_();
   SDA = low;
   _nop_();_nop_();
   SCL = low;

}

/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
   SDA = low;
   _nop_();
   SCL = high;
   _nop_();_nop_();
   SDA = high;
}
/**********************************************
// IIC Write byte
**********************************************/
bit Write_IIC_Byte(unsigned char IIC_Byte)
{
        unsigned char i;
        bit Ack_Bit;                    
        for(i=0;i<8;i++)               
        {
                if(IIC_Byte & 0x80)               
                SDA=high;
                else
                SDA=low;
                //Delay_us(1);
                SCL=high;
                _nop_();_nop_();
                SCL=low;
                //Delay_us(1);
                IIC_Byte<<=1;               
        }
         SDA = high;                              
        _nop_();_nop_();
        SCL = high;                    
        _nop_();_nop_();
        Ack_Bit = SDA;                           
        SCL = low;
        return Ack_Bit;       
}  
/**********************************************
// IIC Write Command
**********************************************/
void LCD_WrCmd(unsigned char IIC_Command)
{
   IIC_Start();
   Write_IIC_Byte(0x78);            //Slave address,SA0=0
   Write_IIC_Byte(0x00);                        //write command
   Write_IIC_Byte(IIC_Command);
   IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void LCD_WrDat(unsigned char IIC_Data)
{
   IIC_Start();
   Write_IIC_Byte(0x78);                       
   Write_IIC_Byte(0x40);                        //write data
   Write_IIC_Byte(IIC_Data);
   IIC_Stop();
}

/*********************OLED 设置坐标************************************/
void LCD_Set_Pos(unsigned char x, unsigned char y)
{
        LCD_WrCmd(0xb2+y);
        LCD_WrCmd(((x&0xf0)>>4)|0x10);
        LCD_WrCmd((x&0x0f)|0x01);
}
/*********************OLED全屏************************************/
void LCD_Fill(unsigned char bmp_dat)
{
        unsigned char y,x;
        for(y=0;y<8;y++)
        {
                LCD_WrCmd(0xb0+y);
                LCD_WrCmd(0x01);
                LCD_WrCmd(0x10);
                for(x=0;x<X_WIDTH;x++)
                LCD_WrDat(bmp_dat);
        }
}
/*********************复位************************************/
void LCD_CLS(void)
{
        unsigned char y,x;       
        for(y=0;y<8;y++)
        {
                LCD_WrCmd(0xb0+y);
                LCD_WrCmd(0x01);
                LCD_WrCmd(0x10);
                for(x=0;x<X_WIDTH;x++)
                LCD_WrDat(0);
        }
}
/*********************LCD初始化************************************/
void LCD_Init(void)     
{
        delay(500);//这里的延时很重要,一般50ms即可,保险起见,可以长一些
        LCD_WrCmd(0xae);//--turn off oled panel
        LCD_WrCmd(0x00);//---set low column address
        LCD_WrCmd(0x10);//---set high column address
        LCD_WrCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
        LCD_WrCmd(0x81);//--set contrast control register
        LCD_WrCmd(0xcf); // Set SEG Output Current Brightness
        LCD_WrCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
        LCD_WrCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
        LCD_WrCmd(0xa6);//--set normal display
        LCD_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
        LCD_WrCmd(0x3f);//--1/64 duty
        LCD_WrCmd(0xd3);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
        LCD_WrCmd(0x00);//-not offset
        LCD_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
        LCD_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
        LCD_WrCmd(0xd9);//--set pre-charge period
        LCD_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
        LCD_WrCmd(0xda);//--set com pins hardware configuration
        LCD_WrCmd(0x12);
        LCD_WrCmd(0xdb);//--set vcomh
        LCD_WrCmd(0x40);//Set VCOM Deselect Level
        LCD_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
        LCD_WrCmd(0x02);//
        LCD_WrCmd(0x8d);//--set Charge Pump enable/disable
        LCD_WrCmd(0x14);//--set(0x10) disable
        LCD_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
        LCD_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
        LCD_WrCmd(0xaf);//--turn on oled panel
        LCD_Fill(0x00);  //初始清屏
        LCD_Set_Pos(0,0);        
}
/***************功能描述:显示6*8一组标准ASCII字符串        显示的坐标(x,y),y为页范围0~7****************/
void LCD_P6X8Str(unsigned char x, y,unsigned char ch[])
{
        unsigned char c=0,i=0,j=0;
        while (ch[j]!='\0')
        {
                c =ch[j]-32;
                if(x>126){x=0;y++;}
                LCD_Set_Pos(x,y);
                for(i=0;i<6;i++)
                LCD_WrDat(F6x8[c][i]);
                x+=6;
                j++;
        }
}
/*******************功能描述:显示8*16一组标准ASCII字符串         显示的坐标(x,y),y为页范围0~7****************/
void LCD_P8X16Str(unsigned char x, y,unsigned char ch[])
{
        unsigned char c=0,i=0,j=0;
        while (ch[j]!='\0')
        {
                c =ch[j]-32;
                if(x>120){x=0;y++;}
                LCD_Set_Pos(x,y);
                for(i=0;i<8;i++)
                LCD_WrDat(F8X16[c*16+i]);
                LCD_Set_Pos(x,y+1);
                for(i=0;i<8;i++)
                LCD_WrDat(F8X16[c*16+i+8]);
                x+=8;
                j++;
        }
}
/******************功能描述:显示16*24一组标准ASCII字符显示的坐标(x,y),y为页范围**************************************************/

  void LCD_P16X16Str(unsigned char x, y,unsigned char ch[])
{
        unsigned char c=0,i=0,j=0;
        while (ch[j]!='\0')
        {
                c =ch[j]-32;
                if(x>120){x=0;y++;}
                LCD_Set_Pos(x,y);
                for(i=0;i<16;i++)
                LCD_WrDat(F16X16[c*32+i]);
                LCD_Set_Pos(x,y+1);
                for(i=0;i<16;i++)
                LCD_WrDat(F16X16[c*32+i+16]);
                x+=16;
                j++;
        }
}
/******************功能描述:显示16*24一组标准ASCII字符显示的坐标(x,y),y为页范围**************************************************/

  void LCD_P16X24Str(unsigned char x, y,unsigned char ch[])
{
        unsigned char c=0,i=0,j=0;
        while (ch[j]!='\0')
        {
                c =ch[j]-32;
                if(x>112){x=0;y++;}
                LCD_Set_Pos(x,y);
                for(i=0;i<16;i++)
                LCD_WrDat(F16X24[c*48+i]);
                LCD_Set_Pos(x,y+1);
                for(i=0;i<16;i++)
                LCD_WrDat(F16X24[c*48+i+16]);
                LCD_Set_Pos(x,y+2);
                for(i=0;i<16;i++)
                LCD_WrDat(F16X24[c*48+i+16*2]);
                x+=16;
                j++;
        }
}
/***********************************************************************************/
void Oled_write_data_3(unsigned char x, y,unsigned int data1)
{

    unsigned char translateData[6];
    translateData[0] = data1 % 10000 / 1000 + '0';
    translateData[1] = data1 % 1000 / 100 + '0';
          translateData[2] = '.';
          translateData[3] = data1 % 100 / 10 + '0';
    translateData[4] = data1 % 10 + '0';
    translateData[5] = '\0';
   LCD_P8X16Str(x,y,translateData) ;
//LCD_P16X24Str(x,y,translateData) ;
}

/***********************************************************************************/
void Oled_write_data_4(unsigned char x, y,unsigned int data1)
{

    unsigned char translateData0[6];
         translateData0[0] = data1 % 100000/ 10000 + '0' ;
    translateData0[1] = data1 % 10000 / 1000 + '0';
    translateData0[2] = data1 % 1000 / 100 + '0';
          translateData0[3] = '.';
          translateData0[4] = data1 % 100 / 10 + '0';
   // translateData0[5] = data1 % 10 + '0';
    translateData0[5] = '\0';
   // LCD_P8X16Str(x,y,translateData0) ;
         LCD_P16X24Str(x,y,translateData0) ;
}

/*****************功能描述:显示16*16点阵  显示的坐标(x,y),y为页范围0~7****************************/
//void LCD_P16x16Ch(unsigned char x, y, N)
//{
//        unsigned char wm=0;
//        unsigned int adder=32*N;
//        LCD_Set_Pos(x , y);
//        for(wm = 0;wm < 16;wm++)
//        {
//                LCD_WrDat(F16x16[adder]);
//                adder += 1;
//        }
//        LCD_Set_Pos(x,y + 1);
//        for(wm = 0;wm < 16;wm++)
//        {
//                LCD_WrDat(F16x16[adder]);
//                adder += 1;
//        }                  
//}
/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
//void Draw_BMP(unsigned char x0, y0,x1, y1,unsigned char BMP[])
//{        
// unsigned int j=0;
// unsigned char x,y;
//  
//  if(y1%8==0) y=y1/8;      
//  else y=y1/8+1;
//        for(y=y0;y<y1;y++)
//        {
//                LCD_Set_Pos(x0,y);
//    for(x=x0;x<x1;x++)
//            {      
//                    LCD_WrDat(BMP[j++]);                   
//            }
//        }
//}


回复

使用道具 举报

ID:1 发表于 2019-3-8 02:11 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表