TX-1C Board
The 1602 LCD in parallel mode.
The 1st and 2nd pins are gnd and vcc.
The 3rd pin is for contrast adjustment.
The 4th pin is for data/command selection RS.
The 5th pin is for read/write selection R/W. We only write but not read this time, so we connect it to ground directly.
The 6th pin is for read/write enable EN.
The 7th to 14th pins are for data port, which we connect to the P0 port of STC89C52RC.
The 15th and 16th pins are for backlight.
Final LCD display
Tom is playing the TNT game!
#include
#include "MY51.H"
void write_data(uchar dataValue); //write data
void write_cmd(uchar cmdValue); //write command
uchar code tableLCD1[] ={"Tom is playing"};
uchar code tableLCD2[] ={" the TNT game!"};
void lcdInit() //initialization function
{
dula=0; //
wela=0; //Prevent digital tube from displaying incorrectly and wasting current
lcdEN=low; //The state before giving high pulse
lcdRS=high; //Initial high level, in fact, the default is also high level when powered on
write_cmd(0x38); //LCD display mode initialization
write_cmd(0x0c); //Turn on the LCD, do not display the cursor, and do not blink the cursor
write_cmd(0x06); //Address pointer increments, and cursor increments
write_cmd(0x01); //LCD data pointer and display clear
}
void write_cmd(uchar cmdValue) //write command
{
lcdRS=low; //When it is just powered on, it is high level, and when it is low level, it is command mode
P0=cmdValue;
delayms(1);
lcdEN=high; //A high pulse reads the data
delayms(1);
lcdEN=low;
}
void write_data(uchar dataValue) //Write data
{
lcdRS=high; //High level is data mode
P0=dataValue;
delayms(1);
lcdEN=high; // give a high pulse
delayms(1);
lcdEN=low;
}
void lcdShow(uchar* pValue) //LCD display function
{
while(*pValue!='\0')
{
write_data(*pValue);
pValue++;
}
}
void main()
{
lcdInit(); // Initialization
lcdShow(tableLCD1); //Display the first row
write_cmd(0x80+0x40); //Reset the data display pointer
lcdShow(tableLCD2); //Display the second line
while(1);
}
#ifndef _MY51_H_
#define _MY51_H_
#include
#include
typedef int int16;
typedef int INT16;
typedef unsigned int uint16;
typedef unsigned int UINT16;
typedef unsigned short uint ;
typedef unsigned short UINT;
typedef unsigned short word;
typedef unsigned short WORD;
typedef unsigned long uint32;
typedef unsigned long UINT32;
typedef unsigned long DWORD;
typedef unsigned long dword;
typedef signed long int32;
typedef signed long INT32;
typedef float float32;
typedef double double64;
typedef signed char int8;
typedef signed char INT8;
typedef unsigned char byte;
typedef unsigned char BYTE ; //This is how it is defined in windef.h of WINDOWS
typedef unsigned char uchar;
typedef unsigned char UCHAR;
typedef unsigned char UINT8;
typedef unsigned char uint8;
typedef unsigned char BOOL; // BOOL is defined as int in windows
typedef unsigned char bool ; //bool is a built-in type of C++
#define TRUE 1
#define true 1
#define FALSE 0
#define false 0
#define open 1 //open and close are used to mark the open and closed states
#define OPEN 1
#define close 0
#define CLOSE 0
#define lock 0
#define start 1
#define START 1
#define stop 0
#define STOP 0
#define keyDown 0
#define keyUp 1
#define gnd 0 // ground
#define GND 0 // Ground
#define high 1 //High level
#define low 0 //Low level
#define yes 1
#define YES 1
#define no 0
#define NO 0
sbit dula =P2^6; //Segment select latch control control pen segment
sbit wela =P2^7; //bit select latch control control position
#define led P1 //light bus control
sbit led0=P1^0; //8 LED lights, the cathode sends a low level to light up
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
sbit led4=P1^4;
sbit led5=P1^5;
sbit led6=P1^6;
sbit led7=P1^7;
sbit keyS2=P3^4; //4 independent buttons
sbit keyS3=P3^5;
sbit keyS4=P3^6;
sbit keyS5=P3^7;
sbit lcdEN=P3^4; //LCD communication enable terminal en, high pulse is valid
sbit lcdRS=P3^5; // LCD pin 4, RS, low level is command mode, high level is data mode
//sbit lcdR/W //LCD pin 5, low level is write mode, because we only write and not read, so ground
sbit csda=P3^2; //DAC0832 analog-to-digital conversion cs port
sbit adwr=P3^6; //ADC0804 is the same as DAC0832
sbit dawr=P3^6;
sbit adrd=P3^7; //ADC0804
sbit beep=P2^3; //Buzzer
void displaySMG(uint8 one,uint8 two,uint8 three,uint8 four,uint8 five,uint8 six,uint8 dot);
void delayms(uint16 ms);
void T0_Work();
void delayms(uint16 ms) //soft delay function
{
uint16 i,j;
for(i=ms;i>0;i--)
{
for(j=113;j>0;j--)
{}
}
}
///////////////////////////////////////////////////// ////////////////////////////
#define dark 0x11 //In the segment, 0x11 is the 17th element, 0 is low level, the digital tube is not lit
#define dotDark 0xff //When the decimal point is completely dark
uint8 code table[]= { //0~F plus decimal point and empty output digital tube code
0x3f , 0x06 , 0x5b , 0x4f , // 0 1 2 3
0x66 , 0x6d , 0x7d , 0x07 , // 4 5 6 7
0x7f , 0x6f , 0x77 , 0x7c , // 8 9 A B
0x39 , 0x5e , 0x79 , 0x71 , // C D E F
0x80, 0x00, 0x40 //. Empty, negative sign, empty, it is 0x11, which is the 17th element
};
uint8 dotTable[]={ //decimal point position
0xff , //Full dark
0xfe, 0xfd, 0xfb, //1 2 3
0xf7, 0xef, 0xdf //4 5 6
};
///////////////////////////////////////////////////// //////////////////////////////
uint8 TH0Cout=0; //initial value
uint8 TL0Cout = 0;
uint16 T0IntCout=0; //interrupt count
uint16 T0IntCountAll=0; //(N-1)/65536+1; //Total number of interrupts
bool bT0Delay=false; //Use delay function flag, not used initially
bool bT0Over=false; //One of the results of the interrupt processing function execution
void startT0(uint32 ms) //Start the timer
{
float32 t=ms/1000.0; //timing time
double64 fox =11.0592*(pow(10,6)); //Crystal oscillator frequency
uint32 N=(t*fox)/12; //Timer total value
TH0Cout = (65536-N%65536)/256; //Load the initial value of the timing value fraction count
TL0Cout = (65536-N%65536)%256;
T0IntCountAll=(N-1)/65536+1; //Total number of interrupts
TMOD=TMOD | 0x01; //Set the working mode of timer 0 to 1
EA =open; //Open the general interrupt
ET0=open; //Open the timer interrupt
TH0=TH0Cout; //Load the initial value into the timer
TL0=TL0Cout;
TR0=start; //Start the timer
}
void delayT0(uint32 ms) //Hard delay function, it is not easy to use if I write it by myself, please give me some advice
{
startT0(ms); //Start the timer
bT0Delay=true; //Tell T0 timer to enable delay mode
while(bT0Over==false); //Continue testing if the time is not up
bT0Over=false; //Time is up, reset the flag
}
void T0_times() interrupt 1 //T0 timer interrupt function
{
T0IntCout++;
if(T0IntCout==T0IntCountAll) //The total number of interrupts is reached
{
T0IntCout=0; //Reset the interrupt times and restart timing
bT0Over=true; //The time is really up
if(bT0Delay) //Is this interrupt used for delay?
{
TR0=stop; //If T0 is turned on by the delay function, turn off T0
return;
}
TH0=TH0Cout; //If the timing is cyclic, the initial value must be reloaded, and the timing is 1 second each time, and the initial value must be reloaded once
TL0=TL0Cout;
T0_Work(); //Work function
}
}
///////////////////////////////////////////////////// /////////////////////////////////
void displaySMG(uint8 oneWela,uint8 twoWela,uint8 threeWela,uint8 fourWela,uint8 fiveWela,uint8 sixWela,uint8 dot)
{
//Control the 6-bit digital tube display function, use the parameter dark for the bits that are not displayed, and keep the chip select signal of ADC0804
uint8 csadState=0x80&P0; //Extract the highest bit, i.e. the chip select signal of ADC0804
uint8 tempP0=((csadState==0)?0x7f:0xff); //Digital tube bit selection initial signal, cathode all set to high level
P0=tempP0; //0x7f means the digital tube is off and the ADC0804 chip selection is valid
wela=1; //Note: wela and dula are 1 by default when powered on
P0=tempP0;
wela=0;
P0=0; //Since the digital tube has a common cathode, the anode sends a low level, the light does not light up, to prevent the light from lighting up by mistake
dula=1;
P0=0;
dula=0; //Segment selection data is cleared and locked
/////////////////////////////oneWela
{ // Eliminate overlapping images, set the cathode of the digital tube to a high level, and latch
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0; // low level is sent to the anode of the digital tube to prevent the digital tube from lighting up by mistake
dula=1;
P0=table[oneWela]|((0x01&dot)?0x00:0x80); //Send segment data, superimpose decimal point display
dula=0;
P0=tempP0; //Turn off all displays before sending data and keep csad signal
wela=1;
P0=tempP0 & 0xfe; //0111 1110 The highest bit is the AD chip select, the lower 6 bits are the digital tube bit select, low level is effective
wela=0;
delayms(2);
///////////////////////////twoWela
{ //Eliminate overlapping images
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0;
dula=1;
P0=table[twoWela]|((0x02&dot)?0x00:0x80);
dula=0;
P0=tempP0;
wela=1;
P0=tempP0 & 0xfd; //0111 1101
wela=0;
delayms(2);
////////////////////////////threeWela
{ //Eliminate overlapping images
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0;
dula=1;
P0=table[threeWela]|((0x04&dot)?0x00:0x80);
dula=0;
P0=tempP0;
wela=1;
P0=tempP0 & 0xfb; //0111 1011
wela=0;
delayms(2);
///////////////////////////fourWela
{ //Eliminate overlapping images
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0;
dula=1;
P0=table[fourWela]|((0x08&dot)?0x00:0x80);
dula=0;
P0=tempP0;
wela=1;
P0=tempP0 & 0xf7; //0111 0111
wela=0;
delayms(2);
///////////////////////////fiveWela
{ //Eliminate overlapping images
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0;
dula=1;
P0=table[fiveWela]|((0x10&dot)?0x00:0x80);
dula=0;
P0=tempP0;
wela=1;
P0=tempP0 & 0xef; //0110 1111
wela=0;
delayms(2);
///////////////////////////sixWela
{ //Eliminate overlapping images
P0=tempP0;
wela=1;
P0=tempP0;
wela=0;
}
P0=0;
dula=1;
P0=table[sixWela]|((0x20&dot)?0x00:0x80);
dula=0;
P0=tempP0;
wela=1;
P0=tempP0 & 0xdf; //0101 1111
wela=0;
delayms(2);
}
#endif
Previous article:51 MCU study notes, simulate iic bus to continuously read and write 24c02 memory
Next article:51 MCU learning notes: using ADC0804 analog-to-digital converter to collect voltage
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The world is moving towards 5G. Do you think it’s time to switch to a 5G phone?
- EETALK: What products might be reshaped in the 5G era? (Give away 10-100 Chip Coins)
- [Good book download] 300 examples of classic intelligent circuits!
- Allegro Issues
- 【National Technology N32G430】1: Preliminary Study
- How to draw large areas of PCB
- How to tell whether a component is genuine or fake?
- How to layout the PCB of photovoltaic power supply?
- Questions and Answers about bq24650 and BQ25601
- A Beginner's Guide to PCB Design