⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oled.c

📁 OLEDP16801的显示驱动
💻 C
字号:

#include "Board.h"
#include "mytype.h"
#include "AT91SAM7X256.h"

extern unsigned char const gbhz16x16dot[];
extern unsigned char const Asc8x16dot[];

delays(n)
{
  for(int j=0;j<n;j++)
  {
  for(int i=0;i<50;i++);
  }
}

void TurnOnOLED17V(void)
{
  AT91C_BASE_PIOA->PIO_PER=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_OWDR=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_SODR=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_OER=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_PPUDR=PIN_OLED_17V;
}

void ShutDownOLED17V(void)
{
  AT91C_BASE_PIOA->PIO_PER=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_OWDR=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_CODR=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_OER=PIN_OLED_17V;
  AT91C_BASE_PIOA->PIO_PPUDR=PIN_OLED_17V;
}

void OLEDCsHigh(void)
{
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDCS;
  delays(1);
}

void OLEDCsLow(void)
{
  AT91C_BASE_PIOB->PIO_CODR=PIN_OLEDCS;
  delays(1);
}

void InitOLEDIO(void)
{
  //ShutDownOLED17V();
  AT91C_BASE_PIOB->PIO_PPUDR=PIN_OLEDCS+PIN_OLEDWR+PIN_OLEDRESET+PIN_OLEDDC+PIN_OLEDRD+0xff+0x1f00;
  AT91C_BASE_PIOB->PIO_PER=PIN_OLEDCS+PIN_OLEDWR+PIN_OLEDRESET+PIN_OLEDDC+PIN_OLEDRD+0xff+0x1f00;
  AT91C_BASE_PIOB->PIO_OWDR=PIN_OLEDCS+PIN_OLEDWR+PIN_OLEDRESET+PIN_OLEDDC+PIN_OLEDRD+0x1f00;
  AT91C_BASE_PIOB->PIO_OWER=0xff;
  AT91C_BASE_PIOB->PIO_ODSR=0xff;
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDCS+PIN_OLEDWR+PIN_OLEDDC+PIN_OLEDRD+0x1f00;
  AT91C_BASE_PIOB->PIO_CODR=PIN_OLEDRESET;
  AT91C_BASE_PIOB->PIO_OER=PIN_OLEDCS+PIN_OLEDWR+PIN_OLEDRESET+PIN_OLEDDC+PIN_OLEDRD+0xff;
  AT91C_BASE_PIOB->PIO_ODR=0x1f00;
  delays(1);
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDRESET;
}

void WriteOLEDCmd(uint8 cmd)
{
  AT91C_BASE_PIOB->PIO_CODR=PIN_OLEDDC;
  AT91C_BASE_PIOB->PIO_CODR=PIN_OLEDWR;
  AT91C_BASE_PIOB->PIO_ODSR=cmd<<0;
  //delays(0);
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDWR;
}

void WriteOLEDData(uint8 data)
{
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDDC;
  AT91C_BASE_PIOB->PIO_CODR=PIN_OLEDWR;
  AT91C_BASE_PIOB->PIO_ODSR=data;
  //delays(0);
  AT91C_BASE_PIOB->PIO_SODR=PIN_OLEDWR;
}

void OLEDFillMode(uint8 en_dis)
{
  OLEDCsLow();
  WriteOLEDCmd(0x26);
  en_dis&=0x11;
  WriteOLEDData(en_dis);
  OLEDCsHigh();
}

void InitOLEDScr(void)
{ 
  InitOLEDIO();
  OLEDCsLow(); 
  WriteOLEDCmd(0xae);
  TurnOnOLED17V(); 
  WriteOLEDCmd(0xae);
  WriteOLEDCmd(0xad);WriteOLEDData(0x86);
  WriteOLEDCmd(0xa8);WriteOLEDData(0x7f);
  WriteOLEDCmd(0xa1);WriteOLEDData(0x00);
  WriteOLEDCmd(0xa2);WriteOLEDData(0x00);
  WriteOLEDCmd(0xa4);
  WriteOLEDCmd(0xa0);WriteOLEDData(0x80+0x04+0x30);
  WriteOLEDCmd(0x87);WriteOLEDData(0x0b);
  WriteOLEDCmd(0x81);WriteOLEDData(0x74);
  WriteOLEDCmd(0x82);WriteOLEDData(0x6e);
  WriteOLEDCmd(0x83);WriteOLEDData(0xef);
  WriteOLEDCmd(0xb1);WriteOLEDData(0x22);
  WriteOLEDCmd(0xb3);WriteOLEDData(0x40);
  WriteOLEDCmd(0xbb);WriteOLEDData(0x08);
  WriteOLEDCmd(0xbe);WriteOLEDData(0x3c);
  WriteOLEDCmd(0xb9);
  WriteOLEDCmd(0xaf);
  OLEDFillMode(1);
}

void OLEDDrawLine(x1,y1,x2,y2,r,g,b)
{
  OLEDCsLow();
  WriteOLEDCmd(0x21);
  WriteOLEDData(x1);WriteOLEDData(y1);
  WriteOLEDData(x2);WriteOLEDData(y2);
  WriteOLEDData(r>>2);WriteOLEDData(g>>2);WriteOLEDData(b>>2);
  OLEDCsHigh();
}

