LCD1602 driver based on MEGA16 (four-bit data line)

Publisher:LianaiLatest update time:2017-12-19 Source: eefocusKeywords:MEGA16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//Compiler: ICC-AVR v6.31A 
//Target chip: M16 
//Clock: 8.0000MHz 
/*-------------------------------------------------------------

LCD Pin Definition 
1---GND  
2---VCC 
3---VO 
4---RS 
5---RW 
6---EN 
7 to 14--D0-D7 
15--Backlight+ 
16--Backlight- 
---------------------------------------------------------------------------------*/ 
#include  
#include  

/*--------------------------------------------------------------- 
Below is the connection information between AVR and LCD 
PA2 ->RS 
PA3 ->EN 
Ground ->RW 
PA4 ->D4 
PA5 ->D5 
PA6 ->D6 
PA7 ->D7 
To use this driver, just change the following configuration information 
-----------------------------------------------------------------*/ 
#define LCD_EN_PORT PORTA //The following 2 should be set to the same port 
#define LCD_EN_DDR DDRA 
#define LCD_RS_PORT PORTA //The following 2 should be set to the same port 
#define LCD_RS_DDR DDRA 
#define LCD_DATA_PORT PORTA //The following 3 should be set to the same port 
#define LCD_DATA_DDR DDRA //Must use the high 4 bits 
#define LCD_DATA_ PIN    PINA 
#define LCD_RS (1<Function description 
-----------------------------------------------------------------------------*/ 
void LCD_init(void); 
void LCD_en_write( void); 
void LCD_write_command(unsigned char command 
); void LCD_write_data(unsigned char data);  void LCD_set_xy (unsigned char x
, unsigned char y) 
; void LCD_write_string(unsigned char X, unsigned char Y, unsigned char *s) 

void LCD_write_char(unsigned char delay_nus(unsigned int n); 
void delay_nms(unsigned int n); 
============================================================================= 

#include "LCD.h" 

void LCD_init(void) //LCD initialization 

  LCD_DATA_DDR|=LCD_DATA; //Data port direction is output 
  LCD_EN_DDR|=LCD_EN; //Set EN direction to output 
  LCD_RS_DDR|=LCD_RS; //Set RS direction to output 
  LCD_write_command(0x28);  
  LCD_en_write(); 
  delay_nus(40); 
  LCD_write_command(0x28); //4-bit display 
  LCD_write_command(0x0c); //Display on 
  LCD_write_command(0x01); //Clear screen 
  delay_nms(2); 


void LCD_en_write(void) //LCD enable 

  LCD_EN_PORT|=LCD_EN; 
  delay_nus(1); 
  LCD_EN_PORT&=~LCD_EN; 


void LCD_write_command(unsigned char command) //Write command 

  delay_nus(16); 
  LCD_RS_PORT&=~LCD_RS; //RS=0 
  LCD_DATA_PORT&=0X0f; //Clear four bits 
  LCD_DATA_PORT|=command&0xf0; //Write high four bits 
  LCD_en_write(); 
  command=command<<4; //Move low four bits to high four bits 
  LCD_DATA_PORT&=0x0f; //Clear high four bits 
  LCD_DATA_PORT|=command&0xf0; //Write low four bits 
  LCD_en_write(); 
   


void LCD_write_data(unsigned char data) //Write data 

  delay_nus(16); 
  LCD_RS_PORT|=LCD_RS; //RS=1 
  LCD_DATA_PORT&=0X0f; //Clear high four bits 
  LCD_DATA_PORT|=data&0xf0; //Write high four bits 
  LCD_en_write(); 
  data=data<<4; //Move low four bits to high four bits 
  LCD_DATA_PORT&=0X0f; //Clear high four bits 
  LCD_DATA_PORT|=data&0xf0; //Write low four bits 
  LCD_en_write(); 



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_command( address); 

   
void LCD_write_string(unsigned char Y )      ; //Write address      while (*s) //Write     display      characters 
{        LCD_write_data( *s );        s ++;      }  }  void LCD_write_char(unsigned char X, unsigned char Y, unsigned char data   )  //Column x=0~15, row y=0,1  {    LCD_set_xy  (  =======================================================================  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





       






   













   





   



























   LCD_write_char(6,0,'8');    
   } 
  
}


0 0| 0 1 0 0 // 0x04 | 0 0 0| 0 0 
1  0  0       //       0x04       |  0       0  0       |  0       1  1  1  0 //       0x00  unsigned 
char  data[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x00};//↑  unsigned char data1[8]={0x04,0x04,0x04,0x04,0x15,0x0e,0x04,0x00};//↓  void my_signs(unsigned char ascii,unsigned char *z)//0x00  {    unsigned char address=0,i,temp=0; //ascii is the ascii code value corresponding to the pattern to be defined (0x00 to    temp=(ascii&0x07)<<3; //any value between 0x007)     for(i=0;i<8;i++)     {          address = 0x40 +temp+ i;        LCD_write_command( address );        delay_nus(1);        LCD_write_data ( *(z++));        delay_nus(1);     }  }  //Create pattern  void LCD_init(void) //LCD initialization  {   LCD_DATA_DDR|=LCD_DATA; //Data is output   LCD_EN_DDR|=LCD_EN; //Set RS.EN direction   LCD_RS_DDR|=LCD_RS;   LCD_write_command(0x28); //4-bit display    LCD_en_write();    delay_nms(15);   LCD_write_command(0x28); //4-bit display   LCD_write_command(0x0c); //Display on    my_signs(0x00,data);//↑ //Create pattern   my_signs(0x01,data1);//↓   LCD_write_command(0x01); //Clear screen  }  Display custom pattern  LCD_write_char(9,0,0x00);//↑  LCD_write_char(9,0,0x01);//↓




















































Special Tip: If the compilation fails, please delete the header file


Keywords:MEGA16 Reference address:LCD1602 driver based on MEGA16 (four-bit data line)

Previous article:Programmer Notepad Configuration and AVRGCC Introduction
Next article:AVR infrared sending and receiving header file

Latest Microcontroller Articles
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号