Configuration:
ATMega16L @3.3V @7.3728MHz
1602B LCD @5.0V @6 lines
Compiler: WinAVR 20060125
I built a multi-purpose board myself, 1602 is powered by 5V, M16 is powered by 3.3V. In order to make the LCD work as soon as possible, and also because of laziness... I referred to many 1602 posts on the website, including website collection posts. After downloading the program, I modified it slightly, compiled it, and downloaded it to M16, but it always did not respond.
During this process, I was sure that there was no problem with my 1602 hardware, because I had a time thermometer (1602+18B20) made with 51 before, and it could display normally when I put it on.
During the whole process, I felt that whether it was the essence posts collected on the website or the 1602 programs in ordinary posts, they were long and complicated... For example, in a post, I saw a 1602 initialization function that actually used more than 10 lines of code! In 51, it only had 4 sentences...
Later, I encountered the following program, which was concise and successful at one time. Of course, this is also copied from the forum, I made some modifications:
#include
#define LCD_EN_PORT PORTC
#define LCD_RW_PORT PORTC
#define LCD_RS_PORT PORTC
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_ PIN PINA
//LCD's r/w pins are directly connected to GND
#define LCD_EN 0x80 //portd7 out
#define LCD_RS 0x40 //portc6 out
#define LCD_DATA 0xF0 //porta 4/5/6/7 out
/*--------------------------------------------------------------------------------------------------
Publ IC function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned command,unsigned data); void
LCD_set_xy (unsigned char x, unsigned char y);
LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus (unsigned int n);
void delay_nms (unsigned int n);
void LCD_init(void) //LCD initialization
{
delay_nms(15);
DDRA |= LCD_DATA; //Data is output
DDRC |= LCD_RS | LCD_EN; //Set RS.EN
LCD_write_char(0x28,0); //4-bit display
LCD_write_char(0x0c,0); //Display on
LCD_write_char(0x01,0); //Clear screen
delay_nms(60);
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //Write address
while (*s) {
LCD_write_char( 0, *s );
s ++;
}
}
void LCD_set_xy( unsigned char x, unsigned char y ) //Write address function
{
unsigned char address;
if (y == 0)
address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_char(address, 0 );
}
void LCD_en_write(void) //LCD enable
{
LCD_EN_PORT |= LCD_EN;
delay_nus(1);
LCD_EN_PORT &= ~LCD _EN;
}
void LCD_write_char(unsigned command,unsigned data) //Write data
{
unsigned command_temp,data_temp;
command_temp = command;
data_temp = data;
delay_nus(16);
if(command == 0) {
LCD_RS_PORT |= LCD_RS; //RS=1
LCD_DATA_PORT &= 0X0f;
LCD_DATA_PORT |= data_temp & 0xf0; //Write the upper four bits
LCD_en_write();
data_temp = data_temp << 4;
LCD_DATA_PORT &= 0X0f;
LCD_DATA_PORT |= data_temp & 0xF0; //Write the lower four bits
LCD_en_write();
}
else {
LCD_RS_PORT &= ~LCD_RS; //RS=0
LCD_DATA_PORT &= 0X0f;
LCD_DATA_PORT |= command_temp & 0xF0; //Write the upper four bits
LCD_en_write();
command_temp = command_temp << 4;
LCD_DATA_PORT &= 0x0F;
LCD_DATA_PORT |= command_temp & 0xF0 ; //Write the lower four bits
LCD_en_write();
}
}
int main(void)
{
LCD_init();
LCD_write_string(0,0,"Hello,AVR WORLD!!!");
LCD_write_string(0,1,"hitro@tom.com");
while(1);
}
/*--------------------------- --------------------------------------------
Delay function
system clock: 8M
------------------------------------------------- ----------------------*/
void delay_1us(void) //1us delay function
{
asm("nop");
}
void delay_nus(unsigned int n ) //N us delay function
{
unsigned int i=0;
for (i=0;i
}
void delay_1ms(void) //1ms delay function
{
unsigned int i;
for ( i=0;i<1356;i++);
}
void delay_nms(unsigned int n) //N ms delay function
{
unsigned int i=0;
for (i=0;i
}
Previous article:Using 89S51 to Make a Simple Parallel Programmer ATmega16
Next article:AVR Parallel Port ISP Downloader
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- [NUCLEO-WL55JC2 Review]——by Lan Yuye
- WI-FI 6E
- How to achieve the same positive and negative display effects on the same code screen
- Recruiting embedded system development engineers Annual salary: 200,000-500,000 | Experience: 5 years or more | Work location: Beijing
- ADC question
- Hetai MCU Problem
- [Chuanglong TL570x-EVM] Customized SD card and artificial intelligence framework introduction
- MSP430FR6989 function introduction first look
- Basic knowledge of FPGA architecture and applications
- Selection summary of mainstream Bluetooth BLE MESH module Bluetooth chip IC