独闷闷网

 找回密码
 立即注册
搜索
查看: 3499|回复: 2
收起左侧

[原创] ST7920驱动芯片的12864液晶屏的C语言程序

[复制链接]
发表于 2013-12-27 00:03:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 ligang 于 2013-12-27 00:05 编辑

//转自电子开发者论坛
//                12864 ST7920 演示程序(并口)
//连线表:    CPUClock=12Mhz                      *
//  LCM   -----   CPU                                           *
//  RS    -----   P2.0                                          *
//  R/W   -----   P2.1                                          *
//  E     -----   /(WR*RD)(P3.6与P3.7与非)(总线模式)            *
//  DB0~7 -----   P0.0~7                                        *
//  注:本程序BUS总线模式程序和IO模式程序的硬件连线方式一致      *
//     但IO模式的时候需要在P0口接10K上拉电阻                    *
//***************************************************************
#include <reg52.h>
#include <intrins.h>
#define LcmXPixel 128 //横像素
#define LcmYPixel 64 //纵像素
//#define BUS_MODE //总线模式
#define IO_MODE //IO模式
//总线模式程序接口
#ifdef BUS_MODE
char xdata LcmWCmdPort  _at_ 0x0000; //写命令
char xdata LcmWDatPort  _at_ 0x0100; //写数据
char xdata LcmRStaPort  _at_ 0x0200; //读状态
char xdata LcmRDatPort  _at_ 0x0300; //读数据
#endif
void int_zhongduan();
void delay(unsigned int t);
//IO模式程序接口
#ifdef IO_MODE
sbit RSP = P2^0; //RSP表示RS Port,P=Port,下同
sbit RWP = P2^1;
sbit EP  = P2^2; //E信号在本程序中是通过单片机的WR和RD与非出来的,是为了不改动总线模式的连线方式
sbit RES =P2^3;
#define LcmDataBus P0 //用户可以不用这样操作,直接用一个IO口来操作,替换本程序中_E1_()和_E0_()两个函数
#endif

sbit P33=P3^3;
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
unsigned char code str1[]="黄河远上白云间,一片孤城万仞山。羌笛何须怨杨柳,春风不度玉门关。";
unsigned char code str2[]="简体字 or ";                       //简体字or
unsigned char code str3[]={0xC1,0x63,0xC5,0xE9,0xA6,0x72,0};//繁體字
unsigned char code bmp1[];
//IO模式下,在不改变总线模式连线方式情况下,E只能通过操作WR与RD来实现
#ifdef IO_MODE
void _E1_(void)
{
   EP=1;
}
void _E0_(void)
{
   EP=0;
}
#endif
void CheckBusy( void )
{
#ifdef BUS_MODE
while(LcmRStaPort&0x80);
#endif
#ifdef IO_MODE
RSP = 0;
RWP = 1;
LcmDataBus = 0xFF; //输出0xff以便读取正确
// EP  = 1;
_E1_();   //相当于EP=1
_nop_();
while(LcmDataBus & 0x80); //Status Read Bit7 = BUSY
// EP  = 0;
_E0_();   //相当于EP=0
_nop_();
#endif
}
void WriteCommand( unsigned char Cbyte )
{
CheckBusy();
#ifdef BUS_MODE
LcmWCmdPort = Cbyte;
#endif
#ifdef IO_MODE
RSP = 0;
RWP = 0;
LcmDataBus = Cbyte;
// EP  = 1;
_E1_();   //相当于EP=1
_nop_();
// EP  = 0;
_E0_();   //相当于EP=0
_nop_();
#endif
}
void WriteData( unsigned char Dbyte )
{
CheckBusy();
#ifdef BUS_MODE
LcmWDatPort = Dbyte;
#endif
#ifdef IO_MODE
RSP = 1;
RWP = 0;
LcmDataBus = Dbyte;
// EP  = 1;
_E1_();   //相当于EP=1
_nop_();
// EP  = 0;
_E0_();   //相当于EP=0
_nop_();
#endif
}
unsigned char ReadData( void )
{
unsigned char RData;
CheckBusy();
#ifdef BUS_MODE
RData = LcmRDatPort;
#endif
#ifdef IO_MODE
RSP = 1;
RWP = 1;
LcmDataBus = 0xFF; //输出0xff以便读取正确
// EP  = 1;
_E1_();   //相当于EP=1
_nop_();
RData = LcmDataBus;
// EP  = 0;
_E0_();   //相当于EP=0
_nop_();
#endif
return RData;
}
void DelayMS(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0)
{
  usn = 2; //for 12M
  while(usn!=0)
  {
   us=0xf6;
   while (us!=0){us--;};
   usn--;
  }
  MS--;
}
}
//转自电子开发者论坛
void DelayKey(unsigned int Second , unsigned int MS100)
{
unsigned int i;
for(i=0;i<Second*100+MS100*10;i++)
{
  DelayMS(10);
}
}
void LcmInit( void )
{
    RES=0;
DelayMS(10);
    RES=1;
DelayMS(30);
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(0x03); //AC归0,不改变DDRAM内容
WriteCommand(0x0C); //显示ON,游标OFF,游标位反白OFF
WriteCommand(0x01); //清屏,AC归0
WriteCommand(0x06); //写入时,游标右移动
}
void LcmClearTXT( unsigned char FillByte )
{
unsigned char i;
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(0x80); //AC归起始位
for(i=0;i<64;i++)
    WriteData(FillByte);
}
void LcmClearBMP( unsigned char FillByte )
{
unsigned char i,j;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<32;i++)  //12864实际为256x32
{
  WriteCommand(0x80|i);
  WriteCommand(0x80);
  for(j=0;j<32;j++)
      WriteData(FillByte);
}
}

