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

📄 hgs12864z.c

📁 HGS12864Z is OLED Display Screen for Instrument display application.
💻 C
字号:

#include <reg52.h>
#include "tsingtek.h"
#include <stdio.h>
#include <intrins.h>
//********************************************************添加2006.3.8
#include "fp1.h"
#include "yl1.h"
#include "yl2.h"
#include "fp2.h"
#include "fp3.h"
#include "fp4.h"
#include "fp5.h"
#include "fp6.h"
#include "fp7.h"
#include "fp8.h"
#include "fp9.h"
#include "fp10.h"
#include "fp11.h"
#include "fp12.h"
#include "fp13.h"
#include "fp14.h"
#include "fp15.h"
#include "fp16.h"
#include "fp17.h"
#include "fp18.h"

#include "cat_off.h"
#include "cat_on.h"
#include "number.h"  //0--9 figure
#include "chinese.h"  //1616 chinese word

//**************************************************************

#define  uchar unsigned char
#define  uint unsigned  int
#define  All_Screen_ON   0xff
#define  All_Screen_OFF  0x00
#define  ON    0
#define  OFF   1

#define REST  P2_2
#define CS    P2_1
#define CD    P2_0
#define WR    P3_6
#define RD    P3_7
#define M80   P2_3     //时序选择  M80 MS=11,80时序
#define MS    P2_4

uchar data_4byte[4];
void write_data(unsigned char DATA);
void WriteCommand(unsigned char command);
void oled_init();
void reset_oled();
void oled_display_zw(uchar DATA);
void oled_display_picture(uchar picture[]);
void write_4_byte(uchar DATA);
void delay(uint uiSec);

//**********************************************************添加

void oled_display(uchar picture[]);
void clear_screen();
void time();
void time1();
void time2();
void time3();
void time4();
void w_word(uchar Row,uchar Column,uchar a);/*write word column、 Row、 a-word*/
void word_1616(uchar Row,uchar Column,uchar a);//16*16word
void show_cat(uchar cat[]);
void cat_offon();
void scroll();

uchar page;

void main()
{
   unsigned char i=5;
   P3=0XFF;
   M80=1;
   MS=1;
   delay(10);
   oled_init();
   oled_display_zw(0x00);
   delay(100);
  while(1)
  {
	clear_screen();
        delay(10000);
        oled_display(fp1);
	delay(6);
        oled_display_picture(Image_1);
        delay(10000);
        oled_display_picture(Image_2);
        delay(10000);
        CD = 0;
        WriteCommand(0xA4);
        clear_screen();
	oled_display(fp2);
	delay(6);
	oled_display(fp3);
	delay(6);
	oled_display(fp4);
	delay(6);
	oled_display(fp5);
	delay(6);
	oled_display(fp6);
	delay(6);
	oled_display(fp7);
	delay(6);
	oled_display(fp8);
	delay(6);
	oled_display(fp9);
	delay(6);
	oled_display(fp10);
	delay(100);
	oled_display(fp9);
	delay(6);
	oled_display(fp8);
	delay(6);
	oled_display(fp7);
	delay(6);
	oled_display(fp6);
	delay(6);
	oled_display(fp5);
	delay(6);
	oled_display(fp4);
	delay(6);
	oled_display(fp3);
	delay(6);
	oled_display(fp2);
	delay(100);
	oled_display(fp1);
	delay(100);
	oled_display(fp10);
	delay(6);
	oled_display(fp11);
	delay(6);
	oled_display(fp12);
	delay(6);
	oled_display(fp13);
	delay(6);
	oled_display(fp14);
	delay(6);
	oled_display(fp15);
	delay(6);
	oled_display(fp16);
	delay(6);
	oled_display(fp17);
	delay(6);
	oled_display(fp18);
	delay(6);
	delay(200);
	delay(200);
	delay(200);
	clear_screen();
    //******************************
        time();
	clear_screen();
        cat_offon();
        word_1616(0,40,8);//速
	word_1616(0,48,5);
	cat_offon();
	word_1616(0,48,9);//度
	word_1616(0,56,5);
	cat_offon();
        word_1616(0,56,10);//快
	word_1616(16,40,5);
        cat_offon();
        word_1616(16,40,11);//温
	word_1616(16,48,5);
        cat_offon();
        word_1616(16,48,9);//度
	word_1616(16,56,5);
        cat_offon();
        word_1616(16,56,12);//宽
	word_1616(32,40,5);
	cat_offon();
        word_1616(32,40,13);//视
	word_1616(32,48,5);
	cat_offon();
        word_1616(32,48,14);//角
	word_1616(32,56,5);
	cat_offon();
        word_1616(32,56,15);//大
	word_1616(48,40,5);
	cat_offon();
        word_1616(48,40,16);//功
	word_1616(48,48,5);
	cat_offon();
        word_1616(48,48,17);//耗
	word_1616(48,56,5);
	cat_offon();
        word_1616(48,56,18);//低
        scroll();
	for(i=0;i<10;i++)
	{
	    delay(100);
	}

          CD = 0;
          WriteCommand(0x15); 	/* Set Column Address */
	  WriteCommand(0x00); 	  /* Start = 0 */
	  WriteCommand(0x3F); 	  /* End = 127 */

// Row Address
	  WriteCommand(0x75); 	/* Set Row Address */
	  WriteCommand(0x0c); 	  /* Start = 0 */
	  WriteCommand(0x4b); 	  /* End = 63 */
	  WriteCommand(0xA1); 	/* Set Display Start Line */
	  WriteCommand(0x0c);
	  WriteCommand(0xA2); 	/* Set Display Offset */
	  WriteCommand(0x4c); 	  /* No offset */
  }

}

