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

📄 oledhy.c

📁 oled驱动
💻 C
字号:
//Senbo OLED DISPLAY DRIVER

#include <stdio.h>
#include "oledhy.h"


#define inportb(x)		inb(x) 	
#define outportb(y,x)   outb(x,y)

unsigned int bas_adr,port_ctl,port1,port2,port0;


void delay1()
{
	usleep(1000);
}

void w_c(unsigned char cmd)
{
  unsigned char i,tmp1,tmp2,tmp3;
  tmp3=inportb(0x378);
  tmp3=tmp3&0x3f;
  outportb(0x378,tmp3|0xc0);  //set d0,d1 high
  outportb(0x37a,0x0d);  //set d/c and cs low
  delay1();
  for(i=0;i<8;i++){
    tmp1=cmd;

    tmp2=tmp1&0x80;
    tmp2=tmp2|tmp3;
    outportb(0x378,tmp2);
    delay1();
    tmp2=tmp2|0x40;
    outportb(0x378,tmp2);
    delay1();
    cmd=cmd<<1;
  }
  outportb(0x378,tmp3|0xc0);
  outportb(0x37a,0x04); //set d/c and cs high
}


void w_d(unsigned char ddd)
{
  unsigned char i,tmp1,tmp2,tmp3;
  tmp3=inportb(0x378);
  tmp3=tmp3&0x3f;
  outportb(0x378,tmp3|0xc0);  //set d0,d1 high
  outportb(0x37a,0x0c);  //set d/c and cs low
  delay1();
  for(i=0;i<8;i++){
    tmp1=ddd;
    tmp2=tmp1&0x80;
    tmp2=tmp2|tmp3;
    outportb(0x378,tmp2);
    delay1();
    tmp2=tmp2|0x40;
    outportb(0x378,tmp2);
    delay1();
    ddd=ddd<<1;
  }
  outportb(0x378,tmp3|0xc0);
  outportb(0x37a,0x04); //set d/c and cs high
}


//void init_OLED()
void init_lcd_ctl()
{
  unsigned char tmp1;
  tmp1=inportb(0x378); //protect port378 D0-D5
  tmp1=tmp1|0xc0;  //set D0,D1 high
  outportb(0x378,tmp1);
  outportb(0x37a,0x06); //rest oled use afd , set afd low
  delay(10);
  outportb(0x37a,0x04); //set afd high , end reset

  w_c(0xe2); //soft rest
  delay(10);
  w_c(0xaa);
  delay(1);
  w_c(0xae);
  delay(1);
  w_c(0xad);
  delay(1);
  w_c(0x8a);
  delay(1);
  w_c(0xa8);
  delay(1);
  w_c(0x3f);
  delay(1);
  w_c(0xd3);
  delay(1);
  w_c(0x00);
  delay(1);
  w_c(0x40);
  delay(1);
  w_c(0xa0);
  delay(1);
  w_c(0xc8);
  delay(1);
  w_c(0xa6);
  delay(1);
  w_c(0xa4);
  delay(1);
  w_c(0x81);
  delay(1);
  w_c(0xff);
  delay(1);
  w_c(0xd5);
  delay(1);
  w_c(0x60);
  delay(1);
  w_c(0xd8);
  delay(1);
  w_c(0x00);
  delay(1);
  w_c(0xd9);
  delay(1);
  w_c(0x84);
  delay(1);
  w_c(0xaf);
}

void all_screen(unsigned char display_data)
{
  unsigned char j,page;
  for(page=0;page<8;page++)
  {
    delay(1);
    w_c(0xB0+page);
    delay(1);
    w_c(0x00);
    delay(1);
    w_c(0x10);
    delay(1);
    for(j=0;j<128;j++)
    {
      w_d(display_data);
    }
  }
}

void clr_lcd()
{//clear lcd screen
  all_screen(0x00);
}

void close_lcd()
{//disable oled display
  w_c(0xae);
  delay(10);
  w_c(0xae); //send two disable command
}