void OLEDDrawRect(uint8 x1,uint8 y1,uint8 x2,uint8 y2,uint8 r,uint8 g,uint8 b,uint8 r1,uint8 g1,uint8 b1)
{
  OLEDCsLow();
  WriteOLEDCmd(0x22);
  WriteOLEDData(x1);WriteOLEDData(y1);
  WriteOLEDData(x2);WriteOLEDData(y2);
  WriteOLEDData(r>>2);WriteOLEDData(g>>2);WriteOLEDData(b>>2);
  WriteOLEDData(r1>>2);WriteOLEDData(g1>>2);WriteOLEDData(b1>>2);
  OLEDCsHigh();
}

void oledclear(uint8 x1,uint8 y1,uint8 x2,uint8 y2)
{
 OLEDCsLow();
 WriteOLEDCmd(0x24);
 WriteOLEDData(x1);
 WriteOLEDData(y1);
 WriteOLEDData(x2);
 WriteOLEDData(y2);
 OLEDCsHigh();
}

void Oledcopy(x1,y1,x2,y2,newx1,newy1)
{
 OLEDCsLow();
 WriteOLEDCmd(0x23);
 WriteOLEDData(x1);
 WriteOLEDData(y1);
 WriteOLEDData(x2);
 WriteOLEDData(y2);
 WriteOLEDData(newx1);
 WriteOLEDData(newy1);
 OLEDCsHigh();
}


/*--------------------------显示单色文字和图象------------------------*/
void OLEDPutBWData(uint8 x1,uint8 y1,uint8 width,uint8 hight,uint32 bufsize,uint8 *blockbuf)
{
  uint32 ui32,ui32a;
  uint8 ui8;
  OLEDCsLow();
  WriteOLEDCmd(0x15);WriteOLEDData(x1);WriteOLEDData(x1+width-1);
  WriteOLEDCmd(0x75);WriteOLEDData(y1);WriteOLEDData(y1+hight-1);
  OLEDCsHigh();
  OLEDCsLow();
  WriteOLEDCmd(0x5c);
  for(ui32=0;ui32<bufsize;ui32++)
  {
    ui8=blockbuf[ui32];
    for(ui32a=0;ui32a<8;ui32a++)
    {
      if((ui8&0x80)!=0)          //若该第7位为1,则写一种颜色A
      {
        WriteOLEDData(0xed>>2);
        WriteOLEDData(0xe6>>2);
        WriteOLEDData(0x56>>2);
      }
      else                       //否则写一种颜色B
      {
        WriteOLEDData(0x23>>2);
        WriteOLEDData(0x26>>2);
        WriteOLEDData(0x3e>>2);
      }
      ui8<<=1;
    }

  }
  OLEDCsHigh();
}

void OLEDDisplayAsc8x16(x,y,Asc_number)
{ 
  uint8 *Ascbuf;
  Ascbuf=(uint8 *)Asc8x16dot+Asc_number*16;
  OLEDPutBWData(x,y,8,16,16,Ascbuf);
}

void OLEDDisplaygbhz16x16(x,y,ch_number)
{ 
  uint8 *fontbuf;
  fontbuf=(uint8 *)gbhz16x16dot+ch_number*32;
  OLEDPutBWData(x,y,16,16,32,fontbuf);
}

void writearea(x1,y1,width,hight,r,g,b)
{
  OLEDCsLow();
  WriteOLEDCmd(0x15);WriteOLEDData(x1);WriteOLEDData(x1+width-1);//设置列开始和结束地址
  WriteOLEDCmd(0x75);WriteOLEDData(y1);WriteOLEDData(y1+hight-1);//设置行开始和结束地址
  OLEDCsHigh();
  OLEDCsLow();
  WriteOLEDCmd(0x5c);//开始写入数据
  for(int j=0;j<hight;j++)
    {
      for(int i=0;i<width;i++)
        {
        WriteOLEDData(r>>2);
        WriteOLEDData(g>>2);
        WriteOLEDData(b>>2);
        }
    } 
OLEDCsHigh();
}

void writecolor(uint8 x1,uint8 y1,uint8 width,uint8 hight,uint8 *buf2)
{
  int k=3*width-1;
  OLEDCsLow();
  WriteOLEDCmd(0x15);WriteOLEDData(x1);WriteOLEDData(x1+width-1);//设置列开始和结束地址
  WriteOLEDCmd(0x75);WriteOLEDData(y1);WriteOLEDData(y1+hight-1);//设置行开始和结束地址
  OLEDCsHigh();
  OLEDCsLow();
  WriteOLEDCmd(0x5c);//开始写入数据
  for(int j=0;j<hight;j++)
    {
      for(int i=0;i<3*width;i++)
        {
        WriteOLEDData(buf2[k]>>2);   //此段将图象倒置
        k--;
        }
      k=k+6*width;
    } 

OLEDCsHigh();
}        


void LoadPicture(xstar,ystar,picw,pich,lv)
{
  uint8 *buf2;
  buf2=(uint8 *)lv;
  writecolor(xstar,ystar,picw,pich,buf2);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -