AVR 4 Bit LCD Interfacing Tutorial (ATMEGA16) | Arrogance Gizmo

AVR 4 Bit LCD Interfacing Tutorial (ATMEGA16)

Wednesday 7 March 2012

The mikroC PRO for AVR provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface. An example of Lcd connections is given on the schematic at the bottom of this page.

For LCD Basics: Character LCD Basics 


Lcd Library



External dependencies of Lcd Library
The following variables must be defined in all projects using Lcd Library :
Description :
Example :
extern sfr sbit LCD_RS:
Register Select line.
sbit LCD_RS at PORTD2_bit;
extern sfr sbit LCD_EN:
Enable line.
sbit LCD_EN at PORTD3_bit;
extern sfr sbit LCD_D7;
Data 7 line.
sbit LCD_D7 at PORTD4_bit;
extern sfr sbit LCD_D6;
Data 6 line.
sbit LCD_D6 at PORTD5_bit;
extern sfr sbit LCD_D5;
Data 5 line.
sbit LCD_D5 at PORTD6_bit;
extern sfr sbit LCD_D4;
Data 4 line.
sbit LCD_D4 at PORTD7_bit;
extern sfr sbit LCD_RS_Direction;
Register Select direction pin.
sbit LCD_RS_Direction at DDD2_bit;
extern sfr sbit LCD_EN_Direction;
Enable direction pin.
sbit LCD_EN_Direction at DDD3_bit;
extern sfr sbit LCD_D7_Direction;
Data 7 direction pin.
sbit LCD_D7_Direction at DDD4_bit;
extern sfr sbit LCD_D6_Direction;
Data 6 direction pin.
sbit LCD_D6_Direction at DDD5_bit;
extern sfr sbit LCD_D5_Direction;
Data 5 direction pin.
sbit LCD_D5_Direction at DDD6_bit;
extern sfr sbit LCD_D4_Direction;
Data 4 direction pin.
sbit LCD_D4_Direction at DDD7_bit;
Library Routines
Lcd_Init
Prototype
void Lcd_Init();
Returns
Nothing.
Description
Initializes Lcd module.
Requires
Global variables:
  • LCD_D7: Data bit 7
  • LCD_D6: Data bit 6
  • LCD_D5: Data bit 5
  • LCD_D4: Data bit 4
  • LCD_RS: Register Select (data/instruction) signal pin
  • LCD_EN: Enable signal pin
  • LCD_D7_Direction: Direction of the Data 7 pin
  • LCD_D6_Direction: Direction of the Data 6 pin
  • LCD_D5_Direction: Direction of the Data 5 pin
  • LCD_D4_Direction: Direction of the Data 4 pin
  • LCD_RS_Direction: Direction of the Register Select pin
  • LCD_EN_Direction: Direction of the Enable signal pin
must be defined before using this function.
Example
// Lcd pinout settings
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D7 at PORTD4_bit;
sbit LCD_D6 at PORTD5_bit;
sbit LCD_D5 at PORTD6_bit;
sbit LCD_D4 at PORTD7_bit;

// Pin direction
sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D7_Direction at DDD4_bit;
sbit LCD_D6_Direction at DDD5_bit;
sbit LCD_D5_Direction at DDD6_bit;
sbit LCD_D4_Direction at DDD7_bit;
...

Lcd_Init();
Lcd_Out
Prototype
void Lcd_Out(char row, char column, char *text);
Returns
Nothing.
Description
Prints text on Lcd starting from specified position. Both string variables and literals can be passed as a text.
Parameters :
  • row: starting position row number
  • column: starting position column number
  • text: text to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write text "Hello!" on Lcd starting from row 1, column 3:
Lcd_Out(1, 3, "Hello!");
Lcd_Out_Cp
Prototype
void Lcd_Out_Cp(char *text);
Returns
Nothing.
Description
Prints text on Lcd at current cursor position. Both string variables and literals can be passed as a text.
Parameters :
  • text: text to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write text "Here!" at current cursor position:
Lcd_Out_Cp("Here!");
Lcd_Chr
Prototype
void Lcd_Chr(char row, char column, char out_char);
Returns
Nothing.
Description
Prints character on Lcd at specified position. Both variables and literals can be passed as a character.
Parameters :
  • row: writing position row number
  • column: writing position column number
  • out_char: character to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write character "i" at row 2, column 3:
Lcd_Chr(2, 3, 'i');
Lcd_Chr_Cp
Prototype
void Lcd_Chr_Cp(char out_char);
Returns
Nothing.
Description
Prints character on Lcd at current cursor position. Both variables and literals can be passed as a character.
Parameters :
  • out_char: character to be written
Requires
The Lcd module needs to be initialized. See Lcd_Init routine.
Example
// Write character "e" at current cursor position:
Lcd_Chr_Cp('e');
Lcd_Cmd
Prototype
void Lcd_Cmd(char out_char);
Returns
Nothing.
Description
Sends command to Lcd.
Parameters :
  • out_char: command to be sent
  Note : Predefined constants can be passed to the function, see Available Lcd Commands.
Requires
The Lcd module needs to be initialized. See Lcd_Init table.
Example
// Clear Lcd display:
Lcd_Cmd(_LCD_CLEAR);
Available Lcd Commands
Lcd Command
Purpose
_LCD_FIRST_ROW
Move cursor to the 1st row
_LCD_SECOND_ROW
Move cursor to the 2nd row
_LCD_THIRD_ROW
Move cursor to the 3rd row
_LCD_FOURTH_ROW
Move cursor to the 4th row
_LCD_CLEAR
Clear display
_LCD_RETURN_HOME
Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected.
_LCD_CURSOR_OFF
Turn off cursor
_LCD_UNDERLINE_ON
Underline cursor on
_LCD_BLINK_CURSOR_ON
Blink cursor on
_LCD_MOVE_CURSOR_LEFT
Move cursor left without changing display data RAM
_LCD_MOVE_CURSOR_RIGHT
Move cursor right without changing display data RAM
_LCD_TURN_ON
Turn Lcd display on
_LCD_TURN_OFF
Turn Lcd display off
_LCD_SHIFT_LEFT
Shift display left without changing display data RAM
_LCD_SHIFT_RIGHT
Shift display right without changing display data RAM
Example
The following code demonstrates usage of the Lcd Library routines:
           // LCD module connections
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;

sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;
// End LCD module connections

char txt1[] = "Embedded";
char txt2[] = "Projects";
char txt3[] = "Lcd 4 bit";
char txt4[] = "Tutorial";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(500);                     // You can change the moving speed here
}

void main(){
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

  Lcd_Out(1,6,txt3);                 // Write text in first row
  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,4,txt2);                 // Write text in second row
  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop
    for(i=0; i<7; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<7; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }

  }
}

Circuit diagram

Copyright @ 2013 Arrogance Gizmo |

Follow Us On Facebook

Labels

.Net (2) 1 KM Range Wireless (1) 16x2 Character LCD (9) 2 UART (1) 4x3 Keypad (1) 4x4 keypad (1) 8051 (5) 8051 Project (2) 8051 Projects (1) 8051 Tutorial (3) 89C51 (4) 89C51 Project (2) 89S51 (1) 89S52 (1) Additional UART (1) Android (3) Android AVD (1) Android Programming Tutorial (1) Android Tutorial (2) Arduino Board (1) ARM Projects (1) Atiny (1) Atmega8 (1) AVR (5) AVR Projects (1) Build From Scratch (1) C# (1) C# Serial Port (2) C# serial Terminal (1) C# Voltmeter (1) Camera (1) Clock (1) Digital Voltmeter (2) Digital watch (1) DIY (2) EEPROM (2) Electronic code lock (1) Embedded (16) Embedded Project (9) Embedded Projects (2) Embedded Tutorial (12) Embeded (4) Extra UART (1) Flutter (1) Getting Started With Android (2) Home Security (1) Internet Based Device Control (1) Java of Things (1) Keypad (1) Keypad Tutorial (1) LCD (6) LCD Tutorial (4) Linux (1) Lock (1) LPG Sensor (1) MAX232 (3) Microcontroller (14) Microcontroller generates sound (1) Microcontroller Interrupt (1) Microcontroller Project (5) Microcontroller Tutorial (11) Microcontroller Tutorial. 8051 Tutorial (1) Mikoc 4 Bit LCD (3) MikroC (14) MikroC AVR (3) MikroC AVR Tutorial (3) MikroC EEPROM (2) MikroC for 8051 (4) MikroC Getting Started (1) MikroC PIC Tutorial (6) MikroC Tutorial (3) Motor Control (1) NETMF (1) New Embedded Boards (2) Optical Mouse (1) Optical Mouse To Camera (1) Password Lock (1) PC Based Voltmeter (1) PIC (7) PIC Based Electronic Lock (1) PIC 12F629 (1) PIC ADC (2) pic interrupt (1) PIC Music (1) pic project (1) PIC Projects (1) PIC sound melody (1) PIC Tutorial (3) PIC UART (1) PIC Voltmeter (2) Project (8) Quadcopter (1) Real Time Monitoring (1) RF (1) Robotic Projects (1) RS 232 (1) Run Android On PC (1) Security System (1) Serial Communication (3) Single Bord Computer (1) Smart home (1) Smart Home Project (1) STM32 (4) STM32F4 (3) STM32F4 Discovery (3) STM32F4 Project (1) STM32F4 Tutorials (3) STMicroelectronics (1) Temperature Sensor (2) Timer (1) Tutorial (8) UART (6) Udoo (1) USB (1) USB to RS 232 (1) USB to UART (1) VISUAL STUDIO (1) Water Level Control (1) WiFi (1)

Search This Blog

Popular Posts