ligang 发表于 2013-12-20 00:28:53

液晶模块1601和1602的C程序分享

//                  1601 & 1602 测试程序
//***************************************************************************
//RS=P2.0            R/W=P2.1                                             *
//FOSC=12MHz         D0-D7=P1.0-P1.7   E=P2.2      /RSET=/(CPU RSET)*
//***************************************************************************
//来自电子开发者论坛
#include <reg52.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>

#defineDATA_BUS P0

sbit RS=P2^0 ;
sbit RW=P2^1 ;
sbit E=P2^2 ;

sbit P33=P3^3; //P3.3是一个暂停和启动按键
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;       //P3.3是一个暂停和启动按键
    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 CheckBusy( void )
{
unsigned char redata;

RS=0;//选择指令寄存器
RW=1;//读取指令寄存器

P0=0xff;
E=1;
redata=P0;
while(redata&0x80)
{
redata=P0;
}
E=0;

         
}
void WriteCmd( unsigned char CmdByte )
{
CheckBusy();
         RS=0;//选择指令寄存器

         RW=0;//MCU向LCD写入命令数据

DATA_BUS= CmdByte;
   
         E=1;
   
         E=0;
}
void WriteData( unsigned char DataByte )
{
CheckBusy();
         RS=1;//选择数据寄存器

         RW=0;//MCU向LCD写入
      
DATA_BUS = DataByte;
      
          E=1;
         
          E=0;
}
void DelayMS( unsigned int MS )
{
unsigned char us,usn;
while(MS!=0)
{
usn = 2;
while(usn!=0)
{
   us=0xf2;
   while (us!=0){us--;};
   usn--;
}
MS--;
}
}

void DelayKey(unsigned int Second , unsigned int MS100)
{      //输入精确到0.1S,是用,
unsigned int i;
for(i=0;i<Second*100+MS100*10;i++)
{
DelayMS(10);
}
}
void LcmInit( void ) //电子开发者论坛
{
WriteCmd(0x38);
WriteCmd(0x06);
WriteCmd(0x0C);
WriteCmd(0x01);
}
void SetCGRAM( unsigned char *puts )
{
unsigned char i;
WriteCmd(0x40);
for(i=0;i<64;i++)
{
WriteData( puts );
}
}
void PutStr(unsigned char *puts)
{
unsigned char i,j;
WriteCmd(0x80);
j = 0;
for(i=0;i<40;i++)
{
WriteData(puts);
j++;
}
WriteCmd(0xC0);
j = 0;
for(i=0;i<40;i++)
{
WriteData(puts);
j++;
}
}
void FullDDRAM(unsigned char fillchar)
{
unsigned char i;
WriteCmd(0x80); //AC=00H
for(i=0;i<128;i++)
{
WriteData(fillchar); //电子开发者论坛
}
}
void ROMCheck(void) //把CGROM字库后面的字体显示出来判断是俄文还是日文
{
unsigned char i,j;
WriteCmd(0x80);
j = 0;
for(i=0;i<40;i++) //最多40x2字符点阵
{
WriteData(0xA1+j);
j++;
}
WriteCmd(0xC0); //第二行显示,和第一行一样
j = 0;
for(i=0;i<40;i++)
{
WriteData(0xA1+j);
j++;
}
}

unsigned char code str1[]="Hello World 2009/0123456789ABCDEFGHIJKLM";
unsigned char code CUSTOM[]={
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00, //横线1
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff, //横线2
0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15, //竖线1
0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a, //竖线2
0x15,0x0a,0x15,0x0a,0x15,0x0a,0x15,0x0a, //网点
0x0a,0x15,0x0a,0x15,0x0a,0x15,0x0a,0x15, //网点
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //全黑
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //全白
};
void main( void )
{
DelayMS(50);
               //外部中断1初始化
      IT1 = 0;
      EX1 = 1;
      EA = 1;//打开中断
         
LcmInit();
SetCGRAM(CUSTOM);
while(1)
{
FullDDRAM(0x00); //横线1
DelayKey(0,6);
FullDDRAM(0x01); //横线2
DelayKey(0,6);
FullDDRAM(0x02); //竖线1
DelayKey(0,6);
FullDDRAM(0x03); //竖线2
DelayKey(0,6);
FullDDRAM(0x04); //网点1
DelayKey(0,6);
FullDDRAM(0x05); //网点2
DelayKey(0,6);
FullDDRAM(0x06); //全黑
DelayKey(0,6);
PutStr(str1);//电子开发者论坛
DelayKey(0,6);
ROMCheck();
DelayKey(0,6);
FullDDRAM(0x20); //全空白
DelayKey(1,2);
}
}

Somebody 发表于 2013-12-20 08:41:16

页: [1]
查看完整版本: 液晶模块1601和1602的C程序分享