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

📄 ks0108.c

📁 128*64 LCD 中文显示例程
💻 C
字号:
#include<reg52.h>
#include<intrins.h>
#include "KS0108.H"

sbit CS1 = P3^4; // LOW Select First 64 Cols
sbit CS2 = P3^5; // LOW Select Second 64 Cols
sbit RST= P1^4; // Reset  ,Low significant
sbit DI = P1^5; // High Data ,Low Instruction
sbit RW = P1^6; // High Read ,Low Write
sbit E  = P1^7; // High Enable
#define Data P0

void _KS0108_WriteCommand(BYTE btCmd)
{
  E=0;DI=0;RW=0;Data=(btCmd);E=1;E=0;
}
void  _KS0108_WriteByte(BYTE btData)
{
  E=0;DI=1;RW=0;Data=btData;E=1;E=0;
}

BYTE _KS0108_ReadByte()
{
  BYTE btData;
  E=0;DI=1;RW=1;E=1;_nop_();E=0;//First Dummy Read,latch data to Out Reg
  E=0;DI=1;RW=1;Data=0xff;E=1;btData=Data;E=0;//Second Read ,Read the Out Reg
  return btData;
}

#ifdef _KS0108
void _KS0108_SwitchWay(BYTE Way)
{
   CS1=(!(Way));CS2=(Way);
}
#endif
#ifdef _19264
void _19264_SwitchWay(BYTE Way)
{
   CS1=((Way)&0X01);CS2=((Way)&0X02);
}
#endif
void _KS0108_LCD_DisplayOn(BYTE Way)
{
#ifdef _KS0108
   _KS0108_SwitchWay(Way);
#endif
#ifdef _19264
   _19264_SwitchWay(Way);
#endif
   _KS0108_WriteCommand(CMD_DISPLAY_ON);
}

void KS0108_Reset()
{
   BYTE k;
   for (k=0;k<
#ifdef _KS0108
   2
#endif
#ifdef _19264
   3
#endif
   ;k++)
   {
#ifdef _KS0108
      _KS0108_SwitchWay(k);
#endif
#ifdef _19264
      _19264_SwitchWay(k);
#endif
       RST = 0;
       RST = 1;
   }
}

void _KS0108_LCD_DisplayOff(BYTE Way)
{
#ifdef _KS0108
   _KS0108_SwitchWay(Way);
#endif
#ifdef _19264
   _19264_SwitchWay(Way);
#endif
   _KS0108_WriteCommand(CMD_DISPLAY_OFF);
}

/*
BYTE CheckState(bit Way)
{
   BYTE data bt;
   E=0;DI=0;RW=1;Data=0xff;E=1;bt=Data;E=0;
   return bt;
}
*/
void KS0108_LCD_Off()
{
   _KS0108_LCD_DisplayOff(0);
   _KS0108_LCD_DisplayOff(1);
#ifdef _19264
   _KS0108_LCD_DisplayOff(2);
#endif
}

void KS0108_LCD_On()
{
   _KS0108_LCD_DisplayOn(0);
   _KS0108_LCD_DisplayOn(1);
#ifdef _19264
   _KS0108_LCD_DisplayOn(2);
#endif
}

BYTE KS0108_ReadData(BYTE Row,BYTE Col)
{
#ifdef _KS0108
  _KS0108_SwitchWay(Col>63);
#endif
#ifdef _19264
   _19264_SwitchWay(Col/64);
#endif
  _KS0108_SetStartLine(0);
  _KS0108_SetPageAddr(Row);
  _KS0108_SetColAddr(Col%64);
  return _KS0108_ReadByte();
}

void KS0108_WriteData(BYTE Row,BYTE Col,BYTE btData)
{
#ifdef _KS0108
  _KS0108_SwitchWay(Col>63);
#endif
#ifdef _19264
   _19264_SwitchWay(Col/64);
#endif
  _KS0108_SetStartLine(0);
  _KS0108_SetPageAddr(Row);
  _KS0108_SetColAddr(Col%64);
  _KS0108_WriteByte(btData);
}

void KS0108_ClearLine(BYTE Line,BYTE fillchar)
{
   BYTE j,k;
   for (k=0;k<
#ifdef _KS0108
   2
#endif
#ifdef _19264
   3
#endif
   ;k++)
   {
#ifdef _KS0108
      _KS0108_SwitchWay(k);
#endif
#ifdef _19264
      _19264_SwitchWay(k);
#endif
      _KS0108_SetStartLine(0);
      _KS0108_SetPageAddr(Line);
      _KS0108_SetColAddr(0);
      for (j=0;j<64;j++)
      {
            _KS0108_WriteByte(fillchar);
      }
   }
}
void KS0108_ClearScreen(BYTE fillchar)
{
   BYTE i,j,k;
   for (k=0;k<
#ifdef _KS0108
   2
#endif
#ifdef _19264
   3
#endif
   ;k++)
   {
#ifdef _KS0108
      _KS0108_SwitchWay(k);
#endif
#ifdef _19264
      _19264_SwitchWay(k);
#endif
      _KS0108_SetStartLine(0);
      for (i=0;i<8;i++)
      {
         _KS0108_SetPageAddr(i);
         _KS0108_SetColAddr(0);
         for (j=0;j<64;j++)
         {
            _KS0108_WriteByte(fillchar);
         }
      }
   }
}

void KS0108_WriteStr(BYTE code *HzDot,BYTE code *CharDot,BYTE Row,BYTE Col,unsigned char *str,bit Inverse)
{
   BYTE i,bt;
   BYTE c=0;
   BYTE code *p ;
   bit bdata bFound ;
   while (str[c])
   {
      bFound = 0;
      if (str[c]<128) // is Char
      {
          p = CharDot;
          while (*p != 0x00)
          {
             if (*p == str[c])
             {
                bFound  = 1;
                break;
             }
             p+=17;
          }
          if (bFound)
          {
             p++; //skip asc code
             for (i=0;i<8;i++)
             {
                bt = *p++;
                if (Inverse) bt = ~bt;
                KS0108_WriteData(Row,Col*8+c*8+i,bt);
             }
             for (i=0;i<8;i++)
             {
                bt = *p++;
                if (Inverse) bt = ~bt;
                KS0108_WriteData(Row+1,Col*8+c*8+i,bt);
             }
          }
          c++;
      }
      else
      {
         p = HzDot;
         while ( !(*p==0x55&& *(p+1)==0xaa))
         {
            if (*p == str[c] && *(p+1) == str[c+1])
            {
               bFound  = 1;
               break;
            }
            p+=34;
         }
         if (bFound)
         {
            p +=2;
            for (i=0;i<16;++i)
            {
               bt = *p++;
               if (Inverse) bt = ~bt;
               KS0108_WriteData(Row,Col*8+i+c*8,bt);
            }
            for (i=0;i<16;++i)
            {
               bt = *p++;
               if (Inverse) bt = ~bt;
               KS0108_WriteData(Row+1,Col*8+i+c*8,bt);
            }
         }
         c+= 2;
         //Delay(100);
      }
   }
}

⌨️ 快捷键说明

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