void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(AC_TABLE[8*row+col]); //起始位置
while(*puts != '\0')
{
  if(col==8)
  {
   col=0;
   row++;
  }
  if(row==4) row=0; //一屏显示完,回到屏左上角
  WriteCommand(AC_TABLE[8*row+col]);
  WriteData(*puts);
  puts++;
  WriteData(*puts);
  puts++;
  col++;
}
}
//文本区读取测试
//可以将显示的文本内容读取出来
void TXTReadDemo( void )
{
unsigned char i;
unsigned char x,y;
PutStr(0,0,str2);
PutStr(0,5,str3);
for(i=0;i<8;i++)
{
  WriteCommand(0x80|i);
  x = ReadData();  //假读一次
  x = ReadData();
  y = ReadData();
  WriteCommand(0x90|i); //将读取的内容写到第三行
  WriteData(x);
  WriteData(y);
  WriteCommand(0x88|i); //将读取的内容写到第二行
  WriteData(x);
  WriteData(y);
  WriteCommand(0x98|i); //将读取的内容写到第四行
  WriteData(x);
  WriteData(y);
}
}
void PutBMP(unsigned char *puts)
{
unsigned int x=0;
unsigned char i,j;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<32;i++)
{
  WriteCommand(0x80|i); //行位置
  WriteCommand(0x80); //列位置
  for(j=0;j<32;j++)
  {
   WriteData(puts[x]);
   x++;
  }
}
}
//转自电子开发者论坛
//反向当前显示的图形内容,只能用于图形区
void RevBMP( void )
{
unsigned char x,y;
unsigned char i,j;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<32;i++)
{
  for(j=0;j<16;j++)
  {
   WriteCommand(0x80|i); //行位置
   WriteCommand(0x80|j); //列位置
   x = ReadData();  //假读一次
   x = ReadData();
   y = ReadData();
   WriteCommand(0x80|i); //重新设置回地址
   WriteCommand(0x80|j);
   WriteData(~x);  //注意:绘图模式下显示数据必须一次写2byte
   WriteData(~y);
  }
}
}
void PutDots(unsigned char DotByte,unsigned char n)
{
unsigned char i,j,k;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<(32/n);i++)
{
  for(k=0;k<n;k++)
  {
   WriteCommand(0x80|(i*n+k)); //行位置
   WriteCommand(0x80);  //列位置
   for(j=0;j<32;j++)
   {
    WriteData(DotByte);
   }
  }
  DotByte=~DotByte;
}
}

