#include "LCD12864.h"
#include "STC12C5A.h"
#include "intrins.h"
/**************************************************************
iO口宏定义区
***************************************************************/
sbit LCD12864_CS = P3^5;//RS
sbit LCD12864_SID = P3^6;//r/w
sbit LCD12864_SCK = P3^7;//e
/*******************************************************************
常量声明区
********************************************************************/
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,
};
/****************************************************************
发送一个字节
*****************************************************************/
void LCD12864_SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
LCD12864_SCK = 0;
Dbyte=Dbyte<<1;
LCD12864_SID = CY;
LCD12864_SCK = 1;
LCD12864_SCK = 0;
}
}
/**********************************************************
接收一个字节
***********************************************************/
unsigned char LCD12864_ReceiveByte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
LCD12864_SCK = 0;
LCD12864_SCK = 1;
LCD12864_SCK = 0;
if(LCD12864_SID) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
LCD12864_SCK = 0;
LCD12864_SCK = 1;
LCD12864_SCK = 0;
if(LCD12864_SID) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
/****************************************************************
检查忙状态
******************************************************************/
void LCD12864_CheckBusy( void )
{
do LCD12864_SendByte(0xfc); //11111,RW(1),RS(0),0
while(0x80&LCD12864_ReceiveByte());
}
/******************************************************************
写一个字节的指令
*******************************************************************/
void LCD12864_WriteCmd( unsigned char Cbyte )
{
LCD12864_CS = 1;
LCD12864_CheckBusy();
LCD12864_SendByte(0xf8); //11111,RW(0),RS(0),0
LCD12864_SendByte(0xf0&Cbyte);
LCD12864_SendByte(0xf0&Cbyte<<4);
LCD12864_CS = 0;
}
/*************************************************************
写一个字节的数据
**************************************************************/
void LCD12864_WriteData( unsigned char Dbyte )
{
LCD12864_CS = 1;
LCD12864_CheckBusy();
LCD12864_SendByte(0xfa); //11111,RW(0),RS(1),0
LCD12864_SendByte(0xf0&Dbyte);
LCD12864_SendByte(0xf0&Dbyte<<4);
LCD12864_CS = 0;
}
/******************************************************************
lcd初始化函数
*******************************************************************/
void LCD12864_Init( void )
{
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(0x03);
LCD12864_WriteCmd(0x0c);
LCD12864_WriteCmd(0x01);
LCD12864_WriteCmd(0x06);
_nop_();_nop_();_nop_();
LCD12864_ClearTXT();
LCD12864_ClearBMP();
}
/******************************************************************************
设定光标函数
******************************************************************************/
void LCD12864_SetCursor(unsigned char x,unsigned char y)
{
switch(x)
{
case 0:
x=0x80;break;
case 1:
x=0x90;break;
case 2:
x=0x88;break;
case 3:
x=0x98;break;
default:
x=0x80;
}
y=y&0x07;
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(y+x);
LCD12864_WriteCmd(y+x);
}
/***********************************************************************************
清除文本
************************************************************************************/
void LCD12864_ClearTXT( void )
{
unsigned char i;
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(0x80);
for(i=0;i<64;i++)
LCD12864_WriteData(0x20);
LCD12864_SetCursor(0,0);
}
/**************************************************************************************
清除图片
*****************************************************************************************/
void LCD12864_ClearBMP( void )
{
unsigned char i,j;
LCD12864_WriteCmd(0x34);
LCD12864_WriteCmd(0x36);
for(i=0;i<32;i++)
{
LCD12864_WriteCmd(0x80|i);
LCD12864_WriteCmd(0x80);
for(j=0;j<32;j++)
LCD12864_WriteData(0);
}
}
/****************************************************************************************
显示字符串
*****************************************************************************************/
void LCD12864_PutStr(unsigned char row,unsigned char col,unsigned char const *puts)
{
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(AC_TABLE[8*row+col]);
while(*puts != '\0')
{
if(col==8)
{
col=0;
row++;
}
if(row==4) row=0;
LCD12864_WriteCmd(AC_TABLE[8*row+col]);
LCD12864_WriteData(*puts);
puts++;
if(*puts != '\0')
{
LCD12864_WriteData(*puts);
puts++;
col++;
}
}
}
/****************************************************************************************
显示数字
*****************************************************************************************/
void LCD12864_PutNum(unsigned char row,unsigned char col,unsigned int num)
{
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(AC_TABLE[8*row+col]);
if(num<100) //两位数
{
LCD12864_WriteData(num/10+48);
LCD12864_WriteData(num%10+48);
}else
{
LCD12864_WriteData((num/1000)+48);
LCD12864_WriteData(((num%1000)/100)+48);
LCD12864_WriteData(((num%100)/10)+48);
LCD12864_WriteData((num%10)+48);
}
}
/**************************************************************************
显示全屏图片
***************************************************************************/
void LCD12864_PutBMP(unsigned char const *puts)
{
unsigned int x=0,y=0;
unsigned char i,j;
LCD12864_WriteCmd(0x34);
LCD12864_WriteCmd(0x36);
for(i=0;i<32;i++)
{
LCD12864_WriteCmd(0x80|i);
LCD12864_WriteCmd(0x80);
for(j=0;j<16;j++)
{
LCD12864_WriteData(puts[x]);
x++;
}
y=x-16;
for(j=0;j<16;j++)
{
LCD12864_WriteData(puts[y+512]);
y++;
}
}
}
void PutXiao(unsigned char row,unsigned char col,unsigned long i)
{
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(AC_TABLE[8*row+2*col]);
if(i<1000)
{
LCD12864_WriteData((((unsigned long)i%1000)/100)+48);
LCD12864_WriteData((((unsigned long)i%100)/10)+48);
LCD12864_WriteData(((unsigned long)i%10)+48);
LCD12864_WriteData(72);
LCD12864_WriteData(122);
}
else if(i>=1000000)
{
LCD12864_WriteData(((unsigned long)i/100000000)+48);
LCD12864_WriteData(((unsigned long)i%100000000)/10000000+48);
LCD12864_WriteData(((unsigned long)i%10000000)/1000000+48);
LCD12864_WriteData(0x2e);
LCD12864_WriteData(((unsigned long)(i*100)%100000000)/10000000+48);
LCD12864_WriteData(((unsigned long)(i*100)%10000000)/1000000+48);
LCD12864_WriteData(((unsigned long)(i*100)%1000000)/100000+48);
LCD12864_WriteData(77);
LCD12864_WriteData(72);
LCD12864_WriteData(122);
}
else
{
LCD12864_WriteData(((unsigned long)i/100000)+48);
LCD12864_WriteData(((unsigned long)i%100000)/10000+48);
LCD12864_WriteData(((unsigned long)i%10000)/1000+48);
LCD12864_WriteData(0x2e);
LCD12864_WriteData(((unsigned long)i%1000)/100+48);
LCD12864_WriteData(((unsigned long)i%100)/10+48);
LCD12864_WriteData((unsigned long)i%10+48);
LCD12864_WriteData(75);