void reset_oled()               //低复位
{
    unsigned char i=0;
	REST = 0;
    _nop_();
	REST = 1;
    _nop_();
}

void write_data(unsigned char DATA)
{

        CS=0;
        CD=1;
        P0=DATA;
        WR=0;
        _nop_();
        _nop_();
        WR=1;
        _nop_();
        _nop_();


}

void WriteCommand(unsigned char command)
{

        CS=0;
        CD=0;
        P0=command;
        WR=0;
        _nop_();
        _nop_();
        WR=1;
        _nop_();
        _nop_();

}

 void oled_init()
{
        CS=0;       //Chip select
        _nop_();
        reset_oled();      //oled reset
        CD=0;       //send command
        _nop_();
/******************************************
// SSD0323 Initialization Command
****************************/
// Column Address
	  WriteCommand(0x15); 	/* Set Column Address */
	  WriteCommand(0x00); 	  /* Start = 0 */
	  WriteCommand(0x3F); 	  /* End = 127 */

// Row Address
	  WriteCommand(0x75); 	/* Set Row Address */
	  WriteCommand(0x0c); 	  /* Start = 0 */
	  WriteCommand(0x4b); 	  /* End = 63 */

// Contrast Control
	  WriteCommand(0x81); 	/* Set Contrast Control */
	  WriteCommand(0x7f); 	  /* 0 ~ 127 */

// Current Range
	 WriteCommand(0x86); 	/* Set Current Range84h:Quarter, 85h:Half,86h:Full*/

// Re-map
	  WriteCommand(0xA0); 	/* Set Re-map */
	  WriteCommand(0x52); 	  /* [0]:MX, [1]:Nibble, [2]:H/Vaddress [4]:MY, [6]:Com Split Odd/Even "1000010"*/

// Display Start Line
	  WriteCommand(0xA1); 	/* Set Display Start Line */
	  WriteCommand(0x0c); 	  /* Top */

// Display Offset
	  WriteCommand(0xA2); 	/* Set Display Offset */
	  WriteCommand(0x4c); 	  /* No offset */

// Display Mode
	  WriteCommand(0xA4); 	/* Set DisplaMode,A4:Normal,A5:All ON, A6: All OFF,A7:Inverse */

// Multiplex Ratio
	  WriteCommand(0xA8); 	/* Set Multiplex Ratio */
	  WriteCommand(0x3F); 	  /* [6:0]16~128, 64 rows=3Fh*/

// Phase Length
	  WriteCommand(0xB1); 	/* Set Phase Length */
	  WriteCommand(0x22); 	  /* [3:0]:Phase 1 period of 1~16clocks */
							  /* [7:4]:Phase 2 period of 1~16clocks /* POR = 0111 0100 */
// Row Period
	  WriteCommand(0xB2); 	/* Set Row Period */
	  WriteCommand(0x46); 	  /* [7:0]:18~255,K=P1+P2+GS15 (POR:4+7+29)*/

// Display Clock Divide
	  WriteCommand(0xB3); 	/* Set Clock Divide */
	  WriteCommand(0x41); 	  /* [3:0]:1~16, [7:4]:0~16 */
                              /* POR = 0000 0001 */
// VSL
	  WriteCommand(0xBF); 	/* Set VSL */
	  WriteCommand(0x0D); 	  /* [3:0]:VSL */

// CCOMH
	  WriteCommand(0xBE); 	/* Set VCOMH */
	  WriteCommand(0x00); 	  /* [7:0]:VCOMH */

// VP
	  WriteCommand(0xBC); 	/* Set VP */
	  WriteCommand(0x0B); 	  /* [7:0]:VP */

// Gamma
	  WriteCommand(0xB8); 	/* Set Gamma with next 8 bytes*/
	  WriteCommand(0x01); 	  /* L1[2:1] */
	  WriteCommand(0x11); 	  /* L3[6:4], L2[2:0] 0001 0001*/
	  WriteCommand(0x22); 	  /* L5[6:4], L4[2:0] 0010 0010*/
	  WriteCommand(0x32); 	  /* L7[6:4], L6[2:0] 0011 1011*/
	  WriteCommand(0x43); 	  /* L9[6:4], L8[2:0] 0100 0100*/
	  WriteCommand(0x54); 	  /* LB[6:4], LA[2:0] 0101 0101*/
	  WriteCommand(0x65); 	  /* LD[6:4], LC[2:0] 0110 0110*/
	  WriteCommand(0x76); 	  /* LF[6:4], LE[2:0] 1000 0111*/

// Set DC-DC
	  WriteCommand(0xAD); 	/* Set DC-DC */
	  WriteCommand(0x02); 	  /* 03=ON, 02=Off */

// Display ON/OFF
	  WriteCommand(0xAF); 	/* AF=ON, AE=Sleep Mode */
}

void oled_display_zw(uchar DATA)
{
  uint i,j;
  CD = 1;
  for (j=0;j<64;j++) /* 64 row */
   {
	 for (i=0;i<64;i++) /* 128 column  a nibble of command is a dot*/
	  {
		write_data(DATA);
	  }
   }
}

void oled_display_picture(uchar picture[])                      //有灰度 IMAGETOLCD
{
  uint i,j;
  CD = 0;
  WriteCommand(0xA7);
  CD = 1;
  for (j=0;j<64;j++) /* 64 row */
   {
	 for (i=0;i<64;i++) /* 16*8 column  a nibble of command is a dot*/
	  {
		write_data(picture[(j<<6)+i]);
	  }
   }
}


void write_4_byte(uchar DATA)
{
  uchar k;
  uchar kk,kkk;
  kk=DATA;
  for(k=0;k<4;k++)
   {
     kkk=kk&0xc0;
     switch(kkk)
       {
		 case 0x00:
           data_4byte[k]=0x00;
         break;
         case 0x40:
           data_4byte[k]=0x0f;
         break;
		 case 0x80:
           data_4byte[k]=0xf0;
         break;
         case 0xc0:
           data_4byte[k]=0xff;
         break;
      	 default:
      	 break;
	   }
     kk=kk<<2;
      write_data(data_4byte[k]);                /* 8 column  a nibble of command is a dot*/
   }

}

void delay(uint uiSec)
{
     uchar j;
     while(uiSec--)
     {
     	for(j=0;j<80;j++)
     	{_nop_();}

     }
}

