Microcontroller Report
Microcontroller Report
1. INTRODUCTION
2. INTERFACING
3. PROGRAM
4. APPLICATIONS
5. CONCLUSION
INTRODUCTION
Hardware Connections
For a 16x2 LCD, you typically use the 4-bit mode for interfacing. Here's
how you connect the LCD to the 8051
LCD Pinout:
VSS: Ground (0V)
VDD: Power supply (+5V)
VO: Contrast (connected to a potentiometer)
RS: Register Select (connect to P1.0)
RW: Read/Write (connect to Ground for write-only mode)
EN: Enable (connect to P1.1)
D4-D7: Data bus lines (connect to P1.2 to P1.5)
A: Anode (backlight positive, if used)
K: Cathode (backlight negative, if used)
PROGRAM CODE
MAIN:
MOV P2, #00H ; Set P2 to 0 (for control lines)
MOV LCD, #00H ; Clear LCD data port
; Initialization sequence
ACALL DELAY
MOV A, #02H ; Function set: 4-bit mode
ACALL LCD_CMD
MOV A, #28H ; Function set: 2-line display, 5x7 font
ACALL LCD_CMD
MOV A, #0CH ; Display on, cursor off
ACALL LCD_CMD
MOV A, #01H ; Clear display
ACALL LCD_CMD
MOV A, #06H ; Entry mode set
ACALL LCD_CMD
; Print string
MOV DPTR, #MSG ; Point to message
PRINT_LOOP:
CLR A
MOV A, @DPTR ; Load character from data pointer
JZ END_PRINT ; If zero, end of string
ACALL LCD_DATA ; Send character to LCD
INC DPTR ; Move to next character
SJMP PRINT_LOOP ; Repeat for next character
END_PRINT:
SJMP $ ; Infinite loop
LCD_CMD:
MOV P2, #00H ; RS = 0, RW = 0 (Command mode)
MOV LCD, A ; Send command to LCD
MOV P2, #01H ; Enable
ACALL DELAY ; Short delay
MOV P2, #00H ; Disable
RET
LCD_DATA:
MOV P2, #01H ; RS = 1, RW = 0 (Data mode)
MOV LCD, A ; Send data to LCD
MOV P2, #01H ; Enable
ACALL DELAY ; Short delay
MOV P2, #00H ; Disable
RET
DELAY:
MOV R1, #250 ; Outer loop
DELAY_LOOP1:
MOV R2, #250 ; Inner loop
DELAY_LOOP2:
DJNZ R2, DELAY_LOOP2
DJNZ R1, DELAY_LOOP1
RET
MSG:
DB 'Hello, World!', 0 ; Message to display
END
EXPLANATION OF CODE
1. *Initialization:* Program starts, clears port P2, and sets up the LCD.
2. *LCD Setup:* Configures the LCD for 4-bit mode, 2 lines, and 5x7
font.
3. *Display Message:* Sends 'Hello, World!' to the LCD.
4. *Command Routine:* Sends commands to the LCD.
5. *Data Routine:* Sends characters to the LCD.
6. *Delay:* Provides timing delays for LCD operations.
C LANGUAGE CODE
#include <reg51.h>
// Delay function
void delay(unsigned int time) {
while (time--);
}
// Initialize LCD
void lcd_init(void) {
delay(200); // Power on delay
lcd_command(0x02); // Initialize in 4-bit mode
lcd_command(0x28); // 4-bit, 2-line, 5x7 font
lcd_command(0x0C); // Display on, cursor off
lcd_command(0x01); // Clear display
lcd_command(0x06); // Entry mode set
}
// Main function
void main(void) {
lcd_init(); // Initialize LCD
lcd_command(0x80); // Set cursor to start
lcd_print("Hello, World!"); // Print message
while(1); // Infinite loop
}
EXPLANATION OF CODE
1. *#include <reg51.h>* includes 8051-specific definitions.
2. *#define LCD PORT1* assigns PORT1 to the LCD data lines.
3. *delay(unsigned int time)* provides a delay for timing adjustments.
4. *lcd_command(unsigned char cmd)* sends a command to the LCD by
setting the control lines appropriately and toggling the enable pin.
5. *lcd_data(unsigned char data)* sends data to the LCD, similar to
lcd_command but with RS set to 1 for data mode.
6. *lcd_init(void)* initializes the LCD in 4-bit mode, sets it to 2 lines, and
configures display settings.
7. *lcd_print(char *str)* prints a string to the LCD character by character.
8. *main(void)* initializes the LCD, sets the cursor to the start position,
and prints "Hello, World!".
9. *The program runs indefinitely* after displaying the message, ensuring
the text remains on the LCD.
APPLICATIONS
CONCLUSION