/*================================================
20x4 character LCD main program, compilation software (ICCAVR_6.31)
CPU internal crystal oscillator 8M
data line B0~B7 connects PORTB, E=D7 RW=D6 RS=D5
=======================================================
The wiring diagram is as follows:
_______________ ______________
| --1|GND |
| --2| 5V |
| --3|V0 |
| | |
PD5|--------4|RS |
PD6|--------5|RW |
PD7|--------6|E |
CPU | | |
ATmmega16L PB0|--------7|D0 LCD20x4 |
PB1|--------8|D1 |
PB2|--------9|D2 |
PB3|-------10|D3 |
PB4|-------11|D4 |
PB5|-------12|D5 |
PB6|-------13|D6 |
PB7|-------14|D7 |
| | |
| --15| LED |
| --16|-LED |
_______________| |______________|
==============================================================*/
#include
#define RS_H asm("sbi 0x12,5") //RS set to high level
#define RS_L asm("cbi 0x12,5") //RS is set to low level
#define RW_H asm("sbi 0x12,6
") //RW is set to high level#define RW_L asm("cbi 0x12,6") //RW is set to low level
#define E_H asm("sbi 0x12,7") //E is set to high level
#define E_L asm("cbi 0x12,7") //E is set to low level
//=========================================================
//microsecond delay program
void delay_us(int time)
{
do
{
time--;
}
while (time > 1);
}
//===========================================================
//millisecond delay program
void delay_ms(unsigned int time)
{
while(time != 0)
{
delay_us(1000);
time--;
}
}
//=======================================================
//Read whether the LCD is in internal operation (busy) status
char Lcd_Busy()
{
char r;
DDRB = 0x00; //Port B is set to input mode
E_L;RS_L;RW_H; //E=0 (enable), RS=0 (command), RW=1 (read)
delay_us(2); //LCD delay subroutine
E_H;
delay_us(2); //LCD delay subroutine
r = PINB & 0x80; //Read the eighth bit of lcd_data
E_L;
DDRB=0xff; //Port B is set to output mode
return r; //Read result return
}
//=======================================================
//Send command program to LCD
void Lcd_Command(unsigned char Command)
{
while(Lcd_Busy()); //Judge whether LCD is in internal operation state
E_L;RS_L;RW_L; //E=0(enable),RS=0(command),RW=0(write)
delay_us(2); //LCD delay subroutine
E_H;
PORTB = Command; //Send command to LCD
delay_us(2); //LCD delay subroutine
E_L;
}
//=======================================================
//Program to write a character to the LCD
void Lcd_Write(unsigned char Data)
{
while(Lcd_Busy()); //Judge whether the LCD is in internal operation status
E_L;RS_H;RW_L; //E=0(enable),RS=1(data),RW=0(write)
delay_us(2); //LCD delay subroutine
E_H;
PORTB = Data; //Write a character to the LCD
delay_us(2); //LCD delay subroutine
E_L;
}
/*=============================================================
LCD line 1 displays address 1~20 (0x80~0x93)
LCD line 2 displays address 1~20 (0xc0~0xd3)
LCD line 3 displays address 1~20 (0x94~0xa7)
LCD
line
4 displays address 1~20 (0xd4~
0xe7
)
===
...
//Set port D to output mode
Lcd_Command(0x38); //
Lcd_Command
(0x38); //
Lcd_Command(0x38); // Lcd_Command(0x38); //
Lcd_Command(0x08); //Turn the display off
Lcd_Command(0x01); //Clear the display Lcd_Command(
0x06); //Set the LCD to automatically increase AC by 1 after receiving 1 Byte of data
Lcd_Command(0x0C); //Set the cursor, 0x0c = do not display, 0x0d = display flashing.
}
//========================================================
//Write ASCII string program
void asc_tran(unsigned char *asc)
{
while((*asc) != 0) //Judge whether the word ends
{
Lcd_Write(*asc); //Write string asc to LCD
; //Move to the next character
}
}
//========================================================
//Test main LCD main program
void main()
{
Lcd_Init(); //Initialize LCD
while(1)
{
Lcd_Command(0x83); //Set display address
asc_tran("-----@_@------"); //Display string
Lcd_Command(0x97); //Set display address
asc_tran("www.ouravr.com"); //Display string
delay_ms(1000); //Delay 1 second
Lcd_Command(0x01); //Clear display
Lcd_Command(0x83); //Set display address
asc_tran("-----^_^------"); //Display string
Lcd_Command(0x94); //Set display address
asc_tran("http://59.36.96.196"); //Display string
delay_ms(1000); //Delay
Lcd_Command(0x01); //Clear display
}
}
Previous article:Characteristics and Applications of AVR IO Ports
Next article:High-precision frequency regulator based on Atmega16L microcontroller
Recommended ReadingLatest update time:2024-11-16 18:05
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- Download any of them to get a gift | ADI [Lithium battery solutions, reference circuit collection]
- Crystal Oscillator Storage and Stopping Analysis
- One week evaluation information delivered~
- msp430g2331 ds1302 clock module
- [RVB2601 Creative Application Development] Dynamically load the wiring method of MBRE LED color screen ILI9341 TFT
- MSP430F5438A serial port
- Making a CPU out of a bunch of switches?
- MSP430G2553-PC communication protocol
- Problems with TINA-TI Applications
- Japanese 0X-01 oxygen detector in-depth disassembly