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

📄 12864_1.c

📁 A16单片机的128*64液晶程序(ks0108驱动)
💻 C
字号:
 /**************************************************************************
                           
 程序名称:ks0108\ks0107驱动12864液晶显示程序
 程序简介:本程序简单介绍了12864液晶显示图片及字符和汉字的程序
 程序效果:可以见12864_1.dsn中的仿真
 作者:张聪斌
 时间:2006年11月11日
 晶振:内部 4M
 编译工具:iccavr6.31	
 ***************************************************************************/	
 //所用字模生成软件为pctolcd2002,设置格式为阴码 列行式 逆向 c51格式
 #include<iom16v.h>
 #include"macros.h"
 /*  常量定义  */
#define uchar unsigned char
#define uint  unsigned int
/*
#define  LCD_STATUS_BUSY 0x80    //判断忙指令 
#define  DISPLAY_ON      0x3f    //开显示命令
#define  DISPLAY_OFF     0x3e   //关显示命令
#define  SET_COLUMN      0x40  //设定列地址0-63命令
#define  SET_LINE        0xb8  //设定行地址(页)0-7指令
#define  START_LINE0     0xc0    //设定开始行命令*/
//#define  BIT(x)  (1<<x)

//PORTA---数据口  PORTB----控制口
#define LCD_DIR_DDR   DDRA
#define LCD_IP_PIN    PINA
#define LCD_DATA_PORT   PORTA

#define LCD_EN_PORT   PORTB
#define LCD_CS2_PORT  PORTB
#define LCD_CS1_PORT  PORTB
#define LCD_RW_PORT   PORTB
#define LCD_DI_PORT   PORTB

//uchar LCD_EN_BIT  = BIT(4);
//uchar LCD_CS2_BIT = BIT(2);
//uchar LCD_CS1_BIT = BIT(3);
uchar LCD_RW_BIT  = BIT(1);
//#define LCD_DI_BIT    BIT(0) 
uchar LCD_DI_BIT  = BIT(0);
//#define LCD_RW_BIT    (1<<1)
#define LCD_EN_BIT    BIT(4) 
#define LCD_CS2_BIT   BIT(2)
#define LCD_CS1_BIT   BIT(3)





#define SET_LCD_E      	        LCD_EN_PORT |= LCD_EN_BIT  //LCD使能
#define CLR_LCD_E               LCD_EN_PORT &= ~LCD_EN_BIT  //LCD禁止

#define SET_LCD_DI              LCD_DI_PORT |= LCD_DI_BIT    //选择指令端口
#define CLR_LCD_DI              LCD_DI_PORT &= ~LCD_DI_BIT   //选择数据端口

#define SET_LCD_RW      	    LCD_RW_PORT |= LCD_RW_BIT     //读模式
#define CLR_LCD_RW      	    LCD_RW_PORT &= ~LCD_RW_BIT    //写模式

#define SET_LCD_CS2      	    LCD_CS2_PORT |= LCD_CS2_BIT  //左屏选择禁止
#define CLR_LCD_CS2      	    LCD_CS2_PORT &= ~LCD_CS2_BIT //左屏选择使能

#define SET_LCD_CS1      	    LCD_CS1_PORT |= LCD_CS1_BIT  //左屏选择禁止
#define CLR_LCD_CS1      	    LCD_CS1_PORT &= ~LCD_CS1_BIT //左屏选择使能

uchar  Numcode[11][16]={
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00},
{0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00}
					  };                      