//转自电子开发者论坛
void main( void )
{
DelayMS(100); //等待复位
        int_zhongduan();
LcmInit();
LcmClearTXT(0x20);
LcmClearBMP(0x00);
while(1)
{
  LcmClearTXT(0x20);
  PutBMP(bmp1);
  DelayKey(1,5);
  RevBMP();
  DelayKey(1,5);
  LcmClearBMP(0xFF);
  DelayKey(1,5);
  LcmClearBMP(0x00);
  PutStr(0,0,str1);
  DelayKey(1,5);
  LcmClearTXT(0x20);
  TXTReadDemo();
  DelayKey(1,5);
  LcmClearTXT(0x20);
  PutDots(0x55,1);
  DelayKey(1,5);
  PutDots(0xAA,1);
  DelayKey(1,5);
  PutDots(0x33,2);
  DelayKey(1,5);
  PutDots(0xCC,2);
  DelayKey(1,5);
  
  PutDots(0x0F,4);
  DelayKey(1,5);
  PutDots(0xF0,4);
  DelayKey(1,5);
}
}
unsigned char code bmp1[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x80,0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x3F,0xC0,0x00,0x07,0x07,0x00,0x04,0x01,0x00,0x00,0x38,0x60,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x78,0xE0,0x00,0x3F,0x83,0x80,0x06,0x03,0x80,0x00,0x1C,0x70,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x38,0x70,0x00,0xFF,0x07,0x80,0x06,0x39,0x80,0x00,0x1F,0x70,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x1C,0x30,0x00,0x7C,0x0E,0x00,0x06,0x19,0xC0,0x00,0x1F,0x80,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x1C,0x30,0x00,0x3E,0x1C,0x00,0x06,0x1F,0xC0,0x00,0x7F,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x03,0x3E,0x30,0x00,0x62,0x1A,0x00,0x0F,0x79,0x00,0x03,0xFE,0x00,0x00,0x01,
0x80,0x00,0x00,0x60,0x0F,0x00,0x3F,0x00,0x00,0x00,0x3C,0x00,0xC0,0x00,0x00,0x01,
0x80,0x07,0x76,0x18,0x00,0xC3,0xC1,0x00,0x1F,0xFB,0x00,0x03,0xFE,0x00,0x00,0x01,
0x80,0x00,0x01,0xE0,0x3F,0xC0,0xF1,0xC0,0x00,0x00,0xF0,0x01,0xC0,0x00,0x00,0x01,
0x80,0x07,0x6E,0x18,0x00,0xCF,0xC1,0x80,0x7E,0x3F,0x80,0x01,0xE2,0x00,0x00,0x01,
0x80,0x00,0x07,0xE0,0x7F,0xE0,0xE0,0xE0,0x00,0x03,0xC0,0x03,0xC0,0x00,0x00,0x01,
0x80,0x07,0x7F,0x98,0x00,0xFE,0x83,0x81,0xFC,0x1E,0xC0,0x00,0x83,0x00,0x00,0x01,
0x80,0x00,0x0F,0xE0,0x63,0xE1,0xE0,0xE0,0x00,0x07,0x00,0x03,0xC0,0x00,0x00,0x01,
0x80,0x06,0xFF,0xD8,0x01,0xFA,0x87,0x00,0xFD,0xF8,0xC0,0x01,0x73,0x00,0x00,0x01,
0x80,0x00,0x01,0xE0,0xC1,0xE1,0xE0,0xE0,0x00,0x0F,0x00,0x07,0xC0,0x00,0x00,0x01,
0x80,0x0F,0xD9,0xD8,0x03,0xE3,0x0E,0x80,0xFE,0x7E,0xC0,0x03,0xF9,0x80,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xE1,0xF0,0xE0,0x00,0x1E,0x00,0x0B,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xB8,0x18,0x1F,0xC3,0x08,0xC0,0x0E,0x6D,0x80,0x03,0xF1,0x80,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xE0,0xFD,0xC7,0xEF,0x9E,0x00,0x13,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xFE,0x18,0x0F,0xC2,0x10,0xE0,0x1C,0x3F,0x00,0x03,0xE0,0xC0,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xC0,0xFF,0x83,0xC6,0x1F,0xE0,0x13,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xCE,0x18,0x06,0xC2,0x01,0xC0,0x38,0x3E,0x00,0x00,0xE8,0xC0,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0x80,0x3F,0x81,0xE4,0x3C,0x78,0x23,0xC0,0x00,0x00,0x01,
0x80,0x0C,0x4C,0x18,0x00,0xC2,0x03,0x80,0x78,0x1F,0xF0,0x00,0xFC,0x60,0x00,0x01,
0x80,0x00,0x01,0xE0,0x03,0x80,0x3F,0xC1,0xE8,0x3C,0x78,0x43,0xC0,0x00,0x00,0x01,
0x80,0x0C,0x5E,0x18,0x00,0xC2,0x07,0x00,0x68,0x3F,0xF8,0x03,0xFC,0x22,0x00,0x01,
0x80,0x00,0x01,0xE0,0x03,0x00,0x6F,0xC0,0xF0,0x3C,0x3C,0x83,0xC0,0x00,0x00,0x01,
0x80,0x06,0x3F,0x38,0x01,0x82,0x0E,0x00,0x09,0xF8,0x00,0x03,0xC0,0x32,0x00,0x01,
0x80,0x00,0x01,0xE0,0x06,0x00,0xC3,0xE0,0x78,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x07,0x10,0x38,0x00,0x82,0x1C,0x00,0x0B,0x99,0xC0,0x03,0x00,0x1A,0x00,0x01,
0x80,0x00,0x01,0xE0,0x0C,0x11,0xC3,0xE0,0x78,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x03,0xFF,0xF0,0x00,0x02,0x38,0x00,0x08,0x30,0xE0,0x02,0x00,0x1E,0x00,0x01,
0x80,0x00,0x01,0xE0,0x08,0x11,0xC1,0xE0,0x7C,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x01,0xEF,0xF0,0x00,0x00,0xE0,0x00,0x08,0xE0,0x60,0x00,0x00,0x0E,0x00,0x01,
0x80,0x00,0x01,0xE0,0x1F,0xF1,0xC1,0xE0,0xBC,0x1C,0x3C,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x03,0xE0,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x07,0x00,0x01,
0x80,0x00,0x01,0xE0,0x3F,0xE1,0xC1,0xC0,0x9E,0x1E,0x38,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
0x80,0x00,0x01,0xF0,0x7F,0xE0,0xE3,0x83,0x1F,0x0E,0x70,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x0F,0xFC,0xFF,0xE0,0x3F,0x07,0xDF,0x83,0xE0,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x01,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};

void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++)
;
}
void Interrupt1 (void) interrupt 2  //暂停和启动的按键
{
    P33=1;
    delay(10);
    delay(10);
while(P33==0)
    {
       P33=1;
    delay(10);
    delay(10);
    }

    delay(10);
    delay(10);
while(P33==1)
    {
       P33=1;
    delay(10);
    delay(10);
    }

    delay(10);
    delay(10);
while(P33==0)
    {
       P33=1;
    delay(10);
    delay(10);

    }
}

void int_zhongduan()
{

//外部中断1初始化
IT1 = 0;
EX1 = 1;
EA = 1;//打开中断
}


乐于分享,勇于质疑!
发表于 2013-12-29 16:54:52 | 显示全部楼层
好东西!顶你!
乐于分享,勇于质疑!
 楼主| 发表于 2013-12-30 23:27:32 | 显示全部楼层

谢谢你顶我。有空常来顶顶。
乐于分享,勇于质疑!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|独闷闷网 ( 粤ICP备12007667号-2 )

GMT+8, 2021-9-29 09:05 , Processed in 0.153131 second(s), 18 queries .

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