LCD1602 Program

Publisher:温柔的心情Latest update time:2017-12-20 Source: eefocusKeywords:LCD1602  ATMega16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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   delay_1us(); 
  } 
   
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   delay_1ms(); 
  } 


Keywords:LCD1602  ATMega16 Reference address:LCD1602 Program

Previous article:Using 89S51 to Make a Simple Parallel Programmer ATmega16
Next article:AVR Parallel Port ISP Downloader

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号