uchar  HZcode[][32]=
{

{0x40,0x40,0x40,0x5F,0x55,0x55,0x55,0x75,0x55,0x55,0x55,0x5F,0x40,0x40,0x40,0x00,
 0x00,0x20,0x20,0x17,0x1D,0x45,0x85,0x7D,0x05,0x05,0x0D,0x17,0x70,0x20,0x00,0x00/*"景",0*/},   

{0x10,0x88,0xE4,0x3B,0x12,0x04,0xF4,0x94,0xF4,0x9F,0x94,0xF4,0x94,0xF4,0x04,0x00,
0x01,0x00,0xFF,0x00,0x42,0x3A,0x02,0x3A,0x42,0x4A,0x52,0x42,0x62,0x0A,0x32,0x00/*"德",1*/},

{0x40,0x30,0x2F,0xE4,0x24,0x24,0x00,0xF4,0x54,0x5C,0x57,0x54,0xF4,0x04,0x00,0x00,
0x01,0x01,0x01,0xFF,0x41,0x31,0x90,0x5F,0x35,0x15,0x15,0x35,0x5F,0x90,0x10,0x00/*"镇",2*/},

{0x00,0xFE,0x22,0x52,0x8E,0x10,0xCC,0xB7,0xA4,0xE4,0xA4,0xA4,0x84,0xFC,0x00,0x00,
0x00,0xFF,0x02,0x04,0x03,0x00,0x00,0x0E,0x08,0x0F,0x08,0x4E,0x80,0x7F,0x00,0x00/*"陶",3*/},

{0x00,0x21,0x22,0xF6,0x08,0x14,0x88,0x47,0x22,0x1E,0x22,0x22,0x42,0xC6,0x40,0x00,
0x01,0x01,0x01,0xE1,0x9F,0x45,0x4D,0x35,0x05,0x05,0x7D,0x81,0x81,0x81,0xE1,0x00/*"瓷",4*/},

{0x40,0x30,0x10,0x12,0x5C,0x54,0x50,0x51,0x5E,0xD4,0x50,0x18,0x57,0x32,0x10,0x00,
0x00,0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x00/*"学",5*/},

{0xFE,0x02,0x32,0x4A,0x86,0x0C,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0x0C,0x04,0x00,
0xFF,0x00,0x02,0x04,0x83,0x41,0x31,0x0F,0x01,0x01,0x7F,0x81,0x81,0x81,0xF1,0x00/*"院",6*/},

{0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0xFF,0x48,0x48,0x48,0x48,0xF8,0x00,0x00,0x00,
0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x3F,0x44,0x44,0x44,0x44,0x4F,0x40,0x70,0x00/*"电",9*/},

{0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xE2,0x12,0x0A,0x06,0x02,0x00,0x80,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x41,0x81,0x7F,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00/*"子",10*/},

 {0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xC0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00/*"一",11*/},

 {0x42,0x42,0xFE,0x42,0x42,0xE0,0x00,0xFF,0x00,0x42,0x42,0xFE,0x42,0x42,0x42,0x00,
0x10,0x10,0x0F,0x88,0x49,0x20,0x18,0x07,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x00/*"班",12*/},

{0x02,0xF2,0x12,0x12,0x1E,0x00,0x80,0x80,0xFF,0xA0,0x90,0x88,0x86,0x84,0x80,0x00,
0x01,0x03,0x41,0x81,0x7F,0x00,0x00,0x40,0xFF,0x40,0x23,0x0C,0x10,0x60,0x20,0x00/*"张",13*/},

{0x02,0xFE,0x92,0x92,0xFE,0x02,0x00,0xF2,0x9C,0x94,0x90,0x98,0x96,0xF4,0x00,0x00,
0x10,0x1F,0x08,0x08,0xFF,0x04,0x38,0x00,0x3C,0x40,0x42,0x44,0x70,0x04,0x18,0x00/*"聪",14*/},

{0x08,0x48,0x8A,0x0C,0xE8,0x18,0xD4,0x14,0xF4,0x14,0x10,0xFF,0x12,0x14,0x10,0x00,
0x20,0x10,0x09,0x06,0x19,0x20,0x3F,0x20,0x1F,0x11,0x11,0x07,0x18,0x20,0x78,0x00/*"斌",15*/},

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}//空格
 };  
void CheckState(void) //判断忙标志
{
     uchar dat = 0x00;
   
   /* if(lr==LEFT)
    {
        CLEAR_LCD_CS2; //cs2=0
        SET_LCD_CS1;   //cs1=1
    }
    else
    {
        SET_LCD_CS2;   //cs2=1
        CLEAR_LCD_CS1; //cs1=0
    }	*/
	
	DDRB=0XFF;
	CLR_LCD_DI ;          //其中di要为低电平
	SET_LCD_RW;           //rw要为高电平 
	SET_LCD_E;					
    asm("nop");	
    asm("nop");
    asm("nop");
    asm("nop");
	do
	{
		LCD_DIR_DDR=0X00;      //置位输入,读取当前的状态值
		SET_LCD_E;
		dat=LCD_IP_PIN ;      //读取当前的引脚逻辑电平值
		CLR_LCD_E;
		dat&=0x80;            //仅当第7位为0时才可操作
	}while(dat);
}