//***********************************************************添加
void clear_screen()
{
    uint j,i;
	  CD=0;
	    WriteCommand(0x15); 	/* Set Column Address */
	    WriteCommand(0x00); 	  /* Start = 0 */
	    WriteCommand(0x3F); 	  /* End = 127 */

// Row Address
	  WriteCommand(0x75); 	/* Set Row Address */
	    WriteCommand(0x0c); 	  /* Start = 0 */
	    WriteCommand(0x4b); 	  /* End = 63 */
      CD=1;
        for (j=0;j<64;j++) /* 64 row */
         {
	       for (i=0;i<16;i++) /* 16*8 column  a nibble of command is a dot*/
	        {
		      write_4_byte(0x00);
	        }
         }

}
//================================================================
//
void oled_display(uchar picture[])                              //字模3软件  无灰度
{
	uint j,i;
	  CD=0;
	    WriteCommand(0x15); 	/* Set Column Address */
	    WriteCommand(0x00); 	  /* Start = 0 */
	    WriteCommand(0x3F); 	  /* End = 127 */

// Row Address
	    WriteCommand(0x75); 	/* Set Row Address */
	    WriteCommand(0x0c); 	  /* Start = 0 */
	    WriteCommand(0x4b); 	  /* End = 63 */
          CD=1;
		for(j=0;j<64;j++)
		{
	       for (i=0;i<16;i++) /* 16*8 column  a nibble of command is a dot*/
	        {
		      write_4_byte(picture[(j<<4)+i]);
	        }
		}
}
/******************************************************************/
void time()
{
        word_1616(8,4,0);//物
        word_1616(8,12,1);//理
	word_1616(8,20,2);//量
	word_1616(8,28,3);//监
	word_1616(8,36,4);//测
	word_1616(8,44,6);//计
	word_1616(8,52,7);//时
        w_word(32,12,0);
        w_word(32,20,0);
        w_word(32,28,10);
	w_word(32,36,0);
	w_word(32,44,0);
	time4();
}
/**************************************************************************/
void time1()
{
    uchar i;
    for(i=0;i<10;i++)
	{
	    w_word(32,44,i);
		delay(100);
	}
}
/*************************************************************************/
void time2()
{
    uchar i;
	for(i=0;i<2;i++)
	{
	    w_word(32,36,i);
		time1();
	}
}
/*************************************************************************/
void time3()
{
    uchar i;
	for(i=0;i<2;i++)
	{
	    w_word(32,20,i);
		time2();
	}
}
/***************************************************************************/
void time4()
{
    uchar i;
	for(i=0;i<2;i++)
	{
	    w_word(32,12,i);
		time3();
	}
}
/**********************************************************************/
void w_word(uchar Row,uchar Column,uchar a)//write 0-9 figure
{
    uint i,j;

	for(j=0;j<32;j++)
	 {
        CD=0;
	    WriteCommand(0x75); 	/* Set Row Address */
	    WriteCommand(0x0c+Row+j); 	  /* Start = Row */
	    WriteCommand(0x0c+Row+31); 	  /* End = Row+16 */
	    WriteCommand(0x15); 	/* Set Column Address */
	    WriteCommand(Column); 	  /* Start = Column */
	    WriteCommand(Column+7); 	  /* End = Column+32 */
        CD=1;
	   for (i=0;i<2;i++) /* 2*8 column , a nibble of command is a dot*/
	     {
		   write_4_byte(number[a][(j<<1)+i]);
	     }
	 }
}
/************************************************************************/
void word_1616(uchar Row,uchar Column,uchar a)//write chinese word of1616
{
    uint i,j;

	for(j=0;j<16;j++)
	 {
        CD=0;
	    WriteCommand(0x75); 	/* Set Row Address */
	    WriteCommand(0x0c+Row+j); 	  /* Start = Row */
	    WriteCommand(0x0c+Row+15); 	  /* End = Row+16 */
	    WriteCommand(0x15); 	/* Set Column Address */
	    WriteCommand(Column); 	  /* Start = Column */
	    WriteCommand(Column+7); 	  /* End = Column+16 */
        CD=1;
	   for (i=0;i<2;i++) /* 2*8 column , a nibble of command is a dot*/
	     {
		   write_4_byte(chinese[a][(j<<1)+i]);
	     }
	 }
}
/***************************************************************************
void word_1616(uchar Row,uchar Column,uchar a)//write chinese word of1616
{
    uint i,j;

	  CD=0;
	  WriteCommand(0x15); 	//* Set Column Address
	    WriteCommand(Column); 	  //* Start = Column
	    WriteCommand(0x3F); 	  //* End = 127
	  WriteCommand(0x75); 	//* Set Row Address
	    WriteCommand(0x0c+Row); 	  //* Start = Row
	    WriteCommand(0x4b); 	  //* End = 63
      CD=1;
	for(j=0;j<16;j++)
	 {
	   for (i=0;i<2;i++) //* 16*8 column  a nibble of command is a dot
	     {
		   write_4_byte(chinese[a][(j<<1)+i]);
	     }
	 }
}
/***************************************************/
//******************************************************************************
void show_cat(uchar picture[])
{
   uint i,j;
   for(j=0;j<64;j++)
    {
	  CD=0;
	  WriteCommand(0x15); 	/* Set Column Address */
	    WriteCommand(0); 	  /* Start = Column */
	    WriteCommand(39); 	  /* End = 127 */

	  WriteCommand(0x75); 	/* Set Row Address */
	    WriteCommand(0x0c+j); 	  /* Start = Row */
	    WriteCommand(0x0c+0x3f); 	  /* End = 63 */
      CD=1;
		for(i=0;i<10;i++)//picture is 58*64
		{
     		write_4_byte(picture[(j*10)+i]);
		}
    }

}
/***************************************************************************/
void cat_offon()
{
    uchar i;
    show_cat(cat_off);
	for(i=0;i<5;i++)
	{
	    delay(200);
	}
	show_cat(cat_on);
	for(i=0;i<5;i++)
	{
	    delay(200);
	}
}
/*****************************************************************************/
void scroll()
{
    uchar i;
    CD=0;
    for(i=0x0c;i<0x4b;i++)
      {
	    WriteCommand(0xA2); 	/* Set Display Offset */
	    WriteCommand(i); 	  /* No offset */
        delay(800);
      }
}
/****************************************************/

⌨️ 快捷键说明

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