int wr_char(unsigned char row,unsigned char colum,unsigned char char1,unsigned char char2,unsigned char att)
{//row=0 - 3 , colum=0 - 15
//att=0(black char , light background) =1(light char , black background)
//char1=ascii code or hanzi left , char2=hanzi right
  unsigned char cs1,page1,yaddr,tmp1,tmp2;
  int i,j;
  FILE *fp;
  long int f_off,off1;
  unsigned char font1[32],font2[16],font3[16];

  if(row>3)return 1;
  if(colum>15)return 2;
//get font from file
  if((char1>=0xa1)&&(char2>=0xa1)){
    if(!(fp=fopen("c:\\lcdhzk16","rb"))){
      printf("open chs16.fon error\n");
      return 4;
    }
    f_off=(char1-0xa1)*94+char2-0xa1;
    f_off=f_off*32;
    fseek(fp,f_off,SEEK_SET);
    fread(font1,32,1,fp);
    fclose(fp);
  }
  else{
    if(!(fp=fopen("c:\\asc16","rb"))){
      printf("open font16 error\n");
      return 3;
    }
    f_off=char1*16;
    fseek(fp,f_off,SEEK_SET);
    fread(font1,16,1,fp);
    fclose(fp);
  }
//display on lcd
//set display start position

//set page : 8 pages on oled , 16 rows for font , so two pages for
//one charactor , this is the difference from old lcd
  w_c(0xb0+row*2);  //set oled page
//set colum address , two cmd needed
  colum=colum*8;
  w_c(colum&0x0f);  //set colum address , low 4 bits
  tmp1=colum>>4;
  tmp1=tmp1|0x10;
  w_c(tmp1);  //set colum address , high 4 bits

  for(i=0;i<=7;i++){
    if(att==0)tmp1=font1[i];
    else tmp1=~font1[i];
    w_d(tmp1);
  }

//one charactor , this is the difference from old lcd
  w_c(0xb0+row*2+1);  //set oled page
//set colum address , two cmd needed
  w_c(colum&0x0f);  //set colum address , low 4 bits
  tmp1=colum>>4;
  tmp1=tmp1|0x10;
  w_c(tmp1);  //set colum address , high 4 bits

  for(i=8;i<=15;i++){
    if(att==0)tmp1=font1[i];
    else tmp1=~font1[i];
    w_d(tmp1);
  }

//display hanzi right half
  if((char1>=0xa1)&&(char2>=0xa1)){
//set display start position

//set page : 8 pages on oled , 16 rows for font , so two pages for
//one charactor , this is the difference from old lcd
    w_c(0xb0+row*2);  //set oled page
//set colum address , two cmd needed
    colum=colum+8;
    w_c(colum&0x0f);  //set colum address , low 4 bits
    tmp1=colum>>4;
    tmp1=tmp1|0x10;
    w_c(tmp1);  //set colum address , high 4 bits


    for(i=16;i<=23;i++){
      if(att==0)tmp1=font1[i];
      else tmp1=~font1[i];
      w_d(tmp1);
    }

//set page : 8 pages on oled , 16 rows for font , so two pages for
//one charactor , this is the difference from old lcd
    w_c(0xb0+row*2+1);  //set oled page
//set colum address , two cmd needed
    w_c(colum&0x0f);  //set colum address , low 4 bits
    tmp1=colum>>4;
    tmp1=tmp1|0x10;
    w_c(tmp1);  //set colum address , high 4 bits

    for(i=24;i<=31;i++){
      if(att==0)tmp1=font1[i];
      else tmp1=~font1[i];
      w_d(tmp1);
    }
  }

  return 0;
}

main()
{
  unsigned char i,j,l,m;
  init_lcd_ctl();
  l=0;
  m=0;
  for(i=0xa1;i<0xf0;i++){
    for(j=0xa1;j<0xf0;j++){
      if(bioskey(1)!=0)return 0;
      wr_char(l,m,i,j,1);
      m=m+2;
      if(m>=15){
	m=0;
	l++;
	if(l>3){
	  m=0;
	  l=0;
	}
      }
    }
  }
}

⌨️ 快捷键说明

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