void WriteByte(uchar dat)
{
   CheckState();
   DDRB=0XFF;
   SET_LCD_DI;
   CLR_LCD_RW;
   LCD_DIR_DDR=0X00;
   LCD_DATA_PORT=dat;
   SET_LCD_E;
   CLR_LCD_E;
   }
   SendCommand(uchar command)
   {
    CheckState();
	DDRB=0XFF;
	CLR_LCD_RW;
	CLR_LCD_DI;
     LCD_DIR_DDR=0x00;
	LCD_DATA_PORT=command;
	SET_LCD_E;
	CLR_LCD_E;
	}
	void SetLine(uchar line)
	{
	 line&=0x07;
	 line|=0xb8;
	 SendCommand(line);
	 }
	 void SetColumn(uchar column)
	 {
	  column&=0x3f;
	  column|=0x40;
	  SendCommand(column);
	  }
	  void SetStartline(uchar startline)
	  {
	   startline|=0xc0;
	   SendCommand(startline);
	   }
	    void SetOnOff(uchar onoff)
		{
		 onoff|=0x3e;
		 SendCommand(onoff);
		 }
		 void SelectScreen(uchar screen)
		 {
		  switch(screen)
		   {
		    case 0:
			 CLR_LCD_CS1;
			 asm("nop");
			 CLR_LCD_CS2;
			 asm("nop");
			 break;
			 
			 case 1:
			 CLR_LCD_CS2;
			 asm("nop");
			 SET_LCD_CS1;
			 asm("nop");
			 break;
			 
			 case 2:
			 CLR_LCD_CS1;
			 asm("nop");
			 SET_LCD_CS2;
			 asm("nop");
			 break;
			 }
			}
			void ClearScreen(uchar screen)
			{
			 uchar i,j;
			 SelectScreen(screen);
			 for(i=0;i<8;i++)
			 {
			  SetLine(i);
			  for(j=0;j<64;j++)
			  {
			   WriteByte(0x00);
			   }
			  }
			 }
			 void Show8x8(uchar line ,uchar column,uchar *address)
			 {
			  uchar i;
			  if(column<64)
			   {
			    SelectScreen(2);
				SetLine(line);
				SetColumn(column);
				for(i=0;i<8;i++)     //8*16      
				{
				 if(column+i<64)   
				  {
				   WriteByte(*(address+i));   //每写完一行,行数自动加一 
				   }
				   else 
				   {
				    SelectScreen(2);
					SetLine(line);
					SetColumn(column-64+i);
					WriteByte(*(address+i));
					}
				 }
				 }	   
			  else 
			  {
			   SelectScreen(1);
			   column-=64;
			   SetLine(line);
			   
			   SetColumn(column);
			   for(i=0;i<8;i++)
			   {
			    if(column+i<64)
				{
				WriteByte(*(address+i));
				}
				else
				{
				 SelectScreen(1);
				 SetLine(line);
				 SetColumn(column-64+i);
				 WriteByte(*(address+i));
				 }
				}
			   }
			  }
		void ShowNumber(uchar line ,uchar column,uchar num)
		{
		 uchar *address;
		 address=&Numcode[num][0];
		 Show8x8(line,column,address);
		 Show8x8(line+1,column,address+8);
		 }
		void ShowChina(uchar line,uchar column,uchar num)
		{
		 uchar *address;
		 address=&HZcode[num][0];
		 Show8x8(line,column,address);
		 Show8x8(line,column+8,address+8);
		 Show8x8(line+1,column,address+16);
		 Show8x8(line+1,column+8,address+24);	
		}
		void InitLcd(void)
		{
		 uchar i=2000;
		 while(i--);
		 SetOnOff(1);
		 ClearScreen(1);
		 ClearScreen(2);
		 SetStartline(0);
		 }
		 	
	void main(void)
	{
	uchar line;
	uchar column,i,j;
	 DDRB = 0xff; 
     DDRA=0XFF;
	 InitLcd();
	 
	 ShowChina(0,0,0);     //景
	 ShowChina(0,16,1);    //德
	 ShowChina(0,32,2);    //镇
	 ShowChina(0,48,3);    //陶
	 ShowChina(0,64,4);    //瓷
	 ShowChina(0,80,5);    // 学
	 ShowChina(0,96,6);    //院
	 ShowChina(0,112,14);  // 
	
	 ShowNumber(2,0,0);    //0
	 ShowNumber(2,8,3);    //3
	 ShowChina(2,16,7);    //电
	 ShowChina(2,32,8);    //子
	 ShowChina(2,48,9);    //一
	 ShowChina(2,64,10);   //班
	 ShowChina(2,80,14);   
	 ShowChina(2,96,14);   
	 ShowChina(2,112,14);  
	 
	 ShowChina(4,0,11);    //张
	 ShowChina(4,16,12);   //聪
	 ShowChina(4,32,13);   //斌
	 ShowChina(4,48,14);   
	 ShowChina(4,64,14);
	 ShowChina(4,80,14);
	 ShowChina(4,96,14);
	 ShowChina(4,112,14);
	 
	 ShowChina(6,0,14);
	 ShowChina(6,16,14);
	 ShowChina(6,32,14);
	 ShowChina(6,48,14);
	 ShowChina(6,64,14);
	 ShowChina(6,80,14);
	 ShowChina(6,96,14);
	 ShowChina(6,112,14);
	   	  
   	  
	 }

⌨️ 快捷键说明

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