📄 glcd.c
字号:
#include <At89x52.h>
#include <Common.h>
#include <FONT.h>
#include <GLCD.H>
/*全局坐标*/
U8 data u8CursorX=0, u8CursorY=0;
/*-------------------------------------------------------------------------------
廷时程序
-------------------------------------------------------------------------------*/
void LcdDelay(long u32Duration){
long u32Delay;
for (u32Delay=0; u32Delay<u32Duration; u32Delay++);
}
/*-------------------------------------------------------------------------------
检测是否忙?
-------------------------------------------------------------------------------*/
void LcdWaitBusy (){
U8 TEMP;
do{
LCD_DI = 0;
LCD_RW = 1;
LCD_DATA = 0xFF;/* p0口输入 */
LCD_E = 1;
TEMP=LCD_DATA;
LCD_E=0;
} /* 读状态 */
while ((TEMP & 0x80)== BUSY); /* mask the other status bits and test the BUSY bit */
}
/*-------------------------------------------------------------------------------
传送数据
-------------------------------------------------------------------------------*/
void LcdDataWrite (U8 u8Data){
LcdWaitBusy ();
LCD_DI = 1; /* 数据模式 */
LCD_RW = 0; /* 写数据 */
LCD_DATA = u8Data; /* 输出 */
LCD_E = 1;
LcdDelay(1);
LCD_E = 0;
}
/*-------------------------------------------------------------------------------
传送指令
-------------------------------------------------------------------------------*/
void LcdInstructionWrite (U8 u8Instruction){
LcdWaitBusy ();
LCD_DI = 0; /* 指令模式 */
LCD_RW = 0; /* 写指令 */
LCD_DATA = u8Instruction;/* 输出 */
LCD_E = 1;
LcdDelay(1);
LCD_E = 0;
}
/*-------------------------------------------------------------------------------
读数据
-------------------------------------------------------------------------------*/
U8 LcdDataRead (){
LCD_DATA = 0xFF; /* p0口输入 */
LCD_E = 0;
LcdDelay(1);
LCD_E = 1;
LCD_RW = 1; /* 读模式 */
LCD_DI = 1; /* 数据模式 */
LcdDelay(1);
return LCD_DATA; /*返回数据*/
}
/*-------------------------------------------------------------------------------
选择屏幕
LEFT or RIGHT
-------------------------------------------------------------------------------*/
void LcdSelectSide(U8 u8LcdSide){
if(u8LcdSide == RIGHT) {/* 转到右面 */
LCD_E=0;
LCD_DI=0;
LCD_RW=0;
LCD_CS1=1;
LCD_CS2=0;
LcdInstructionWrite(Y_ADDRESS); /* 从0列开始 */
}
else{ /*转到左面*/
LCD_E=0;
LCD_DI=0;
LCD_RW=0;
LCD_CS1=0;
LCD_CS2=1;
LcdInstructionWrite(Y_ADDRESS); /*从0列开始*/
}
}
/*-------------------------------------------------------------------------------
清屏
-------------------------------------------------------------------------------*/
void LcdClearScreen (void){
U8 data u8Page=0;
U8 data u8Column=0;
/* process the 8 pages of the LCD */
for (u8Page = 0; u8Page < 8; u8Page++){
LcdSelectSide(LEFT); /* 选择左面 */
LcdInstructionWrite(X_ADDRESS | u8Page); /* 选择页数 */
LcdInstructionWrite(Y_ADDRESS); /* 从0列开始 */
/* process a page on both sides */
for (u8Column = 0; u8Column <128; u8Column++){
if (u8Column == 64){
LcdSelectSide(RIGHT); /* Select right side */
LcdInstructionWrite(X_ADDRESS | u8Page); /* Set the page number */
LcdInstructionWrite(Y_ADDRESS); /* Set column to 0 */
}
LcdDataWrite (0x00); /* erase a column */
}
}
}
/*-------------------------------------------------------------------------------
LCD 初始化
-------------------------------------------------------------------------------*/
void LcdInit(void){
LCD_DATA = 0;
LCD_DI = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_CS1 = 0;
LCD_CS2 = 0;
LCD_RST = 1;
LcdDelay(20);
LCD_RST=0 ;
LcdDelay(20);
LCD_RST=1;
LcdSelectSide(LEFT);
LcdInstructionWrite(DISPLAY_OFF); /* Display OFF */
LcdInstructionWrite(START_LINE);
LcdInstructionWrite(X_ADDRESS);
LcdInstructionWrite(Y_ADDRESS);
LcdInstructionWrite(DISPLAY_ON); /* Display ON */
LcdSelectSide(RIGHT);
LcdInstructionWrite(DISPLAY_OFF); /* Display OFF */
LcdInstructionWrite(START_LINE);
LcdInstructionWrite(X_ADDRESS);
LcdInstructionWrite(Y_ADDRESS);
LcdInstructionWrite(DISPLAY_ON); /* Display ON */
LcdClearScreen();
}
//画点
void LcdSetDot (U8 u8Xaxis, U8 u8Yaxis)
{
U8 data u8DataRead=0;
LcdInstructionWrite(START_LINE); /* Set adress for line 0 */
/* Left side */
if (u8Xaxis < 64)
{
LcdSelectSide (LEFT); /* Select left side */
LcdInstructionWrite (X_ADDRESS + (u8Yaxis / 8)); /* Select page number */
LcdInstructionWrite (Y_ADDRESS + u8Xaxis); /* Select column */
u8DataRead = LcdDataRead (); /* read the current location */
u8DataRead = LcdDataRead (); /* on the LCD. dummy read */
LcdInstructionWrite (X_ADDRESS + (u8Yaxis / 8)); /* Select page number */
LcdInstructionWrite (Y_ADDRESS + u8Xaxis); /* Select column */
LcdDataWrite (u8DataRead | (1 << (u8Yaxis % 8)));/* plot the dot */
}
else
/* Right side */
{
LcdSelectSide(RIGHT); /* Select left side */
LcdInstructionWrite (X_ADDRESS + (u8Yaxis / 8)); /* Select page number */
LcdInstructionWrite (Y_ADDRESS + u8Xaxis - 64); /* Select column */
u8DataRead = LcdDataRead (); /* read the current location */
u8DataRead = LcdDataRead (); /* on the LCD. dummy read */
LcdInstructionWrite (X_ADDRESS + (u8Yaxis / 8)); /* Select page number */
LcdInstructionWrite (Y_ADDRESS + u8Xaxis - 64); /* Select column */
LcdDataWrite (u8DataRead | (1 << (u8Yaxis % 8)));/* plot the dot */
}
LcdInstructionWrite(START_LINE); /* Set adress for line 0 */
}
//对一页的字反显
void LcdReverse(U8 u8Page){
U8 data u8DataRead=0,u8Column,u8temp;
LcdInstructionWrite(START_LINE);
LcdSelectSide(LEFT);
LcdInstructionWrite(X_ADDRESS | u8Page);
for (u8Column = 0; u8Column <128; u8Column++){
if (u8Column == 64){
LcdSelectSide(RIGHT); /* Select right side */
LcdInstructionWrite(X_ADDRESS | u8Page); /* Set the page number */
}
LcdInstructionWrite (Y_ADDRESS | u8Column);
u8DataRead = LcdDataRead (); /* read the current location */
u8DataRead = LcdDataRead (); /* on the LCD. dummy read */
LcdInstructionWrite (Y_ADDRESS | u8Column);
u8temp=u8DataRead;
LcdDataWrite(~u8temp);
}
}
/*-------------------------------------------------------------------------------
定位
-------------------------------------------------------------------------------*/
void LcdLocate (U8 u8Column, U8 u8Line){
u8CursorX = u8Column;
u8CursorY = u8Line;
}
/*-------------------------------------------------------------------------------
显示一个字符
-------------------------------------------------------------------------------*/
void LcdPutChar (U8 u8Char, FONT_DEF *toto){
U8 data u8CharColumn=0,u8RightSide=0;
U8 data u8Page=0 ,u8UpperCharPointer=1;
LcdInstructionWrite(START_LINE);
/* test for carrier return */
if (u8CursorX > 128 - (toto->u8Width) ) {
u8CursorX = 0;
u8CursorY++;
if (u8CursorY == 8)
u8CursorY = 0;
}
/* Select the side of the LCD */
if (u8CursorX < 64) {
/* Select left side */
LcdSelectSide (LEFT);
LcdInstructionWrite (X_ADDRESS | u8CursorY);
LcdInstructionWrite (Y_ADDRESS | u8CursorX);
}
else{
/* Select right side */
LcdSelectSide (RIGHT);
LcdInstructionWrite (X_ADDRESS | u8CursorY);
LcdInstructionWrite (Y_ADDRESS | u8CursorX);
}
/* Draw a char */
while (u8CharColumn < (toto->u8Width) ) {
if ((toto->u8Height) > 8){
u8UpperCharPointer = 2;
//if (u8CursorX >64)
// u8RightSide=64;
LcdInstructionWrite (X_ADDRESS | u8CursorY);
LcdInstructionWrite (Y_ADDRESS | u8CursorX);
LcdDataWrite ((toto->au8FontTable)[( (u8Char) * u8UpperCharPointer * (toto->u8Width) ) + (u8CharColumn+(toto->u8Width))]);
}
LcdInstructionWrite (X_ADDRESS |( u8CursorY-1));
LcdInstructionWrite (Y_ADDRESS | u8CursorX);
LcdDataWrite ((toto->au8FontTable)[( (u8Char) * u8UpperCharPointer * (toto->u8Width) ) + u8CharColumn]);
u8CharColumn++;
u8CursorX++;
/* test if a char is wrote on both sides of LCD */
if (u8CursorX == 64){
u8RightSide=64;
LcdSelectSide (RIGHT); /* if yes, select right side */
//LcdInstructionWrite (Y_ADDRESS + u8CursorX -u8RightSide);
}
}
/* Insert a space after a char */
if (u8CursorX < 128) {/* Check if this is the last char of the line */
if ((toto->u8Height) > 8){
LcdInstructionWrite (X_ADDRESS | (u8CursorY - 1)); /* Select the page of the LCD */
LcdInstructionWrite (Y_ADDRESS | u8CursorX);
LcdDataWrite(0x00);
}
LcdInstructionWrite (X_ADDRESS | u8CursorY); /* Select the page of the LCD */
LcdInstructionWrite (Y_ADDRESS | u8CursorX );
LcdDataWrite(0x00); /* if not then insert a space before next letter */
}
u8CharColumn++;
u8CursorX++;
}
void LcdDisplayPicture (U8 *au8PictureData){
U8 data u8Page=0,u8Column=0;
/* Send the left side */
LcdSelectSide(LEFT);
for (u8Page = 0; u8Page < 8; u8Page++) /* loop on the 8 pages */
{
LcdInstructionWrite(X_ADDRESS | u8Page); /* Set the page */
for (u8Column = 0; u8Column < 64; u8Column++)
LcdDataWrite(au8PictureData[(128*u8Page)+u8Column]);
}
/* Send the right side */
LcdSelectSide(RIGHT);
for (u8Page = 0; u8Page < 8; u8Page++) /* loop on the 8 pages */
{
LcdInstructionWrite(X_ADDRESS | u8Page); /* Set the page */
for (u8Column = 64; u8Column <127; u8Column++)
LcdDataWrite(au8PictureData[(128*u8Page)+u8Column]);
}
}
void LcdCircle (U8 u8CenterX, U8 u8CenterY, U8 u8Radius)
{
U8 data s16tswitch=0, s16y=0, s16x=0,u8d;
u8d = u8CenterY - u8CenterX;
s16y = u8Radius;
s16tswitch = 3 - 2 * u8Radius;
while (s16x <= s16y)
{
LcdSetDot(u8CenterX + s16x, u8CenterY + s16y);
LcdSetDot(u8CenterX + s16x, u8CenterY - s16y);
LcdSetDot(u8CenterX - s16x, u8CenterY + s16y);
LcdSetDot(u8CenterX - s16x, u8CenterY - s16y);
LcdSetDot(u8CenterY + s16y - u8d, u8CenterY + s16x);
LcdSetDot(u8CenterY + s16y - u8d, u8CenterY - s16x);
LcdSetDot(u8CenterY - s16y - u8d, u8CenterY + s16x);
LcdSetDot(u8CenterY - s16y - u8d, u8CenterY - s16x);
if (s16tswitch < 0)
s16tswitch += (4 * s16x + 6);
else
{
s16tswitch += (4 * (s16x - s16y) + 10);
s16y--;
}
s16x++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -