The drive of C51 single-chip microcomputer to liquid crystal

The 192*64 LCD driver program written by Keil C51, thanks to many netizens for providing the reference source program,

This program is rewritten on the basis of the programs provided by netizens, and there are many places that have won the patience of experts

Thank you very much for your guidance. This is a complete C51 source program, I think it is very suitable for a beginner learning C51

It is easier for people to get started by providing a complete simple program.

-------------------------------------------------- ------------------------------------------------*/

/*The LCD sub-process LCD screen for writing Chinese characters is divided into 4 rows*12 columns of Chinese characters, all of which use the analog interface method.

/* TGLCMLIMIT64A interface program (simulation mode)

LCD module suppliers:

Email:

;************************************************ ****************************

;Connection diagram:

;*LCM---89C52* *LCM---89C52* *LCM-------89C52* *LCM----------89C52* *

;*DB0---P0.0* *DB4---P0.4* *D/I-------P2.6* *CS1----------P2.4* *

;*DB1---P0.1* *DB5---P0.5* *R/W-------P2.7* *CS2----------P2.5* *

;*DB2---P0.2* *DB6---P0.6* *RST--------VCC* *CS3----------P3.2* *

;*DB3---P0.3* *DB7---P0.7* *E---------P2.3* *

;Note: The crystal frequency of 89C52 is 12MHz*

;************************************************ ****************************/

//Please refer to the c program of avr for the line drawing part.

/*#pragma src /*Generate ASM file switch, open if necessary*/

#include

#include

#include

#define Uchar unsigned char

/***********LCD interface pin definition ***************/

sbit Elcm= P2^3; //

sbit CS1LCM= P2^4; //

sbit CS2LCM= P2^5; //

sbit CS3LCM= P3^2; /*This connection is only a temporary connection for experimentation. */

sbit Dilcm= P2^6; //

sbit Rwlcm= P2^7; //

sfr Datalcm= 0x80; //Data port

/***********Common operation commands and parameter definitions****************/

#define DISPON 0x3f /*display on */

#define DISPOFF 0x3e /*Display off */

#define DISPFIRST 0xc0 /*Display start line definition*/

#define SETX 0x40 /*X positioning setting command (page) */

#define SETY 0xb8 /*Y positioning setting command (column) */

#define Lcdbusy 0x80 /*LCM busy judgment bit*/

/****************Show partition boundary position****************/

#define MODL 0x00 /*Left area*/

#define MODM 0x40 /*Left area and middle area boundary*/

#define MODR 0x80 /*Boundary between the middle area and the right area*/

#define LCMLIMIT 0xC0 /*The right border of the display area*/

/****************Global variable definitions********************/

Uchar col, row, cbyte; /* column x, row (page) y, output data */

bit xy; /*Line drawing direction flag: 1 horizontal*/

/********************Function List**********************/

void Lcminit(void); /*LCD module initialization*/

void Delay(Uchar); /*Delay, the number of entries is Ms */

void lcdbusyL(void); /*busy judgment, wait (left area) */

void lcdbusyM(void); /*busy judgment, waiting (middle area) */

void lcdbusyR(void); /*busy judgment, wait (right area) */

void Putedot(Uchar); /* Half-width character output*/

void Putcdot(Uchar); /*Full-width (Chinese character) output*/

void Wrdata(Uchar); /*Data output to LCM */

void Lcmcls( void ); /*LCM full screen clear (fill with 0) */

void wtcom(void); /* public busy waiting */

void Locatexy(void); /*Cursor positioning*/

void WrcmdL(Uchar); /*Left area command output*/

void WrcmdM(Uchar); /*Central area command output*/

void WrcmdR(Uchar); /*Right area command output*/

void Putstr(Uchar *puts, Uchar i); /*Chinese and English string output*/

void Rollscreen(Uchar x); /* Screen scroll up demo */

void Rddata(void); /* Read data from LCD*/

void Linehv(Uchar length); /*Draw a line in the horizontal (vertical) direction*/

void point(void); /* dot */

void Linexy(Uchar endx, Uchar endy);

/********************Array List************************/

Uchar code Ezk[]; /*ASCII regular character bitmap*/

Uchar code Hzk[]; /*Dot matrix code table of Chinese characters for your own use*/

Uchar code STR1[]; /*custom string*/

Uchar code STR2[]; //

Uchar code STR3[]; //

Uchar code STR4[]; //

/********************************/

/* Demonstration main program */

/********************************/

void main(void)

{

Uchar x;

col=0;

row=0;

Delay(40); /* Delay about 40Ms, waiting for the peripheral to be ready */

Lcminit(); /*LCD module initialization, including full screen clearing*/

Putstr(STR2, 24); /*The first line of character output, 24 bytes*/

col=0;

row=2;

Putstr(STR1, 12); /* The second line of character output, 12 bytes */

col=0;

row=4;

Putstr(STR3, 24); /*The third line of character output, 24 bytes*/

col=0;

row=6;

Putstr(STR4, 24); /* The fourth line of character output, 12 bytes */

x=0;

col=0;

row=0;

xy = 1; /*Direction flag. Set to horizontal */

Linehv(192); /* draw a horizontal line (0, 0) - (191, 0) */

col=0;

row=15;

xy = 1;

Linehv(192); /* Draw a horizontal line (0, 15) - (191, 15) */

col=0;

row=32;

xy = 1;

Linehv(192); /* draw a horizontal line (0, 32) - (191, 32) */

col=0;

row=1;

xy = 0; /*Direction flag. Set as vertical */

Linehv(31); /* draw a vertical line (0, 1) - (0, 31) */

col=191;

row=1;

xy = 0;

Linehv(31); /* draw a vertical line (191, 1) - (191, 31) */

col=0; /*Set the coordinates of the starting point of the slash*/

row=63;

Linexy(44, 31); /* Draw a diagonal line (0, 63)-(44, 31) */

col=44;

row=31;

Linexy(190, 62); /* Continue to draw slashes (44, 31)-(191, 63) */

while(1){

Rollscreen(x); /*Locate the new display start line*/

x++;

Delay(100); /* Delay, control scroll speed */

};

}

/****************************************************/

/* Draw lines. Slashes in any direction, vertical or horizontal lines are not supported */

/****************************************************/

void Linexy(Uchar endx, Uchar endy)

{

register Uchar t;

int xerr=0, yerr=0, delta_x, delta_y, distance;

Uchar incx, incy;

/* compute the distance in both direcTIons */

delta_x=endx-col;

delta_y=endy-row;

/* compute the direcTIon of the increment,

an increment of “0” means either a vertical or horizontal lines */

if(delta_x>0) incx=1;

else if ( delta_x==0 ) incx=0;

else incx=-1;

if(delta_y>0) incy=1;

else if ( delta_y==0 ) incy=0;

else incy=-1;

/* determine which distance is greater */

delta_x = cabs( delta_x );

delta_y = cabs( delta_y );

if ( delta_x > delta_y ) distance=delta_x;

else distance=delta_y;

/* draw the line */

for( t=0;t <= distance+1; t++ ) {

point();

xerr += delta_x ;

yerr += delta_y ;

if( xerr > distance ) {

xerr-=distance;

col+=incx;

}

if(yerr > distance) {

yerr-=distance;

row+=incy;

}

}

}

/****************************************/

/* Draw lines. Only provide X or Y direction, does not support slash */

/****************************************/

void Linehv(Uchar length)

{

Uchar xs, ys;

if (xy) { ys = col;

for (xs=0; xscol = ys + xs;

point();}

}

else {xs = row;

for (ys=0;ysrow = xs + ys;

point();}

}

}

/****************************************/

/* draw points */

/****************************************/

void point(void)

{

Uchar x1, y1, x, y;

x1=col;

y1=row;

row=y1》》3; /* Take the Y direction paging address*/

Rddata();

y=y1&0x07; /*Calculation of position in bytes*/

x=0x01;

x=x<Wrdata(cbyte|x); /*Draw on the screen*/

col=x1; /*Restore xy coordinates*/

row=y1;

}

/****************************************/

/* screen scroll positioning */

/****************************************/

void Rollscreen(Uchar x)

{

cbyte = DISPFIRST|x; /*Define display start behavior x? */

WrcmdL(cbyte);

WrcmdM(cbyte);

WrcmdR(cbyte);

}

/****************************************/

/* output of a string */

/****************************************/

void Putstr(Uchar *puts, Uchar i)

{

Uchar j, X;

for (j=0;j{

X = puts[j];

if (X&0x80)

{

Putcdot(X&0x7f); /*Reserve only the lower 7 bits*/

}

else Putedot(X-0x20); /*ascii code table starts from 0x20*/

}

}

/****************************************/

/* Half-width character dot matrix data output */

/****************************************/

void Puteddot(Uchar Order)

{

Uchar i, bakerx, bakery; /*A total of 4 local variables are defined*/

int x; /*Offset, the less character can be defined as UCHAR */

bakerx = col; /* Temporarily store the x, y coordinates, and prepare the next half character for use*/

bakery = row;

x=Order * 0x10; /*Half-width characters, 16 bytes per character*/

/* Upper half character output, 8 columns */

for(i=0;i<8;i++)

{

cbyte = Ezk[x]; /*Get the lattice code, rom array*/

Wrdata(cbyte); /*Write output one byte*/

x++;

col++;

if (col==LCMLIMIT){col=0;row++;row++;}; /*The next column, if the column is out of bounds, wrap the line */

if (row>7) row=0; /*If the row is out of bounds, return to the first row*/

} /* The end of the output of the first half of the characters */

col = bakerx; /*column alignment*/

row = bakery+1; /*point to the lower half character row*/

/* The lower half character output, 8 columns */

for(i=0;i<8;i++)

{

cbyte = Ezk[x]; /*Get the lattice code*/

Wrdata(cbyte); /*Write output one byte*/

x++;

col++;

if (col==LCMLIMIT){col=0;row=row+2;}; /*The next column, if the column is out of bounds, wrap the line*/

if (row>7) row=1; /*If the row is out of bounds, return the first row*/

} /*End of the next half character output*/

row=bakery;

} /*End of whole character output*/

/****************************************/

/* Full-width character bitmap data output*/

/****************************************/

void Putcdot(Uchar Order)

{

Uchar i, bakerx, bakery; /*A total of 3 local variables are defined*/

int x; /*Offset, the less character can be defined as UCHAR */

bakerx = col; /* Temporarily store the x, y coordinates, and prepare the next half character for use*/

bakery = row;

x=Order * 0x20; /*32 bytes per character*/

/* Upper half character output, 16 columns */

for(i=0;i<16;i++)

{

Wrdata(Hzk[x]); /*Write output one byte*/

x++;

col++;

if (col==LCMLIMIT){ col=0;row++;row++;} /* next column, wrap if the column is out of bounds */

if (row>6) row=0; /*If the row is out of bounds, return the first row*/

} /* The end of the output of the first half of the characters */

/* The second half character output, 16 columns */

col = bakerx;

row = bakery+1;

for(i=0;i<16;i++) /*The lower part*/

{

Wrdata(Hzk[x]);

x++;

col++;

if (col==LCMLIMIT){col=0;row++;row++;} /*The next column, if the column is out of bounds, wrap the line */

if (row>7) row=1; /*If the row is out of bounds, return the first row*/

} /*End of the next half character output*/

row = bakery;

} /*End of whole character output*/

/****************************************/

/* Clear the screen, clear the full screen */

/****************************************/

void Lcmcls( void )

{

for(row=0;row<8;row++)

for(col=0;col}

/****************************************/

/* Read data from the LCD and keep it in global variables */

/****************************************/

void Rddata(void)

{

Locatexy(); /* Coordinate positioning, keep the partition status unchanged when returning */

Datalcm=0xFF;

Dilcm = 1; /*data*/

Rwlcm = 1; /*Read data*/

Elcm = 1; /*Read into LCM*/

_nop_();

cbyte = Datalcm; /*Virtual read once*/

Elcm = 0;

Locatexy(); /* Coordinate positioning, keep the partition status unchanged when returning */

Datalcm=0xFF;

_nop_();

Dilcm = 1; /*data*/

Rwlcm = 1; /*Read data*/

Elcm = 1; /*Read into LCM*/

_nop_();

cbyte = Datalcm; /*Read data from the data port, really read*/

Elcm = 0;

}

/****************************************/

/* data write output */

/****************************************/

void Wrdata(Uchar X)

{

Locatexy(); /* Coordinate positioning, keep the partition status unchanged when returning */

// wtcom();

Dilcm = 1; /*Data output*/

Rwlcm = 0; /*write output*/

Datalcm = X; /*Data output to data port*/

Elcm = 1; /*Read into LCM*/

_nop_();

Elcm = 0;

}

Headphones

We've been around for over 16+ years. We make sure our sound is The Best Sound.
Our products include gaming headset, Bluetooth Earphone, Headphones Noise Cancelling, Best Wireless Earbuds, Bluetooth Mask, Headphones For Sleeping, Headphones in Headband, Bluetooth Beanie Hat, bluetooth for motorcycle helmet, etc
Manufacturing high-quality products for customers according to international standards, such as CE ROHS FCC REACH UL SGS BQB etc.
We help 200+ customers create custom Bluetooth headphones, earbuds, earphones, etc audio products design for various industries.

Customized Headphones, personalized gifts, promotional products custom , Bluetooth Earphones,Best Headphones

TOPNOTCH INTERNATIONAL GROUP LIMITED , https://www.mic11.com